blob: 1d7b99dfc81e4e5f05bb6c35973157fc88a844da [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) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800618 // Note: No need to use glDiscardFramebufferEXT() since we never
619 // create/compose layers that are not on screen with this
620 // code path
621 // See LayerRenderer::destroyLayer(Layer*)
622
Romain Guyeb993562010-10-05 18:14:38 -0700623 // Detach the texture from the FBO
624 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
625 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
626 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
627
628 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
629 mCaches.fboCache.put(current->fbo);
630 }
631
Romain Guy746b7402010-10-26 16:27:31 -0700632 dirtyClip();
633
Romain Guyeb993562010-10-05 18:14:38 -0700634 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700635 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700636 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700637 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700638 delete layer;
639 }
640}
641
Romain Guyaa6c24c2011-04-28 18:40:04 -0700642void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700643 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700644
Romain Guy302a9df2011-08-16 13:55:02 -0700645 mat4& transform = layer->getTransform();
646 if (!transform.isIdentity()) {
647 save(0);
648 mSnapshot->transform->multiply(transform);
649 }
650
Romain Guyaa6c24c2011-04-28 18:40:04 -0700651 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700652 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700653 setupDrawWithTexture();
654 } else {
655 setupDrawWithExternalTexture();
656 }
657 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700658 setupDrawColor(alpha, alpha, alpha, alpha);
659 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700660 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700661 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700662 setupDrawPureColorUniforms();
663 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700664 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
665 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700666 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700667 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700668 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700669 if (mSnapshot->transform->isPureTranslate() &&
670 layer->getWidth() == (uint32_t) rect.getWidth() &&
671 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700672 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
673 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
674
Romain Guyd21b6e12011-11-30 20:21:23 -0800675 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700676 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
677 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800678 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700679 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
680 }
681 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700682 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
683
684 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
685
686 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700687
688 if (!transform.isIdentity()) {
689 restore();
690 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700691}
692
Romain Guy5b3b3522010-10-27 18:57:51 -0700693void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700694 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700695 const Rect& texCoords = layer->texCoords;
696 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
697 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700698
Romain Guy9ace8f52011-07-07 20:50:11 -0700699 float x = rect.left;
700 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700701 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700702 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700703 layer->getHeight() == (uint32_t) rect.getHeight();
704
705 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700706 // When we're swapping, the layer is already in screen coordinates
707 if (!swap) {
708 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
709 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
710 }
711
Romain Guyd21b6e12011-11-30 20:21:23 -0800712 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700713 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800714 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700715 }
716
717 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
718 layer->getTexture(), layer->getAlpha() / 255.0f,
719 layer->getMode(), layer->isBlend(),
720 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
721 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700722
Romain Guyaa6c24c2011-04-28 18:40:04 -0700723 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
724 } else {
725 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
726 drawTextureLayer(layer, rect);
727 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
728 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700729}
730
731void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
732#if RENDER_LAYERS_AS_REGIONS
733 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700734 layer->setRegionAsRect();
735
Romain Guy40667672011-03-18 14:34:03 -0700736 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700737
Romain Guy5b3b3522010-10-27 18:57:51 -0700738 layer->region.clear();
739 return;
740 }
741
Romain Guy8a3957d2011-09-07 17:55:15 -0700742 // TODO: See LayerRenderer.cpp::generateMesh() for important
743 // information about this implementation
Romain Guy5b3b3522010-10-27 18:57:51 -0700744 if (!layer->region.isEmpty()) {
745 size_t count;
746 const android::Rect* rects = layer->region.getArray(&count);
747
Romain Guy9ace8f52011-07-07 20:50:11 -0700748 const float alpha = layer->getAlpha() / 255.0f;
749 const float texX = 1.0f / float(layer->getWidth());
750 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800751 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700752
753 TextureVertex* mesh = mCaches.getRegionMesh();
754 GLsizei numQuads = 0;
755
Romain Guy7230a742011-01-10 22:26:16 -0800756 setupDraw();
757 setupDrawWithTexture();
758 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800759 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700760 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800761 setupDrawProgram();
762 setupDrawDirtyRegionsDisabled();
763 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800764 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700765 setupDrawTexture(layer->getTexture());
766 if (mSnapshot->transform->isPureTranslate()) {
767 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
768 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
769
Romain Guyd21b6e12011-11-30 20:21:23 -0800770 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700771 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
772 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800773 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700774 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
775 }
Romain Guy7230a742011-01-10 22:26:16 -0800776 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700777
778 for (size_t i = 0; i < count; i++) {
779 const android::Rect* r = &rects[i];
780
781 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800782 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700783 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800784 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700785
786 // TODO: Reject quads outside of the clip
787 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
788 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
789 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
790 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
791
792 numQuads++;
793
794 if (numQuads >= REGION_MESH_QUAD_COUNT) {
795 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
796 numQuads = 0;
797 mesh = mCaches.getRegionMesh();
798 }
799 }
800
801 if (numQuads > 0) {
802 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
803 }
804
805 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800806 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700807
808#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800809 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700810#endif
811
812 layer->region.clear();
813 }
814#else
815 composeLayerRect(layer, rect);
816#endif
817}
818
Romain Guy3a3133d2011-02-01 22:59:58 -0800819void OpenGLRenderer::drawRegionRects(const Region& region) {
820#if DEBUG_LAYERS_AS_REGIONS
821 size_t count;
822 const android::Rect* rects = region.getArray(&count);
823
824 uint32_t colors[] = {
825 0x7fff0000, 0x7f00ff00,
826 0x7f0000ff, 0x7fff00ff,
827 };
828
829 int offset = 0;
830 int32_t top = rects[0].top;
831
832 for (size_t i = 0; i < count; i++) {
833 if (top != rects[i].top) {
834 offset ^= 0x2;
835 top = rects[i].top;
836 }
837
838 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
839 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
840 SkXfermode::kSrcOver_Mode);
841 }
842#endif
843}
844
Romain Guy5b3b3522010-10-27 18:57:51 -0700845void OpenGLRenderer::dirtyLayer(const float left, const float top,
846 const float right, const float bottom, const mat4 transform) {
847#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800848 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700849 Rect bounds(left, top, right, bottom);
850 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800851 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700852 }
853#endif
854}
855
856void OpenGLRenderer::dirtyLayer(const float left, const float top,
857 const float right, const float bottom) {
858#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800859 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700860 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800861 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800862 }
863#endif
864}
865
866void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
867#if RENDER_LAYERS_AS_REGIONS
868 if (bounds.intersect(*mSnapshot->clipRect)) {
869 bounds.snapToPixelBoundaries();
870 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
871 if (!dirty.isEmpty()) {
872 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700873 }
874 }
875#endif
876}
877
Romain Guy54be1cd2011-06-13 19:04:27 -0700878void OpenGLRenderer::clearLayerRegions() {
879 const size_t count = mLayers.size();
880 if (count == 0) return;
881
882 if (!mSnapshot->isIgnored()) {
883 // Doing several glScissor/glClear here can negatively impact
884 // GPUs with a tiler architecture, instead we draw quads with
885 // the Clear blending mode
886
887 // The list contains bounds that have already been clipped
888 // against their initial clip rect, and the current clip
889 // is likely different so we need to disable clipping here
890 glDisable(GL_SCISSOR_TEST);
891
892 Vertex mesh[count * 6];
893 Vertex* vertex = mesh;
894
895 for (uint32_t i = 0; i < count; i++) {
896 Rect* bounds = mLayers.itemAt(i);
897
898 Vertex::set(vertex++, bounds->left, bounds->bottom);
899 Vertex::set(vertex++, bounds->left, bounds->top);
900 Vertex::set(vertex++, bounds->right, bounds->top);
901 Vertex::set(vertex++, bounds->left, bounds->bottom);
902 Vertex::set(vertex++, bounds->right, bounds->top);
903 Vertex::set(vertex++, bounds->right, bounds->bottom);
904
905 delete bounds;
906 }
907
908 setupDraw(false);
909 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
910 setupDrawBlending(true, SkXfermode::kClear_Mode);
911 setupDrawProgram();
912 setupDrawPureColorUniforms();
913 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
914
915 mCaches.unbindMeshBuffer();
916 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
917 gVertexStride, &mesh[0].position[0]);
918 glDrawArrays(GL_TRIANGLES, 0, count * 6);
919
920 glEnable(GL_SCISSOR_TEST);
921 } else {
922 for (uint32_t i = 0; i < count; i++) {
923 delete mLayers.itemAt(i);
924 }
925 }
926
927 mLayers.clear();
928}
929
Romain Guybd6b79b2010-06-26 00:13:53 -0700930///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700931// Transforms
932///////////////////////////////////////////////////////////////////////////////
933
934void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700935 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700936}
937
938void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700939 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700940}
941
942void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700943 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700944}
945
Romain Guy807daf72011-01-18 11:19:19 -0800946void OpenGLRenderer::skew(float sx, float sy) {
947 mSnapshot->transform->skew(sx, sy);
948}
949
Romain Guyf6a11b82010-06-23 17:47:49 -0700950void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -0700951 if (matrix) {
952 mSnapshot->transform->load(*matrix);
953 } else {
954 mSnapshot->transform->loadIdentity();
955 }
Romain Guyf6a11b82010-06-23 17:47:49 -0700956}
957
958void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700959 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700960}
961
962void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700963 SkMatrix transform;
964 mSnapshot->transform->copyTo(transform);
965 transform.preConcat(*matrix);
966 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700967}
968
969///////////////////////////////////////////////////////////////////////////////
970// Clipping
971///////////////////////////////////////////////////////////////////////////////
972
Romain Guybb9524b2010-06-22 18:56:38 -0700973void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700974 Rect clip(*mSnapshot->clipRect);
975 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700976 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700977 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700978}
979
980const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700981 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700982}
983
Romain Guyc7d53492010-06-25 13:41:57 -0700984bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800985 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700986 return true;
987 }
988
Romain Guy1d83e192010-08-17 11:37:00 -0700989 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700990 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700991 r.snapToPixelBoundaries();
992
993 Rect clipRect(*mSnapshot->clipRect);
994 clipRect.snapToPixelBoundaries();
995
996 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700997}
998
Romain Guy079ba2c2010-07-16 14:12:24 -0700999bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1000 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001001 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001002 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001003 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001004 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001005}
1006
Romain Guyf6a11b82010-06-23 17:47:49 -07001007///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001008// Drawing commands
1009///////////////////////////////////////////////////////////////////////////////
1010
Romain Guy54be1cd2011-06-13 19:04:27 -07001011void OpenGLRenderer::setupDraw(bool clear) {
1012 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001013 if (mDirtyClip) {
1014 setScissorFromClip();
1015 }
1016 mDescription.reset();
1017 mSetShaderColor = false;
1018 mColorSet = false;
1019 mColorA = mColorR = mColorG = mColorB = 0.0f;
1020 mTextureUnit = 0;
1021 mTrackDirtyRegions = true;
Romain Guy8d0d4782010-12-14 20:13:35 -08001022 mTexCoordsSlot = -1;
Romain Guy70ca14e2010-12-13 18:24:33 -08001023}
1024
1025void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1026 mDescription.hasTexture = true;
1027 mDescription.hasAlpha8Texture = isAlpha8;
1028}
1029
Romain Guyaa6c24c2011-04-28 18:40:04 -07001030void OpenGLRenderer::setupDrawWithExternalTexture() {
1031 mDescription.hasExternalTexture = true;
1032}
1033
Chet Haase5b0200b2011-04-13 17:58:08 -07001034void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001035 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001036}
1037
Romain Guyed6fcb02011-03-21 13:11:28 -07001038void OpenGLRenderer::setupDrawPoint(float pointSize) {
1039 mDescription.isPoint = true;
1040 mDescription.pointSize = pointSize;
1041}
1042
Romain Guy70ca14e2010-12-13 18:24:33 -08001043void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001044 setupDrawColor(color, (color >> 24) & 0xFF);
1045}
1046
1047void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1048 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001049 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1050 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001051 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001052 mColorR = a * ((color >> 16) & 0xFF);
1053 mColorG = a * ((color >> 8) & 0xFF);
1054 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001055 mColorSet = true;
1056 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1057}
1058
Romain Guy86568192010-12-14 15:55:39 -08001059void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1060 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001061 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1062 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001063 const float a = mColorA / 255.0f;
1064 mColorR = a * ((color >> 16) & 0xFF);
1065 mColorG = a * ((color >> 8) & 0xFF);
1066 mColorB = a * ((color ) & 0xFF);
1067 mColorSet = true;
1068 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1069}
1070
Romain Guy70ca14e2010-12-13 18:24:33 -08001071void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1072 mColorA = a;
1073 mColorR = r;
1074 mColorG = g;
1075 mColorB = b;
1076 mColorSet = true;
1077 mSetShaderColor = mDescription.setColor(r, g, b, a);
1078}
1079
Romain Guy86568192010-12-14 15:55:39 -08001080void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1081 mColorA = a;
1082 mColorR = r;
1083 mColorG = g;
1084 mColorB = b;
1085 mColorSet = true;
1086 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1087}
1088
Romain Guy70ca14e2010-12-13 18:24:33 -08001089void OpenGLRenderer::setupDrawShader() {
1090 if (mShader) {
1091 mShader->describe(mDescription, mCaches.extensions);
1092 }
1093}
1094
1095void OpenGLRenderer::setupDrawColorFilter() {
1096 if (mColorFilter) {
1097 mColorFilter->describe(mDescription, mCaches.extensions);
1098 }
1099}
1100
Romain Guyf09ef512011-05-27 11:43:46 -07001101void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1102 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1103 mColorA = 1.0f;
1104 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001105 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001106 }
1107}
1108
Romain Guy70ca14e2010-12-13 18:24:33 -08001109void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001110 // When the blending mode is kClear_Mode, we need to use a modulate color
1111 // argb=1,0,0,0
1112 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001113 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1114 mDescription, swapSrcDst);
1115}
1116
1117void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001118 // When the blending mode is kClear_Mode, we need to use a modulate color
1119 // argb=1,0,0,0
1120 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001121 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1122 mDescription, swapSrcDst);
1123}
1124
1125void OpenGLRenderer::setupDrawProgram() {
1126 useProgram(mCaches.programCache.get(mDescription));
1127}
1128
1129void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1130 mTrackDirtyRegions = false;
1131}
1132
1133void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1134 bool ignoreTransform) {
1135 mModelView.loadTranslate(left, top, 0.0f);
1136 if (!ignoreTransform) {
1137 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1138 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1139 } else {
1140 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1141 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1142 }
1143}
1144
Chet Haase8a5cc922011-04-26 07:28:09 -07001145void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1146 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001147}
1148
Romain Guy70ca14e2010-12-13 18:24:33 -08001149void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1150 bool ignoreTransform, bool ignoreModelView) {
1151 if (!ignoreModelView) {
1152 mModelView.loadTranslate(left, top, 0.0f);
1153 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001154 } else {
1155 mModelView.loadIdentity();
1156 }
Romain Guy86568192010-12-14 15:55:39 -08001157 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1158 if (!ignoreTransform) {
1159 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1160 if (mTrackDirtyRegions && dirty) {
1161 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1162 }
1163 } else {
1164 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1165 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1166 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001167}
1168
Romain Guyed6fcb02011-03-21 13:11:28 -07001169void OpenGLRenderer::setupDrawPointUniforms() {
1170 int slot = mCaches.currentProgram->getUniform("pointSize");
1171 glUniform1f(slot, mDescription.pointSize);
1172}
1173
Romain Guy70ca14e2010-12-13 18:24:33 -08001174void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001175 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001176 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1177 }
1178}
1179
Romain Guy86568192010-12-14 15:55:39 -08001180void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001181 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001182 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001183 }
1184}
1185
Romain Guy70ca14e2010-12-13 18:24:33 -08001186void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1187 if (mShader) {
1188 if (ignoreTransform) {
1189 mModelView.loadInverse(*mSnapshot->transform);
1190 }
1191 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1192 }
1193}
1194
Romain Guy8d0d4782010-12-14 20:13:35 -08001195void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1196 if (mShader) {
1197 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1198 }
1199}
1200
Romain Guy70ca14e2010-12-13 18:24:33 -08001201void OpenGLRenderer::setupDrawColorFilterUniforms() {
1202 if (mColorFilter) {
1203 mColorFilter->setupProgram(mCaches.currentProgram);
1204 }
1205}
1206
1207void OpenGLRenderer::setupDrawSimpleMesh() {
1208 mCaches.bindMeshBuffer();
1209 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1210 gMeshStride, 0);
1211}
1212
1213void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1214 bindTexture(texture);
1215 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1216
1217 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1218 glEnableVertexAttribArray(mTexCoordsSlot);
1219}
1220
Romain Guyaa6c24c2011-04-28 18:40:04 -07001221void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1222 bindExternalTexture(texture);
1223 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1224
1225 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1226 glEnableVertexAttribArray(mTexCoordsSlot);
1227}
1228
Romain Guy8f0095c2011-05-02 17:24:22 -07001229void OpenGLRenderer::setupDrawTextureTransform() {
1230 mDescription.hasTextureTransform = true;
1231}
1232
1233void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001234 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1235 GL_FALSE, &transform.data[0]);
1236}
1237
Romain Guy70ca14e2010-12-13 18:24:33 -08001238void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1239 if (!vertices) {
1240 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1241 } else {
1242 mCaches.unbindMeshBuffer();
1243 }
1244 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1245 gMeshStride, vertices);
David Licf289572011-02-25 12:05:44 -08001246 if (mTexCoordsSlot >= 0) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001247 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1248 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001249}
1250
Chet Haase5b0200b2011-04-13 17:58:08 -07001251void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
1252 mCaches.unbindMeshBuffer();
1253 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1254 gVertexStride, vertices);
1255}
1256
1257/**
1258 * 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 -07001259 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1260 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1261 * attributes (one per vertex) are values from zero to one that tells the fragment
1262 * shader where the fragment is in relation to the line width/length overall; these values are
1263 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1264 * region of the line.
1265 * Note that we only pass down the width values in this setup function. The length coordinates
1266 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001267 */
Chet Haase99585ad2011-05-02 15:00:16 -07001268void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Chet Haase99ecdc42011-05-06 12:06:34 -07001269 GLvoid* lengthCoords, float boundaryWidthProportion) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001270 mCaches.unbindMeshBuffer();
1271 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Chet Haase99585ad2011-05-02 15:00:16 -07001272 gAAVertexStride, vertices);
1273 int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
1274 glEnableVertexAttribArray(widthSlot);
1275 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
1276 int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
1277 glEnableVertexAttribArray(lengthSlot);
1278 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Chet Haase5b0200b2011-04-13 17:58:08 -07001279 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001280 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07001281 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001282 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001283 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
Chet Haase5b0200b2011-04-13 17:58:08 -07001284}
1285
Romain Guy70ca14e2010-12-13 18:24:33 -08001286void OpenGLRenderer::finishDrawTexture() {
1287 glDisableVertexAttribArray(mTexCoordsSlot);
1288}
1289
1290///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001291// Drawing
1292///////////////////////////////////////////////////////////////////////////////
1293
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001294bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
1295 Rect& dirty, uint32_t level) {
1296 if (quickReject(0.0f, 0.0f, width, height)) {
1297 return false;
1298 }
1299
Romain Guy0fe478e2010-11-08 12:08:41 -08001300 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1301 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001302 if (displayList && displayList->isRenderable()) {
Romain Guycabfcc12011-03-07 18:06:46 -08001303 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001304 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001305
Chet Haasedaf98e92011-01-10 14:10:36 -08001306 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001307}
1308
Chet Haaseed30fd82011-04-22 16:18:45 -07001309void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1310 if (displayList) {
1311 displayList->output(*this, level);
1312 }
1313}
1314
Romain Guya168d732011-03-18 16:50:13 -07001315void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1316 int alpha;
1317 SkXfermode::Mode mode;
1318 getAlphaAndMode(paint, &alpha, &mode);
1319
Romain Guya168d732011-03-18 16:50:13 -07001320 float x = left;
1321 float y = top;
1322
Romain Guye3c26852011-07-25 16:36:01 -07001323 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001324 bool ignoreTransform = false;
1325 if (mSnapshot->transform->isPureTranslate()) {
1326 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1327 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1328 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001329 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001330 } else {
1331 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001332 }
1333
1334 setupDraw();
1335 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001336 if (paint) {
1337 setupDrawAlpha8Color(paint->getColor(), alpha);
1338 }
Romain Guya168d732011-03-18 16:50:13 -07001339 setupDrawColorFilter();
1340 setupDrawShader();
1341 setupDrawBlending(true, mode);
1342 setupDrawProgram();
1343 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001344
Romain Guya168d732011-03-18 16:50:13 -07001345 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001346 texture->setWrap(GL_CLAMP_TO_EDGE);
1347 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001348
Romain Guya168d732011-03-18 16:50:13 -07001349 setupDrawPureColorUniforms();
1350 setupDrawColorFilterUniforms();
1351 setupDrawShaderUniforms();
1352 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1353
1354 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1355
1356 finishDrawTexture();
1357}
1358
Chet Haase5c13d892010-10-08 08:37:55 -07001359void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001360 const float right = left + bitmap->width();
1361 const float bottom = top + bitmap->height();
1362
1363 if (quickReject(left, top, right, bottom)) {
1364 return;
1365 }
1366
Romain Guy86568192010-12-14 15:55:39 -08001367 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001368 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001369 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001370 const AutoTexture autoCleanup(texture);
1371
Romain Guya168d732011-03-18 16:50:13 -07001372 if (bitmap->getConfig() == SkBitmap::kA8_Config) {
1373 drawAlphaBitmap(texture, left, top, paint);
1374 } else {
1375 drawTextureRect(left, top, right, bottom, texture, paint);
1376 }
Romain Guyce0537b2010-06-29 21:05:21 -07001377}
1378
Chet Haase5c13d892010-10-08 08:37:55 -07001379void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001380 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1381 const mat4 transform(*matrix);
1382 transform.mapRect(r);
1383
Romain Guy6926c722010-07-12 20:20:03 -07001384 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1385 return;
1386 }
1387
Romain Guy86568192010-12-14 15:55:39 -08001388 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001389 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001390 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001391 const AutoTexture autoCleanup(texture);
1392
Romain Guy5b3b3522010-10-27 18:57:51 -07001393 // This could be done in a cheaper way, all we need is pass the matrix
1394 // to the vertex shader. The save/restore is a bit overkill.
1395 save(SkCanvas::kMatrix_SaveFlag);
1396 concatMatrix(matrix);
1397 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1398 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001399}
1400
Romain Guy5a7b4662011-01-20 19:09:30 -08001401void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1402 float* vertices, int* colors, SkPaint* paint) {
1403 // TODO: Do a quickReject
1404 if (!vertices || mSnapshot->isIgnored()) {
1405 return;
1406 }
1407
1408 glActiveTexture(gTextureUnits[0]);
1409 Texture* texture = mCaches.textureCache.get(bitmap);
1410 if (!texture) return;
1411 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001412
Romain Guyd21b6e12011-11-30 20:21:23 -08001413 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1414 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001415
1416 int alpha;
1417 SkXfermode::Mode mode;
1418 getAlphaAndMode(paint, &alpha, &mode);
1419
Romain Guy5a7b4662011-01-20 19:09:30 -08001420 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001421
Romain Guyb18d2d02011-02-10 15:52:54 -08001422 float left = FLT_MAX;
1423 float top = FLT_MAX;
1424 float right = FLT_MIN;
1425 float bottom = FLT_MIN;
1426
1427#if RENDER_LAYERS_AS_REGIONS
1428 bool hasActiveLayer = hasLayer();
1429#else
1430 bool hasActiveLayer = false;
1431#endif
1432
Romain Guya566b7c2011-01-23 16:36:11 -08001433 // TODO: Support the colors array
1434 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001435 TextureVertex* vertex = mesh;
1436 for (int32_t y = 0; y < meshHeight; y++) {
1437 for (int32_t x = 0; x < meshWidth; x++) {
1438 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1439
1440 float u1 = float(x) / meshWidth;
1441 float u2 = float(x + 1) / meshWidth;
1442 float v1 = float(y) / meshHeight;
1443 float v2 = float(y + 1) / meshHeight;
1444
1445 int ax = i + (meshWidth + 1) * 2;
1446 int ay = ax + 1;
1447 int bx = i;
1448 int by = bx + 1;
1449 int cx = i + 2;
1450 int cy = cx + 1;
1451 int dx = i + (meshWidth + 1) * 2 + 2;
1452 int dy = dx + 1;
1453
1454 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1455 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1456 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1457
1458 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1459 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1460 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001461
1462#if RENDER_LAYERS_AS_REGIONS
1463 if (hasActiveLayer) {
1464 // TODO: This could be optimized to avoid unnecessary ops
1465 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1466 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1467 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1468 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1469 }
1470#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001471 }
1472 }
1473
Romain Guyb18d2d02011-02-10 15:52:54 -08001474#if RENDER_LAYERS_AS_REGIONS
1475 if (hasActiveLayer) {
1476 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1477 }
1478#endif
1479
Romain Guy5a7b4662011-01-20 19:09:30 -08001480 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1481 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001482 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001483}
1484
Romain Guy8ba548f2010-06-30 19:21:21 -07001485void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1486 float srcLeft, float srcTop, float srcRight, float srcBottom,
1487 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001488 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001489 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1490 return;
1491 }
1492
Romain Guy746b7402010-10-26 16:27:31 -07001493 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001494 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001495 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001496 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001497
Romain Guy8ba548f2010-06-30 19:21:21 -07001498 const float width = texture->width;
1499 const float height = texture->height;
1500
Romain Guy68169722011-08-22 17:33:33 -07001501 const float u1 = fmax(0.0f, srcLeft / width);
1502 const float v1 = fmax(0.0f, srcTop / height);
1503 const float u2 = fmin(1.0f, srcRight / width);
1504 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001505
Romain Guy03750a02010-10-18 14:06:08 -07001506 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001507 resetDrawTextureTexCoords(u1, v1, u2, v2);
1508
Romain Guy03750a02010-10-18 14:06:08 -07001509 int alpha;
1510 SkXfermode::Mode mode;
1511 getAlphaAndMode(paint, &alpha, &mode);
1512
Romain Guyd21b6e12011-11-30 20:21:23 -08001513 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1514
Romain Guy6620c6d2010-12-06 18:07:02 -08001515 if (mSnapshot->transform->isPureTranslate()) {
1516 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1517 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1518
Romain Guyb5014982011-07-28 15:39:12 -07001519 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001520 // Enable linear filtering if the source rectangle is scaled
1521 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001522 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001523 }
Romain Guyb5014982011-07-28 15:39:12 -07001524
Romain Guyd21b6e12011-11-30 20:21:23 -08001525 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001526 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1527 texture->id, alpha / 255.0f, mode, texture->blend,
1528 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1529 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1530 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001531 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001532 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1533 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1534 GL_TRIANGLE_STRIP, gMeshCount);
1535 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001536
1537 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1538}
1539
Romain Guy4aa90572010-09-26 18:40:37 -07001540void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001541 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001542 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001543 if (quickReject(left, top, right, bottom)) {
1544 return;
1545 }
1546
Romain Guy746b7402010-10-26 16:27:31 -07001547 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001548 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001549 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001550 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001551 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1552 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001553
1554 int alpha;
1555 SkXfermode::Mode mode;
1556 getAlphaAndMode(paint, &alpha, &mode);
1557
Romain Guy2728f962010-10-08 18:36:15 -07001558 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001559 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001560
Romain Guya5ef39a2010-12-03 16:48:20 -08001561 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001562 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001563#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001564 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001565 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001566 const float offsetX = left + mSnapshot->transform->getTranslateX();
1567 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001568 const size_t count = mesh->quads.size();
1569 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001570 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001571 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001572 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1573 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1574 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001575 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001576 dirtyLayer(left + bounds.left, top + bounds.top,
1577 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001578 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001579 }
1580 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001581#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001582
Romain Guy6620c6d2010-12-06 18:07:02 -08001583 if (pureTranslate) {
1584 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1585 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1586
1587 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1588 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1589 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1590 true, !mesh->hasEmptyQuads);
1591 } else {
1592 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1593 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1594 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1595 true, !mesh->hasEmptyQuads);
1596 }
Romain Guy054dc182010-10-15 17:55:25 -07001597 }
Romain Guyf7f93552010-07-08 19:17:03 -07001598}
1599
Chet Haase99ecdc42011-05-06 12:06:34 -07001600/**
Chet Haase858aa932011-05-12 09:06:00 -07001601 * This function uses a similar approach to that of AA lines in the drawLines() function.
1602 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1603 * shader to compute the translucency of the color, determined by whether a given pixel is
1604 * within that boundary region and how far into the region it is.
1605 */
1606void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001607 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001608 float inverseScaleX = 1.0f;
1609 float inverseScaleY = 1.0f;
1610 // The quad that we use needs to account for scaling.
1611 if (!mSnapshot->transform->isPureTranslate()) {
1612 Matrix4 *mat = mSnapshot->transform;
1613 float m00 = mat->data[Matrix4::kScaleX];
1614 float m01 = mat->data[Matrix4::kSkewY];
1615 float m02 = mat->data[2];
1616 float m10 = mat->data[Matrix4::kSkewX];
1617 float m11 = mat->data[Matrix4::kScaleX];
1618 float m12 = mat->data[6];
1619 float scaleX = sqrt(m00 * m00 + m01 * m01);
1620 float scaleY = sqrt(m10 * m10 + m11 * m11);
1621 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1622 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1623 }
1624
1625 setupDraw();
1626 setupDrawAALine();
1627 setupDrawColor(color);
1628 setupDrawColorFilter();
1629 setupDrawShader();
1630 setupDrawBlending(true, mode);
1631 setupDrawProgram();
1632 setupDrawModelViewIdentity(true);
1633 setupDrawColorUniforms();
1634 setupDrawColorFilterUniforms();
1635 setupDrawShaderIdentityUniforms();
1636
1637 AAVertex rects[4];
1638 AAVertex* aaVertices = &rects[0];
1639 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1640 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1641
1642 float boundarySizeX = .5 * inverseScaleX;
1643 float boundarySizeY = .5 * inverseScaleY;
1644
1645 // Adjust the rect by the AA boundary padding
1646 left -= boundarySizeX;
1647 right += boundarySizeX;
1648 top -= boundarySizeY;
1649 bottom += boundarySizeY;
1650
1651 float width = right - left;
1652 float height = bottom - top;
1653
1654 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1655 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
1656 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
1657 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1658 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1659 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
1660 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryHeightProportion));
1661
1662 if (!quickReject(left, top, right, bottom)) {
1663 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1664 AAVertex::set(aaVertices++, left, top, 1, 0);
1665 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1666 AAVertex::set(aaVertices++, right, top, 0, 0);
1667 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1668 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1669 }
1670}
1671
1672/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001673 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1674 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1675 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1676 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1677 * of the line. Hairlines are more involved because we need to account for transform scaling
1678 * to end up with a one-pixel-wide line in screen space..
1679 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1680 * in combination with values that we calculate and pass down in this method. The basic approach
1681 * is that the quad we create contains both the core line area plus a bounding area in which
1682 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1683 * proportion of the width and the length of a given segment is represented by the boundary
1684 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1685 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1686 * on the inside). This ends up giving the result we want, with pixels that are completely
1687 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1688 * how far into the boundary region they are, which is determined by shader interpolation.
1689 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001690void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1691 if (mSnapshot->isIgnored()) return;
1692
1693 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001694 // We use half the stroke width here because we're going to position the quad
1695 // corner vertices half of the width away from the line endpoints
1696 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001697 // A stroke width of 0 has a special meaning in Skia:
1698 // it draws a line 1 px wide regardless of current transform
1699 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase99ecdc42011-05-06 12:06:34 -07001700 float inverseScaleX = 1.0f;
1701 float inverseScaleY = 1.0f;
1702 bool scaled = false;
Chet Haase8a5cc922011-04-26 07:28:09 -07001703 int alpha;
1704 SkXfermode::Mode mode;
1705 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001706 int verticesCount = count;
1707 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001708 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001709 verticesCount += (count - 4);
1710 }
1711
1712 if (isHairLine || isAA) {
1713 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1714 // the line on the screen should always be one pixel wide regardless of scale. For
1715 // AA lines, we only want one pixel of translucent boundary around the quad.
1716 if (!mSnapshot->transform->isPureTranslate()) {
1717 Matrix4 *mat = mSnapshot->transform;
1718 float m00 = mat->data[Matrix4::kScaleX];
1719 float m01 = mat->data[Matrix4::kSkewY];
1720 float m02 = mat->data[2];
1721 float m10 = mat->data[Matrix4::kSkewX];
1722 float m11 = mat->data[Matrix4::kScaleX];
1723 float m12 = mat->data[6];
1724 float scaleX = sqrt(m00*m00 + m01*m01);
1725 float scaleY = sqrt(m10*m10 + m11*m11);
1726 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1727 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1728 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1729 scaled = true;
1730 }
1731 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001732 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001733
1734 getAlphaAndMode(paint, &alpha, &mode);
1735 setupDraw();
1736 if (isAA) {
1737 setupDrawAALine();
1738 }
1739 setupDrawColor(paint->getColor(), alpha);
1740 setupDrawColorFilter();
1741 setupDrawShader();
1742 if (isAA) {
1743 setupDrawBlending(true, mode);
1744 } else {
1745 setupDrawBlending(mode);
1746 }
1747 setupDrawProgram();
1748 setupDrawModelViewIdentity(true);
1749 setupDrawColorUniforms();
1750 setupDrawColorFilterUniforms();
1751 setupDrawShaderIdentityUniforms();
1752
1753 if (isHairLine) {
1754 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001755 halfStrokeWidth = isAA? 1 : .5;
1756 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001757 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001758 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001759 }
1760 Vertex lines[verticesCount];
1761 Vertex* vertices = &lines[0];
Chet Haase99585ad2011-05-02 15:00:16 -07001762 AAVertex wLines[verticesCount];
1763 AAVertex* aaVertices = &wLines[0];
Chet Haase5b0200b2011-04-13 17:58:08 -07001764 if (!isAA) {
1765 setupDrawVertices(vertices);
1766 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001767 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1768 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001769 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001770 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1771 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001772 // We will need to calculate the actual width proportion on each segment for
1773 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1774 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
1775 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
Chet Haase5b0200b2011-04-13 17:58:08 -07001776 }
1777
Chet Haase99ecdc42011-05-06 12:06:34 -07001778 AAVertex* prevAAVertex = NULL;
1779 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001780
Chet Haase99585ad2011-05-02 15:00:16 -07001781 int boundaryLengthSlot = -1;
1782 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001783 int boundaryWidthSlot = -1;
1784 int inverseBoundaryWidthSlot = -1;
Chet Haase5b0200b2011-04-13 17:58:08 -07001785 for (int i = 0; i < count; i += 4) {
1786 // a = start point, b = end point
1787 vec2 a(points[i], points[i + 1]);
1788 vec2 b(points[i + 2], points[i + 3]);
Chet Haase99585ad2011-05-02 15:00:16 -07001789 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001790 float boundaryLengthProportion = 0;
1791 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001792
Chet Haase5b0200b2011-04-13 17:58:08 -07001793 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001794 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001795 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001796 if (isAA) {
1797 float wideningFactor;
1798 if (fabs(n.x) >= fabs(n.y)) {
1799 wideningFactor = fabs(1.0f / n.x);
1800 } else {
1801 wideningFactor = fabs(1.0f / n.y);
1802 }
1803 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001804 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001805 if (scaled) {
1806 n.x *= inverseScaleX;
1807 n.y *= inverseScaleY;
1808 }
1809 } else if (scaled) {
1810 // Extend n by .5 pixel on each side, post-transform
1811 vec2 extendedN = n.copyNormalized();
1812 extendedN /= 2;
1813 extendedN.x *= inverseScaleX;
1814 extendedN.y *= inverseScaleY;
1815 float extendedNLength = extendedN.length();
1816 // We need to set this value on the shader prior to drawing
1817 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1818 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001819 }
1820 float x = n.x;
1821 n.x = -n.y;
1822 n.y = x;
1823
Chet Haase99585ad2011-05-02 15:00:16 -07001824 // aa lines expand the endpoint vertices to encompass the AA boundary
1825 if (isAA) {
1826 vec2 abVector = (b - a);
1827 length = abVector.length();
1828 abVector.normalize();
Chet Haase99ecdc42011-05-06 12:06:34 -07001829 if (scaled) {
1830 abVector.x *= inverseScaleX;
1831 abVector.y *= inverseScaleY;
1832 float abLength = abVector.length();
1833 boundaryLengthProportion = abLength / (length + abLength);
1834 } else {
1835 boundaryLengthProportion = .5 / (length + 1);
1836 }
1837 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001838 a -= abVector;
1839 b += abVector;
1840 }
1841
Chet Haase5b0200b2011-04-13 17:58:08 -07001842 // Four corners of the rectangle defining a thick line
1843 vec2 p1 = a - n;
1844 vec2 p2 = a + n;
1845 vec2 p3 = b + n;
1846 vec2 p4 = b - n;
1847
Chet Haase99585ad2011-05-02 15:00:16 -07001848
Chet Haase5b0200b2011-04-13 17:58:08 -07001849 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1850 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1851 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1852 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1853
1854 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001855 if (!isAA) {
1856 if (prevVertex != NULL) {
1857 // Issue two repeat vertices to create degenerate triangles to bridge
1858 // between the previous line and the new one. This is necessary because
1859 // we are creating a single triangle_strip which will contain
1860 // potentially discontinuous line segments.
1861 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1862 Vertex::set(vertices++, p1.x, p1.y);
1863 generatedVerticesCount += 2;
1864 }
1865 Vertex::set(vertices++, p1.x, p1.y);
1866 Vertex::set(vertices++, p2.x, p2.y);
1867 Vertex::set(vertices++, p4.x, p4.y);
1868 Vertex::set(vertices++, p3.x, p3.y);
1869 prevVertex = vertices - 1;
1870 generatedVerticesCount += 4;
1871 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001872 if (!isHairLine && scaled) {
1873 // Must set width proportions per-segment for scaled non-hairlines to use the
1874 // correct AA boundary dimensions
1875 if (boundaryWidthSlot < 0) {
1876 boundaryWidthSlot =
1877 mCaches.currentProgram->getUniform("boundaryWidth");
1878 inverseBoundaryWidthSlot =
1879 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1880 }
1881 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1882 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1883 }
Chet Haase99585ad2011-05-02 15:00:16 -07001884 if (boundaryLengthSlot < 0) {
1885 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1886 inverseBoundaryLengthSlot =
1887 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1888 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001889 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
1890 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07001891
Chet Haase5b0200b2011-04-13 17:58:08 -07001892 if (prevAAVertex != NULL) {
1893 // Issue two repeat vertices to create degenerate triangles to bridge
1894 // between the previous line and the new one. This is necessary because
1895 // we are creating a single triangle_strip which will contain
1896 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07001897 AAVertex::set(aaVertices++,prevAAVertex->position[0],
1898 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
1899 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07001900 generatedVerticesCount += 2;
1901 }
Chet Haase99585ad2011-05-02 15:00:16 -07001902 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
1903 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
1904 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
1905 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Chet Haase5b0200b2011-04-13 17:58:08 -07001906 prevAAVertex = aaVertices - 1;
1907 generatedVerticesCount += 4;
1908 }
Chet Haase99585ad2011-05-02 15:00:16 -07001909 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
1910 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
1911 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07001912 }
1913 }
1914 if (generatedVerticesCount > 0) {
1915 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
1916 }
1917}
1918
Romain Guyed6fcb02011-03-21 13:11:28 -07001919void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
1920 if (mSnapshot->isIgnored()) return;
1921
1922 // TODO: The paint's cap style defines whether the points are square or circular
1923 // TODO: Handle AA for round points
1924
Chet Haase5b0200b2011-04-13 17:58:08 -07001925 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07001926 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07001927 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07001928 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001929 if (isHairLine) {
1930 // Now that we know it's hairline, we can set the effective width, to be used later
1931 strokeWidth = 1.0f;
1932 }
1933 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07001934 int alpha;
1935 SkXfermode::Mode mode;
1936 getAlphaAndMode(paint, &alpha, &mode);
1937
1938 int verticesCount = count >> 1;
1939 int generatedVerticesCount = 0;
1940
1941 TextureVertex pointsData[verticesCount];
1942 TextureVertex* vertex = &pointsData[0];
1943
1944 setupDraw();
Chet Haase8a5cc922011-04-26 07:28:09 -07001945 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07001946 setupDrawColor(paint->getColor(), alpha);
1947 setupDrawColorFilter();
1948 setupDrawShader();
1949 setupDrawBlending(mode);
1950 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07001951 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07001952 setupDrawColorUniforms();
1953 setupDrawColorFilterUniforms();
1954 setupDrawPointUniforms();
1955 setupDrawShaderIdentityUniforms();
1956 setupDrawMesh(vertex);
1957
1958 for (int i = 0; i < count; i += 2) {
1959 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1960 generatedVerticesCount++;
Chet Haase8a5cc922011-04-26 07:28:09 -07001961 float left = points[i] - halfWidth;
1962 float right = points[i] + halfWidth;
1963 float top = points[i + 1] - halfWidth;
1964 float bottom = points [i + 1] + halfWidth;
1965 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07001966 }
1967
1968 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
1969}
1970
Romain Guy85bf02f2010-06-22 13:11:24 -07001971void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001972 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001973 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001974
Romain Guyae88e5e2010-10-22 17:49:18 -07001975 Rect& clip(*mSnapshot->clipRect);
1976 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001977
Romain Guy3d58c032010-07-14 16:34:53 -07001978 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001979}
Romain Guy9d5316e2010-06-24 19:30:36 -07001980
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001981void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001982 if (!texture) return;
1983 const AutoTexture autoCleanup(texture);
1984
1985 const float x = left + texture->left - texture->offset;
1986 const float y = top + texture->top - texture->offset;
1987
1988 drawPathTexture(texture, x, y, paint);
1989}
1990
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001991void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1992 float rx, float ry, SkPaint* paint) {
1993 if (mSnapshot->isIgnored()) return;
1994
1995 glActiveTexture(gTextureUnits[0]);
1996 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1997 right - left, bottom - top, rx, ry, paint);
1998 drawShape(left, top, texture, paint);
1999}
2000
Romain Guy01d58e42011-01-19 21:54:02 -08002001void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2002 if (mSnapshot->isIgnored()) return;
2003
2004 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08002005 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002006 drawShape(x - radius, y - radius, texture, paint);
2007}
Romain Guy01d58e42011-01-19 21:54:02 -08002008
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002009void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2010 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002011
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002012 glActiveTexture(gTextureUnits[0]);
2013 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2014 drawShape(left, top, texture, paint);
2015}
2016
Romain Guy8b2f5262011-01-23 16:15:02 -08002017void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2018 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2019 if (mSnapshot->isIgnored()) return;
2020
2021 if (fabs(sweepAngle) >= 360.0f) {
2022 drawOval(left, top, right, bottom, paint);
2023 return;
2024 }
2025
2026 glActiveTexture(gTextureUnits[0]);
2027 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2028 startAngle, sweepAngle, useCenter, paint);
2029 drawShape(left, top, texture, paint);
2030}
2031
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002032void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2033 SkPaint* paint) {
2034 if (mSnapshot->isIgnored()) return;
2035
2036 glActiveTexture(gTextureUnits[0]);
2037 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2038 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002039}
2040
Chet Haase5c13d892010-10-08 08:37:55 -07002041void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002042 if (p->getStyle() != SkPaint::kFill_Style) {
2043 drawRectAsShape(left, top, right, bottom, p);
2044 return;
2045 }
2046
Romain Guy6926c722010-07-12 20:20:03 -07002047 if (quickReject(left, top, right, bottom)) {
2048 return;
2049 }
2050
Romain Guy026c5e162010-06-28 17:12:22 -07002051 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002052 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002053 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2054 if (!isMode) {
2055 // Assume SRC_OVER
2056 mode = SkXfermode::kSrcOver_Mode;
2057 }
2058 } else {
2059 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002060 }
2061
Romain Guy026c5e162010-06-28 17:12:22 -07002062 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002063 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002064 drawAARect(left, top, right, bottom, color, mode);
2065 } else {
2066 drawColorRect(left, top, right, bottom, color, mode);
2067 }
Romain Guyc7d53492010-06-25 13:41:57 -07002068}
Romain Guy9d5316e2010-06-24 19:30:36 -07002069
Romain Guye8e62a42010-07-23 18:55:21 -07002070void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002071 float x, float y, SkPaint* paint, float length) {
Romain Guyb146b122010-12-15 17:06:45 -08002072 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07002073 return;
2074 }
Romain Guyaf636eb2010-12-09 17:47:21 -08002075 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07002076
Romain Guy8f9a9f62011-12-05 11:53:26 -08002077 // NOTE: AA and glyph id encoding are set in DisplayListRenderer.cpp
Romain Guye8e62a42010-07-23 18:55:21 -07002078
Romain Guye8e62a42010-07-23 18:55:21 -07002079 switch (paint->getTextAlign()) {
2080 case SkPaint::kCenter_Align:
Romain Guycac5fd32011-12-01 20:08:50 -08002081 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002082 x -= length / 2.0f;
2083 break;
2084 case SkPaint::kRight_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;
2087 break;
2088 default:
2089 break;
2090 }
2091
Romain Guycac5fd32011-12-01 20:08:50 -08002092 SkPaint::FontMetrics metrics;
2093 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy8f9a9f62011-12-05 11:53:26 -08002094 // If no length was specified, just perform the hit test on the Y axis
Romain Guycac5fd32011-12-01 20:08:50 -08002095 if (quickReject(x, y + metrics.fTop,
2096 x + (length >= 0.0f ? length : INT_MAX / 2), y + metrics.fBottom)) {
2097 return;
2098 }
2099
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002100 const float oldX = x;
2101 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002102 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2103 if (pureTranslate) {
2104 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2105 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2106 }
2107
Romain Guyb45c0c92010-08-26 20:35:23 -07002108 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002109#if DEBUG_GLYPHS
2110 LOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
2111#endif
Romain Guyb45c0c92010-08-26 20:35:23 -07002112 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002113 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002114
Romain Guy86568192010-12-14 15:55:39 -08002115 int alpha;
2116 SkXfermode::Mode mode;
2117 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002118
Romain Guy1e45aae2010-08-13 19:39:53 -07002119 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07002120 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002121 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2122 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002123 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002124
Romain Guy740bf2b2011-04-26 15:33:10 -07002125 const float sx = oldX - shadow->left + mShadowDx;
2126 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002127
Romain Guy86568192010-12-14 15:55:39 -08002128 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002129 int shadowColor = mShadowColor;
2130 if (mShader) {
2131 shadowColor = 0xffffffff;
2132 }
Romain Guy86568192010-12-14 15:55:39 -08002133
2134 glActiveTexture(gTextureUnits[0]);
2135 setupDraw();
2136 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002137 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2138 setupDrawColorFilter();
2139 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002140 setupDrawBlending(true, mode);
2141 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002142 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002143 setupDrawTexture(shadow->id);
2144 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002145 setupDrawColorFilterUniforms();
2146 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002147 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2148
Romain Guy0a417492010-08-16 20:26:20 -07002149 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy740bf2b2011-04-26 15:33:10 -07002150
Romain Guy86568192010-12-14 15:55:39 -08002151 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07002152 }
2153
Romain Guyb146b122010-12-15 17:06:45 -08002154 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
2155 return;
2156 }
2157
Romain Guy6620c6d2010-12-06 18:07:02 -08002158 // Pick the appropriate texture filtering
2159 bool linearFilter = mSnapshot->transform->changesBounds();
2160 if (pureTranslate && !linearFilter) {
2161 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2162 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002163
Romain Guy86568192010-12-14 15:55:39 -08002164 glActiveTexture(gTextureUnits[0]);
2165 setupDraw();
2166 setupDrawDirtyRegionsDisabled();
2167 setupDrawWithTexture(true);
2168 setupDrawAlpha8Color(paint->getColor(), alpha);
2169 setupDrawColorFilter();
2170 setupDrawShader();
2171 setupDrawBlending(true, mode);
2172 setupDrawProgram();
2173 setupDrawModelView(x, y, x, y, pureTranslate, true);
2174 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2175 setupDrawPureColorUniforms();
2176 setupDrawColorFilterUniforms();
2177 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002178
Romain Guy6620c6d2010-12-06 18:07:02 -08002179 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002180 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2181
2182#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002183 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002184#else
Romain Guyf219da52011-01-16 12:54:25 -08002185 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002186#endif
Romain Guy03750a02010-10-18 14:06:08 -07002187 mCaches.unbindMeshBuffer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002188
2189 // Tell font renderer the locations of position and texture coord
2190 // attributes so it can bind its data properly
2191 int positionSlot = mCaches.currentProgram->position;
2192 fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot);
Romain Guy6620c6d2010-12-06 18:07:02 -08002193 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002194 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002195#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002196 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002197 if (!pureTranslate) {
2198 mSnapshot->transform->mapRect(bounds);
2199 }
Romain Guyf219da52011-01-16 12:54:25 -08002200 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002201 }
2202#endif
2203 }
Romain Guy694b5192010-07-21 21:33:20 -07002204
2205 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07002206 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07002207
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002208 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002209}
2210
Romain Guy7fbcc042010-08-04 15:40:07 -07002211void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002212 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002213
Romain Guy01d58e42011-01-19 21:54:02 -08002214 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07002215
Romain Guyfb8b7632010-08-23 21:05:08 -07002216 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -07002217 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002218 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002219
Romain Guy8b55f372010-08-18 17:10:07 -07002220 const float x = texture->left - texture->offset;
2221 const float y = texture->top - texture->offset;
2222
Romain Guy01d58e42011-01-19 21:54:02 -08002223 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002224}
2225
Romain Guyada830f2011-01-13 12:13:20 -08002226void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2227 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002228 return;
2229 }
2230
2231 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08002232
2233 int alpha;
2234 SkXfermode::Mode mode;
2235 getAlphaAndMode(paint, &alpha, &mode);
2236
Romain Guy9ace8f52011-07-07 20:50:11 -07002237 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002238
Romain Guyf219da52011-01-16 12:54:25 -08002239#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08002240 if (!layer->region.isEmpty()) {
2241 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002242 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002243 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002244 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002245 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002246
Romain Guyc88e3572011-01-22 00:32:12 -08002247 setupDraw();
2248 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002249 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002250 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002251 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002252 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002253 setupDrawPureColorUniforms();
2254 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002255 setupDrawTexture(layer->getTexture());
2256 if (mSnapshot->transform->isPureTranslate()) {
2257 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2258 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2259
Romain Guyd21b6e12011-11-30 20:21:23 -08002260 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002261 setupDrawModelViewTranslate(x, y,
2262 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2263 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002264 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002265 setupDrawModelViewTranslate(x, y,
2266 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2267 }
Romain Guyc88e3572011-01-22 00:32:12 -08002268 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002269
Romain Guyc88e3572011-01-22 00:32:12 -08002270 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2271 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002272
Romain Guyc88e3572011-01-22 00:32:12 -08002273 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002274
2275#if DEBUG_LAYERS_AS_REGIONS
2276 drawRegionRects(layer->region);
2277#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002278 }
Romain Guyf219da52011-01-16 12:54:25 -08002279 }
2280#else
Romain Guyada830f2011-01-13 12:13:20 -08002281 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2282 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002283#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002284}
2285
Romain Guy6926c722010-07-12 20:20:03 -07002286///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002287// Shaders
2288///////////////////////////////////////////////////////////////////////////////
2289
2290void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002291 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002292}
2293
Romain Guy06f96e22010-07-30 19:18:16 -07002294void OpenGLRenderer::setupShader(SkiaShader* shader) {
2295 mShader = shader;
2296 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002297 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002298 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002299}
2300
Romain Guyd27977d2010-07-14 19:18:51 -07002301///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002302// Color filters
2303///////////////////////////////////////////////////////////////////////////////
2304
2305void OpenGLRenderer::resetColorFilter() {
2306 mColorFilter = NULL;
2307}
2308
2309void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2310 mColorFilter = filter;
2311}
2312
2313///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002314// Drop shadow
2315///////////////////////////////////////////////////////////////////////////////
2316
2317void OpenGLRenderer::resetShadow() {
2318 mHasShadow = false;
2319}
2320
2321void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2322 mHasShadow = true;
2323 mShadowRadius = radius;
2324 mShadowDx = dx;
2325 mShadowDy = dy;
2326 mShadowColor = color;
2327}
2328
2329///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002330// Drawing implementation
2331///////////////////////////////////////////////////////////////////////////////
2332
Romain Guy01d58e42011-01-19 21:54:02 -08002333void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2334 float x, float y, SkPaint* paint) {
2335 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2336 return;
2337 }
2338
2339 int alpha;
2340 SkXfermode::Mode mode;
2341 getAlphaAndMode(paint, &alpha, &mode);
2342
2343 setupDraw();
2344 setupDrawWithTexture(true);
2345 setupDrawAlpha8Color(paint->getColor(), alpha);
2346 setupDrawColorFilter();
2347 setupDrawShader();
2348 setupDrawBlending(true, mode);
2349 setupDrawProgram();
2350 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2351 setupDrawTexture(texture->id);
2352 setupDrawPureColorUniforms();
2353 setupDrawColorFilterUniforms();
2354 setupDrawShaderUniforms();
2355 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2356
2357 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2358
2359 finishDrawTexture();
2360}
2361
Romain Guyf607bdc2010-09-10 19:20:06 -07002362// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002363#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2364#define kStdUnderline_Offset (1.0f / 9.0f)
2365#define kStdUnderline_Thickness (1.0f / 18.0f)
2366
2367void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2368 float x, float y, SkPaint* paint) {
2369 // Handle underline and strike-through
2370 uint32_t flags = paint->getFlags();
2371 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002372 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002373 float underlineWidth = length;
2374 // If length is > 0.0f, we already measured the text for the text alignment
2375 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002376 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002377 }
2378
2379 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002380 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002381 case SkPaint::kCenter_Align:
2382 offsetX = underlineWidth * 0.5f;
2383 break;
2384 case SkPaint::kRight_Align:
2385 offsetX = underlineWidth;
2386 break;
2387 default:
2388 break;
2389 }
2390
2391 if (underlineWidth > 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002392 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002393 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002394
Romain Guye20ecbd2010-09-22 19:49:04 -07002395 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002396 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002397
Romain Guyf6834472011-01-23 13:32:12 -08002398 int linesCount = 0;
2399 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2400 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2401
2402 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002403 float points[pointsCount];
2404 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002405
2406 if (flags & SkPaint::kUnderlineText_Flag) {
2407 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002408 points[currentPoint++] = left;
2409 points[currentPoint++] = top;
2410 points[currentPoint++] = left + underlineWidth;
2411 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002412 }
2413
2414 if (flags & SkPaint::kStrikeThruText_Flag) {
2415 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002416 points[currentPoint++] = left;
2417 points[currentPoint++] = top;
2418 points[currentPoint++] = left + underlineWidth;
2419 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002420 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002421
Romain Guy726aeba2011-06-01 14:52:00 -07002422 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002423
Romain Guy726aeba2011-06-01 14:52:00 -07002424 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002425 }
2426 }
2427}
2428
Romain Guy026c5e162010-06-28 17:12:22 -07002429void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002430 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002431 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002432 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002433 color |= 0x00ffffff;
2434 }
2435
Romain Guy70ca14e2010-12-13 18:24:33 -08002436 setupDraw();
2437 setupDrawColor(color);
2438 setupDrawShader();
2439 setupDrawColorFilter();
2440 setupDrawBlending(mode);
2441 setupDrawProgram();
2442 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2443 setupDrawColorUniforms();
2444 setupDrawShaderUniforms(ignoreTransform);
2445 setupDrawColorFilterUniforms();
2446 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002447
Romain Guyc95c8d62010-09-17 15:31:32 -07002448 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2449}
2450
Romain Guy82ba8142010-07-09 13:25:56 -07002451void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002452 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002453 int alpha;
2454 SkXfermode::Mode mode;
2455 getAlphaAndMode(paint, &alpha, &mode);
2456
Romain Guyd21b6e12011-11-30 20:21:23 -08002457 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002458
Romain Guy6620c6d2010-12-06 18:07:02 -08002459 if (mSnapshot->transform->isPureTranslate()) {
2460 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2461 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2462
Romain Guyd21b6e12011-11-30 20:21:23 -08002463 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002464 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2465 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2466 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2467 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002468 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002469 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2470 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2471 GL_TRIANGLE_STRIP, gMeshCount);
2472 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002473}
2474
Romain Guybd6b79b2010-06-26 00:13:53 -07002475void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002476 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2477 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002478 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002479}
2480
2481void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002482 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002483 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002484 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002485
Romain Guy746b7402010-10-26 16:27:31 -07002486 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002487 setupDrawWithTexture();
2488 setupDrawColor(alpha, alpha, alpha, alpha);
2489 setupDrawColorFilter();
2490 setupDrawBlending(blend, mode, swapSrcDst);
2491 setupDrawProgram();
2492 if (!dirty) {
2493 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002494 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002495 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002496 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002497 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002498 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002499 }
Romain Guy86568192010-12-14 15:55:39 -08002500 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002501 setupDrawColorFilterUniforms();
2502 setupDrawTexture(texture);
2503 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002504
Romain Guy6820ac82010-09-15 18:11:50 -07002505 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002506
2507 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002508}
2509
Romain Guya5aed0d2010-09-09 14:42:43 -07002510void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002511 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002512 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2513 if (blend) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002514 if (mode <= SkXfermode::kScreen_Mode) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002515 if (!mCaches.blend) {
2516 glEnable(GL_BLEND);
2517 }
Romain Guy82ba8142010-07-09 13:25:56 -07002518
Romain Guyf607bdc2010-09-10 19:20:06 -07002519 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2520 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07002521
Romain Guya5aed0d2010-09-09 14:42:43 -07002522 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2523 glBlendFunc(sourceMode, destMode);
2524 mCaches.lastSrcMode = sourceMode;
2525 mCaches.lastDstMode = destMode;
2526 }
2527 } else {
2528 // These blend modes are not supported by OpenGL directly and have
2529 // to be implemented using shaders. Since the shader will perform
2530 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07002531 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002532 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002533 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002534 }
2535
2536 if (mCaches.blend) {
2537 glDisable(GL_BLEND);
2538 }
2539 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07002540 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002541 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002542 glDisable(GL_BLEND);
2543 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002544 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002545}
2546
Romain Guy889f8d12010-07-29 14:37:42 -07002547bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002548 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002549 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002550 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002551 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002552 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002553 }
Romain Guy6926c722010-07-12 20:20:03 -07002554 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002555}
2556
Romain Guy026c5e162010-06-28 17:12:22 -07002557void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002558 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002559 TextureVertex::setUV(v++, u1, v1);
2560 TextureVertex::setUV(v++, u2, v1);
2561 TextureVertex::setUV(v++, u1, v2);
2562 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002563}
2564
Chet Haase5c13d892010-10-08 08:37:55 -07002565void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002566 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002567 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002568
2569 // Skia draws using the color's alpha channel if < 255
2570 // Otherwise, it uses the paint's alpha
2571 int color = paint->getColor();
2572 *alpha = (color >> 24) & 0xFF;
2573 if (*alpha == 255) {
2574 *alpha = paint->getAlpha();
2575 }
2576 } else {
2577 *mode = SkXfermode::kSrcOver_Mode;
2578 *alpha = 255;
2579 }
Romain Guy026c5e162010-06-28 17:12:22 -07002580}
2581
Romain Guya5aed0d2010-09-09 14:42:43 -07002582SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002583 SkXfermode::Mode resultMode;
2584 if (!SkXfermode::AsMode(mode, &resultMode)) {
2585 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002586 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002587 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002588}
2589
Romain Guy9d5316e2010-06-24 19:30:36 -07002590}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002591}; // namespace android