blob: 1a8c19977ca61eb97168575e229b6fbb718fd647 [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) {
Romain Guyda96f8a2011-11-10 19:23:58 -0800614 // Note: No need to use glDiscardFramebufferEXT() since we never
615 // create/compose layers that are not on screen with this
616 // code path
617 // See LayerRenderer::destroyLayer(Layer*)
618
Romain Guyeb993562010-10-05 18:14:38 -0700619 // Detach the texture from the FBO
620 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
621 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
622 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
623
624 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
625 mCaches.fboCache.put(current->fbo);
626 }
627
Romain Guy746b7402010-10-26 16:27:31 -0700628 dirtyClip();
629
Romain Guyeb993562010-10-05 18:14:38 -0700630 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700631 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700632 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700633 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700634 delete layer;
635 }
636}
637
Romain Guyaa6c24c2011-04-28 18:40:04 -0700638void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700639 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700640
Romain Guy302a9df2011-08-16 13:55:02 -0700641 mat4& transform = layer->getTransform();
642 if (!transform.isIdentity()) {
643 save(0);
644 mSnapshot->transform->multiply(transform);
645 }
646
Romain Guyaa6c24c2011-04-28 18:40:04 -0700647 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700648 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700649 setupDrawWithTexture();
650 } else {
651 setupDrawWithExternalTexture();
652 }
653 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700654 setupDrawColor(alpha, alpha, alpha, alpha);
655 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700656 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700657 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700658 setupDrawPureColorUniforms();
659 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700660 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
661 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700662 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700663 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700664 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700665 if (mSnapshot->transform->isPureTranslate() &&
666 layer->getWidth() == (uint32_t) rect.getWidth() &&
667 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700668 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
669 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
670
671 layer->setFilter(GL_NEAREST, GL_NEAREST);
672 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
673 } else {
674 layer->setFilter(GL_LINEAR, GL_LINEAR);
675 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
676 }
677 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700678 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
679
680 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
681
682 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700683
684 if (!transform.isIdentity()) {
685 restore();
686 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700687}
688
Romain Guy5b3b3522010-10-27 18:57:51 -0700689void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700690 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700691 const Rect& texCoords = layer->texCoords;
692 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
693 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700694
Romain Guy9ace8f52011-07-07 20:50:11 -0700695 float x = rect.left;
696 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700697 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700698 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700699 layer->getHeight() == (uint32_t) rect.getHeight();
700
701 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700702 // When we're swapping, the layer is already in screen coordinates
703 if (!swap) {
704 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
705 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
706 }
707
708 layer->setFilter(GL_NEAREST, GL_NEAREST, true);
709 } else {
710 layer->setFilter(GL_LINEAR, GL_LINEAR, true);
711 }
712
713 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
714 layer->getTexture(), layer->getAlpha() / 255.0f,
715 layer->getMode(), layer->isBlend(),
716 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
717 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700718
Romain Guyaa6c24c2011-04-28 18:40:04 -0700719 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
720 } else {
721 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
722 drawTextureLayer(layer, rect);
723 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
724 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700725}
726
727void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
728#if RENDER_LAYERS_AS_REGIONS
729 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700730 layer->setRegionAsRect();
731
Romain Guy40667672011-03-18 14:34:03 -0700732 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700733
Romain Guy5b3b3522010-10-27 18:57:51 -0700734 layer->region.clear();
735 return;
736 }
737
Romain Guy8a3957d2011-09-07 17:55:15 -0700738 // TODO: See LayerRenderer.cpp::generateMesh() for important
739 // information about this implementation
Romain Guy5b3b3522010-10-27 18:57:51 -0700740 if (!layer->region.isEmpty()) {
741 size_t count;
742 const android::Rect* rects = layer->region.getArray(&count);
743
Romain Guy9ace8f52011-07-07 20:50:11 -0700744 const float alpha = layer->getAlpha() / 255.0f;
745 const float texX = 1.0f / float(layer->getWidth());
746 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800747 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700748
749 TextureVertex* mesh = mCaches.getRegionMesh();
750 GLsizei numQuads = 0;
751
Romain Guy7230a742011-01-10 22:26:16 -0800752 setupDraw();
753 setupDrawWithTexture();
754 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800755 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700756 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800757 setupDrawProgram();
758 setupDrawDirtyRegionsDisabled();
759 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800760 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700761 setupDrawTexture(layer->getTexture());
762 if (mSnapshot->transform->isPureTranslate()) {
763 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
764 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
765
766 layer->setFilter(GL_NEAREST, GL_NEAREST);
767 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
768 } else {
769 layer->setFilter(GL_LINEAR, GL_LINEAR);
770 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
771 }
Romain Guy7230a742011-01-10 22:26:16 -0800772 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700773
774 for (size_t i = 0; i < count; i++) {
775 const android::Rect* r = &rects[i];
776
777 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800778 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700779 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800780 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700781
782 // TODO: Reject quads outside of the clip
783 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
784 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
785 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
786 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
787
788 numQuads++;
789
790 if (numQuads >= REGION_MESH_QUAD_COUNT) {
791 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
792 numQuads = 0;
793 mesh = mCaches.getRegionMesh();
794 }
795 }
796
797 if (numQuads > 0) {
798 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
799 }
800
801 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800802 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700803
804#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800805 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700806#endif
807
808 layer->region.clear();
809 }
810#else
811 composeLayerRect(layer, rect);
812#endif
813}
814
Romain Guy3a3133d2011-02-01 22:59:58 -0800815void OpenGLRenderer::drawRegionRects(const Region& region) {
816#if DEBUG_LAYERS_AS_REGIONS
817 size_t count;
818 const android::Rect* rects = region.getArray(&count);
819
820 uint32_t colors[] = {
821 0x7fff0000, 0x7f00ff00,
822 0x7f0000ff, 0x7fff00ff,
823 };
824
825 int offset = 0;
826 int32_t top = rects[0].top;
827
828 for (size_t i = 0; i < count; i++) {
829 if (top != rects[i].top) {
830 offset ^= 0x2;
831 top = rects[i].top;
832 }
833
834 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
835 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
836 SkXfermode::kSrcOver_Mode);
837 }
838#endif
839}
840
Romain Guy5b3b3522010-10-27 18:57:51 -0700841void OpenGLRenderer::dirtyLayer(const float left, const float top,
842 const float right, const float bottom, const mat4 transform) {
843#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800844 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700845 Rect bounds(left, top, right, bottom);
846 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800847 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700848 }
849#endif
850}
851
852void OpenGLRenderer::dirtyLayer(const float left, const float top,
853 const float right, const float bottom) {
854#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800855 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700856 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800857 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800858 }
859#endif
860}
861
862void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
863#if RENDER_LAYERS_AS_REGIONS
864 if (bounds.intersect(*mSnapshot->clipRect)) {
865 bounds.snapToPixelBoundaries();
866 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
867 if (!dirty.isEmpty()) {
868 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700869 }
870 }
871#endif
872}
873
Romain Guy54be1cd2011-06-13 19:04:27 -0700874void OpenGLRenderer::clearLayerRegions() {
875 const size_t count = mLayers.size();
876 if (count == 0) return;
877
878 if (!mSnapshot->isIgnored()) {
879 // Doing several glScissor/glClear here can negatively impact
880 // GPUs with a tiler architecture, instead we draw quads with
881 // the Clear blending mode
882
883 // The list contains bounds that have already been clipped
884 // against their initial clip rect, and the current clip
885 // is likely different so we need to disable clipping here
886 glDisable(GL_SCISSOR_TEST);
887
888 Vertex mesh[count * 6];
889 Vertex* vertex = mesh;
890
891 for (uint32_t i = 0; i < count; i++) {
892 Rect* bounds = mLayers.itemAt(i);
893
894 Vertex::set(vertex++, bounds->left, bounds->bottom);
895 Vertex::set(vertex++, bounds->left, bounds->top);
896 Vertex::set(vertex++, bounds->right, bounds->top);
897 Vertex::set(vertex++, bounds->left, bounds->bottom);
898 Vertex::set(vertex++, bounds->right, bounds->top);
899 Vertex::set(vertex++, bounds->right, bounds->bottom);
900
901 delete bounds;
902 }
903
904 setupDraw(false);
905 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
906 setupDrawBlending(true, SkXfermode::kClear_Mode);
907 setupDrawProgram();
908 setupDrawPureColorUniforms();
909 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
910
911 mCaches.unbindMeshBuffer();
912 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
913 gVertexStride, &mesh[0].position[0]);
914 glDrawArrays(GL_TRIANGLES, 0, count * 6);
915
916 glEnable(GL_SCISSOR_TEST);
917 } else {
918 for (uint32_t i = 0; i < count; i++) {
919 delete mLayers.itemAt(i);
920 }
921 }
922
923 mLayers.clear();
924}
925
Romain Guybd6b79b2010-06-26 00:13:53 -0700926///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700927// Transforms
928///////////////////////////////////////////////////////////////////////////////
929
930void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700931 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700932}
933
934void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700935 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700936}
937
938void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700939 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700940}
941
Romain Guy807daf72011-01-18 11:19:19 -0800942void OpenGLRenderer::skew(float sx, float sy) {
943 mSnapshot->transform->skew(sx, sy);
944}
945
Romain Guyf6a11b82010-06-23 17:47:49 -0700946void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700947 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700948}
949
950void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700951 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700952}
953
954void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700955 SkMatrix transform;
956 mSnapshot->transform->copyTo(transform);
957 transform.preConcat(*matrix);
958 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700959}
960
961///////////////////////////////////////////////////////////////////////////////
962// Clipping
963///////////////////////////////////////////////////////////////////////////////
964
Romain Guybb9524b2010-06-22 18:56:38 -0700965void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700966 Rect clip(*mSnapshot->clipRect);
967 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700968 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700969 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700970}
971
972const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700973 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700974}
975
Romain Guyc7d53492010-06-25 13:41:57 -0700976bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800977 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700978 return true;
979 }
980
Romain Guy1d83e192010-08-17 11:37:00 -0700981 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700982 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700983 r.snapToPixelBoundaries();
984
985 Rect clipRect(*mSnapshot->clipRect);
986 clipRect.snapToPixelBoundaries();
987
988 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700989}
990
Romain Guy079ba2c2010-07-16 14:12:24 -0700991bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
992 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700993 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700994 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700995 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700996 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700997}
998
Romain Guyf6a11b82010-06-23 17:47:49 -0700999///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001000// Drawing commands
1001///////////////////////////////////////////////////////////////////////////////
1002
Romain Guy54be1cd2011-06-13 19:04:27 -07001003void OpenGLRenderer::setupDraw(bool clear) {
1004 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001005 if (mDirtyClip) {
1006 setScissorFromClip();
1007 }
1008 mDescription.reset();
1009 mSetShaderColor = false;
1010 mColorSet = false;
1011 mColorA = mColorR = mColorG = mColorB = 0.0f;
1012 mTextureUnit = 0;
1013 mTrackDirtyRegions = true;
Romain Guy8d0d4782010-12-14 20:13:35 -08001014 mTexCoordsSlot = -1;
Romain Guy70ca14e2010-12-13 18:24:33 -08001015}
1016
1017void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1018 mDescription.hasTexture = true;
1019 mDescription.hasAlpha8Texture = isAlpha8;
1020}
1021
Romain Guyaa6c24c2011-04-28 18:40:04 -07001022void OpenGLRenderer::setupDrawWithExternalTexture() {
1023 mDescription.hasExternalTexture = true;
1024}
1025
Chet Haase5b0200b2011-04-13 17:58:08 -07001026void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001027 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001028}
1029
Romain Guyed6fcb02011-03-21 13:11:28 -07001030void OpenGLRenderer::setupDrawPoint(float pointSize) {
1031 mDescription.isPoint = true;
1032 mDescription.pointSize = pointSize;
1033}
1034
Romain Guy70ca14e2010-12-13 18:24:33 -08001035void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001036 setupDrawColor(color, (color >> 24) & 0xFF);
1037}
1038
1039void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1040 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001041 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1042 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001043 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001044 mColorR = a * ((color >> 16) & 0xFF);
1045 mColorG = a * ((color >> 8) & 0xFF);
1046 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001047 mColorSet = true;
1048 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1049}
1050
Romain Guy86568192010-12-14 15:55:39 -08001051void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1052 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001053 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1054 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001055 const float a = mColorA / 255.0f;
1056 mColorR = a * ((color >> 16) & 0xFF);
1057 mColorG = a * ((color >> 8) & 0xFF);
1058 mColorB = a * ((color ) & 0xFF);
1059 mColorSet = true;
1060 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1061}
1062
Romain Guy70ca14e2010-12-13 18:24:33 -08001063void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1064 mColorA = a;
1065 mColorR = r;
1066 mColorG = g;
1067 mColorB = b;
1068 mColorSet = true;
1069 mSetShaderColor = mDescription.setColor(r, g, b, a);
1070}
1071
Romain Guy86568192010-12-14 15:55:39 -08001072void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1073 mColorA = a;
1074 mColorR = r;
1075 mColorG = g;
1076 mColorB = b;
1077 mColorSet = true;
1078 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1079}
1080
Romain Guy70ca14e2010-12-13 18:24:33 -08001081void OpenGLRenderer::setupDrawShader() {
1082 if (mShader) {
1083 mShader->describe(mDescription, mCaches.extensions);
1084 }
1085}
1086
1087void OpenGLRenderer::setupDrawColorFilter() {
1088 if (mColorFilter) {
1089 mColorFilter->describe(mDescription, mCaches.extensions);
1090 }
1091}
1092
Romain Guyf09ef512011-05-27 11:43:46 -07001093void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1094 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1095 mColorA = 1.0f;
1096 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001097 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001098 }
1099}
1100
Romain Guy70ca14e2010-12-13 18:24:33 -08001101void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001102 // When the blending mode is kClear_Mode, we need to use a modulate color
1103 // argb=1,0,0,0
1104 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001105 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1106 mDescription, swapSrcDst);
1107}
1108
1109void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001110 // When the blending mode is kClear_Mode, we need to use a modulate color
1111 // argb=1,0,0,0
1112 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001113 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1114 mDescription, swapSrcDst);
1115}
1116
1117void OpenGLRenderer::setupDrawProgram() {
1118 useProgram(mCaches.programCache.get(mDescription));
1119}
1120
1121void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1122 mTrackDirtyRegions = false;
1123}
1124
1125void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1126 bool ignoreTransform) {
1127 mModelView.loadTranslate(left, top, 0.0f);
1128 if (!ignoreTransform) {
1129 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1130 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1131 } else {
1132 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1133 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1134 }
1135}
1136
Chet Haase8a5cc922011-04-26 07:28:09 -07001137void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1138 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001139}
1140
Romain Guy70ca14e2010-12-13 18:24:33 -08001141void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1142 bool ignoreTransform, bool ignoreModelView) {
1143 if (!ignoreModelView) {
1144 mModelView.loadTranslate(left, top, 0.0f);
1145 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001146 } else {
1147 mModelView.loadIdentity();
1148 }
Romain Guy86568192010-12-14 15:55:39 -08001149 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1150 if (!ignoreTransform) {
1151 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1152 if (mTrackDirtyRegions && dirty) {
1153 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1154 }
1155 } else {
1156 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1157 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1158 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001159}
1160
Romain Guyed6fcb02011-03-21 13:11:28 -07001161void OpenGLRenderer::setupDrawPointUniforms() {
1162 int slot = mCaches.currentProgram->getUniform("pointSize");
1163 glUniform1f(slot, mDescription.pointSize);
1164}
1165
Romain Guy70ca14e2010-12-13 18:24:33 -08001166void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001167 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001168 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1169 }
1170}
1171
Romain Guy86568192010-12-14 15:55:39 -08001172void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001173 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001174 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001175 }
1176}
1177
Romain Guy70ca14e2010-12-13 18:24:33 -08001178void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1179 if (mShader) {
1180 if (ignoreTransform) {
1181 mModelView.loadInverse(*mSnapshot->transform);
1182 }
1183 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1184 }
1185}
1186
Romain Guy8d0d4782010-12-14 20:13:35 -08001187void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1188 if (mShader) {
1189 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1190 }
1191}
1192
Romain Guy70ca14e2010-12-13 18:24:33 -08001193void OpenGLRenderer::setupDrawColorFilterUniforms() {
1194 if (mColorFilter) {
1195 mColorFilter->setupProgram(mCaches.currentProgram);
1196 }
1197}
1198
1199void OpenGLRenderer::setupDrawSimpleMesh() {
1200 mCaches.bindMeshBuffer();
1201 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1202 gMeshStride, 0);
1203}
1204
1205void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1206 bindTexture(texture);
1207 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1208
1209 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1210 glEnableVertexAttribArray(mTexCoordsSlot);
1211}
1212
Romain Guyaa6c24c2011-04-28 18:40:04 -07001213void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1214 bindExternalTexture(texture);
1215 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1216
1217 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1218 glEnableVertexAttribArray(mTexCoordsSlot);
1219}
1220
Romain Guy8f0095c2011-05-02 17:24:22 -07001221void OpenGLRenderer::setupDrawTextureTransform() {
1222 mDescription.hasTextureTransform = true;
1223}
1224
1225void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001226 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1227 GL_FALSE, &transform.data[0]);
1228}
1229
Romain Guy70ca14e2010-12-13 18:24:33 -08001230void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1231 if (!vertices) {
1232 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1233 } else {
1234 mCaches.unbindMeshBuffer();
1235 }
1236 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1237 gMeshStride, vertices);
David Licf289572011-02-25 12:05:44 -08001238 if (mTexCoordsSlot >= 0) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001239 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1240 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001241}
1242
Chet Haase5b0200b2011-04-13 17:58:08 -07001243void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
1244 mCaches.unbindMeshBuffer();
1245 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1246 gVertexStride, vertices);
1247}
1248
1249/**
1250 * 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 -07001251 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1252 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1253 * attributes (one per vertex) are values from zero to one that tells the fragment
1254 * shader where the fragment is in relation to the line width/length overall; these values are
1255 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1256 * region of the line.
1257 * Note that we only pass down the width values in this setup function. The length coordinates
1258 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001259 */
Chet Haase99585ad2011-05-02 15:00:16 -07001260void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Chet Haase99ecdc42011-05-06 12:06:34 -07001261 GLvoid* lengthCoords, float boundaryWidthProportion) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001262 mCaches.unbindMeshBuffer();
1263 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Chet Haase99585ad2011-05-02 15:00:16 -07001264 gAAVertexStride, vertices);
1265 int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
1266 glEnableVertexAttribArray(widthSlot);
1267 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
1268 int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
1269 glEnableVertexAttribArray(lengthSlot);
1270 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Chet Haase5b0200b2011-04-13 17:58:08 -07001271 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001272 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07001273 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001274 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001275 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
Chet Haase5b0200b2011-04-13 17:58:08 -07001276}
1277
Romain Guy70ca14e2010-12-13 18:24:33 -08001278void OpenGLRenderer::finishDrawTexture() {
1279 glDisableVertexAttribArray(mTexCoordsSlot);
1280}
1281
1282///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001283// Drawing
1284///////////////////////////////////////////////////////////////////////////////
1285
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001286bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
1287 Rect& dirty, uint32_t level) {
1288 if (quickReject(0.0f, 0.0f, width, height)) {
1289 return false;
1290 }
1291
Romain Guy0fe478e2010-11-08 12:08:41 -08001292 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1293 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001294 if (displayList && displayList->isRenderable()) {
Romain Guycabfcc12011-03-07 18:06:46 -08001295 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001296 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001297
Chet Haasedaf98e92011-01-10 14:10:36 -08001298 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001299}
1300
Chet Haaseed30fd82011-04-22 16:18:45 -07001301void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1302 if (displayList) {
1303 displayList->output(*this, level);
1304 }
1305}
1306
Romain Guya168d732011-03-18 16:50:13 -07001307void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1308 int alpha;
1309 SkXfermode::Mode mode;
1310 getAlphaAndMode(paint, &alpha, &mode);
1311
Romain Guya168d732011-03-18 16:50:13 -07001312 float x = left;
1313 float y = top;
1314
Romain Guye3c26852011-07-25 16:36:01 -07001315 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001316 bool ignoreTransform = false;
1317 if (mSnapshot->transform->isPureTranslate()) {
1318 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1319 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1320 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001321 filter = GL_NEAREST;
Romain Guya168d732011-03-18 16:50:13 -07001322 }
1323
1324 setupDraw();
1325 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001326 if (paint) {
1327 setupDrawAlpha8Color(paint->getColor(), alpha);
1328 }
Romain Guya168d732011-03-18 16:50:13 -07001329 setupDrawColorFilter();
1330 setupDrawShader();
1331 setupDrawBlending(true, mode);
1332 setupDrawProgram();
1333 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001334
Romain Guya168d732011-03-18 16:50:13 -07001335 setupDrawTexture(texture->id);
Romain Guye3c26852011-07-25 16:36:01 -07001336 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1337 texture->setFilter(filter, filter);
1338
Romain Guya168d732011-03-18 16:50:13 -07001339 setupDrawPureColorUniforms();
1340 setupDrawColorFilterUniforms();
1341 setupDrawShaderUniforms();
1342 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1343
1344 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1345
1346 finishDrawTexture();
1347}
1348
Chet Haase5c13d892010-10-08 08:37:55 -07001349void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001350 const float right = left + bitmap->width();
1351 const float bottom = top + bitmap->height();
1352
1353 if (quickReject(left, top, right, bottom)) {
1354 return;
1355 }
1356
Romain Guy86568192010-12-14 15:55:39 -08001357 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001358 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001359 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001360 const AutoTexture autoCleanup(texture);
1361
Romain Guya168d732011-03-18 16:50:13 -07001362 if (bitmap->getConfig() == SkBitmap::kA8_Config) {
1363 drawAlphaBitmap(texture, left, top, paint);
1364 } else {
1365 drawTextureRect(left, top, right, bottom, texture, paint);
1366 }
Romain Guyce0537b2010-06-29 21:05:21 -07001367}
1368
Chet Haase5c13d892010-10-08 08:37:55 -07001369void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001370 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1371 const mat4 transform(*matrix);
1372 transform.mapRect(r);
1373
Romain Guy6926c722010-07-12 20:20:03 -07001374 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1375 return;
1376 }
1377
Romain Guy86568192010-12-14 15:55:39 -08001378 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001379 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001380 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001381 const AutoTexture autoCleanup(texture);
1382
Romain Guy5b3b3522010-10-27 18:57:51 -07001383 // This could be done in a cheaper way, all we need is pass the matrix
1384 // to the vertex shader. The save/restore is a bit overkill.
1385 save(SkCanvas::kMatrix_SaveFlag);
1386 concatMatrix(matrix);
1387 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1388 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001389}
1390
Romain Guy5a7b4662011-01-20 19:09:30 -08001391void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1392 float* vertices, int* colors, SkPaint* paint) {
1393 // TODO: Do a quickReject
1394 if (!vertices || mSnapshot->isIgnored()) {
1395 return;
1396 }
1397
1398 glActiveTexture(gTextureUnits[0]);
1399 Texture* texture = mCaches.textureCache.get(bitmap);
1400 if (!texture) return;
1401 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001402
1403 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, true);
1404 texture->setFilter(GL_LINEAR, GL_LINEAR, true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001405
1406 int alpha;
1407 SkXfermode::Mode mode;
1408 getAlphaAndMode(paint, &alpha, &mode);
1409
Romain Guy5a7b4662011-01-20 19:09:30 -08001410 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001411
Romain Guyb18d2d02011-02-10 15:52:54 -08001412 float left = FLT_MAX;
1413 float top = FLT_MAX;
1414 float right = FLT_MIN;
1415 float bottom = FLT_MIN;
1416
1417#if RENDER_LAYERS_AS_REGIONS
1418 bool hasActiveLayer = hasLayer();
1419#else
1420 bool hasActiveLayer = false;
1421#endif
1422
Romain Guya566b7c2011-01-23 16:36:11 -08001423 // TODO: Support the colors array
1424 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001425 TextureVertex* vertex = mesh;
1426 for (int32_t y = 0; y < meshHeight; y++) {
1427 for (int32_t x = 0; x < meshWidth; x++) {
1428 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1429
1430 float u1 = float(x) / meshWidth;
1431 float u2 = float(x + 1) / meshWidth;
1432 float v1 = float(y) / meshHeight;
1433 float v2 = float(y + 1) / meshHeight;
1434
1435 int ax = i + (meshWidth + 1) * 2;
1436 int ay = ax + 1;
1437 int bx = i;
1438 int by = bx + 1;
1439 int cx = i + 2;
1440 int cy = cx + 1;
1441 int dx = i + (meshWidth + 1) * 2 + 2;
1442 int dy = dx + 1;
1443
1444 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1445 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1446 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1447
1448 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1449 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1450 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001451
1452#if RENDER_LAYERS_AS_REGIONS
1453 if (hasActiveLayer) {
1454 // TODO: This could be optimized to avoid unnecessary ops
1455 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1456 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1457 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1458 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1459 }
1460#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001461 }
1462 }
1463
Romain Guyb18d2d02011-02-10 15:52:54 -08001464#if RENDER_LAYERS_AS_REGIONS
1465 if (hasActiveLayer) {
1466 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1467 }
1468#endif
1469
Romain Guy5a7b4662011-01-20 19:09:30 -08001470 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1471 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001472 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001473}
1474
Romain Guy8ba548f2010-06-30 19:21:21 -07001475void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1476 float srcLeft, float srcTop, float srcRight, float srcBottom,
1477 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001478 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001479 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1480 return;
1481 }
1482
Romain Guy746b7402010-10-26 16:27:31 -07001483 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001484 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001485 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001486 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001487 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, true);
Romain Guy8ba548f2010-06-30 19:21:21 -07001488
Romain Guy8ba548f2010-06-30 19:21:21 -07001489 const float width = texture->width;
1490 const float height = texture->height;
1491
Romain Guy68169722011-08-22 17:33:33 -07001492 const float u1 = fmax(0.0f, srcLeft / width);
1493 const float v1 = fmax(0.0f, srcTop / height);
1494 const float u2 = fmin(1.0f, srcRight / width);
1495 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001496
Romain Guy03750a02010-10-18 14:06:08 -07001497 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001498 resetDrawTextureTexCoords(u1, v1, u2, v2);
1499
Romain Guy03750a02010-10-18 14:06:08 -07001500 int alpha;
1501 SkXfermode::Mode mode;
1502 getAlphaAndMode(paint, &alpha, &mode);
1503
Romain Guy6620c6d2010-12-06 18:07:02 -08001504 if (mSnapshot->transform->isPureTranslate()) {
1505 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1506 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1507
Romain Guyb5014982011-07-28 15:39:12 -07001508 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001509 // Enable linear filtering if the source rectangle is scaled
1510 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyb5014982011-07-28 15:39:12 -07001511 filter = GL_LINEAR;
1512 }
1513 texture->setFilter(filter, filter, true);
1514
Romain Guy6620c6d2010-12-06 18:07:02 -08001515 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1516 texture->id, alpha / 255.0f, mode, texture->blend,
1517 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1518 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1519 } else {
Romain Guye3c26852011-07-25 16:36:01 -07001520 texture->setFilter(GL_LINEAR, GL_LINEAR, true);
Romain Guyb5014982011-07-28 15:39:12 -07001521
Romain Guy6620c6d2010-12-06 18:07:02 -08001522 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1523 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1524 GL_TRIANGLE_STRIP, gMeshCount);
1525 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001526
1527 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1528}
1529
Romain Guy4aa90572010-09-26 18:40:37 -07001530void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001531 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001532 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001533 if (quickReject(left, top, right, bottom)) {
1534 return;
1535 }
1536
Romain Guy746b7402010-10-26 16:27:31 -07001537 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001538 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001539 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001540 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001541 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, true);
1542 texture->setFilter(GL_LINEAR, GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001543
1544 int alpha;
1545 SkXfermode::Mode mode;
1546 getAlphaAndMode(paint, &alpha, &mode);
1547
Romain Guy2728f962010-10-08 18:36:15 -07001548 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001549 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001550
Romain Guya5ef39a2010-12-03 16:48:20 -08001551 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001552 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001553#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001554 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001555 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001556 const float offsetX = left + mSnapshot->transform->getTranslateX();
1557 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001558 const size_t count = mesh->quads.size();
1559 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001560 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001561 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001562 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1563 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1564 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001565 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001566 dirtyLayer(left + bounds.left, top + bounds.top,
1567 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001568 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001569 }
1570 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001571#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001572
Romain Guy6620c6d2010-12-06 18:07:02 -08001573 if (pureTranslate) {
1574 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1575 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1576
1577 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1578 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1579 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1580 true, !mesh->hasEmptyQuads);
1581 } else {
1582 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1583 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1584 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1585 true, !mesh->hasEmptyQuads);
1586 }
Romain Guy054dc182010-10-15 17:55:25 -07001587 }
Romain Guyf7f93552010-07-08 19:17:03 -07001588}
1589
Chet Haase99ecdc42011-05-06 12:06:34 -07001590/**
Chet Haase858aa932011-05-12 09:06:00 -07001591 * This function uses a similar approach to that of AA lines in the drawLines() function.
1592 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1593 * shader to compute the translucency of the color, determined by whether a given pixel is
1594 * within that boundary region and how far into the region it is.
1595 */
1596void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001597 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001598 float inverseScaleX = 1.0f;
1599 float inverseScaleY = 1.0f;
1600 // The quad that we use needs to account for scaling.
1601 if (!mSnapshot->transform->isPureTranslate()) {
1602 Matrix4 *mat = mSnapshot->transform;
1603 float m00 = mat->data[Matrix4::kScaleX];
1604 float m01 = mat->data[Matrix4::kSkewY];
1605 float m02 = mat->data[2];
1606 float m10 = mat->data[Matrix4::kSkewX];
1607 float m11 = mat->data[Matrix4::kScaleX];
1608 float m12 = mat->data[6];
1609 float scaleX = sqrt(m00 * m00 + m01 * m01);
1610 float scaleY = sqrt(m10 * m10 + m11 * m11);
1611 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1612 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1613 }
1614
1615 setupDraw();
1616 setupDrawAALine();
1617 setupDrawColor(color);
1618 setupDrawColorFilter();
1619 setupDrawShader();
1620 setupDrawBlending(true, mode);
1621 setupDrawProgram();
1622 setupDrawModelViewIdentity(true);
1623 setupDrawColorUniforms();
1624 setupDrawColorFilterUniforms();
1625 setupDrawShaderIdentityUniforms();
1626
1627 AAVertex rects[4];
1628 AAVertex* aaVertices = &rects[0];
1629 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1630 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1631
1632 float boundarySizeX = .5 * inverseScaleX;
1633 float boundarySizeY = .5 * inverseScaleY;
1634
1635 // Adjust the rect by the AA boundary padding
1636 left -= boundarySizeX;
1637 right += boundarySizeX;
1638 top -= boundarySizeY;
1639 bottom += boundarySizeY;
1640
1641 float width = right - left;
1642 float height = bottom - top;
1643
1644 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1645 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
1646 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
1647 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1648 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1649 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
1650 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryHeightProportion));
1651
1652 if (!quickReject(left, top, right, bottom)) {
1653 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1654 AAVertex::set(aaVertices++, left, top, 1, 0);
1655 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1656 AAVertex::set(aaVertices++, right, top, 0, 0);
1657 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1658 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1659 }
1660}
1661
1662/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001663 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1664 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1665 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1666 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1667 * of the line. Hairlines are more involved because we need to account for transform scaling
1668 * to end up with a one-pixel-wide line in screen space..
1669 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1670 * in combination with values that we calculate and pass down in this method. The basic approach
1671 * is that the quad we create contains both the core line area plus a bounding area in which
1672 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1673 * proportion of the width and the length of a given segment is represented by the boundary
1674 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1675 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1676 * on the inside). This ends up giving the result we want, with pixels that are completely
1677 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1678 * how far into the boundary region they are, which is determined by shader interpolation.
1679 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001680void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1681 if (mSnapshot->isIgnored()) return;
1682
1683 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001684 // We use half the stroke width here because we're going to position the quad
1685 // corner vertices half of the width away from the line endpoints
1686 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001687 // A stroke width of 0 has a special meaning in Skia:
1688 // it draws a line 1 px wide regardless of current transform
1689 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase99ecdc42011-05-06 12:06:34 -07001690 float inverseScaleX = 1.0f;
1691 float inverseScaleY = 1.0f;
1692 bool scaled = false;
Chet Haase8a5cc922011-04-26 07:28:09 -07001693 int alpha;
1694 SkXfermode::Mode mode;
1695 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001696 int verticesCount = count;
1697 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001698 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001699 verticesCount += (count - 4);
1700 }
1701
1702 if (isHairLine || isAA) {
1703 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1704 // the line on the screen should always be one pixel wide regardless of scale. For
1705 // AA lines, we only want one pixel of translucent boundary around the quad.
1706 if (!mSnapshot->transform->isPureTranslate()) {
1707 Matrix4 *mat = mSnapshot->transform;
1708 float m00 = mat->data[Matrix4::kScaleX];
1709 float m01 = mat->data[Matrix4::kSkewY];
1710 float m02 = mat->data[2];
1711 float m10 = mat->data[Matrix4::kSkewX];
1712 float m11 = mat->data[Matrix4::kScaleX];
1713 float m12 = mat->data[6];
1714 float scaleX = sqrt(m00*m00 + m01*m01);
1715 float scaleY = sqrt(m10*m10 + m11*m11);
1716 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1717 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1718 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1719 scaled = true;
1720 }
1721 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001722 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001723
1724 getAlphaAndMode(paint, &alpha, &mode);
1725 setupDraw();
1726 if (isAA) {
1727 setupDrawAALine();
1728 }
1729 setupDrawColor(paint->getColor(), alpha);
1730 setupDrawColorFilter();
1731 setupDrawShader();
1732 if (isAA) {
1733 setupDrawBlending(true, mode);
1734 } else {
1735 setupDrawBlending(mode);
1736 }
1737 setupDrawProgram();
1738 setupDrawModelViewIdentity(true);
1739 setupDrawColorUniforms();
1740 setupDrawColorFilterUniforms();
1741 setupDrawShaderIdentityUniforms();
1742
1743 if (isHairLine) {
1744 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001745 halfStrokeWidth = isAA? 1 : .5;
1746 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001747 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001748 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001749 }
1750 Vertex lines[verticesCount];
1751 Vertex* vertices = &lines[0];
Chet Haase99585ad2011-05-02 15:00:16 -07001752 AAVertex wLines[verticesCount];
1753 AAVertex* aaVertices = &wLines[0];
Chet Haase5b0200b2011-04-13 17:58:08 -07001754 if (!isAA) {
1755 setupDrawVertices(vertices);
1756 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001757 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1758 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001759 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001760 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1761 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001762 // We will need to calculate the actual width proportion on each segment for
1763 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1764 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
1765 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
Chet Haase5b0200b2011-04-13 17:58:08 -07001766 }
1767
Chet Haase99ecdc42011-05-06 12:06:34 -07001768 AAVertex* prevAAVertex = NULL;
1769 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001770
Chet Haase99585ad2011-05-02 15:00:16 -07001771 int boundaryLengthSlot = -1;
1772 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001773 int boundaryWidthSlot = -1;
1774 int inverseBoundaryWidthSlot = -1;
Chet Haase5b0200b2011-04-13 17:58:08 -07001775 for (int i = 0; i < count; i += 4) {
1776 // a = start point, b = end point
1777 vec2 a(points[i], points[i + 1]);
1778 vec2 b(points[i + 2], points[i + 3]);
Chet Haase99585ad2011-05-02 15:00:16 -07001779 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001780 float boundaryLengthProportion = 0;
1781 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001782
Chet Haase5b0200b2011-04-13 17:58:08 -07001783 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001784 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001785 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001786 if (isAA) {
1787 float wideningFactor;
1788 if (fabs(n.x) >= fabs(n.y)) {
1789 wideningFactor = fabs(1.0f / n.x);
1790 } else {
1791 wideningFactor = fabs(1.0f / n.y);
1792 }
1793 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001794 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001795 if (scaled) {
1796 n.x *= inverseScaleX;
1797 n.y *= inverseScaleY;
1798 }
1799 } else if (scaled) {
1800 // Extend n by .5 pixel on each side, post-transform
1801 vec2 extendedN = n.copyNormalized();
1802 extendedN /= 2;
1803 extendedN.x *= inverseScaleX;
1804 extendedN.y *= inverseScaleY;
1805 float extendedNLength = extendedN.length();
1806 // We need to set this value on the shader prior to drawing
1807 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1808 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001809 }
1810 float x = n.x;
1811 n.x = -n.y;
1812 n.y = x;
1813
Chet Haase99585ad2011-05-02 15:00:16 -07001814 // aa lines expand the endpoint vertices to encompass the AA boundary
1815 if (isAA) {
1816 vec2 abVector = (b - a);
1817 length = abVector.length();
1818 abVector.normalize();
Chet Haase99ecdc42011-05-06 12:06:34 -07001819 if (scaled) {
1820 abVector.x *= inverseScaleX;
1821 abVector.y *= inverseScaleY;
1822 float abLength = abVector.length();
1823 boundaryLengthProportion = abLength / (length + abLength);
1824 } else {
1825 boundaryLengthProportion = .5 / (length + 1);
1826 }
1827 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001828 a -= abVector;
1829 b += abVector;
1830 }
1831
Chet Haase5b0200b2011-04-13 17:58:08 -07001832 // Four corners of the rectangle defining a thick line
1833 vec2 p1 = a - n;
1834 vec2 p2 = a + n;
1835 vec2 p3 = b + n;
1836 vec2 p4 = b - n;
1837
Chet Haase99585ad2011-05-02 15:00:16 -07001838
Chet Haase5b0200b2011-04-13 17:58:08 -07001839 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1840 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1841 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1842 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1843
1844 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001845 if (!isAA) {
1846 if (prevVertex != NULL) {
1847 // Issue two repeat vertices to create degenerate triangles to bridge
1848 // between the previous line and the new one. This is necessary because
1849 // we are creating a single triangle_strip which will contain
1850 // potentially discontinuous line segments.
1851 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1852 Vertex::set(vertices++, p1.x, p1.y);
1853 generatedVerticesCount += 2;
1854 }
1855 Vertex::set(vertices++, p1.x, p1.y);
1856 Vertex::set(vertices++, p2.x, p2.y);
1857 Vertex::set(vertices++, p4.x, p4.y);
1858 Vertex::set(vertices++, p3.x, p3.y);
1859 prevVertex = vertices - 1;
1860 generatedVerticesCount += 4;
1861 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001862 if (!isHairLine && scaled) {
1863 // Must set width proportions per-segment for scaled non-hairlines to use the
1864 // correct AA boundary dimensions
1865 if (boundaryWidthSlot < 0) {
1866 boundaryWidthSlot =
1867 mCaches.currentProgram->getUniform("boundaryWidth");
1868 inverseBoundaryWidthSlot =
1869 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1870 }
1871 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1872 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1873 }
Chet Haase99585ad2011-05-02 15:00:16 -07001874 if (boundaryLengthSlot < 0) {
1875 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1876 inverseBoundaryLengthSlot =
1877 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1878 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001879 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
1880 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07001881
Chet Haase5b0200b2011-04-13 17:58:08 -07001882 if (prevAAVertex != NULL) {
1883 // Issue two repeat vertices to create degenerate triangles to bridge
1884 // between the previous line and the new one. This is necessary because
1885 // we are creating a single triangle_strip which will contain
1886 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07001887 AAVertex::set(aaVertices++,prevAAVertex->position[0],
1888 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
1889 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07001890 generatedVerticesCount += 2;
1891 }
Chet Haase99585ad2011-05-02 15:00:16 -07001892 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
1893 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
1894 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
1895 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Chet Haase5b0200b2011-04-13 17:58:08 -07001896 prevAAVertex = aaVertices - 1;
1897 generatedVerticesCount += 4;
1898 }
Chet Haase99585ad2011-05-02 15:00:16 -07001899 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
1900 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
1901 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07001902 }
1903 }
1904 if (generatedVerticesCount > 0) {
1905 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
1906 }
1907}
1908
Romain Guyed6fcb02011-03-21 13:11:28 -07001909void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
1910 if (mSnapshot->isIgnored()) return;
1911
1912 // TODO: The paint's cap style defines whether the points are square or circular
1913 // TODO: Handle AA for round points
1914
Chet Haase5b0200b2011-04-13 17:58:08 -07001915 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07001916 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07001917 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07001918 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001919 if (isHairLine) {
1920 // Now that we know it's hairline, we can set the effective width, to be used later
1921 strokeWidth = 1.0f;
1922 }
1923 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07001924 int alpha;
1925 SkXfermode::Mode mode;
1926 getAlphaAndMode(paint, &alpha, &mode);
1927
1928 int verticesCount = count >> 1;
1929 int generatedVerticesCount = 0;
1930
1931 TextureVertex pointsData[verticesCount];
1932 TextureVertex* vertex = &pointsData[0];
1933
1934 setupDraw();
Chet Haase8a5cc922011-04-26 07:28:09 -07001935 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07001936 setupDrawColor(paint->getColor(), alpha);
1937 setupDrawColorFilter();
1938 setupDrawShader();
1939 setupDrawBlending(mode);
1940 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07001941 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07001942 setupDrawColorUniforms();
1943 setupDrawColorFilterUniforms();
1944 setupDrawPointUniforms();
1945 setupDrawShaderIdentityUniforms();
1946 setupDrawMesh(vertex);
1947
1948 for (int i = 0; i < count; i += 2) {
1949 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1950 generatedVerticesCount++;
Chet Haase8a5cc922011-04-26 07:28:09 -07001951 float left = points[i] - halfWidth;
1952 float right = points[i] + halfWidth;
1953 float top = points[i + 1] - halfWidth;
1954 float bottom = points [i + 1] + halfWidth;
1955 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07001956 }
1957
1958 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
1959}
1960
Romain Guy85bf02f2010-06-22 13:11:24 -07001961void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001962 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001963 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001964
Romain Guyae88e5e2010-10-22 17:49:18 -07001965 Rect& clip(*mSnapshot->clipRect);
1966 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001967
Romain Guy3d58c032010-07-14 16:34:53 -07001968 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001969}
Romain Guy9d5316e2010-06-24 19:30:36 -07001970
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001971void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001972 if (!texture) return;
1973 const AutoTexture autoCleanup(texture);
1974
1975 const float x = left + texture->left - texture->offset;
1976 const float y = top + texture->top - texture->offset;
1977
1978 drawPathTexture(texture, x, y, paint);
1979}
1980
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001981void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1982 float rx, float ry, SkPaint* paint) {
1983 if (mSnapshot->isIgnored()) return;
1984
1985 glActiveTexture(gTextureUnits[0]);
1986 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1987 right - left, bottom - top, rx, ry, paint);
1988 drawShape(left, top, texture, paint);
1989}
1990
Romain Guy01d58e42011-01-19 21:54:02 -08001991void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1992 if (mSnapshot->isIgnored()) return;
1993
1994 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08001995 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001996 drawShape(x - radius, y - radius, texture, paint);
1997}
Romain Guy01d58e42011-01-19 21:54:02 -08001998
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001999void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2000 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002001
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002002 glActiveTexture(gTextureUnits[0]);
2003 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2004 drawShape(left, top, texture, paint);
2005}
2006
Romain Guy8b2f5262011-01-23 16:15:02 -08002007void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2008 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2009 if (mSnapshot->isIgnored()) return;
2010
2011 if (fabs(sweepAngle) >= 360.0f) {
2012 drawOval(left, top, right, bottom, paint);
2013 return;
2014 }
2015
2016 glActiveTexture(gTextureUnits[0]);
2017 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2018 startAngle, sweepAngle, useCenter, paint);
2019 drawShape(left, top, texture, paint);
2020}
2021
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002022void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2023 SkPaint* paint) {
2024 if (mSnapshot->isIgnored()) return;
2025
2026 glActiveTexture(gTextureUnits[0]);
2027 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2028 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002029}
2030
Chet Haase5c13d892010-10-08 08:37:55 -07002031void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002032 if (p->getStyle() != SkPaint::kFill_Style) {
2033 drawRectAsShape(left, top, right, bottom, p);
2034 return;
2035 }
2036
Romain Guy6926c722010-07-12 20:20:03 -07002037 if (quickReject(left, top, right, bottom)) {
2038 return;
2039 }
2040
Romain Guy026c5e162010-06-28 17:12:22 -07002041 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002042 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002043 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2044 if (!isMode) {
2045 // Assume SRC_OVER
2046 mode = SkXfermode::kSrcOver_Mode;
2047 }
2048 } else {
2049 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002050 }
2051
Romain Guy026c5e162010-06-28 17:12:22 -07002052 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002053 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002054 drawAARect(left, top, right, bottom, color, mode);
2055 } else {
2056 drawColorRect(left, top, right, bottom, color, mode);
2057 }
Romain Guyc7d53492010-06-25 13:41:57 -07002058}
Romain Guy9d5316e2010-06-24 19:30:36 -07002059
Romain Guye8e62a42010-07-23 18:55:21 -07002060void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
2061 float x, float y, SkPaint* paint) {
Romain Guyb146b122010-12-15 17:06:45 -08002062 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07002063 return;
2064 }
Romain Guyaf636eb2010-12-09 17:47:21 -08002065 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07002066
Romain Guy67ffc362011-06-03 18:50:11 -07002067 // TODO: We should probably make a copy of the paint instead of modifying
2068 // it; modifying the paint will change its generationID the first
2069 // time, which might impact caches. More investigation needed to
2070 // see if it matters.
2071 // If we make a copy, then drawTextDecorations() should *not* make
2072 // its own copy as it does right now.
Romain Guyf607bdc2010-09-10 19:20:06 -07002073 paint->setAntiAlias(true);
Romain Guy67ffc362011-06-03 18:50:11 -07002074#if RENDER_TEXT_AS_GLYPHS
2075 paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding);
2076#endif
Romain Guye8e62a42010-07-23 18:55:21 -07002077
Romain Guya674ab72010-08-10 17:26:42 -07002078 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07002079 switch (paint->getTextAlign()) {
2080 case SkPaint::kCenter_Align:
2081 length = paint->measureText(text, bytesCount);
2082 x -= length / 2.0f;
2083 break;
2084 case SkPaint::kRight_Align:
2085 length = paint->measureText(text, bytesCount);
2086 x -= length;
2087 break;
2088 default:
2089 break;
2090 }
2091
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002092 const float oldX = x;
2093 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002094 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2095 if (pureTranslate) {
2096 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2097 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2098 }
2099
Romain Guyb45c0c92010-08-26 20:35:23 -07002100 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2101 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002102 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002103
Romain Guy86568192010-12-14 15:55:39 -08002104 int alpha;
2105 SkXfermode::Mode mode;
2106 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002107
Romain Guy1e45aae2010-08-13 19:39:53 -07002108 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07002109 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002110 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2111 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002112 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002113
Romain Guy740bf2b2011-04-26 15:33:10 -07002114 const float sx = oldX - shadow->left + mShadowDx;
2115 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002116
Romain Guy86568192010-12-14 15:55:39 -08002117 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002118 int shadowColor = mShadowColor;
2119 if (mShader) {
2120 shadowColor = 0xffffffff;
2121 }
Romain Guy86568192010-12-14 15:55:39 -08002122
2123 glActiveTexture(gTextureUnits[0]);
2124 setupDraw();
2125 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002126 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2127 setupDrawColorFilter();
2128 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002129 setupDrawBlending(true, mode);
2130 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002131 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002132 setupDrawTexture(shadow->id);
2133 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002134 setupDrawColorFilterUniforms();
2135 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002136 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2137
Romain Guy0a417492010-08-16 20:26:20 -07002138 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy740bf2b2011-04-26 15:33:10 -07002139
Romain Guy86568192010-12-14 15:55:39 -08002140 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07002141 }
2142
Romain Guyb146b122010-12-15 17:06:45 -08002143 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
2144 return;
2145 }
2146
Romain Guy6620c6d2010-12-06 18:07:02 -08002147 // Pick the appropriate texture filtering
2148 bool linearFilter = mSnapshot->transform->changesBounds();
2149 if (pureTranslate && !linearFilter) {
2150 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2151 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002152
Romain Guy86568192010-12-14 15:55:39 -08002153 glActiveTexture(gTextureUnits[0]);
2154 setupDraw();
2155 setupDrawDirtyRegionsDisabled();
2156 setupDrawWithTexture(true);
2157 setupDrawAlpha8Color(paint->getColor(), alpha);
2158 setupDrawColorFilter();
2159 setupDrawShader();
2160 setupDrawBlending(true, mode);
2161 setupDrawProgram();
2162 setupDrawModelView(x, y, x, y, pureTranslate, true);
2163 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2164 setupDrawPureColorUniforms();
2165 setupDrawColorFilterUniforms();
2166 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002167
Romain Guy6620c6d2010-12-06 18:07:02 -08002168 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002169 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2170
2171#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002172 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002173#else
Romain Guyf219da52011-01-16 12:54:25 -08002174 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002175#endif
Romain Guy03750a02010-10-18 14:06:08 -07002176 mCaches.unbindMeshBuffer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002177
2178 // Tell font renderer the locations of position and texture coord
2179 // attributes so it can bind its data properly
2180 int positionSlot = mCaches.currentProgram->position;
2181 fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot);
Romain Guy6620c6d2010-12-06 18:07:02 -08002182 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002183 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002184#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002185 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002186 if (!pureTranslate) {
2187 mSnapshot->transform->mapRect(bounds);
2188 }
Romain Guyf219da52011-01-16 12:54:25 -08002189 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002190 }
2191#endif
2192 }
Romain Guy694b5192010-07-21 21:33:20 -07002193
2194 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07002195 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07002196
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002197 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002198}
2199
Romain Guy7fbcc042010-08-04 15:40:07 -07002200void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002201 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002202
Romain Guy01d58e42011-01-19 21:54:02 -08002203 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07002204
Romain Guyfb8b7632010-08-23 21:05:08 -07002205 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07002206 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002207 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002208
Romain Guy8b55f372010-08-18 17:10:07 -07002209 const float x = texture->left - texture->offset;
2210 const float y = texture->top - texture->offset;
2211
Romain Guy01d58e42011-01-19 21:54:02 -08002212 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002213}
2214
Romain Guyada830f2011-01-13 12:13:20 -08002215void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2216 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002217 return;
2218 }
2219
2220 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08002221
2222 int alpha;
2223 SkXfermode::Mode mode;
2224 getAlphaAndMode(paint, &alpha, &mode);
2225
Romain Guy9ace8f52011-07-07 20:50:11 -07002226 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002227
Romain Guyf219da52011-01-16 12:54:25 -08002228#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08002229 if (!layer->region.isEmpty()) {
2230 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002231 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002232 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002233 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002234 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002235
Romain Guyc88e3572011-01-22 00:32:12 -08002236 setupDraw();
2237 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002238 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002239 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002240 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002241 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002242 setupDrawPureColorUniforms();
2243 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002244 setupDrawTexture(layer->getTexture());
2245 if (mSnapshot->transform->isPureTranslate()) {
2246 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2247 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2248
2249 layer->setFilter(GL_NEAREST, GL_NEAREST);
2250 setupDrawModelViewTranslate(x, y,
2251 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2252 } else {
2253 layer->setFilter(GL_LINEAR, GL_LINEAR);
2254 setupDrawModelViewTranslate(x, y,
2255 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2256 }
Romain Guyc88e3572011-01-22 00:32:12 -08002257 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002258
Romain Guyc88e3572011-01-22 00:32:12 -08002259 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2260 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002261
Romain Guyc88e3572011-01-22 00:32:12 -08002262 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002263
2264#if DEBUG_LAYERS_AS_REGIONS
2265 drawRegionRects(layer->region);
2266#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002267 }
Romain Guyf219da52011-01-16 12:54:25 -08002268 }
2269#else
Romain Guyada830f2011-01-13 12:13:20 -08002270 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2271 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002272#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002273}
2274
Romain Guy6926c722010-07-12 20:20:03 -07002275///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002276// Shaders
2277///////////////////////////////////////////////////////////////////////////////
2278
2279void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002280 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002281}
2282
Romain Guy06f96e22010-07-30 19:18:16 -07002283void OpenGLRenderer::setupShader(SkiaShader* shader) {
2284 mShader = shader;
2285 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002286 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002287 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002288}
2289
Romain Guyd27977d2010-07-14 19:18:51 -07002290///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002291// Color filters
2292///////////////////////////////////////////////////////////////////////////////
2293
2294void OpenGLRenderer::resetColorFilter() {
2295 mColorFilter = NULL;
2296}
2297
2298void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2299 mColorFilter = filter;
2300}
2301
2302///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002303// Drop shadow
2304///////////////////////////////////////////////////////////////////////////////
2305
2306void OpenGLRenderer::resetShadow() {
2307 mHasShadow = false;
2308}
2309
2310void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2311 mHasShadow = true;
2312 mShadowRadius = radius;
2313 mShadowDx = dx;
2314 mShadowDy = dy;
2315 mShadowColor = color;
2316}
2317
2318///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002319// Drawing implementation
2320///////////////////////////////////////////////////////////////////////////////
2321
Romain Guy01d58e42011-01-19 21:54:02 -08002322void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2323 float x, float y, SkPaint* paint) {
2324 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2325 return;
2326 }
2327
2328 int alpha;
2329 SkXfermode::Mode mode;
2330 getAlphaAndMode(paint, &alpha, &mode);
2331
2332 setupDraw();
2333 setupDrawWithTexture(true);
2334 setupDrawAlpha8Color(paint->getColor(), alpha);
2335 setupDrawColorFilter();
2336 setupDrawShader();
2337 setupDrawBlending(true, mode);
2338 setupDrawProgram();
2339 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2340 setupDrawTexture(texture->id);
2341 setupDrawPureColorUniforms();
2342 setupDrawColorFilterUniforms();
2343 setupDrawShaderUniforms();
2344 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2345
2346 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2347
2348 finishDrawTexture();
2349}
2350
Romain Guyf607bdc2010-09-10 19:20:06 -07002351// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002352#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2353#define kStdUnderline_Offset (1.0f / 9.0f)
2354#define kStdUnderline_Thickness (1.0f / 18.0f)
2355
2356void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2357 float x, float y, SkPaint* paint) {
2358 // Handle underline and strike-through
2359 uint32_t flags = paint->getFlags();
2360 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002361 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002362 float underlineWidth = length;
2363 // If length is > 0.0f, we already measured the text for the text alignment
2364 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002365 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002366 }
2367
2368 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002369 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002370 case SkPaint::kCenter_Align:
2371 offsetX = underlineWidth * 0.5f;
2372 break;
2373 case SkPaint::kRight_Align:
2374 offsetX = underlineWidth;
2375 break;
2376 default:
2377 break;
2378 }
2379
2380 if (underlineWidth > 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002381 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002382 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002383
Romain Guye20ecbd2010-09-22 19:49:04 -07002384 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002385 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002386
Romain Guyf6834472011-01-23 13:32:12 -08002387 int linesCount = 0;
2388 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2389 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2390
2391 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002392 float points[pointsCount];
2393 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002394
2395 if (flags & SkPaint::kUnderlineText_Flag) {
2396 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002397 points[currentPoint++] = left;
2398 points[currentPoint++] = top;
2399 points[currentPoint++] = left + underlineWidth;
2400 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002401 }
2402
2403 if (flags & SkPaint::kStrikeThruText_Flag) {
2404 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002405 points[currentPoint++] = left;
2406 points[currentPoint++] = top;
2407 points[currentPoint++] = left + underlineWidth;
2408 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002409 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002410
Romain Guy726aeba2011-06-01 14:52:00 -07002411 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002412
Romain Guy726aeba2011-06-01 14:52:00 -07002413 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002414 }
2415 }
2416}
2417
Romain Guy026c5e162010-06-28 17:12:22 -07002418void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002419 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002420 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002421 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002422 color |= 0x00ffffff;
2423 }
2424
Romain Guy70ca14e2010-12-13 18:24:33 -08002425 setupDraw();
2426 setupDrawColor(color);
2427 setupDrawShader();
2428 setupDrawColorFilter();
2429 setupDrawBlending(mode);
2430 setupDrawProgram();
2431 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2432 setupDrawColorUniforms();
2433 setupDrawShaderUniforms(ignoreTransform);
2434 setupDrawColorFilterUniforms();
2435 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002436
Romain Guyc95c8d62010-09-17 15:31:32 -07002437 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2438}
2439
Romain Guy82ba8142010-07-09 13:25:56 -07002440void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002441 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002442 int alpha;
2443 SkXfermode::Mode mode;
2444 getAlphaAndMode(paint, &alpha, &mode);
2445
Romain Guye3c26852011-07-25 16:36:01 -07002446 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002447
Romain Guy6620c6d2010-12-06 18:07:02 -08002448 if (mSnapshot->transform->isPureTranslate()) {
2449 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2450 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2451
Romain Guye3c26852011-07-25 16:36:01 -07002452 texture->setFilter(GL_NEAREST, GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002453 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2454 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2455 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2456 } else {
Romain Guye3c26852011-07-25 16:36:01 -07002457 texture->setFilter(GL_LINEAR, GL_LINEAR, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002458 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2459 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2460 GL_TRIANGLE_STRIP, gMeshCount);
2461 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002462}
2463
Romain Guybd6b79b2010-06-26 00:13:53 -07002464void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002465 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2466 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002467 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002468}
2469
2470void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002471 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002472 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002473 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002474
Romain Guy746b7402010-10-26 16:27:31 -07002475 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002476 setupDrawWithTexture();
2477 setupDrawColor(alpha, alpha, alpha, alpha);
2478 setupDrawColorFilter();
2479 setupDrawBlending(blend, mode, swapSrcDst);
2480 setupDrawProgram();
2481 if (!dirty) {
2482 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002483 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002484 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002485 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002486 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002487 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002488 }
Romain Guy86568192010-12-14 15:55:39 -08002489 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002490 setupDrawColorFilterUniforms();
2491 setupDrawTexture(texture);
2492 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002493
Romain Guy6820ac82010-09-15 18:11:50 -07002494 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002495
2496 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002497}
2498
Romain Guya5aed0d2010-09-09 14:42:43 -07002499void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002500 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002501 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2502 if (blend) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002503 if (mode <= SkXfermode::kScreen_Mode) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002504 if (!mCaches.blend) {
2505 glEnable(GL_BLEND);
2506 }
Romain Guy82ba8142010-07-09 13:25:56 -07002507
Romain Guyf607bdc2010-09-10 19:20:06 -07002508 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2509 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07002510
Romain Guya5aed0d2010-09-09 14:42:43 -07002511 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2512 glBlendFunc(sourceMode, destMode);
2513 mCaches.lastSrcMode = sourceMode;
2514 mCaches.lastDstMode = destMode;
2515 }
2516 } else {
2517 // These blend modes are not supported by OpenGL directly and have
2518 // to be implemented using shaders. Since the shader will perform
2519 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07002520 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002521 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002522 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002523 }
2524
2525 if (mCaches.blend) {
2526 glDisable(GL_BLEND);
2527 }
2528 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07002529 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002530 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002531 glDisable(GL_BLEND);
2532 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002533 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002534}
2535
Romain Guy889f8d12010-07-29 14:37:42 -07002536bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002537 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002538 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002539 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002540 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002541 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002542 }
Romain Guy6926c722010-07-12 20:20:03 -07002543 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002544}
2545
Romain Guy026c5e162010-06-28 17:12:22 -07002546void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002547 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002548 TextureVertex::setUV(v++, u1, v1);
2549 TextureVertex::setUV(v++, u2, v1);
2550 TextureVertex::setUV(v++, u1, v2);
2551 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002552}
2553
Chet Haase5c13d892010-10-08 08:37:55 -07002554void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002555 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002556 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002557
2558 // Skia draws using the color's alpha channel if < 255
2559 // Otherwise, it uses the paint's alpha
2560 int color = paint->getColor();
2561 *alpha = (color >> 24) & 0xFF;
2562 if (*alpha == 255) {
2563 *alpha = paint->getAlpha();
2564 }
2565 } else {
2566 *mode = SkXfermode::kSrcOver_Mode;
2567 *alpha = 255;
2568 }
Romain Guy026c5e162010-06-28 17:12:22 -07002569}
2570
Romain Guya5aed0d2010-09-09 14:42:43 -07002571SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002572 SkXfermode::Mode resultMode;
2573 if (!SkXfermode::AsMode(mode, &resultMode)) {
2574 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002575 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002576 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002577}
2578
Romain Guy9d5316e2010-06-24 19:30:36 -07002579}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002580}; // namespace android