blob: ce424ac341d1a0614dc3292ae224a5e6fed6e10b [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 Guy9d5316e2010-06-24 19:30:36 -070050///////////////////////////////////////////////////////////////////////////////
51// Globals
52///////////////////////////////////////////////////////////////////////////////
53
Romain Guy889f8d12010-07-29 14:37:42 -070054/**
55 * Structure mapping Skia xfermodes to OpenGL blending factors.
56 */
57struct Blender {
58 SkXfermode::Mode mode;
59 GLenum src;
60 GLenum dst;
61}; // struct Blender
62
Romain Guy026c5e162010-06-28 17:12:22 -070063// In this array, the index of each Blender equals the value of the first
64// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
65static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070066 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
67 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
68 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
69 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
70 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
71 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
72 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
73 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
74 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
75 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
76 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
77 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
78 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
79 { SkXfermode::kMultiply_Mode, GL_ZERO, GL_SRC_COLOR },
80 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070081};
Romain Guye4d01122010-06-16 18:44:05 -070082
Romain Guy87a76572010-09-13 18:11:21 -070083// This array contains the swapped version of each SkXfermode. For instance
84// this array's SrcOver blending mode is actually DstOver. You can refer to
85// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070086static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070087 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
88 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
89 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
90 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
91 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
92 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
93 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
94 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
95 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
96 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
97 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
98 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
99 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
100 { SkXfermode::kMultiply_Mode, GL_DST_COLOR, GL_ZERO },
101 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700102};
103
Romain Guy889f8d12010-07-29 14:37:42 -0700104static const GLenum gTextureUnits[] = {
Romain Guyb146b122010-12-15 17:06:45 -0800105 GL_TEXTURE0,
106 GL_TEXTURE1,
107 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -0700108};
109
Romain Guyf6a11b82010-06-23 17:47:49 -0700110///////////////////////////////////////////////////////////////////////////////
111// Constructors/destructor
112///////////////////////////////////////////////////////////////////////////////
113
Romain Guyfb8b7632010-08-23 21:05:08 -0700114OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700115 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700116 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700117 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700118
Romain Guyac670c02010-07-27 17:39:27 -0700119 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
120
Romain Guyae5575b2010-07-29 18:48:04 -0700121 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700122}
123
Romain Guy85bf02f2010-06-22 13:11:24 -0700124OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700125 // The context has already been destroyed at this point, do not call
126 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700127}
128
Romain Guyf6a11b82010-06-23 17:47:49 -0700129///////////////////////////////////////////////////////////////////////////////
130// Setup
131///////////////////////////////////////////////////////////////////////////////
132
Romain Guy85bf02f2010-06-22 13:11:24 -0700133void OpenGLRenderer::setViewport(int width, int height) {
Romain Guyf2fc4602011-07-19 15:20:03 -0700134 glDisable(GL_DITHER);
Romain Guy08ae3172010-06-21 19:35:50 -0700135 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700136 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700137
138 mWidth = width;
139 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700140
141 mFirstSnapshot->height = height;
142 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700143
144 mDirtyClip = false;
Romain Guye4d01122010-06-16 18:44:05 -0700145}
146
Romain Guy6b7bd242010-10-06 19:49:23 -0700147void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800148 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
149}
150
151void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800152 mCaches.clearGarbage();
153
Romain Guy8aef54f2010-09-01 15:13:49 -0700154 mSnapshot = new Snapshot(mFirstSnapshot,
155 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800156 mSnapshot->fbo = getTargetFbo();
157
Romain Guy8fb95422010-08-17 18:38:51 -0700158 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700159
Romain Guyfb8b7632010-08-23 21:05:08 -0700160 glViewport(0, 0, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700161
Romain Guy7d7b5492011-01-24 16:33:45 -0800162 glEnable(GL_SCISSOR_TEST);
163 glScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
164 mSnapshot->setClip(left, top, right, bottom);
165
Romain Guy6b7bd242010-10-06 19:49:23 -0700166 if (!opaque) {
167 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
168 glClear(GL_COLOR_BUFFER_BIT);
169 }
Romain Guybb9524b2010-06-22 18:56:38 -0700170}
171
Romain Guyb025b9c2010-09-16 14:16:48 -0700172void OpenGLRenderer::finish() {
173#if DEBUG_OPENGL
174 GLenum status = GL_NO_ERROR;
175 while ((status = glGetError()) != GL_NO_ERROR) {
176 LOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800177 switch (status) {
178 case GL_OUT_OF_MEMORY:
179 LOGE(" OpenGLRenderer is out of memory!");
180 break;
181 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700182 }
183#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800184#if DEBUG_MEMORY_USAGE
185 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800186#else
187 if (mCaches.getDebugLevel() & kDebugMemory) {
188 mCaches.dumpMemoryUsage();
189 }
Romain Guyc15008e2010-11-10 11:59:15 -0800190#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700191}
192
Romain Guy6c319ca2011-01-11 14:29:25 -0800193void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700194 if (mCaches.currentProgram) {
195 if (mCaches.currentProgram->isInUse()) {
196 mCaches.currentProgram->remove();
197 mCaches.currentProgram = NULL;
198 }
199 }
Romain Guy50c0f092010-10-19 11:42:22 -0700200 mCaches.unbindMeshBuffer();
Romain Guyda8532c2010-08-31 11:50:35 -0700201}
202
Romain Guy6c319ca2011-01-11 14:29:25 -0800203void OpenGLRenderer::resume() {
Romain Guyeb993562010-10-05 18:14:38 -0700204 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700205
206 glEnable(GL_SCISSOR_TEST);
Romain Guy746b7402010-10-26 16:27:31 -0700207 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700208
Romain Guyf607bdc2010-09-10 19:20:06 -0700209 glDisable(GL_DITHER);
210
Romain Guy42f3a4b2011-01-19 13:42:26 -0800211 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy03750a02010-10-18 14:06:08 -0700212 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700213
Romain Guy50c0f092010-10-19 11:42:22 -0700214 mCaches.blend = true;
215 glEnable(GL_BLEND);
216 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
217 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700218}
219
Romain Guycabfcc12011-03-07 18:06:46 -0800220bool OpenGLRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800221 interrupt();
Romain Guyf90f8172011-01-25 22:53:24 -0800222 if (mDirtyClip) {
223 setScissorFromClip();
224 }
Romain Guyd643bb52011-03-01 14:55:21 -0800225
Romain Guy80911b82011-03-16 15:30:12 -0700226 Rect clip(*mSnapshot->clipRect);
227 clip.snapToPixelBoundaries();
228
Romain Guyd643bb52011-03-01 14:55:21 -0800229#if RENDER_LAYERS_AS_REGIONS
230 // Since we don't know what the functor will draw, let's dirty
231 // tne entire clip region
232 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800233 dirtyLayerUnchecked(clip, getRegion());
234 }
235#endif
236
Romain Guy08aa2cb2011-03-17 11:06:57 -0700237 DrawGlInfo info;
238 info.clipLeft = clip.left;
239 info.clipTop = clip.top;
240 info.clipRight = clip.right;
241 info.clipBottom = clip.bottom;
242 info.isLayer = hasLayer();
243 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700244
Romain Guy08aa2cb2011-03-17 11:06:57 -0700245 status_t result = (*functor)(0, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800246
247 if (result != 0) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700248 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800249 dirty.unionWith(localDirty);
250 }
251
Chet Haasedaf98e92011-01-10 14:10:36 -0800252 resume();
Romain Guycabfcc12011-03-07 18:06:46 -0800253 return result != 0;
Chet Haasedaf98e92011-01-10 14:10:36 -0800254}
255
Romain Guyf6a11b82010-06-23 17:47:49 -0700256///////////////////////////////////////////////////////////////////////////////
257// State management
258///////////////////////////////////////////////////////////////////////////////
259
Romain Guybb9524b2010-06-22 18:56:38 -0700260int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700261 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700262}
263
264int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700265 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700266}
267
268void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700269 if (mSaveCount > 1) {
270 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700271 }
Romain Guybb9524b2010-06-22 18:56:38 -0700272}
273
274void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700275 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700276
Romain Guy8fb95422010-08-17 18:38:51 -0700277 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700278 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700279 }
Romain Guybb9524b2010-06-22 18:56:38 -0700280}
281
Romain Guy8aef54f2010-09-01 15:13:49 -0700282int OpenGLRenderer::saveSnapshot(int flags) {
283 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700284 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700285}
286
287bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700288 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700289 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700290 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700291
Romain Guybd6b79b2010-06-26 00:13:53 -0700292 sp<Snapshot> current = mSnapshot;
293 sp<Snapshot> previous = mSnapshot->previous;
294
Romain Guyeb993562010-10-05 18:14:38 -0700295 if (restoreOrtho) {
296 Rect& r = previous->viewport;
297 glViewport(r.left, r.top, r.right, r.bottom);
298 mOrthoMatrix.load(current->orthoMatrix);
299 }
300
Romain Guy8b55f372010-08-18 17:10:07 -0700301 mSaveCount--;
302 mSnapshot = previous;
303
Romain Guy2542d192010-08-18 11:47:12 -0700304 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700305 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700306 }
Romain Guy2542d192010-08-18 11:47:12 -0700307
Romain Guy5ec99242010-11-03 16:19:08 -0700308 if (restoreLayer) {
309 composeLayer(current, previous);
310 }
311
Romain Guy2542d192010-08-18 11:47:12 -0700312 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700313}
314
Romain Guyf6a11b82010-06-23 17:47:49 -0700315///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700316// Layers
317///////////////////////////////////////////////////////////////////////////////
318
319int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700320 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700321 const GLuint previousFbo = mSnapshot->fbo;
322 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700323
Romain Guyaf636eb2010-12-09 17:47:21 -0800324 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700325 int alpha = 255;
326 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700327
Romain Guye45362c2010-11-03 19:58:32 -0700328 if (p) {
329 alpha = p->getAlpha();
330 if (!mCaches.extensions.hasFramebufferFetch()) {
331 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
332 if (!isMode) {
333 // Assume SRC_OVER
334 mode = SkXfermode::kSrcOver_Mode;
335 }
336 } else {
337 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700338 }
339 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700340 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700341 }
Romain Guyd55a8612010-06-28 17:42:46 -0700342
Romain Guydbc26d22010-10-11 17:58:29 -0700343 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
344 }
Romain Guyd55a8612010-06-28 17:42:46 -0700345
346 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700347}
348
349int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
350 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700351 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700352 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700353 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700354 SkPaint paint;
355 paint.setAlpha(alpha);
356 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700357 }
Romain Guyd55a8612010-06-28 17:42:46 -0700358}
Romain Guybd6b79b2010-06-26 00:13:53 -0700359
Romain Guy1c740bc2010-09-13 18:00:09 -0700360/**
361 * Layers are viewed by Skia are slightly different than layers in image editing
362 * programs (for instance.) When a layer is created, previously created layers
363 * and the frame buffer still receive every drawing command. For instance, if a
364 * layer is created and a shape intersecting the bounds of the layers and the
365 * framebuffer is draw, the shape will be drawn on both (unless the layer was
366 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
367 *
368 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
369 * texture. Unfortunately, this is inefficient as it requires every primitive to
370 * be drawn n + 1 times, where n is the number of active layers. In practice this
371 * means, for every primitive:
372 * - Switch active frame buffer
373 * - Change viewport, clip and projection matrix
374 * - Issue the drawing
375 *
376 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700377 * To avoid this, layers are implemented in a different way here, at least in the
378 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
379 * is set. When this flag is set we can redirect all drawing operations into a
380 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700381 *
382 * This implementation relies on the frame buffer being at least RGBA 8888. When
383 * a layer is created, only a texture is created, not an FBO. The content of the
384 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700385 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700386 * buffer and drawing continues as normal. This technique therefore treats the
387 * frame buffer as a scratch buffer for the layers.
388 *
389 * To compose the layers back onto the frame buffer, each layer texture
390 * (containing the original frame buffer data) is drawn as a simple quad over
391 * the frame buffer. The trick is that the quad is set as the composition
392 * destination in the blending equation, and the frame buffer becomes the source
393 * of the composition.
394 *
395 * Drawing layers with an alpha value requires an extra step before composition.
396 * An empty quad is drawn over the layer's region in the frame buffer. This quad
397 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
398 * quad is used to multiply the colors in the frame buffer. This is achieved by
399 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
400 * GL_ZERO, GL_SRC_ALPHA.
401 *
402 * Because glCopyTexImage2D() can be slow, an alternative implementation might
403 * be use to draw a single clipped layer. The implementation described above
404 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700405 *
406 * (1) The frame buffer is actually not cleared right away. To allow the GPU
407 * to potentially optimize series of calls to glCopyTexImage2D, the frame
408 * buffer is left untouched until the first drawing operation. Only when
409 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700410 */
Romain Guyd55a8612010-06-28 17:42:46 -0700411bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700412 float right, float bottom, int alpha, SkXfermode::Mode mode,
413 int flags, GLuint previousFbo) {
414 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700415 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700416
Romain Guyeb993562010-10-05 18:14:38 -0700417 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
418
Romain Guyf607bdc2010-09-10 19:20:06 -0700419 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700420 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700421 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700422 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700423
Romain Guyeb993562010-10-05 18:14:38 -0700424 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700425 if (bounds.intersect(*snapshot->clipRect)) {
426 // We cannot work with sub-pixels in this case
427 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700428
Romain Guyad37cd32011-03-15 11:12:25 -0700429 // When the layer is not an FBO, we may use glCopyTexImage so we
430 // need to make sure the layer does not extend outside the bounds
431 // of the framebuffer
432 if (!bounds.intersect(snapshot->previous->viewport)) {
433 bounds.setEmpty();
434 }
435 } else {
436 bounds.setEmpty();
437 }
Romain Guyeb993562010-10-05 18:14:38 -0700438 }
Romain Guybf434112010-09-16 14:40:17 -0700439
Romain Guy746b7402010-10-26 16:27:31 -0700440 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
441 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800442 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700443 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700444 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700445 }
446
447 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800448 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700449 return false;
450 }
Romain Guyf18fd992010-07-08 11:45:51 -0700451
Romain Guy5b3b3522010-10-27 18:57:51 -0700452 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700453 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700454 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700455 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700456 }
457
Romain Guy9ace8f52011-07-07 20:50:11 -0700458 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700459 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700460 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
461 bounds.getWidth() / float(layer->getWidth()), 0.0f);
462 layer->setColorFilter(mColorFilter);
Romain Guydda570202010-07-06 11:39:32 -0700463
Romain Guy8fb95422010-08-17 18:38:51 -0700464 // Save the layer in the snapshot
465 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700466 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700467
Romain Guyeb993562010-10-05 18:14:38 -0700468 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700469 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700470 } else {
471 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700472 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800473 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700474 if (layer->isEmpty()) {
475 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
476 bounds.left, snapshot->height - bounds.bottom,
477 layer->getWidth(), layer->getHeight(), 0);
478 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800479 } else {
480 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
481 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
482 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700483
Romain Guy54be1cd2011-06-13 19:04:27 -0700484 // Enqueue the buffer coordinates to clear the corresponding region later
485 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700486 }
Romain Guyeb993562010-10-05 18:14:38 -0700487 }
Romain Guyf86ef572010-07-01 11:05:42 -0700488
Romain Guyd55a8612010-06-28 17:42:46 -0700489 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700490}
491
Romain Guy5b3b3522010-10-27 18:57:51 -0700492bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
493 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700494 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700495
496#if RENDER_LAYERS_AS_REGIONS
497 snapshot->region = &snapshot->layer->region;
498 snapshot->flags |= Snapshot::kFlagFboTarget;
499#endif
500
501 Rect clip(bounds);
502 snapshot->transform->mapRect(clip);
503 clip.intersect(*snapshot->clipRect);
504 clip.snapToPixelBoundaries();
505 clip.intersect(snapshot->previous->viewport);
506
507 mat4 inverse;
508 inverse.loadInverse(*mSnapshot->transform);
509
510 inverse.mapRect(clip);
511 clip.snapToPixelBoundaries();
512 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700513 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700514
515 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700516 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700517 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700518 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
519 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
520 snapshot->height = bounds.getHeight();
521 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
522 snapshot->orthoMatrix.load(mOrthoMatrix);
523
524 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700525 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
526 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700527
528 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700529 if (layer->isEmpty()) {
530 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
531 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700532 }
533
534 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700535 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700536
537#if DEBUG_LAYERS_AS_REGIONS
538 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
539 if (status != GL_FRAMEBUFFER_COMPLETE) {
540 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
541
542 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700543 layer->deleteTexture();
544 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700545
546 delete layer;
547
548 return false;
549 }
550#endif
551
552 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
553 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
554 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
555 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
556 glClear(GL_COLOR_BUFFER_BIT);
557
558 dirtyClip();
559
560 // Change the ortho projection
561 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
562 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
563
564 return true;
565}
566
Romain Guy1c740bc2010-09-13 18:00:09 -0700567/**
568 * Read the documentation of createLayer() before doing anything in this method.
569 */
Romain Guy1d83e192010-08-17 11:37:00 -0700570void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
571 if (!current->layer) {
572 LOGE("Attempting to compose a layer that does not exist");
573 return;
574 }
575
Romain Guy5b3b3522010-10-27 18:57:51 -0700576 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700577
578 if (fboLayer) {
579 // Unbind current FBO and restore previous one
580 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
581 }
582
Romain Guy1d83e192010-08-17 11:37:00 -0700583 Layer* layer = current->layer;
584 const Rect& rect = layer->layer;
585
Romain Guy9ace8f52011-07-07 20:50:11 -0700586 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700587 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700588 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700589 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700590 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700591 }
592
Romain Guy03750a02010-10-18 14:06:08 -0700593 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700594
Romain Guy746b7402010-10-26 16:27:31 -0700595 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700596
Romain Guy5b3b3522010-10-27 18:57:51 -0700597 // When the layer is stored in an FBO, we can save a bit of fillrate by
598 // drawing only the dirty region
599 if (fboLayer) {
600 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700601 if (layer->getColorFilter()) {
602 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800603 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700604 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700605 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800606 resetColorFilter();
607 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700608 } else if (!rect.isEmpty()) {
609 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
610 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700611 }
Romain Guy8b55f372010-08-18 17:10:07 -0700612
Romain Guyeb993562010-10-05 18:14:38 -0700613 if (fboLayer) {
614 // Detach the texture from the FBO
615 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
616 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
617 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
618
619 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
620 mCaches.fboCache.put(current->fbo);
621 }
622
Romain Guy746b7402010-10-26 16:27:31 -0700623 dirtyClip();
624
Romain Guyeb993562010-10-05 18:14:38 -0700625 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700626 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700627 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700628 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700629 delete layer;
630 }
631}
632
Romain Guyaa6c24c2011-04-28 18:40:04 -0700633void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700634 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700635
Romain Guy302a9df2011-08-16 13:55:02 -0700636 mat4& transform = layer->getTransform();
637 if (!transform.isIdentity()) {
638 save(0);
639 mSnapshot->transform->multiply(transform);
640 }
641
Romain Guyaa6c24c2011-04-28 18:40:04 -0700642 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700643 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700644 setupDrawWithTexture();
645 } else {
646 setupDrawWithExternalTexture();
647 }
648 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700649 setupDrawColor(alpha, alpha, alpha, alpha);
650 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700651 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700652 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700653 setupDrawPureColorUniforms();
654 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700655 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
656 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700657 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700658 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700659 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700660 if (mSnapshot->transform->isPureTranslate() &&
661 layer->getWidth() == (uint32_t) rect.getWidth() &&
662 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700663 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
664 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
665
666 layer->setFilter(GL_NEAREST, GL_NEAREST);
667 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
668 } else {
669 layer->setFilter(GL_LINEAR, GL_LINEAR);
670 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
671 }
672 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700673 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
674
675 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
676
677 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700678
679 if (!transform.isIdentity()) {
680 restore();
681 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700682}
683
Romain Guy5b3b3522010-10-27 18:57:51 -0700684void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700685 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700686 const Rect& texCoords = layer->texCoords;
687 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
688 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700689
Romain Guy9ace8f52011-07-07 20:50:11 -0700690 float x = rect.left;
691 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700692 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700693 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700694 layer->getHeight() == (uint32_t) rect.getHeight();
695
696 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700697 // When we're swapping, the layer is already in screen coordinates
698 if (!swap) {
699 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
700 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
701 }
702
703 layer->setFilter(GL_NEAREST, GL_NEAREST, true);
704 } else {
705 layer->setFilter(GL_LINEAR, GL_LINEAR, true);
706 }
707
708 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
709 layer->getTexture(), layer->getAlpha() / 255.0f,
710 layer->getMode(), layer->isBlend(),
711 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
712 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700713
Romain Guyaa6c24c2011-04-28 18:40:04 -0700714 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
715 } else {
716 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
717 drawTextureLayer(layer, rect);
718 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
719 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700720}
721
722void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
723#if RENDER_LAYERS_AS_REGIONS
724 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700725 layer->setRegionAsRect();
726
Romain Guy40667672011-03-18 14:34:03 -0700727 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700728
Romain Guy5b3b3522010-10-27 18:57:51 -0700729 layer->region.clear();
730 return;
731 }
732
Romain Guy8a3957d2011-09-07 17:55:15 -0700733 // TODO: See LayerRenderer.cpp::generateMesh() for important
734 // information about this implementation
Romain Guy5b3b3522010-10-27 18:57:51 -0700735 if (!layer->region.isEmpty()) {
736 size_t count;
737 const android::Rect* rects = layer->region.getArray(&count);
738
Romain Guy9ace8f52011-07-07 20:50:11 -0700739 const float alpha = layer->getAlpha() / 255.0f;
740 const float texX = 1.0f / float(layer->getWidth());
741 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800742 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700743
744 TextureVertex* mesh = mCaches.getRegionMesh();
745 GLsizei numQuads = 0;
746
Romain Guy7230a742011-01-10 22:26:16 -0800747 setupDraw();
748 setupDrawWithTexture();
749 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800750 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700751 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800752 setupDrawProgram();
753 setupDrawDirtyRegionsDisabled();
754 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800755 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700756 setupDrawTexture(layer->getTexture());
757 if (mSnapshot->transform->isPureTranslate()) {
758 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
759 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
760
761 layer->setFilter(GL_NEAREST, GL_NEAREST);
762 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
763 } else {
764 layer->setFilter(GL_LINEAR, GL_LINEAR);
765 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
766 }
Romain Guy7230a742011-01-10 22:26:16 -0800767 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700768
769 for (size_t i = 0; i < count; i++) {
770 const android::Rect* r = &rects[i];
771
772 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800773 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700774 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800775 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700776
777 // TODO: Reject quads outside of the clip
778 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
779 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
780 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
781 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
782
783 numQuads++;
784
785 if (numQuads >= REGION_MESH_QUAD_COUNT) {
786 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
787 numQuads = 0;
788 mesh = mCaches.getRegionMesh();
789 }
790 }
791
792 if (numQuads > 0) {
793 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
794 }
795
796 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800797 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700798
799#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800800 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700801#endif
802
803 layer->region.clear();
804 }
805#else
806 composeLayerRect(layer, rect);
807#endif
808}
809
Romain Guy3a3133d2011-02-01 22:59:58 -0800810void OpenGLRenderer::drawRegionRects(const Region& region) {
811#if DEBUG_LAYERS_AS_REGIONS
812 size_t count;
813 const android::Rect* rects = region.getArray(&count);
814
815 uint32_t colors[] = {
816 0x7fff0000, 0x7f00ff00,
817 0x7f0000ff, 0x7fff00ff,
818 };
819
820 int offset = 0;
821 int32_t top = rects[0].top;
822
823 for (size_t i = 0; i < count; i++) {
824 if (top != rects[i].top) {
825 offset ^= 0x2;
826 top = rects[i].top;
827 }
828
829 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
830 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
831 SkXfermode::kSrcOver_Mode);
832 }
833#endif
834}
835
Romain Guy5b3b3522010-10-27 18:57:51 -0700836void OpenGLRenderer::dirtyLayer(const float left, const float top,
837 const float right, const float bottom, const mat4 transform) {
838#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800839 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700840 Rect bounds(left, top, right, bottom);
841 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800842 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700843 }
844#endif
845}
846
847void OpenGLRenderer::dirtyLayer(const float left, const float top,
848 const float right, const float bottom) {
849#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800850 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700851 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800852 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800853 }
854#endif
855}
856
857void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
858#if RENDER_LAYERS_AS_REGIONS
859 if (bounds.intersect(*mSnapshot->clipRect)) {
860 bounds.snapToPixelBoundaries();
861 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
862 if (!dirty.isEmpty()) {
863 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700864 }
865 }
866#endif
867}
868
Romain Guy54be1cd2011-06-13 19:04:27 -0700869void OpenGLRenderer::clearLayerRegions() {
870 const size_t count = mLayers.size();
871 if (count == 0) return;
872
873 if (!mSnapshot->isIgnored()) {
874 // Doing several glScissor/glClear here can negatively impact
875 // GPUs with a tiler architecture, instead we draw quads with
876 // the Clear blending mode
877
878 // The list contains bounds that have already been clipped
879 // against their initial clip rect, and the current clip
880 // is likely different so we need to disable clipping here
881 glDisable(GL_SCISSOR_TEST);
882
883 Vertex mesh[count * 6];
884 Vertex* vertex = mesh;
885
886 for (uint32_t i = 0; i < count; i++) {
887 Rect* bounds = mLayers.itemAt(i);
888
889 Vertex::set(vertex++, bounds->left, bounds->bottom);
890 Vertex::set(vertex++, bounds->left, bounds->top);
891 Vertex::set(vertex++, bounds->right, bounds->top);
892 Vertex::set(vertex++, bounds->left, bounds->bottom);
893 Vertex::set(vertex++, bounds->right, bounds->top);
894 Vertex::set(vertex++, bounds->right, bounds->bottom);
895
896 delete bounds;
897 }
898
899 setupDraw(false);
900 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
901 setupDrawBlending(true, SkXfermode::kClear_Mode);
902 setupDrawProgram();
903 setupDrawPureColorUniforms();
904 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
905
906 mCaches.unbindMeshBuffer();
907 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
908 gVertexStride, &mesh[0].position[0]);
909 glDrawArrays(GL_TRIANGLES, 0, count * 6);
910
911 glEnable(GL_SCISSOR_TEST);
912 } else {
913 for (uint32_t i = 0; i < count; i++) {
914 delete mLayers.itemAt(i);
915 }
916 }
917
918 mLayers.clear();
919}
920
Romain Guybd6b79b2010-06-26 00:13:53 -0700921///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700922// Transforms
923///////////////////////////////////////////////////////////////////////////////
924
925void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700926 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700927}
928
929void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700930 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700931}
932
933void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700934 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700935}
936
Romain Guy807daf72011-01-18 11:19:19 -0800937void OpenGLRenderer::skew(float sx, float sy) {
938 mSnapshot->transform->skew(sx, sy);
939}
940
Romain Guyf6a11b82010-06-23 17:47:49 -0700941void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -0700942 if (matrix) {
943 mSnapshot->transform->load(*matrix);
944 } else {
945 mSnapshot->transform->loadIdentity();
946 }
Romain Guyf6a11b82010-06-23 17:47:49 -0700947}
948
949void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700950 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700951}
952
953void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700954 SkMatrix transform;
955 mSnapshot->transform->copyTo(transform);
956 transform.preConcat(*matrix);
957 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700958}
959
960///////////////////////////////////////////////////////////////////////////////
961// Clipping
962///////////////////////////////////////////////////////////////////////////////
963
Romain Guybb9524b2010-06-22 18:56:38 -0700964void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700965 Rect clip(*mSnapshot->clipRect);
966 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700967 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700968 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700969}
970
971const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700972 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700973}
974
Romain Guyc7d53492010-06-25 13:41:57 -0700975bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800976 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700977 return true;
978 }
979
Romain Guy1d83e192010-08-17 11:37:00 -0700980 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700981 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700982 r.snapToPixelBoundaries();
983
984 Rect clipRect(*mSnapshot->clipRect);
985 clipRect.snapToPixelBoundaries();
986
987 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700988}
989
Romain Guy079ba2c2010-07-16 14:12:24 -0700990bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
991 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700992 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700993 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700994 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700995 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700996}
997
Romain Guyf6a11b82010-06-23 17:47:49 -0700998///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -0800999// Drawing commands
1000///////////////////////////////////////////////////////////////////////////////
1001
Romain Guy54be1cd2011-06-13 19:04:27 -07001002void OpenGLRenderer::setupDraw(bool clear) {
1003 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001004 if (mDirtyClip) {
1005 setScissorFromClip();
1006 }
1007 mDescription.reset();
1008 mSetShaderColor = false;
1009 mColorSet = false;
1010 mColorA = mColorR = mColorG = mColorB = 0.0f;
1011 mTextureUnit = 0;
1012 mTrackDirtyRegions = true;
Romain Guy8d0d4782010-12-14 20:13:35 -08001013 mTexCoordsSlot = -1;
Romain Guy70ca14e2010-12-13 18:24:33 -08001014}
1015
1016void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1017 mDescription.hasTexture = true;
1018 mDescription.hasAlpha8Texture = isAlpha8;
1019}
1020
Romain Guyaa6c24c2011-04-28 18:40:04 -07001021void OpenGLRenderer::setupDrawWithExternalTexture() {
1022 mDescription.hasExternalTexture = true;
1023}
1024
Chet Haase5b0200b2011-04-13 17:58:08 -07001025void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001026 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001027}
1028
Romain Guyed6fcb02011-03-21 13:11:28 -07001029void OpenGLRenderer::setupDrawPoint(float pointSize) {
1030 mDescription.isPoint = true;
1031 mDescription.pointSize = pointSize;
1032}
1033
Romain Guy70ca14e2010-12-13 18:24:33 -08001034void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001035 setupDrawColor(color, (color >> 24) & 0xFF);
1036}
1037
1038void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1039 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001040 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1041 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001042 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001043 mColorR = a * ((color >> 16) & 0xFF);
1044 mColorG = a * ((color >> 8) & 0xFF);
1045 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001046 mColorSet = true;
1047 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1048}
1049
Romain Guy86568192010-12-14 15:55:39 -08001050void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1051 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001052 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1053 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001054 const float a = mColorA / 255.0f;
1055 mColorR = a * ((color >> 16) & 0xFF);
1056 mColorG = a * ((color >> 8) & 0xFF);
1057 mColorB = a * ((color ) & 0xFF);
1058 mColorSet = true;
1059 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1060}
1061
Romain Guy70ca14e2010-12-13 18:24:33 -08001062void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1063 mColorA = a;
1064 mColorR = r;
1065 mColorG = g;
1066 mColorB = b;
1067 mColorSet = true;
1068 mSetShaderColor = mDescription.setColor(r, g, b, a);
1069}
1070
Romain Guy86568192010-12-14 15:55:39 -08001071void OpenGLRenderer::setupDrawAlpha8Color(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.setAlpha8Color(r, g, b, a);
1078}
1079
Romain Guy70ca14e2010-12-13 18:24:33 -08001080void OpenGLRenderer::setupDrawShader() {
1081 if (mShader) {
1082 mShader->describe(mDescription, mCaches.extensions);
1083 }
1084}
1085
1086void OpenGLRenderer::setupDrawColorFilter() {
1087 if (mColorFilter) {
1088 mColorFilter->describe(mDescription, mCaches.extensions);
1089 }
1090}
1091
Romain Guyf09ef512011-05-27 11:43:46 -07001092void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1093 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1094 mColorA = 1.0f;
1095 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001096 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001097 }
1098}
1099
Romain Guy70ca14e2010-12-13 18:24:33 -08001100void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001101 // When the blending mode is kClear_Mode, we need to use a modulate color
1102 // argb=1,0,0,0
1103 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001104 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1105 mDescription, swapSrcDst);
1106}
1107
1108void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001109 // When the blending mode is kClear_Mode, we need to use a modulate color
1110 // argb=1,0,0,0
1111 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001112 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1113 mDescription, swapSrcDst);
1114}
1115
1116void OpenGLRenderer::setupDrawProgram() {
1117 useProgram(mCaches.programCache.get(mDescription));
1118}
1119
1120void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1121 mTrackDirtyRegions = false;
1122}
1123
1124void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1125 bool ignoreTransform) {
1126 mModelView.loadTranslate(left, top, 0.0f);
1127 if (!ignoreTransform) {
1128 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1129 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1130 } else {
1131 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1132 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1133 }
1134}
1135
Chet Haase8a5cc922011-04-26 07:28:09 -07001136void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1137 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001138}
1139
Romain Guy70ca14e2010-12-13 18:24:33 -08001140void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1141 bool ignoreTransform, bool ignoreModelView) {
1142 if (!ignoreModelView) {
1143 mModelView.loadTranslate(left, top, 0.0f);
1144 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001145 } else {
1146 mModelView.loadIdentity();
1147 }
Romain Guy86568192010-12-14 15:55:39 -08001148 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1149 if (!ignoreTransform) {
1150 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1151 if (mTrackDirtyRegions && dirty) {
1152 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1153 }
1154 } else {
1155 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1156 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1157 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001158}
1159
Romain Guyed6fcb02011-03-21 13:11:28 -07001160void OpenGLRenderer::setupDrawPointUniforms() {
1161 int slot = mCaches.currentProgram->getUniform("pointSize");
1162 glUniform1f(slot, mDescription.pointSize);
1163}
1164
Romain Guy70ca14e2010-12-13 18:24:33 -08001165void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001166 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001167 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1168 }
1169}
1170
Romain Guy86568192010-12-14 15:55:39 -08001171void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001172 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001173 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001174 }
1175}
1176
Romain Guy70ca14e2010-12-13 18:24:33 -08001177void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1178 if (mShader) {
1179 if (ignoreTransform) {
1180 mModelView.loadInverse(*mSnapshot->transform);
1181 }
1182 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1183 }
1184}
1185
Romain Guy8d0d4782010-12-14 20:13:35 -08001186void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1187 if (mShader) {
1188 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1189 }
1190}
1191
Romain Guy70ca14e2010-12-13 18:24:33 -08001192void OpenGLRenderer::setupDrawColorFilterUniforms() {
1193 if (mColorFilter) {
1194 mColorFilter->setupProgram(mCaches.currentProgram);
1195 }
1196}
1197
1198void OpenGLRenderer::setupDrawSimpleMesh() {
1199 mCaches.bindMeshBuffer();
1200 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1201 gMeshStride, 0);
1202}
1203
1204void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1205 bindTexture(texture);
1206 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1207
1208 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1209 glEnableVertexAttribArray(mTexCoordsSlot);
1210}
1211
Romain Guyaa6c24c2011-04-28 18:40:04 -07001212void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1213 bindExternalTexture(texture);
1214 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1215
1216 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1217 glEnableVertexAttribArray(mTexCoordsSlot);
1218}
1219
Romain Guy8f0095c2011-05-02 17:24:22 -07001220void OpenGLRenderer::setupDrawTextureTransform() {
1221 mDescription.hasTextureTransform = true;
1222}
1223
1224void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001225 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1226 GL_FALSE, &transform.data[0]);
1227}
1228
Romain Guy70ca14e2010-12-13 18:24:33 -08001229void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1230 if (!vertices) {
1231 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1232 } else {
1233 mCaches.unbindMeshBuffer();
1234 }
1235 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1236 gMeshStride, vertices);
David Licf289572011-02-25 12:05:44 -08001237 if (mTexCoordsSlot >= 0) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001238 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1239 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001240}
1241
Chet Haase5b0200b2011-04-13 17:58:08 -07001242void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
1243 mCaches.unbindMeshBuffer();
1244 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1245 gVertexStride, vertices);
1246}
1247
1248/**
1249 * 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 -07001250 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1251 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1252 * attributes (one per vertex) are values from zero to one that tells the fragment
1253 * shader where the fragment is in relation to the line width/length overall; these values are
1254 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1255 * region of the line.
1256 * Note that we only pass down the width values in this setup function. The length coordinates
1257 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001258 */
Chet Haase99585ad2011-05-02 15:00:16 -07001259void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Chet Haase99ecdc42011-05-06 12:06:34 -07001260 GLvoid* lengthCoords, float boundaryWidthProportion) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001261 mCaches.unbindMeshBuffer();
1262 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Chet Haase99585ad2011-05-02 15:00:16 -07001263 gAAVertexStride, vertices);
1264 int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
1265 glEnableVertexAttribArray(widthSlot);
1266 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
1267 int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
1268 glEnableVertexAttribArray(lengthSlot);
1269 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Chet Haase5b0200b2011-04-13 17:58:08 -07001270 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001271 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07001272 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001273 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001274 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
Chet Haase5b0200b2011-04-13 17:58:08 -07001275}
1276
Romain Guy70ca14e2010-12-13 18:24:33 -08001277void OpenGLRenderer::finishDrawTexture() {
1278 glDisableVertexAttribArray(mTexCoordsSlot);
1279}
1280
1281///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001282// Drawing
1283///////////////////////////////////////////////////////////////////////////////
1284
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001285bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
1286 Rect& dirty, uint32_t level) {
1287 if (quickReject(0.0f, 0.0f, width, height)) {
1288 return false;
1289 }
1290
Romain Guy0fe478e2010-11-08 12:08:41 -08001291 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1292 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001293 if (displayList && displayList->isRenderable()) {
Romain Guycabfcc12011-03-07 18:06:46 -08001294 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001295 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001296
Chet Haasedaf98e92011-01-10 14:10:36 -08001297 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001298}
1299
Chet Haaseed30fd82011-04-22 16:18:45 -07001300void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1301 if (displayList) {
1302 displayList->output(*this, level);
1303 }
1304}
1305
Romain Guya168d732011-03-18 16:50:13 -07001306void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1307 int alpha;
1308 SkXfermode::Mode mode;
1309 getAlphaAndMode(paint, &alpha, &mode);
1310
Romain Guya168d732011-03-18 16:50:13 -07001311 float x = left;
1312 float y = top;
1313
Romain Guye3c26852011-07-25 16:36:01 -07001314 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001315 bool ignoreTransform = false;
1316 if (mSnapshot->transform->isPureTranslate()) {
1317 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1318 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1319 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001320 filter = GL_NEAREST;
Romain Guya168d732011-03-18 16:50:13 -07001321 }
1322
1323 setupDraw();
1324 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001325 if (paint) {
1326 setupDrawAlpha8Color(paint->getColor(), alpha);
1327 }
Romain Guya168d732011-03-18 16:50:13 -07001328 setupDrawColorFilter();
1329 setupDrawShader();
1330 setupDrawBlending(true, mode);
1331 setupDrawProgram();
1332 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001333
Romain Guya168d732011-03-18 16:50:13 -07001334 setupDrawTexture(texture->id);
Romain Guye3c26852011-07-25 16:36:01 -07001335 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1336 texture->setFilter(filter, filter);
1337
Romain Guya168d732011-03-18 16:50:13 -07001338 setupDrawPureColorUniforms();
1339 setupDrawColorFilterUniforms();
1340 setupDrawShaderUniforms();
1341 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1342
1343 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1344
1345 finishDrawTexture();
1346}
1347
Chet Haase5c13d892010-10-08 08:37:55 -07001348void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001349 const float right = left + bitmap->width();
1350 const float bottom = top + bitmap->height();
1351
1352 if (quickReject(left, top, right, bottom)) {
1353 return;
1354 }
1355
Romain Guy86568192010-12-14 15:55:39 -08001356 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001357 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001358 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001359 const AutoTexture autoCleanup(texture);
1360
Romain Guya168d732011-03-18 16:50:13 -07001361 if (bitmap->getConfig() == SkBitmap::kA8_Config) {
1362 drawAlphaBitmap(texture, left, top, paint);
1363 } else {
1364 drawTextureRect(left, top, right, bottom, texture, paint);
1365 }
Romain Guyce0537b2010-06-29 21:05:21 -07001366}
1367
Chet Haase5c13d892010-10-08 08:37:55 -07001368void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001369 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1370 const mat4 transform(*matrix);
1371 transform.mapRect(r);
1372
Romain Guy6926c722010-07-12 20:20:03 -07001373 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1374 return;
1375 }
1376
Romain Guy86568192010-12-14 15:55:39 -08001377 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001378 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001379 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001380 const AutoTexture autoCleanup(texture);
1381
Romain Guy5b3b3522010-10-27 18:57:51 -07001382 // This could be done in a cheaper way, all we need is pass the matrix
1383 // to the vertex shader. The save/restore is a bit overkill.
1384 save(SkCanvas::kMatrix_SaveFlag);
1385 concatMatrix(matrix);
1386 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1387 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001388}
1389
Romain Guy5a7b4662011-01-20 19:09:30 -08001390void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1391 float* vertices, int* colors, SkPaint* paint) {
1392 // TODO: Do a quickReject
1393 if (!vertices || mSnapshot->isIgnored()) {
1394 return;
1395 }
1396
1397 glActiveTexture(gTextureUnits[0]);
1398 Texture* texture = mCaches.textureCache.get(bitmap);
1399 if (!texture) return;
1400 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001401
1402 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, true);
1403 texture->setFilter(GL_LINEAR, GL_LINEAR, true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001404
1405 int alpha;
1406 SkXfermode::Mode mode;
1407 getAlphaAndMode(paint, &alpha, &mode);
1408
Romain Guy5a7b4662011-01-20 19:09:30 -08001409 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001410
Romain Guyb18d2d02011-02-10 15:52:54 -08001411 float left = FLT_MAX;
1412 float top = FLT_MAX;
1413 float right = FLT_MIN;
1414 float bottom = FLT_MIN;
1415
1416#if RENDER_LAYERS_AS_REGIONS
1417 bool hasActiveLayer = hasLayer();
1418#else
1419 bool hasActiveLayer = false;
1420#endif
1421
Romain Guya566b7c2011-01-23 16:36:11 -08001422 // TODO: Support the colors array
1423 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001424 TextureVertex* vertex = mesh;
1425 for (int32_t y = 0; y < meshHeight; y++) {
1426 for (int32_t x = 0; x < meshWidth; x++) {
1427 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1428
1429 float u1 = float(x) / meshWidth;
1430 float u2 = float(x + 1) / meshWidth;
1431 float v1 = float(y) / meshHeight;
1432 float v2 = float(y + 1) / meshHeight;
1433
1434 int ax = i + (meshWidth + 1) * 2;
1435 int ay = ax + 1;
1436 int bx = i;
1437 int by = bx + 1;
1438 int cx = i + 2;
1439 int cy = cx + 1;
1440 int dx = i + (meshWidth + 1) * 2 + 2;
1441 int dy = dx + 1;
1442
1443 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1444 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1445 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1446
1447 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1448 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1449 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001450
1451#if RENDER_LAYERS_AS_REGIONS
1452 if (hasActiveLayer) {
1453 // TODO: This could be optimized to avoid unnecessary ops
1454 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1455 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1456 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1457 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1458 }
1459#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001460 }
1461 }
1462
Romain Guyb18d2d02011-02-10 15:52:54 -08001463#if RENDER_LAYERS_AS_REGIONS
1464 if (hasActiveLayer) {
1465 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1466 }
1467#endif
1468
Romain Guy5a7b4662011-01-20 19:09:30 -08001469 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1470 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001471 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001472}
1473
Romain Guy8ba548f2010-06-30 19:21:21 -07001474void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1475 float srcLeft, float srcTop, float srcRight, float srcBottom,
1476 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001477 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001478 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1479 return;
1480 }
1481
Romain Guy746b7402010-10-26 16:27:31 -07001482 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001483 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001484 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001485 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001486 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, true);
Romain Guy8ba548f2010-06-30 19:21:21 -07001487
Romain Guy8ba548f2010-06-30 19:21:21 -07001488 const float width = texture->width;
1489 const float height = texture->height;
1490
Romain Guy68169722011-08-22 17:33:33 -07001491 const float u1 = fmax(0.0f, srcLeft / width);
1492 const float v1 = fmax(0.0f, srcTop / height);
1493 const float u2 = fmin(1.0f, srcRight / width);
1494 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001495
Romain Guy03750a02010-10-18 14:06:08 -07001496 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001497 resetDrawTextureTexCoords(u1, v1, u2, v2);
1498
Romain Guy03750a02010-10-18 14:06:08 -07001499 int alpha;
1500 SkXfermode::Mode mode;
1501 getAlphaAndMode(paint, &alpha, &mode);
1502
Romain Guy6620c6d2010-12-06 18:07:02 -08001503 if (mSnapshot->transform->isPureTranslate()) {
1504 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1505 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1506
Romain Guyb5014982011-07-28 15:39:12 -07001507 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001508 // Enable linear filtering if the source rectangle is scaled
1509 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyb5014982011-07-28 15:39:12 -07001510 filter = GL_LINEAR;
1511 }
1512 texture->setFilter(filter, filter, true);
1513
Romain Guy6620c6d2010-12-06 18:07:02 -08001514 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1515 texture->id, alpha / 255.0f, mode, texture->blend,
1516 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1517 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1518 } else {
Romain Guye3c26852011-07-25 16:36:01 -07001519 texture->setFilter(GL_LINEAR, GL_LINEAR, true);
Romain Guyb5014982011-07-28 15:39:12 -07001520
Romain Guy6620c6d2010-12-06 18:07:02 -08001521 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1522 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1523 GL_TRIANGLE_STRIP, gMeshCount);
1524 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001525
1526 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1527}
1528
Romain Guy4aa90572010-09-26 18:40:37 -07001529void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001530 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001531 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001532 if (quickReject(left, top, right, bottom)) {
1533 return;
1534 }
1535
Romain Guy746b7402010-10-26 16:27:31 -07001536 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001537 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001538 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001539 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001540 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, true);
1541 texture->setFilter(GL_LINEAR, GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001542
1543 int alpha;
1544 SkXfermode::Mode mode;
1545 getAlphaAndMode(paint, &alpha, &mode);
1546
Romain Guy2728f962010-10-08 18:36:15 -07001547 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001548 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001549
Romain Guya5ef39a2010-12-03 16:48:20 -08001550 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001551 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001552#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001553 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001554 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001555 const float offsetX = left + mSnapshot->transform->getTranslateX();
1556 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001557 const size_t count = mesh->quads.size();
1558 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001559 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001560 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001561 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1562 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1563 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001564 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001565 dirtyLayer(left + bounds.left, top + bounds.top,
1566 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001567 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001568 }
1569 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001570#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001571
Romain Guy6620c6d2010-12-06 18:07:02 -08001572 if (pureTranslate) {
1573 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1574 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1575
1576 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1577 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1578 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1579 true, !mesh->hasEmptyQuads);
1580 } else {
1581 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1582 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1583 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1584 true, !mesh->hasEmptyQuads);
1585 }
Romain Guy054dc182010-10-15 17:55:25 -07001586 }
Romain Guyf7f93552010-07-08 19:17:03 -07001587}
1588
Chet Haase99ecdc42011-05-06 12:06:34 -07001589/**
Chet Haase858aa932011-05-12 09:06:00 -07001590 * This function uses a similar approach to that of AA lines in the drawLines() function.
1591 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1592 * shader to compute the translucency of the color, determined by whether a given pixel is
1593 * within that boundary region and how far into the region it is.
1594 */
1595void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001596 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001597 float inverseScaleX = 1.0f;
1598 float inverseScaleY = 1.0f;
1599 // The quad that we use needs to account for scaling.
1600 if (!mSnapshot->transform->isPureTranslate()) {
1601 Matrix4 *mat = mSnapshot->transform;
1602 float m00 = mat->data[Matrix4::kScaleX];
1603 float m01 = mat->data[Matrix4::kSkewY];
1604 float m02 = mat->data[2];
1605 float m10 = mat->data[Matrix4::kSkewX];
1606 float m11 = mat->data[Matrix4::kScaleX];
1607 float m12 = mat->data[6];
1608 float scaleX = sqrt(m00 * m00 + m01 * m01);
1609 float scaleY = sqrt(m10 * m10 + m11 * m11);
1610 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1611 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1612 }
1613
1614 setupDraw();
1615 setupDrawAALine();
1616 setupDrawColor(color);
1617 setupDrawColorFilter();
1618 setupDrawShader();
1619 setupDrawBlending(true, mode);
1620 setupDrawProgram();
1621 setupDrawModelViewIdentity(true);
1622 setupDrawColorUniforms();
1623 setupDrawColorFilterUniforms();
1624 setupDrawShaderIdentityUniforms();
1625
1626 AAVertex rects[4];
1627 AAVertex* aaVertices = &rects[0];
1628 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1629 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1630
1631 float boundarySizeX = .5 * inverseScaleX;
1632 float boundarySizeY = .5 * inverseScaleY;
1633
1634 // Adjust the rect by the AA boundary padding
1635 left -= boundarySizeX;
1636 right += boundarySizeX;
1637 top -= boundarySizeY;
1638 bottom += boundarySizeY;
1639
1640 float width = right - left;
1641 float height = bottom - top;
1642
1643 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1644 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
1645 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
1646 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1647 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1648 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
1649 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryHeightProportion));
1650
1651 if (!quickReject(left, top, right, bottom)) {
1652 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1653 AAVertex::set(aaVertices++, left, top, 1, 0);
1654 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1655 AAVertex::set(aaVertices++, right, top, 0, 0);
1656 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1657 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1658 }
1659}
1660
1661/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001662 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1663 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1664 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1665 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1666 * of the line. Hairlines are more involved because we need to account for transform scaling
1667 * to end up with a one-pixel-wide line in screen space..
1668 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1669 * in combination with values that we calculate and pass down in this method. The basic approach
1670 * is that the quad we create contains both the core line area plus a bounding area in which
1671 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1672 * proportion of the width and the length of a given segment is represented by the boundary
1673 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1674 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1675 * on the inside). This ends up giving the result we want, with pixels that are completely
1676 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1677 * how far into the boundary region they are, which is determined by shader interpolation.
1678 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001679void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1680 if (mSnapshot->isIgnored()) return;
1681
1682 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001683 // We use half the stroke width here because we're going to position the quad
1684 // corner vertices half of the width away from the line endpoints
1685 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001686 // A stroke width of 0 has a special meaning in Skia:
1687 // it draws a line 1 px wide regardless of current transform
1688 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase99ecdc42011-05-06 12:06:34 -07001689 float inverseScaleX = 1.0f;
1690 float inverseScaleY = 1.0f;
1691 bool scaled = false;
Chet Haase8a5cc922011-04-26 07:28:09 -07001692 int alpha;
1693 SkXfermode::Mode mode;
1694 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001695 int verticesCount = count;
1696 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001697 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001698 verticesCount += (count - 4);
1699 }
1700
1701 if (isHairLine || isAA) {
1702 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1703 // the line on the screen should always be one pixel wide regardless of scale. For
1704 // AA lines, we only want one pixel of translucent boundary around the quad.
1705 if (!mSnapshot->transform->isPureTranslate()) {
1706 Matrix4 *mat = mSnapshot->transform;
1707 float m00 = mat->data[Matrix4::kScaleX];
1708 float m01 = mat->data[Matrix4::kSkewY];
1709 float m02 = mat->data[2];
1710 float m10 = mat->data[Matrix4::kSkewX];
1711 float m11 = mat->data[Matrix4::kScaleX];
1712 float m12 = mat->data[6];
1713 float scaleX = sqrt(m00*m00 + m01*m01);
1714 float scaleY = sqrt(m10*m10 + m11*m11);
1715 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1716 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1717 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1718 scaled = true;
1719 }
1720 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001721 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001722
1723 getAlphaAndMode(paint, &alpha, &mode);
1724 setupDraw();
1725 if (isAA) {
1726 setupDrawAALine();
1727 }
1728 setupDrawColor(paint->getColor(), alpha);
1729 setupDrawColorFilter();
1730 setupDrawShader();
1731 if (isAA) {
1732 setupDrawBlending(true, mode);
1733 } else {
1734 setupDrawBlending(mode);
1735 }
1736 setupDrawProgram();
1737 setupDrawModelViewIdentity(true);
1738 setupDrawColorUniforms();
1739 setupDrawColorFilterUniforms();
1740 setupDrawShaderIdentityUniforms();
1741
1742 if (isHairLine) {
1743 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001744 halfStrokeWidth = isAA? 1 : .5;
1745 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001746 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001747 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001748 }
1749 Vertex lines[verticesCount];
1750 Vertex* vertices = &lines[0];
Chet Haase99585ad2011-05-02 15:00:16 -07001751 AAVertex wLines[verticesCount];
1752 AAVertex* aaVertices = &wLines[0];
Chet Haase5b0200b2011-04-13 17:58:08 -07001753 if (!isAA) {
1754 setupDrawVertices(vertices);
1755 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001756 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1757 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001758 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001759 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1760 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001761 // We will need to calculate the actual width proportion on each segment for
1762 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1763 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
1764 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
Chet Haase5b0200b2011-04-13 17:58:08 -07001765 }
1766
Chet Haase99ecdc42011-05-06 12:06:34 -07001767 AAVertex* prevAAVertex = NULL;
1768 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001769
Chet Haase99585ad2011-05-02 15:00:16 -07001770 int boundaryLengthSlot = -1;
1771 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001772 int boundaryWidthSlot = -1;
1773 int inverseBoundaryWidthSlot = -1;
Chet Haase5b0200b2011-04-13 17:58:08 -07001774 for (int i = 0; i < count; i += 4) {
1775 // a = start point, b = end point
1776 vec2 a(points[i], points[i + 1]);
1777 vec2 b(points[i + 2], points[i + 3]);
Chet Haase99585ad2011-05-02 15:00:16 -07001778 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001779 float boundaryLengthProportion = 0;
1780 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001781
Chet Haase5b0200b2011-04-13 17:58:08 -07001782 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001783 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001784 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001785 if (isAA) {
1786 float wideningFactor;
1787 if (fabs(n.x) >= fabs(n.y)) {
1788 wideningFactor = fabs(1.0f / n.x);
1789 } else {
1790 wideningFactor = fabs(1.0f / n.y);
1791 }
1792 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001793 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001794 if (scaled) {
1795 n.x *= inverseScaleX;
1796 n.y *= inverseScaleY;
1797 }
1798 } else if (scaled) {
1799 // Extend n by .5 pixel on each side, post-transform
1800 vec2 extendedN = n.copyNormalized();
1801 extendedN /= 2;
1802 extendedN.x *= inverseScaleX;
1803 extendedN.y *= inverseScaleY;
1804 float extendedNLength = extendedN.length();
1805 // We need to set this value on the shader prior to drawing
1806 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1807 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001808 }
1809 float x = n.x;
1810 n.x = -n.y;
1811 n.y = x;
1812
Chet Haase99585ad2011-05-02 15:00:16 -07001813 // aa lines expand the endpoint vertices to encompass the AA boundary
1814 if (isAA) {
1815 vec2 abVector = (b - a);
1816 length = abVector.length();
1817 abVector.normalize();
Chet Haase99ecdc42011-05-06 12:06:34 -07001818 if (scaled) {
1819 abVector.x *= inverseScaleX;
1820 abVector.y *= inverseScaleY;
1821 float abLength = abVector.length();
1822 boundaryLengthProportion = abLength / (length + abLength);
1823 } else {
1824 boundaryLengthProportion = .5 / (length + 1);
1825 }
1826 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001827 a -= abVector;
1828 b += abVector;
1829 }
1830
Chet Haase5b0200b2011-04-13 17:58:08 -07001831 // Four corners of the rectangle defining a thick line
1832 vec2 p1 = a - n;
1833 vec2 p2 = a + n;
1834 vec2 p3 = b + n;
1835 vec2 p4 = b - n;
1836
Chet Haase99585ad2011-05-02 15:00:16 -07001837
Chet Haase5b0200b2011-04-13 17:58:08 -07001838 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1839 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1840 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1841 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1842
1843 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001844 if (!isAA) {
1845 if (prevVertex != NULL) {
1846 // Issue two repeat vertices to create degenerate triangles to bridge
1847 // between the previous line and the new one. This is necessary because
1848 // we are creating a single triangle_strip which will contain
1849 // potentially discontinuous line segments.
1850 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1851 Vertex::set(vertices++, p1.x, p1.y);
1852 generatedVerticesCount += 2;
1853 }
1854 Vertex::set(vertices++, p1.x, p1.y);
1855 Vertex::set(vertices++, p2.x, p2.y);
1856 Vertex::set(vertices++, p4.x, p4.y);
1857 Vertex::set(vertices++, p3.x, p3.y);
1858 prevVertex = vertices - 1;
1859 generatedVerticesCount += 4;
1860 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001861 if (!isHairLine && scaled) {
1862 // Must set width proportions per-segment for scaled non-hairlines to use the
1863 // correct AA boundary dimensions
1864 if (boundaryWidthSlot < 0) {
1865 boundaryWidthSlot =
1866 mCaches.currentProgram->getUniform("boundaryWidth");
1867 inverseBoundaryWidthSlot =
1868 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1869 }
1870 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1871 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1872 }
Chet Haase99585ad2011-05-02 15:00:16 -07001873 if (boundaryLengthSlot < 0) {
1874 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1875 inverseBoundaryLengthSlot =
1876 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1877 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001878 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
1879 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07001880
Chet Haase5b0200b2011-04-13 17:58:08 -07001881 if (prevAAVertex != NULL) {
1882 // Issue two repeat vertices to create degenerate triangles to bridge
1883 // between the previous line and the new one. This is necessary because
1884 // we are creating a single triangle_strip which will contain
1885 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07001886 AAVertex::set(aaVertices++,prevAAVertex->position[0],
1887 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
1888 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07001889 generatedVerticesCount += 2;
1890 }
Chet Haase99585ad2011-05-02 15:00:16 -07001891 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
1892 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
1893 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
1894 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Chet Haase5b0200b2011-04-13 17:58:08 -07001895 prevAAVertex = aaVertices - 1;
1896 generatedVerticesCount += 4;
1897 }
Chet Haase99585ad2011-05-02 15:00:16 -07001898 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
1899 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
1900 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07001901 }
1902 }
1903 if (generatedVerticesCount > 0) {
1904 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
1905 }
1906}
1907
Romain Guyed6fcb02011-03-21 13:11:28 -07001908void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
1909 if (mSnapshot->isIgnored()) return;
1910
1911 // TODO: The paint's cap style defines whether the points are square or circular
1912 // TODO: Handle AA for round points
1913
Chet Haase5b0200b2011-04-13 17:58:08 -07001914 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07001915 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07001916 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07001917 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001918 if (isHairLine) {
1919 // Now that we know it's hairline, we can set the effective width, to be used later
1920 strokeWidth = 1.0f;
1921 }
1922 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07001923 int alpha;
1924 SkXfermode::Mode mode;
1925 getAlphaAndMode(paint, &alpha, &mode);
1926
1927 int verticesCount = count >> 1;
1928 int generatedVerticesCount = 0;
1929
1930 TextureVertex pointsData[verticesCount];
1931 TextureVertex* vertex = &pointsData[0];
1932
1933 setupDraw();
Chet Haase8a5cc922011-04-26 07:28:09 -07001934 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07001935 setupDrawColor(paint->getColor(), alpha);
1936 setupDrawColorFilter();
1937 setupDrawShader();
1938 setupDrawBlending(mode);
1939 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07001940 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07001941 setupDrawColorUniforms();
1942 setupDrawColorFilterUniforms();
1943 setupDrawPointUniforms();
1944 setupDrawShaderIdentityUniforms();
1945 setupDrawMesh(vertex);
1946
1947 for (int i = 0; i < count; i += 2) {
1948 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1949 generatedVerticesCount++;
Chet Haase8a5cc922011-04-26 07:28:09 -07001950 float left = points[i] - halfWidth;
1951 float right = points[i] + halfWidth;
1952 float top = points[i + 1] - halfWidth;
1953 float bottom = points [i + 1] + halfWidth;
1954 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07001955 }
1956
1957 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
1958}
1959
Romain Guy85bf02f2010-06-22 13:11:24 -07001960void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001961 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001962 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001963
Romain Guyae88e5e2010-10-22 17:49:18 -07001964 Rect& clip(*mSnapshot->clipRect);
1965 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001966
Romain Guy3d58c032010-07-14 16:34:53 -07001967 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001968}
Romain Guy9d5316e2010-06-24 19:30:36 -07001969
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001970void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001971 if (!texture) return;
1972 const AutoTexture autoCleanup(texture);
1973
1974 const float x = left + texture->left - texture->offset;
1975 const float y = top + texture->top - texture->offset;
1976
1977 drawPathTexture(texture, x, y, paint);
1978}
1979
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001980void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1981 float rx, float ry, SkPaint* paint) {
1982 if (mSnapshot->isIgnored()) return;
1983
1984 glActiveTexture(gTextureUnits[0]);
1985 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1986 right - left, bottom - top, rx, ry, paint);
1987 drawShape(left, top, texture, paint);
1988}
1989
Romain Guy01d58e42011-01-19 21:54:02 -08001990void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1991 if (mSnapshot->isIgnored()) return;
1992
1993 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08001994 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001995 drawShape(x - radius, y - radius, texture, paint);
1996}
Romain Guy01d58e42011-01-19 21:54:02 -08001997
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001998void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
1999 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002000
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002001 glActiveTexture(gTextureUnits[0]);
2002 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2003 drawShape(left, top, texture, paint);
2004}
2005
Romain Guy8b2f5262011-01-23 16:15:02 -08002006void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2007 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2008 if (mSnapshot->isIgnored()) return;
2009
2010 if (fabs(sweepAngle) >= 360.0f) {
2011 drawOval(left, top, right, bottom, paint);
2012 return;
2013 }
2014
2015 glActiveTexture(gTextureUnits[0]);
2016 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2017 startAngle, sweepAngle, useCenter, paint);
2018 drawShape(left, top, texture, paint);
2019}
2020
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002021void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2022 SkPaint* paint) {
2023 if (mSnapshot->isIgnored()) return;
2024
2025 glActiveTexture(gTextureUnits[0]);
2026 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2027 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002028}
2029
Chet Haase5c13d892010-10-08 08:37:55 -07002030void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002031 if (p->getStyle() != SkPaint::kFill_Style) {
2032 drawRectAsShape(left, top, right, bottom, p);
2033 return;
2034 }
2035
Romain Guy6926c722010-07-12 20:20:03 -07002036 if (quickReject(left, top, right, bottom)) {
2037 return;
2038 }
2039
Romain Guy026c5e162010-06-28 17:12:22 -07002040 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002041 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002042 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2043 if (!isMode) {
2044 // Assume SRC_OVER
2045 mode = SkXfermode::kSrcOver_Mode;
2046 }
2047 } else {
2048 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002049 }
2050
Romain Guy026c5e162010-06-28 17:12:22 -07002051 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002052 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002053 drawAARect(left, top, right, bottom, color, mode);
2054 } else {
2055 drawColorRect(left, top, right, bottom, color, mode);
2056 }
Romain Guyc7d53492010-06-25 13:41:57 -07002057}
Romain Guy9d5316e2010-06-24 19:30:36 -07002058
Romain Guye8e62a42010-07-23 18:55:21 -07002059void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
2060 float x, float y, SkPaint* paint) {
Romain Guyb146b122010-12-15 17:06:45 -08002061 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07002062 return;
2063 }
Romain Guyaf636eb2010-12-09 17:47:21 -08002064 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07002065
Romain Guy67ffc362011-06-03 18:50:11 -07002066 // TODO: We should probably make a copy of the paint instead of modifying
2067 // it; modifying the paint will change its generationID the first
2068 // time, which might impact caches. More investigation needed to
2069 // see if it matters.
2070 // If we make a copy, then drawTextDecorations() should *not* make
2071 // its own copy as it does right now.
Romain Guyf607bdc2010-09-10 19:20:06 -07002072 paint->setAntiAlias(true);
Romain Guy67ffc362011-06-03 18:50:11 -07002073#if RENDER_TEXT_AS_GLYPHS
2074 paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding);
2075#endif
Romain Guye8e62a42010-07-23 18:55:21 -07002076
Romain Guya674ab72010-08-10 17:26:42 -07002077 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07002078 switch (paint->getTextAlign()) {
2079 case SkPaint::kCenter_Align:
2080 length = paint->measureText(text, bytesCount);
2081 x -= length / 2.0f;
2082 break;
2083 case SkPaint::kRight_Align:
2084 length = paint->measureText(text, bytesCount);
2085 x -= length;
2086 break;
2087 default:
2088 break;
2089 }
2090
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002091 const float oldX = x;
2092 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002093 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2094 if (pureTranslate) {
2095 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2096 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2097 }
2098
Romain Guyb45c0c92010-08-26 20:35:23 -07002099 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2100 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002101 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002102
Romain Guy86568192010-12-14 15:55:39 -08002103 int alpha;
2104 SkXfermode::Mode mode;
2105 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002106
Romain Guy1e45aae2010-08-13 19:39:53 -07002107 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07002108 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002109 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2110 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002111 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002112
Romain Guy740bf2b2011-04-26 15:33:10 -07002113 const float sx = oldX - shadow->left + mShadowDx;
2114 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002115
Romain Guy86568192010-12-14 15:55:39 -08002116 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002117 int shadowColor = mShadowColor;
2118 if (mShader) {
2119 shadowColor = 0xffffffff;
2120 }
Romain Guy86568192010-12-14 15:55:39 -08002121
2122 glActiveTexture(gTextureUnits[0]);
2123 setupDraw();
2124 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002125 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2126 setupDrawColorFilter();
2127 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002128 setupDrawBlending(true, mode);
2129 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002130 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002131 setupDrawTexture(shadow->id);
2132 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002133 setupDrawColorFilterUniforms();
2134 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002135 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2136
Romain Guy0a417492010-08-16 20:26:20 -07002137 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy740bf2b2011-04-26 15:33:10 -07002138
Romain Guy86568192010-12-14 15:55:39 -08002139 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07002140 }
2141
Romain Guyb146b122010-12-15 17:06:45 -08002142 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
2143 return;
2144 }
2145
Romain Guy6620c6d2010-12-06 18:07:02 -08002146 // Pick the appropriate texture filtering
2147 bool linearFilter = mSnapshot->transform->changesBounds();
2148 if (pureTranslate && !linearFilter) {
2149 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2150 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002151
Romain Guy86568192010-12-14 15:55:39 -08002152 glActiveTexture(gTextureUnits[0]);
2153 setupDraw();
2154 setupDrawDirtyRegionsDisabled();
2155 setupDrawWithTexture(true);
2156 setupDrawAlpha8Color(paint->getColor(), alpha);
2157 setupDrawColorFilter();
2158 setupDrawShader();
2159 setupDrawBlending(true, mode);
2160 setupDrawProgram();
2161 setupDrawModelView(x, y, x, y, pureTranslate, true);
2162 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2163 setupDrawPureColorUniforms();
2164 setupDrawColorFilterUniforms();
2165 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002166
Romain Guy6620c6d2010-12-06 18:07:02 -08002167 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002168 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2169
2170#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002171 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002172#else
Romain Guyf219da52011-01-16 12:54:25 -08002173 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002174#endif
Romain Guy03750a02010-10-18 14:06:08 -07002175 mCaches.unbindMeshBuffer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002176
2177 // Tell font renderer the locations of position and texture coord
2178 // attributes so it can bind its data properly
2179 int positionSlot = mCaches.currentProgram->position;
2180 fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot);
Romain Guy6620c6d2010-12-06 18:07:02 -08002181 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002182 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002183#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002184 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002185 if (!pureTranslate) {
2186 mSnapshot->transform->mapRect(bounds);
2187 }
Romain Guyf219da52011-01-16 12:54:25 -08002188 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002189 }
2190#endif
2191 }
Romain Guy694b5192010-07-21 21:33:20 -07002192
2193 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07002194 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07002195
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002196 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002197}
2198
Romain Guy7fbcc042010-08-04 15:40:07 -07002199void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002200 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002201
Romain Guy01d58e42011-01-19 21:54:02 -08002202 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07002203
Romain Guyfb8b7632010-08-23 21:05:08 -07002204 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07002205 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002206 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002207
Romain Guy8b55f372010-08-18 17:10:07 -07002208 const float x = texture->left - texture->offset;
2209 const float y = texture->top - texture->offset;
2210
Romain Guy01d58e42011-01-19 21:54:02 -08002211 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002212}
2213
Romain Guyada830f2011-01-13 12:13:20 -08002214void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2215 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002216 return;
2217 }
2218
2219 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08002220
2221 int alpha;
2222 SkXfermode::Mode mode;
2223 getAlphaAndMode(paint, &alpha, &mode);
2224
Romain Guy9ace8f52011-07-07 20:50:11 -07002225 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002226
Romain Guyf219da52011-01-16 12:54:25 -08002227#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08002228 if (!layer->region.isEmpty()) {
2229 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002230 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002231 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002232 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002233 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002234
Romain Guyc88e3572011-01-22 00:32:12 -08002235 setupDraw();
2236 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002237 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002238 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002239 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002240 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002241 setupDrawPureColorUniforms();
2242 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002243 setupDrawTexture(layer->getTexture());
2244 if (mSnapshot->transform->isPureTranslate()) {
2245 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2246 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2247
2248 layer->setFilter(GL_NEAREST, GL_NEAREST);
2249 setupDrawModelViewTranslate(x, y,
2250 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2251 } else {
2252 layer->setFilter(GL_LINEAR, GL_LINEAR);
2253 setupDrawModelViewTranslate(x, y,
2254 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2255 }
Romain Guyc88e3572011-01-22 00:32:12 -08002256 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002257
Romain Guyc88e3572011-01-22 00:32:12 -08002258 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2259 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002260
Romain Guyc88e3572011-01-22 00:32:12 -08002261 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002262
2263#if DEBUG_LAYERS_AS_REGIONS
2264 drawRegionRects(layer->region);
2265#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002266 }
Romain Guyf219da52011-01-16 12:54:25 -08002267 }
2268#else
Romain Guyada830f2011-01-13 12:13:20 -08002269 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2270 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002271#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002272}
2273
Romain Guy6926c722010-07-12 20:20:03 -07002274///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002275// Shaders
2276///////////////////////////////////////////////////////////////////////////////
2277
2278void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002279 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002280}
2281
Romain Guy06f96e22010-07-30 19:18:16 -07002282void OpenGLRenderer::setupShader(SkiaShader* shader) {
2283 mShader = shader;
2284 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002285 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002286 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002287}
2288
Romain Guyd27977d2010-07-14 19:18:51 -07002289///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002290// Color filters
2291///////////////////////////////////////////////////////////////////////////////
2292
2293void OpenGLRenderer::resetColorFilter() {
2294 mColorFilter = NULL;
2295}
2296
2297void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2298 mColorFilter = filter;
2299}
2300
2301///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002302// Drop shadow
2303///////////////////////////////////////////////////////////////////////////////
2304
2305void OpenGLRenderer::resetShadow() {
2306 mHasShadow = false;
2307}
2308
2309void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2310 mHasShadow = true;
2311 mShadowRadius = radius;
2312 mShadowDx = dx;
2313 mShadowDy = dy;
2314 mShadowColor = color;
2315}
2316
2317///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002318// Drawing implementation
2319///////////////////////////////////////////////////////////////////////////////
2320
Romain Guy01d58e42011-01-19 21:54:02 -08002321void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2322 float x, float y, SkPaint* paint) {
2323 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2324 return;
2325 }
2326
2327 int alpha;
2328 SkXfermode::Mode mode;
2329 getAlphaAndMode(paint, &alpha, &mode);
2330
2331 setupDraw();
2332 setupDrawWithTexture(true);
2333 setupDrawAlpha8Color(paint->getColor(), alpha);
2334 setupDrawColorFilter();
2335 setupDrawShader();
2336 setupDrawBlending(true, mode);
2337 setupDrawProgram();
2338 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2339 setupDrawTexture(texture->id);
2340 setupDrawPureColorUniforms();
2341 setupDrawColorFilterUniforms();
2342 setupDrawShaderUniforms();
2343 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2344
2345 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2346
2347 finishDrawTexture();
2348}
2349
Romain Guyf607bdc2010-09-10 19:20:06 -07002350// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002351#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2352#define kStdUnderline_Offset (1.0f / 9.0f)
2353#define kStdUnderline_Thickness (1.0f / 18.0f)
2354
2355void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2356 float x, float y, SkPaint* paint) {
2357 // Handle underline and strike-through
2358 uint32_t flags = paint->getFlags();
2359 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002360 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002361 float underlineWidth = length;
2362 // If length is > 0.0f, we already measured the text for the text alignment
2363 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002364 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002365 }
2366
2367 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002368 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002369 case SkPaint::kCenter_Align:
2370 offsetX = underlineWidth * 0.5f;
2371 break;
2372 case SkPaint::kRight_Align:
2373 offsetX = underlineWidth;
2374 break;
2375 default:
2376 break;
2377 }
2378
2379 if (underlineWidth > 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002380 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002381 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002382
Romain Guye20ecbd2010-09-22 19:49:04 -07002383 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002384 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002385
Romain Guyf6834472011-01-23 13:32:12 -08002386 int linesCount = 0;
2387 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2388 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2389
2390 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002391 float points[pointsCount];
2392 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002393
2394 if (flags & SkPaint::kUnderlineText_Flag) {
2395 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002396 points[currentPoint++] = left;
2397 points[currentPoint++] = top;
2398 points[currentPoint++] = left + underlineWidth;
2399 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002400 }
2401
2402 if (flags & SkPaint::kStrikeThruText_Flag) {
2403 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002404 points[currentPoint++] = left;
2405 points[currentPoint++] = top;
2406 points[currentPoint++] = left + underlineWidth;
2407 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002408 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002409
Romain Guy726aeba2011-06-01 14:52:00 -07002410 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002411
Romain Guy726aeba2011-06-01 14:52:00 -07002412 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002413 }
2414 }
2415}
2416
Romain Guy026c5e162010-06-28 17:12:22 -07002417void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002418 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002419 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002420 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002421 color |= 0x00ffffff;
2422 }
2423
Romain Guy70ca14e2010-12-13 18:24:33 -08002424 setupDraw();
2425 setupDrawColor(color);
2426 setupDrawShader();
2427 setupDrawColorFilter();
2428 setupDrawBlending(mode);
2429 setupDrawProgram();
2430 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2431 setupDrawColorUniforms();
2432 setupDrawShaderUniforms(ignoreTransform);
2433 setupDrawColorFilterUniforms();
2434 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002435
Romain Guyc95c8d62010-09-17 15:31:32 -07002436 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2437}
2438
Romain Guy82ba8142010-07-09 13:25:56 -07002439void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002440 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002441 int alpha;
2442 SkXfermode::Mode mode;
2443 getAlphaAndMode(paint, &alpha, &mode);
2444
Romain Guye3c26852011-07-25 16:36:01 -07002445 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002446
Romain Guy6620c6d2010-12-06 18:07:02 -08002447 if (mSnapshot->transform->isPureTranslate()) {
2448 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2449 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2450
Romain Guye3c26852011-07-25 16:36:01 -07002451 texture->setFilter(GL_NEAREST, GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002452 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2453 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2454 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2455 } else {
Romain Guye3c26852011-07-25 16:36:01 -07002456 texture->setFilter(GL_LINEAR, GL_LINEAR, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002457 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2458 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2459 GL_TRIANGLE_STRIP, gMeshCount);
2460 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002461}
2462
Romain Guybd6b79b2010-06-26 00:13:53 -07002463void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002464 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2465 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002466 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002467}
2468
2469void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002470 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002471 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002472 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002473
Romain Guy746b7402010-10-26 16:27:31 -07002474 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002475 setupDrawWithTexture();
2476 setupDrawColor(alpha, alpha, alpha, alpha);
2477 setupDrawColorFilter();
2478 setupDrawBlending(blend, mode, swapSrcDst);
2479 setupDrawProgram();
2480 if (!dirty) {
2481 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002482 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002483 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002484 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002485 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002486 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002487 }
Romain Guy86568192010-12-14 15:55:39 -08002488 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002489 setupDrawColorFilterUniforms();
2490 setupDrawTexture(texture);
2491 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002492
Romain Guy6820ac82010-09-15 18:11:50 -07002493 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002494
2495 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002496}
2497
Romain Guya5aed0d2010-09-09 14:42:43 -07002498void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002499 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002500 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2501 if (blend) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002502 if (mode <= SkXfermode::kScreen_Mode) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002503 if (!mCaches.blend) {
2504 glEnable(GL_BLEND);
2505 }
Romain Guy82ba8142010-07-09 13:25:56 -07002506
Romain Guyf607bdc2010-09-10 19:20:06 -07002507 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2508 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07002509
Romain Guya5aed0d2010-09-09 14:42:43 -07002510 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2511 glBlendFunc(sourceMode, destMode);
2512 mCaches.lastSrcMode = sourceMode;
2513 mCaches.lastDstMode = destMode;
2514 }
2515 } else {
2516 // These blend modes are not supported by OpenGL directly and have
2517 // to be implemented using shaders. Since the shader will perform
2518 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07002519 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002520 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002521 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002522 }
2523
2524 if (mCaches.blend) {
2525 glDisable(GL_BLEND);
2526 }
2527 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07002528 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002529 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002530 glDisable(GL_BLEND);
2531 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002532 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002533}
2534
Romain Guy889f8d12010-07-29 14:37:42 -07002535bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002536 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002537 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002538 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002539 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002540 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002541 }
Romain Guy6926c722010-07-12 20:20:03 -07002542 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002543}
2544
Romain Guy026c5e162010-06-28 17:12:22 -07002545void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002546 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002547 TextureVertex::setUV(v++, u1, v1);
2548 TextureVertex::setUV(v++, u2, v1);
2549 TextureVertex::setUV(v++, u1, v2);
2550 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002551}
2552
Chet Haase5c13d892010-10-08 08:37:55 -07002553void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002554 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002555 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002556
2557 // Skia draws using the color's alpha channel if < 255
2558 // Otherwise, it uses the paint's alpha
2559 int color = paint->getColor();
2560 *alpha = (color >> 24) & 0xFF;
2561 if (*alpha == 255) {
2562 *alpha = paint->getAlpha();
2563 }
2564 } else {
2565 *mode = SkXfermode::kSrcOver_Mode;
2566 *alpha = 255;
2567 }
Romain Guy026c5e162010-06-28 17:12:22 -07002568}
2569
Romain Guya5aed0d2010-09-09 14:42:43 -07002570SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002571 SkXfermode::Mode resultMode;
2572 if (!SkXfermode::AsMode(mode, &resultMode)) {
2573 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002574 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002575 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002576}
2577
Romain Guy9d5316e2010-06-24 19:30:36 -07002578}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002579}; // namespace android