blob: 4d226461a02ad7378402ddb4b65b13344860ca18 [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() {
Chet Haase08837c22011-11-28 11:53:21 -0800204 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
205
206 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700207
208 glEnable(GL_SCISSOR_TEST);
Romain Guy746b7402010-10-26 16:27:31 -0700209 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700210
Romain Guyf607bdc2010-09-10 19:20:06 -0700211 glDisable(GL_DITHER);
212
Chet Haase08837c22011-11-28 11:53:21 -0800213 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy03750a02010-10-18 14:06:08 -0700214 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700215
Romain Guy50c0f092010-10-19 11:42:22 -0700216 mCaches.blend = true;
217 glEnable(GL_BLEND);
218 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
219 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700220}
221
Romain Guycabfcc12011-03-07 18:06:46 -0800222bool OpenGLRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800223 interrupt();
Romain Guyf90f8172011-01-25 22:53:24 -0800224 if (mDirtyClip) {
225 setScissorFromClip();
226 }
Romain Guyd643bb52011-03-01 14:55:21 -0800227
Romain Guy80911b82011-03-16 15:30:12 -0700228 Rect clip(*mSnapshot->clipRect);
229 clip.snapToPixelBoundaries();
230
Romain Guyd643bb52011-03-01 14:55:21 -0800231#if RENDER_LAYERS_AS_REGIONS
232 // Since we don't know what the functor will draw, let's dirty
233 // tne entire clip region
234 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800235 dirtyLayerUnchecked(clip, getRegion());
236 }
237#endif
238
Romain Guy08aa2cb2011-03-17 11:06:57 -0700239 DrawGlInfo info;
240 info.clipLeft = clip.left;
241 info.clipTop = clip.top;
242 info.clipRight = clip.right;
243 info.clipBottom = clip.bottom;
244 info.isLayer = hasLayer();
245 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700246
Romain Guy08aa2cb2011-03-17 11:06:57 -0700247 status_t result = (*functor)(0, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800248
249 if (result != 0) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700250 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800251 dirty.unionWith(localDirty);
252 }
253
Chet Haasedaf98e92011-01-10 14:10:36 -0800254 resume();
Romain Guycabfcc12011-03-07 18:06:46 -0800255 return result != 0;
Chet Haasedaf98e92011-01-10 14:10:36 -0800256}
257
Romain Guyf6a11b82010-06-23 17:47:49 -0700258///////////////////////////////////////////////////////////////////////////////
259// State management
260///////////////////////////////////////////////////////////////////////////////
261
Romain Guybb9524b2010-06-22 18:56:38 -0700262int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700263 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700264}
265
266int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700267 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700268}
269
270void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700271 if (mSaveCount > 1) {
272 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700273 }
Romain Guybb9524b2010-06-22 18:56:38 -0700274}
275
276void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700277 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700278
Romain Guy8fb95422010-08-17 18:38:51 -0700279 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700280 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700281 }
Romain Guybb9524b2010-06-22 18:56:38 -0700282}
283
Romain Guy8aef54f2010-09-01 15:13:49 -0700284int OpenGLRenderer::saveSnapshot(int flags) {
285 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700286 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700287}
288
289bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700290 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700291 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700292 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700293
Romain Guybd6b79b2010-06-26 00:13:53 -0700294 sp<Snapshot> current = mSnapshot;
295 sp<Snapshot> previous = mSnapshot->previous;
296
Romain Guyeb993562010-10-05 18:14:38 -0700297 if (restoreOrtho) {
298 Rect& r = previous->viewport;
299 glViewport(r.left, r.top, r.right, r.bottom);
300 mOrthoMatrix.load(current->orthoMatrix);
301 }
302
Romain Guy8b55f372010-08-18 17:10:07 -0700303 mSaveCount--;
304 mSnapshot = previous;
305
Romain Guy2542d192010-08-18 11:47:12 -0700306 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700307 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700308 }
Romain Guy2542d192010-08-18 11:47:12 -0700309
Romain Guy5ec99242010-11-03 16:19:08 -0700310 if (restoreLayer) {
311 composeLayer(current, previous);
312 }
313
Romain Guy2542d192010-08-18 11:47:12 -0700314 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700315}
316
Romain Guyf6a11b82010-06-23 17:47:49 -0700317///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700318// Layers
319///////////////////////////////////////////////////////////////////////////////
320
321int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700322 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700323 const GLuint previousFbo = mSnapshot->fbo;
324 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700325
Romain Guyaf636eb2010-12-09 17:47:21 -0800326 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700327 int alpha = 255;
328 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700329
Romain Guye45362c2010-11-03 19:58:32 -0700330 if (p) {
331 alpha = p->getAlpha();
332 if (!mCaches.extensions.hasFramebufferFetch()) {
333 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
334 if (!isMode) {
335 // Assume SRC_OVER
336 mode = SkXfermode::kSrcOver_Mode;
337 }
338 } else {
339 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700340 }
341 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700342 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700343 }
Romain Guyd55a8612010-06-28 17:42:46 -0700344
Romain Guydbc26d22010-10-11 17:58:29 -0700345 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
346 }
Romain Guyd55a8612010-06-28 17:42:46 -0700347
348 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700349}
350
351int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
352 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700353 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700354 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700355 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700356 SkPaint paint;
357 paint.setAlpha(alpha);
358 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700359 }
Romain Guyd55a8612010-06-28 17:42:46 -0700360}
Romain Guybd6b79b2010-06-26 00:13:53 -0700361
Romain Guy1c740bc2010-09-13 18:00:09 -0700362/**
363 * Layers are viewed by Skia are slightly different than layers in image editing
364 * programs (for instance.) When a layer is created, previously created layers
365 * and the frame buffer still receive every drawing command. For instance, if a
366 * layer is created and a shape intersecting the bounds of the layers and the
367 * framebuffer is draw, the shape will be drawn on both (unless the layer was
368 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
369 *
370 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
371 * texture. Unfortunately, this is inefficient as it requires every primitive to
372 * be drawn n + 1 times, where n is the number of active layers. In practice this
373 * means, for every primitive:
374 * - Switch active frame buffer
375 * - Change viewport, clip and projection matrix
376 * - Issue the drawing
377 *
378 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700379 * To avoid this, layers are implemented in a different way here, at least in the
380 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
381 * is set. When this flag is set we can redirect all drawing operations into a
382 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700383 *
384 * This implementation relies on the frame buffer being at least RGBA 8888. When
385 * a layer is created, only a texture is created, not an FBO. The content of the
386 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700387 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700388 * buffer and drawing continues as normal. This technique therefore treats the
389 * frame buffer as a scratch buffer for the layers.
390 *
391 * To compose the layers back onto the frame buffer, each layer texture
392 * (containing the original frame buffer data) is drawn as a simple quad over
393 * the frame buffer. The trick is that the quad is set as the composition
394 * destination in the blending equation, and the frame buffer becomes the source
395 * of the composition.
396 *
397 * Drawing layers with an alpha value requires an extra step before composition.
398 * An empty quad is drawn over the layer's region in the frame buffer. This quad
399 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
400 * quad is used to multiply the colors in the frame buffer. This is achieved by
401 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
402 * GL_ZERO, GL_SRC_ALPHA.
403 *
404 * Because glCopyTexImage2D() can be slow, an alternative implementation might
405 * be use to draw a single clipped layer. The implementation described above
406 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700407 *
408 * (1) The frame buffer is actually not cleared right away. To allow the GPU
409 * to potentially optimize series of calls to glCopyTexImage2D, the frame
410 * buffer is left untouched until the first drawing operation. Only when
411 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700412 */
Romain Guyd55a8612010-06-28 17:42:46 -0700413bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700414 float right, float bottom, int alpha, SkXfermode::Mode mode,
415 int flags, GLuint previousFbo) {
416 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700417 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700418
Romain Guyeb993562010-10-05 18:14:38 -0700419 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
420
Romain Guyf607bdc2010-09-10 19:20:06 -0700421 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700422 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700423 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700424 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700425
Romain Guyeb993562010-10-05 18:14:38 -0700426 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700427 if (bounds.intersect(*snapshot->clipRect)) {
428 // We cannot work with sub-pixels in this case
429 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700430
Romain Guyad37cd32011-03-15 11:12:25 -0700431 // When the layer is not an FBO, we may use glCopyTexImage so we
432 // need to make sure the layer does not extend outside the bounds
433 // of the framebuffer
434 if (!bounds.intersect(snapshot->previous->viewport)) {
435 bounds.setEmpty();
436 }
437 } else {
438 bounds.setEmpty();
439 }
Romain Guyeb993562010-10-05 18:14:38 -0700440 }
Romain Guybf434112010-09-16 14:40:17 -0700441
Romain Guy746b7402010-10-26 16:27:31 -0700442 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
443 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800444 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700445 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700446 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700447 }
448
449 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800450 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700451 return false;
452 }
Romain Guyf18fd992010-07-08 11:45:51 -0700453
Romain Guy5b3b3522010-10-27 18:57:51 -0700454 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700455 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700456 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700457 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700458 }
459
Romain Guy9ace8f52011-07-07 20:50:11 -0700460 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700461 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700462 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
463 bounds.getWidth() / float(layer->getWidth()), 0.0f);
464 layer->setColorFilter(mColorFilter);
Romain Guydda57022010-07-06 11:39:32 -0700465
Romain Guy8fb95422010-08-17 18:38:51 -0700466 // Save the layer in the snapshot
467 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700468 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700469
Romain Guyeb993562010-10-05 18:14:38 -0700470 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700471 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700472 } else {
473 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700474 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800475 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700476 if (layer->isEmpty()) {
477 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
478 bounds.left, snapshot->height - bounds.bottom,
479 layer->getWidth(), layer->getHeight(), 0);
480 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800481 } else {
482 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
483 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
484 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700485
Romain Guy54be1cd2011-06-13 19:04:27 -0700486 // Enqueue the buffer coordinates to clear the corresponding region later
487 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700488 }
Romain Guyeb993562010-10-05 18:14:38 -0700489 }
Romain Guyf86ef572010-07-01 11:05:42 -0700490
Romain Guyd55a8612010-06-28 17:42:46 -0700491 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700492}
493
Romain Guy5b3b3522010-10-27 18:57:51 -0700494bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
495 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700496 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700497
498#if RENDER_LAYERS_AS_REGIONS
499 snapshot->region = &snapshot->layer->region;
500 snapshot->flags |= Snapshot::kFlagFboTarget;
501#endif
502
503 Rect clip(bounds);
504 snapshot->transform->mapRect(clip);
505 clip.intersect(*snapshot->clipRect);
506 clip.snapToPixelBoundaries();
507 clip.intersect(snapshot->previous->viewport);
508
509 mat4 inverse;
510 inverse.loadInverse(*mSnapshot->transform);
511
512 inverse.mapRect(clip);
513 clip.snapToPixelBoundaries();
514 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700515 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700516
517 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700518 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700519 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700520 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
521 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
522 snapshot->height = bounds.getHeight();
523 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
524 snapshot->orthoMatrix.load(mOrthoMatrix);
525
526 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700527 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
528 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700529
530 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700531 if (layer->isEmpty()) {
532 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
533 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700534 }
535
536 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700537 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700538
539#if DEBUG_LAYERS_AS_REGIONS
540 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
541 if (status != GL_FRAMEBUFFER_COMPLETE) {
542 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
543
544 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700545 layer->deleteTexture();
546 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700547
548 delete layer;
549
550 return false;
551 }
552#endif
553
554 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
555 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
556 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
557 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
558 glClear(GL_COLOR_BUFFER_BIT);
559
560 dirtyClip();
561
562 // Change the ortho projection
563 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
564 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
565
566 return true;
567}
568
Romain Guy1c740bc2010-09-13 18:00:09 -0700569/**
570 * Read the documentation of createLayer() before doing anything in this method.
571 */
Romain Guy1d83e192010-08-17 11:37:00 -0700572void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
573 if (!current->layer) {
574 LOGE("Attempting to compose a layer that does not exist");
575 return;
576 }
577
Romain Guy5b3b3522010-10-27 18:57:51 -0700578 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700579
580 if (fboLayer) {
581 // Unbind current FBO and restore previous one
582 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
583 }
584
Romain Guy1d83e192010-08-17 11:37:00 -0700585 Layer* layer = current->layer;
586 const Rect& rect = layer->layer;
587
Romain Guy9ace8f52011-07-07 20:50:11 -0700588 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700589 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700590 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700591 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700592 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700593 }
594
Romain Guy03750a02010-10-18 14:06:08 -0700595 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700596
Romain Guy746b7402010-10-26 16:27:31 -0700597 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700598
Romain Guy5b3b3522010-10-27 18:57:51 -0700599 // When the layer is stored in an FBO, we can save a bit of fillrate by
600 // drawing only the dirty region
601 if (fboLayer) {
602 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700603 if (layer->getColorFilter()) {
604 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800605 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700606 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700607 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800608 resetColorFilter();
609 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700610 } else if (!rect.isEmpty()) {
611 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
612 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700613 }
Romain Guy8b55f372010-08-18 17:10:07 -0700614
Romain Guyeb993562010-10-05 18:14:38 -0700615 if (fboLayer) {
616 // Detach the texture from the FBO
617 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
618 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
619 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
620
621 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
622 mCaches.fboCache.put(current->fbo);
623 }
624
Romain Guy746b7402010-10-26 16:27:31 -0700625 dirtyClip();
626
Romain Guyeb993562010-10-05 18:14:38 -0700627 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700628 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700629 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700630 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700631 delete layer;
632 }
633}
634
Romain Guyaa6c24c2011-04-28 18:40:04 -0700635void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700636 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700637
Romain Guy302a9df2011-08-16 13:55:02 -0700638 mat4& transform = layer->getTransform();
639 if (!transform.isIdentity()) {
640 save(0);
641 mSnapshot->transform->multiply(transform);
642 }
643
Romain Guyaa6c24c2011-04-28 18:40:04 -0700644 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700645 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700646 setupDrawWithTexture();
647 } else {
648 setupDrawWithExternalTexture();
649 }
650 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700651 setupDrawColor(alpha, alpha, alpha, alpha);
652 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700653 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700654 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700655 setupDrawPureColorUniforms();
656 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700657 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
658 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700659 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700660 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700661 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700662 if (mSnapshot->transform->isPureTranslate() &&
663 layer->getWidth() == (uint32_t) rect.getWidth() &&
664 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700665 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
666 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
667
668 layer->setFilter(GL_NEAREST, GL_NEAREST);
669 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
670 } else {
671 layer->setFilter(GL_LINEAR, GL_LINEAR);
672 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
673 }
674 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700675 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
676
677 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
678
679 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700680
681 if (!transform.isIdentity()) {
682 restore();
683 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700684}
685
Romain Guy5b3b3522010-10-27 18:57:51 -0700686void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700687 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700688 const Rect& texCoords = layer->texCoords;
689 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
690 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700691
Romain Guy9ace8f52011-07-07 20:50:11 -0700692 float x = rect.left;
693 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700694 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700695 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700696 layer->getHeight() == (uint32_t) rect.getHeight();
697
698 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700699 // When we're swapping, the layer is already in screen coordinates
700 if (!swap) {
701 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
702 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
703 }
704
705 layer->setFilter(GL_NEAREST, GL_NEAREST, true);
706 } else {
707 layer->setFilter(GL_LINEAR, GL_LINEAR, true);
708 }
709
710 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
711 layer->getTexture(), layer->getAlpha() / 255.0f,
712 layer->getMode(), layer->isBlend(),
713 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
714 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700715
Romain Guyaa6c24c2011-04-28 18:40:04 -0700716 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
717 } else {
718 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
719 drawTextureLayer(layer, rect);
720 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
721 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700722}
723
724void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
725#if RENDER_LAYERS_AS_REGIONS
726 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700727 layer->setRegionAsRect();
728
Romain Guy40667672011-03-18 14:34:03 -0700729 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700730
Romain Guy5b3b3522010-10-27 18:57:51 -0700731 layer->region.clear();
732 return;
733 }
734
Romain Guy8a3957d2011-09-07 17:55:15 -0700735 // TODO: See LayerRenderer.cpp::generateMesh() for important
736 // information about this implementation
Romain Guy5b3b3522010-10-27 18:57:51 -0700737 if (!layer->region.isEmpty()) {
738 size_t count;
739 const android::Rect* rects = layer->region.getArray(&count);
740
Romain Guy9ace8f52011-07-07 20:50:11 -0700741 const float alpha = layer->getAlpha() / 255.0f;
742 const float texX = 1.0f / float(layer->getWidth());
743 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800744 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700745
746 TextureVertex* mesh = mCaches.getRegionMesh();
747 GLsizei numQuads = 0;
748
Romain Guy7230a742011-01-10 22:26:16 -0800749 setupDraw();
750 setupDrawWithTexture();
751 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800752 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700753 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800754 setupDrawProgram();
755 setupDrawDirtyRegionsDisabled();
756 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800757 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700758 setupDrawTexture(layer->getTexture());
759 if (mSnapshot->transform->isPureTranslate()) {
760 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
761 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
762
763 layer->setFilter(GL_NEAREST, GL_NEAREST);
764 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
765 } else {
766 layer->setFilter(GL_LINEAR, GL_LINEAR);
767 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
768 }
Romain Guy7230a742011-01-10 22:26:16 -0800769 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700770
771 for (size_t i = 0; i < count; i++) {
772 const android::Rect* r = &rects[i];
773
774 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800775 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700776 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800777 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700778
779 // TODO: Reject quads outside of the clip
780 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
781 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
782 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
783 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
784
785 numQuads++;
786
787 if (numQuads >= REGION_MESH_QUAD_COUNT) {
788 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
789 numQuads = 0;
790 mesh = mCaches.getRegionMesh();
791 }
792 }
793
794 if (numQuads > 0) {
795 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
796 }
797
798 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800799 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700800
801#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800802 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700803#endif
804
805 layer->region.clear();
806 }
807#else
808 composeLayerRect(layer, rect);
809#endif
810}
811
Romain Guy3a3133d2011-02-01 22:59:58 -0800812void OpenGLRenderer::drawRegionRects(const Region& region) {
813#if DEBUG_LAYERS_AS_REGIONS
814 size_t count;
815 const android::Rect* rects = region.getArray(&count);
816
817 uint32_t colors[] = {
818 0x7fff0000, 0x7f00ff00,
819 0x7f0000ff, 0x7fff00ff,
820 };
821
822 int offset = 0;
823 int32_t top = rects[0].top;
824
825 for (size_t i = 0; i < count; i++) {
826 if (top != rects[i].top) {
827 offset ^= 0x2;
828 top = rects[i].top;
829 }
830
831 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
832 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
833 SkXfermode::kSrcOver_Mode);
834 }
835#endif
836}
837
Romain Guy5b3b3522010-10-27 18:57:51 -0700838void OpenGLRenderer::dirtyLayer(const float left, const float top,
839 const float right, const float bottom, const mat4 transform) {
840#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800841 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700842 Rect bounds(left, top, right, bottom);
843 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800844 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700845 }
846#endif
847}
848
849void OpenGLRenderer::dirtyLayer(const float left, const float top,
850 const float right, const float bottom) {
851#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800852 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700853 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800854 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800855 }
856#endif
857}
858
859void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
860#if RENDER_LAYERS_AS_REGIONS
861 if (bounds.intersect(*mSnapshot->clipRect)) {
862 bounds.snapToPixelBoundaries();
863 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
864 if (!dirty.isEmpty()) {
865 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700866 }
867 }
868#endif
869}
870
Romain Guy54be1cd2011-06-13 19:04:27 -0700871void OpenGLRenderer::clearLayerRegions() {
872 const size_t count = mLayers.size();
873 if (count == 0) return;
874
875 if (!mSnapshot->isIgnored()) {
876 // Doing several glScissor/glClear here can negatively impact
877 // GPUs with a tiler architecture, instead we draw quads with
878 // the Clear blending mode
879
880 // The list contains bounds that have already been clipped
881 // against their initial clip rect, and the current clip
882 // is likely different so we need to disable clipping here
883 glDisable(GL_SCISSOR_TEST);
884
885 Vertex mesh[count * 6];
886 Vertex* vertex = mesh;
887
888 for (uint32_t i = 0; i < count; i++) {
889 Rect* bounds = mLayers.itemAt(i);
890
891 Vertex::set(vertex++, bounds->left, bounds->bottom);
892 Vertex::set(vertex++, bounds->left, bounds->top);
893 Vertex::set(vertex++, bounds->right, bounds->top);
894 Vertex::set(vertex++, bounds->left, bounds->bottom);
895 Vertex::set(vertex++, bounds->right, bounds->top);
896 Vertex::set(vertex++, bounds->right, bounds->bottom);
897
898 delete bounds;
899 }
900
901 setupDraw(false);
902 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
903 setupDrawBlending(true, SkXfermode::kClear_Mode);
904 setupDrawProgram();
905 setupDrawPureColorUniforms();
906 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
907
908 mCaches.unbindMeshBuffer();
909 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
910 gVertexStride, &mesh[0].position[0]);
911 glDrawArrays(GL_TRIANGLES, 0, count * 6);
912
913 glEnable(GL_SCISSOR_TEST);
914 } else {
915 for (uint32_t i = 0; i < count; i++) {
916 delete mLayers.itemAt(i);
917 }
918 }
919
920 mLayers.clear();
921}
922
Romain Guybd6b79b2010-06-26 00:13:53 -0700923///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700924// Transforms
925///////////////////////////////////////////////////////////////////////////////
926
927void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700928 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700929}
930
931void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700932 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700933}
934
935void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700936 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700937}
938
Romain Guy807daf72011-01-18 11:19:19 -0800939void OpenGLRenderer::skew(float sx, float sy) {
940 mSnapshot->transform->skew(sx, sy);
941}
942
Romain Guyf6a11b82010-06-23 17:47:49 -0700943void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700944 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700945}
946
947void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700948 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700949}
950
951void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700952 SkMatrix transform;
953 mSnapshot->transform->copyTo(transform);
954 transform.preConcat(*matrix);
955 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700956}
957
958///////////////////////////////////////////////////////////////////////////////
959// Clipping
960///////////////////////////////////////////////////////////////////////////////
961
Romain Guybb9524b2010-06-22 18:56:38 -0700962void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700963 Rect clip(*mSnapshot->clipRect);
964 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700965 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700966 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700967}
968
969const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700970 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700971}
972
Romain Guyc7d53492010-06-25 13:41:57 -0700973bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800974 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700975 return true;
976 }
977
Romain Guy1d83e192010-08-17 11:37:00 -0700978 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700979 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700980 r.snapToPixelBoundaries();
981
982 Rect clipRect(*mSnapshot->clipRect);
983 clipRect.snapToPixelBoundaries();
984
985 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700986}
987
Romain Guy079ba2c2010-07-16 14:12:24 -0700988bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
989 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700990 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700991 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700992 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700993 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700994}
995
Romain Guyf6a11b82010-06-23 17:47:49 -0700996///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -0800997// Drawing commands
998///////////////////////////////////////////////////////////////////////////////
999
Romain Guy54be1cd2011-06-13 19:04:27 -07001000void OpenGLRenderer::setupDraw(bool clear) {
1001 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001002 if (mDirtyClip) {
1003 setScissorFromClip();
1004 }
1005 mDescription.reset();
1006 mSetShaderColor = false;
1007 mColorSet = false;
1008 mColorA = mColorR = mColorG = mColorB = 0.0f;
1009 mTextureUnit = 0;
1010 mTrackDirtyRegions = true;
Romain Guy8d0d4782010-12-14 20:13:35 -08001011 mTexCoordsSlot = -1;
Romain Guy70ca14e2010-12-13 18:24:33 -08001012}
1013
1014void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1015 mDescription.hasTexture = true;
1016 mDescription.hasAlpha8Texture = isAlpha8;
1017}
1018
Romain Guyaa6c24c2011-04-28 18:40:04 -07001019void OpenGLRenderer::setupDrawWithExternalTexture() {
1020 mDescription.hasExternalTexture = true;
1021}
1022
Chet Haase5b0200b2011-04-13 17:58:08 -07001023void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001024 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001025}
1026
Romain Guyed6fcb02011-03-21 13:11:28 -07001027void OpenGLRenderer::setupDrawPoint(float pointSize) {
1028 mDescription.isPoint = true;
1029 mDescription.pointSize = pointSize;
1030}
1031
Romain Guy70ca14e2010-12-13 18:24:33 -08001032void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001033 setupDrawColor(color, (color >> 24) & 0xFF);
1034}
1035
1036void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1037 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001038 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1039 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001040 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001041 mColorR = a * ((color >> 16) & 0xFF);
1042 mColorG = a * ((color >> 8) & 0xFF);
1043 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001044 mColorSet = true;
1045 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1046}
1047
Romain Guy86568192010-12-14 15:55:39 -08001048void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1049 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001050 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1051 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001052 const float a = mColorA / 255.0f;
1053 mColorR = a * ((color >> 16) & 0xFF);
1054 mColorG = a * ((color >> 8) & 0xFF);
1055 mColorB = a * ((color ) & 0xFF);
1056 mColorSet = true;
1057 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1058}
1059
Romain Guy70ca14e2010-12-13 18:24:33 -08001060void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1061 mColorA = a;
1062 mColorR = r;
1063 mColorG = g;
1064 mColorB = b;
1065 mColorSet = true;
1066 mSetShaderColor = mDescription.setColor(r, g, b, a);
1067}
1068
Romain Guy86568192010-12-14 15:55:39 -08001069void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1070 mColorA = a;
1071 mColorR = r;
1072 mColorG = g;
1073 mColorB = b;
1074 mColorSet = true;
1075 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1076}
1077
Romain Guy70ca14e2010-12-13 18:24:33 -08001078void OpenGLRenderer::setupDrawShader() {
1079 if (mShader) {
1080 mShader->describe(mDescription, mCaches.extensions);
1081 }
1082}
1083
1084void OpenGLRenderer::setupDrawColorFilter() {
1085 if (mColorFilter) {
1086 mColorFilter->describe(mDescription, mCaches.extensions);
1087 }
1088}
1089
Romain Guyf09ef512011-05-27 11:43:46 -07001090void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1091 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1092 mColorA = 1.0f;
1093 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001094 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001095 }
1096}
1097
Romain Guy70ca14e2010-12-13 18:24:33 -08001098void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001099 // When the blending mode is kClear_Mode, we need to use a modulate color
1100 // argb=1,0,0,0
1101 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001102 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1103 mDescription, swapSrcDst);
1104}
1105
1106void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001107 // When the blending mode is kClear_Mode, we need to use a modulate color
1108 // argb=1,0,0,0
1109 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001110 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1111 mDescription, swapSrcDst);
1112}
1113
1114void OpenGLRenderer::setupDrawProgram() {
1115 useProgram(mCaches.programCache.get(mDescription));
1116}
1117
1118void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1119 mTrackDirtyRegions = false;
1120}
1121
1122void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1123 bool ignoreTransform) {
1124 mModelView.loadTranslate(left, top, 0.0f);
1125 if (!ignoreTransform) {
1126 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1127 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1128 } else {
1129 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1130 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1131 }
1132}
1133
Chet Haase8a5cc922011-04-26 07:28:09 -07001134void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1135 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001136}
1137
Romain Guy70ca14e2010-12-13 18:24:33 -08001138void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1139 bool ignoreTransform, bool ignoreModelView) {
1140 if (!ignoreModelView) {
1141 mModelView.loadTranslate(left, top, 0.0f);
1142 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001143 } else {
1144 mModelView.loadIdentity();
1145 }
Romain Guy86568192010-12-14 15:55:39 -08001146 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1147 if (!ignoreTransform) {
1148 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1149 if (mTrackDirtyRegions && dirty) {
1150 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1151 }
1152 } else {
1153 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1154 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1155 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001156}
1157
Romain Guyed6fcb02011-03-21 13:11:28 -07001158void OpenGLRenderer::setupDrawPointUniforms() {
1159 int slot = mCaches.currentProgram->getUniform("pointSize");
1160 glUniform1f(slot, mDescription.pointSize);
1161}
1162
Romain Guy70ca14e2010-12-13 18:24:33 -08001163void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001164 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001165 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1166 }
1167}
1168
Romain Guy86568192010-12-14 15:55:39 -08001169void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001170 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001171 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001172 }
1173}
1174
Romain Guy70ca14e2010-12-13 18:24:33 -08001175void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1176 if (mShader) {
1177 if (ignoreTransform) {
1178 mModelView.loadInverse(*mSnapshot->transform);
1179 }
1180 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1181 }
1182}
1183
Romain Guy8d0d4782010-12-14 20:13:35 -08001184void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1185 if (mShader) {
1186 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1187 }
1188}
1189
Romain Guy70ca14e2010-12-13 18:24:33 -08001190void OpenGLRenderer::setupDrawColorFilterUniforms() {
1191 if (mColorFilter) {
1192 mColorFilter->setupProgram(mCaches.currentProgram);
1193 }
1194}
1195
1196void OpenGLRenderer::setupDrawSimpleMesh() {
1197 mCaches.bindMeshBuffer();
1198 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1199 gMeshStride, 0);
1200}
1201
1202void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1203 bindTexture(texture);
1204 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1205
1206 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1207 glEnableVertexAttribArray(mTexCoordsSlot);
1208}
1209
Romain Guyaa6c24c2011-04-28 18:40:04 -07001210void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1211 bindExternalTexture(texture);
1212 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1213
1214 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1215 glEnableVertexAttribArray(mTexCoordsSlot);
1216}
1217
Romain Guy8f0095c2011-05-02 17:24:22 -07001218void OpenGLRenderer::setupDrawTextureTransform() {
1219 mDescription.hasTextureTransform = true;
1220}
1221
1222void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001223 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1224 GL_FALSE, &transform.data[0]);
1225}
1226
Romain Guy70ca14e2010-12-13 18:24:33 -08001227void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1228 if (!vertices) {
1229 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1230 } else {
1231 mCaches.unbindMeshBuffer();
1232 }
1233 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1234 gMeshStride, vertices);
David Licf289572011-02-25 12:05:44 -08001235 if (mTexCoordsSlot >= 0) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001236 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1237 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001238}
1239
Chet Haase5b0200b2011-04-13 17:58:08 -07001240void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
1241 mCaches.unbindMeshBuffer();
1242 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1243 gVertexStride, vertices);
1244}
1245
1246/**
1247 * 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 -07001248 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1249 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1250 * attributes (one per vertex) are values from zero to one that tells the fragment
1251 * shader where the fragment is in relation to the line width/length overall; these values are
1252 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1253 * region of the line.
1254 * Note that we only pass down the width values in this setup function. The length coordinates
1255 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001256 */
Chet Haase99585ad2011-05-02 15:00:16 -07001257void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Chet Haase99ecdc42011-05-06 12:06:34 -07001258 GLvoid* lengthCoords, float boundaryWidthProportion) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001259 mCaches.unbindMeshBuffer();
1260 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Chet Haase99585ad2011-05-02 15:00:16 -07001261 gAAVertexStride, vertices);
1262 int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
1263 glEnableVertexAttribArray(widthSlot);
1264 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
1265 int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
1266 glEnableVertexAttribArray(lengthSlot);
1267 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Chet Haase5b0200b2011-04-13 17:58:08 -07001268 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001269 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Chet Haase99585ad2011-05-02 15:00:16 -07001270 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001271 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001272 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
Chet Haase5b0200b2011-04-13 17:58:08 -07001273}
1274
Romain Guy70ca14e2010-12-13 18:24:33 -08001275void OpenGLRenderer::finishDrawTexture() {
1276 glDisableVertexAttribArray(mTexCoordsSlot);
1277}
1278
1279///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001280// Drawing
1281///////////////////////////////////////////////////////////////////////////////
1282
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001283bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
1284 Rect& dirty, uint32_t level) {
1285 if (quickReject(0.0f, 0.0f, width, height)) {
1286 return false;
1287 }
1288
Romain Guy0fe478e2010-11-08 12:08:41 -08001289 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1290 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001291 if (displayList && displayList->isRenderable()) {
Romain Guycabfcc12011-03-07 18:06:46 -08001292 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001293 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001294
Chet Haasedaf98e92011-01-10 14:10:36 -08001295 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001296}
1297
Chet Haaseed30fd82011-04-22 16:18:45 -07001298void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1299 if (displayList) {
1300 displayList->output(*this, level);
1301 }
1302}
1303
Romain Guya168d732011-03-18 16:50:13 -07001304void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1305 int alpha;
1306 SkXfermode::Mode mode;
1307 getAlphaAndMode(paint, &alpha, &mode);
1308
Romain Guya168d732011-03-18 16:50:13 -07001309 float x = left;
1310 float y = top;
1311
Romain Guye3c26852011-07-25 16:36:01 -07001312 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001313 bool ignoreTransform = false;
1314 if (mSnapshot->transform->isPureTranslate()) {
1315 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1316 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1317 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001318 filter = GL_NEAREST;
Romain Guya168d732011-03-18 16:50:13 -07001319 }
1320
1321 setupDraw();
1322 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001323 if (paint) {
1324 setupDrawAlpha8Color(paint->getColor(), alpha);
1325 }
Romain Guya168d732011-03-18 16:50:13 -07001326 setupDrawColorFilter();
1327 setupDrawShader();
1328 setupDrawBlending(true, mode);
1329 setupDrawProgram();
1330 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001331
Romain Guya168d732011-03-18 16:50:13 -07001332 setupDrawTexture(texture->id);
Romain Guye3c26852011-07-25 16:36:01 -07001333 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1334 texture->setFilter(filter, filter);
1335
Romain Guya168d732011-03-18 16:50:13 -07001336 setupDrawPureColorUniforms();
1337 setupDrawColorFilterUniforms();
1338 setupDrawShaderUniforms();
1339 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1340
1341 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1342
1343 finishDrawTexture();
1344}
1345
Chet Haase5c13d892010-10-08 08:37:55 -07001346void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001347 const float right = left + bitmap->width();
1348 const float bottom = top + bitmap->height();
1349
1350 if (quickReject(left, top, right, bottom)) {
1351 return;
1352 }
1353
Romain Guy86568192010-12-14 15:55:39 -08001354 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001355 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001356 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001357 const AutoTexture autoCleanup(texture);
1358
Romain Guya168d732011-03-18 16:50:13 -07001359 if (bitmap->getConfig() == SkBitmap::kA8_Config) {
1360 drawAlphaBitmap(texture, left, top, paint);
1361 } else {
1362 drawTextureRect(left, top, right, bottom, texture, paint);
1363 }
Romain Guyce0537b2010-06-29 21:05:21 -07001364}
1365
Chet Haase5c13d892010-10-08 08:37:55 -07001366void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001367 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1368 const mat4 transform(*matrix);
1369 transform.mapRect(r);
1370
Romain Guy6926c722010-07-12 20:20:03 -07001371 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1372 return;
1373 }
1374
Romain Guy86568192010-12-14 15:55:39 -08001375 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001376 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001377 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001378 const AutoTexture autoCleanup(texture);
1379
Romain Guy5b3b3522010-10-27 18:57:51 -07001380 // This could be done in a cheaper way, all we need is pass the matrix
1381 // to the vertex shader. The save/restore is a bit overkill.
1382 save(SkCanvas::kMatrix_SaveFlag);
1383 concatMatrix(matrix);
1384 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1385 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001386}
1387
Romain Guy5a7b4662011-01-20 19:09:30 -08001388void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1389 float* vertices, int* colors, SkPaint* paint) {
1390 // TODO: Do a quickReject
1391 if (!vertices || mSnapshot->isIgnored()) {
1392 return;
1393 }
1394
1395 glActiveTexture(gTextureUnits[0]);
1396 Texture* texture = mCaches.textureCache.get(bitmap);
1397 if (!texture) return;
1398 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001399
1400 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, true);
1401 texture->setFilter(GL_LINEAR, GL_LINEAR, true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001402
1403 int alpha;
1404 SkXfermode::Mode mode;
1405 getAlphaAndMode(paint, &alpha, &mode);
1406
Romain Guy5a7b4662011-01-20 19:09:30 -08001407 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001408
Romain Guyb18d2d02011-02-10 15:52:54 -08001409 float left = FLT_MAX;
1410 float top = FLT_MAX;
1411 float right = FLT_MIN;
1412 float bottom = FLT_MIN;
1413
1414#if RENDER_LAYERS_AS_REGIONS
1415 bool hasActiveLayer = hasLayer();
1416#else
1417 bool hasActiveLayer = false;
1418#endif
1419
Romain Guya566b7c2011-01-23 16:36:11 -08001420 // TODO: Support the colors array
1421 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001422 TextureVertex* vertex = mesh;
1423 for (int32_t y = 0; y < meshHeight; y++) {
1424 for (int32_t x = 0; x < meshWidth; x++) {
1425 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1426
1427 float u1 = float(x) / meshWidth;
1428 float u2 = float(x + 1) / meshWidth;
1429 float v1 = float(y) / meshHeight;
1430 float v2 = float(y + 1) / meshHeight;
1431
1432 int ax = i + (meshWidth + 1) * 2;
1433 int ay = ax + 1;
1434 int bx = i;
1435 int by = bx + 1;
1436 int cx = i + 2;
1437 int cy = cx + 1;
1438 int dx = i + (meshWidth + 1) * 2 + 2;
1439 int dy = dx + 1;
1440
1441 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1442 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1443 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1444
1445 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1446 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1447 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001448
1449#if RENDER_LAYERS_AS_REGIONS
1450 if (hasActiveLayer) {
1451 // TODO: This could be optimized to avoid unnecessary ops
1452 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1453 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1454 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1455 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1456 }
1457#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001458 }
1459 }
1460
Romain Guyb18d2d02011-02-10 15:52:54 -08001461#if RENDER_LAYERS_AS_REGIONS
1462 if (hasActiveLayer) {
1463 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1464 }
1465#endif
1466
Romain Guy5a7b4662011-01-20 19:09:30 -08001467 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1468 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001469 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001470}
1471
Romain Guy8ba548f2010-06-30 19:21:21 -07001472void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1473 float srcLeft, float srcTop, float srcRight, float srcBottom,
1474 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001475 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001476 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1477 return;
1478 }
1479
Romain Guy746b7402010-10-26 16:27:31 -07001480 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001481 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001482 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001483 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001484 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, true);
Romain Guy8ba548f2010-06-30 19:21:21 -07001485
Romain Guy8ba548f2010-06-30 19:21:21 -07001486 const float width = texture->width;
1487 const float height = texture->height;
1488
Romain Guy68169722011-08-22 17:33:33 -07001489 const float u1 = fmax(0.0f, srcLeft / width);
1490 const float v1 = fmax(0.0f, srcTop / height);
1491 const float u2 = fmin(1.0f, srcRight / width);
1492 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001493
Romain Guy03750a02010-10-18 14:06:08 -07001494 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001495 resetDrawTextureTexCoords(u1, v1, u2, v2);
1496
Romain Guy03750a02010-10-18 14:06:08 -07001497 int alpha;
1498 SkXfermode::Mode mode;
1499 getAlphaAndMode(paint, &alpha, &mode);
1500
Romain Guy6620c6d2010-12-06 18:07:02 -08001501 if (mSnapshot->transform->isPureTranslate()) {
1502 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1503 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1504
Romain Guyb5014982011-07-28 15:39:12 -07001505 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001506 // Enable linear filtering if the source rectangle is scaled
1507 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyb5014982011-07-28 15:39:12 -07001508 filter = GL_LINEAR;
1509 }
1510 texture->setFilter(filter, filter, true);
1511
Romain Guy6620c6d2010-12-06 18:07:02 -08001512 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1513 texture->id, alpha / 255.0f, mode, texture->blend,
1514 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1515 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1516 } else {
Romain Guye3c26852011-07-25 16:36:01 -07001517 texture->setFilter(GL_LINEAR, GL_LINEAR, true);
Romain Guyb5014982011-07-28 15:39:12 -07001518
Romain Guy6620c6d2010-12-06 18:07:02 -08001519 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1520 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1521 GL_TRIANGLE_STRIP, gMeshCount);
1522 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001523
1524 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1525}
1526
Romain Guy4aa90572010-09-26 18:40:37 -07001527void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001528 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001529 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001530 if (quickReject(left, top, right, bottom)) {
1531 return;
1532 }
1533
Romain Guy746b7402010-10-26 16:27:31 -07001534 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001535 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001536 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001537 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001538 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, true);
1539 texture->setFilter(GL_LINEAR, GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001540
1541 int alpha;
1542 SkXfermode::Mode mode;
1543 getAlphaAndMode(paint, &alpha, &mode);
1544
Romain Guy2728f962010-10-08 18:36:15 -07001545 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001546 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001547
Romain Guya5ef39a2010-12-03 16:48:20 -08001548 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001549 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001550#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001551 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001552 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001553 const float offsetX = left + mSnapshot->transform->getTranslateX();
1554 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001555 const size_t count = mesh->quads.size();
1556 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001557 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001558 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001559 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1560 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1561 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001562 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001563 dirtyLayer(left + bounds.left, top + bounds.top,
1564 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001565 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001566 }
1567 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001568#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001569
Romain Guy6620c6d2010-12-06 18:07:02 -08001570 if (pureTranslate) {
1571 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1572 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1573
1574 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1575 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1576 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1577 true, !mesh->hasEmptyQuads);
1578 } else {
1579 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1580 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1581 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1582 true, !mesh->hasEmptyQuads);
1583 }
Romain Guy054dc182010-10-15 17:55:25 -07001584 }
Romain Guyf7f93552010-07-08 19:17:03 -07001585}
1586
Chet Haase99ecdc42011-05-06 12:06:34 -07001587/**
Chet Haase858aa932011-05-12 09:06:00 -07001588 * This function uses a similar approach to that of AA lines in the drawLines() function.
1589 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1590 * shader to compute the translucency of the color, determined by whether a given pixel is
1591 * within that boundary region and how far into the region it is.
1592 */
1593void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001594 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001595 float inverseScaleX = 1.0f;
1596 float inverseScaleY = 1.0f;
1597 // The quad that we use needs to account for scaling.
1598 if (!mSnapshot->transform->isPureTranslate()) {
1599 Matrix4 *mat = mSnapshot->transform;
1600 float m00 = mat->data[Matrix4::kScaleX];
1601 float m01 = mat->data[Matrix4::kSkewY];
1602 float m02 = mat->data[2];
1603 float m10 = mat->data[Matrix4::kSkewX];
1604 float m11 = mat->data[Matrix4::kScaleX];
1605 float m12 = mat->data[6];
1606 float scaleX = sqrt(m00 * m00 + m01 * m01);
1607 float scaleY = sqrt(m10 * m10 + m11 * m11);
1608 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1609 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1610 }
1611
1612 setupDraw();
1613 setupDrawAALine();
1614 setupDrawColor(color);
1615 setupDrawColorFilter();
1616 setupDrawShader();
1617 setupDrawBlending(true, mode);
1618 setupDrawProgram();
1619 setupDrawModelViewIdentity(true);
1620 setupDrawColorUniforms();
1621 setupDrawColorFilterUniforms();
1622 setupDrawShaderIdentityUniforms();
1623
1624 AAVertex rects[4];
1625 AAVertex* aaVertices = &rects[0];
1626 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1627 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1628
1629 float boundarySizeX = .5 * inverseScaleX;
1630 float boundarySizeY = .5 * inverseScaleY;
1631
1632 // Adjust the rect by the AA boundary padding
1633 left -= boundarySizeX;
1634 right += boundarySizeX;
1635 top -= boundarySizeY;
1636 bottom += boundarySizeY;
1637
1638 float width = right - left;
1639 float height = bottom - top;
1640
1641 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1642 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
1643 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
1644 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1645 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1646 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
1647 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryHeightProportion));
1648
1649 if (!quickReject(left, top, right, bottom)) {
1650 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1651 AAVertex::set(aaVertices++, left, top, 1, 0);
1652 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1653 AAVertex::set(aaVertices++, right, top, 0, 0);
1654 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1655 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1656 }
1657}
1658
1659/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001660 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1661 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1662 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1663 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1664 * of the line. Hairlines are more involved because we need to account for transform scaling
1665 * to end up with a one-pixel-wide line in screen space..
1666 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1667 * in combination with values that we calculate and pass down in this method. The basic approach
1668 * is that the quad we create contains both the core line area plus a bounding area in which
1669 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1670 * proportion of the width and the length of a given segment is represented by the boundary
1671 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1672 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1673 * on the inside). This ends up giving the result we want, with pixels that are completely
1674 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1675 * how far into the boundary region they are, which is determined by shader interpolation.
1676 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001677void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1678 if (mSnapshot->isIgnored()) return;
1679
1680 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001681 // We use half the stroke width here because we're going to position the quad
1682 // corner vertices half of the width away from the line endpoints
1683 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001684 // A stroke width of 0 has a special meaning in Skia:
1685 // it draws a line 1 px wide regardless of current transform
1686 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase99ecdc42011-05-06 12:06:34 -07001687 float inverseScaleX = 1.0f;
1688 float inverseScaleY = 1.0f;
1689 bool scaled = false;
Chet Haase8a5cc922011-04-26 07:28:09 -07001690 int alpha;
1691 SkXfermode::Mode mode;
1692 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001693 int verticesCount = count;
1694 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001695 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001696 verticesCount += (count - 4);
1697 }
1698
1699 if (isHairLine || isAA) {
1700 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1701 // the line on the screen should always be one pixel wide regardless of scale. For
1702 // AA lines, we only want one pixel of translucent boundary around the quad.
1703 if (!mSnapshot->transform->isPureTranslate()) {
1704 Matrix4 *mat = mSnapshot->transform;
1705 float m00 = mat->data[Matrix4::kScaleX];
1706 float m01 = mat->data[Matrix4::kSkewY];
1707 float m02 = mat->data[2];
1708 float m10 = mat->data[Matrix4::kSkewX];
1709 float m11 = mat->data[Matrix4::kScaleX];
1710 float m12 = mat->data[6];
1711 float scaleX = sqrt(m00*m00 + m01*m01);
1712 float scaleY = sqrt(m10*m10 + m11*m11);
1713 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1714 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1715 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1716 scaled = true;
1717 }
1718 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001719 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001720
1721 getAlphaAndMode(paint, &alpha, &mode);
1722 setupDraw();
1723 if (isAA) {
1724 setupDrawAALine();
1725 }
1726 setupDrawColor(paint->getColor(), alpha);
1727 setupDrawColorFilter();
1728 setupDrawShader();
1729 if (isAA) {
1730 setupDrawBlending(true, mode);
1731 } else {
1732 setupDrawBlending(mode);
1733 }
1734 setupDrawProgram();
1735 setupDrawModelViewIdentity(true);
1736 setupDrawColorUniforms();
1737 setupDrawColorFilterUniforms();
1738 setupDrawShaderIdentityUniforms();
1739
1740 if (isHairLine) {
1741 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001742 halfStrokeWidth = isAA? 1 : .5;
1743 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001744 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001745 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001746 }
1747 Vertex lines[verticesCount];
1748 Vertex* vertices = &lines[0];
Chet Haase99585ad2011-05-02 15:00:16 -07001749 AAVertex wLines[verticesCount];
1750 AAVertex* aaVertices = &wLines[0];
Chet Haase5b0200b2011-04-13 17:58:08 -07001751 if (!isAA) {
1752 setupDrawVertices(vertices);
1753 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001754 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1755 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001756 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001757 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1758 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001759 // We will need to calculate the actual width proportion on each segment for
1760 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1761 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
1762 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
Chet Haase5b0200b2011-04-13 17:58:08 -07001763 }
1764
Chet Haase99ecdc42011-05-06 12:06:34 -07001765 AAVertex* prevAAVertex = NULL;
1766 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001767
Chet Haase99585ad2011-05-02 15:00:16 -07001768 int boundaryLengthSlot = -1;
1769 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001770 int boundaryWidthSlot = -1;
1771 int inverseBoundaryWidthSlot = -1;
Chet Haase5b0200b2011-04-13 17:58:08 -07001772 for (int i = 0; i < count; i += 4) {
1773 // a = start point, b = end point
1774 vec2 a(points[i], points[i + 1]);
1775 vec2 b(points[i + 2], points[i + 3]);
Chet Haase99585ad2011-05-02 15:00:16 -07001776 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001777 float boundaryLengthProportion = 0;
1778 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001779
Chet Haase5b0200b2011-04-13 17:58:08 -07001780 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001781 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001782 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001783 if (isAA) {
1784 float wideningFactor;
1785 if (fabs(n.x) >= fabs(n.y)) {
1786 wideningFactor = fabs(1.0f / n.x);
1787 } else {
1788 wideningFactor = fabs(1.0f / n.y);
1789 }
1790 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001791 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001792 if (scaled) {
1793 n.x *= inverseScaleX;
1794 n.y *= inverseScaleY;
1795 }
1796 } else if (scaled) {
1797 // Extend n by .5 pixel on each side, post-transform
1798 vec2 extendedN = n.copyNormalized();
1799 extendedN /= 2;
1800 extendedN.x *= inverseScaleX;
1801 extendedN.y *= inverseScaleY;
1802 float extendedNLength = extendedN.length();
1803 // We need to set this value on the shader prior to drawing
1804 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1805 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001806 }
1807 float x = n.x;
1808 n.x = -n.y;
1809 n.y = x;
1810
Chet Haase99585ad2011-05-02 15:00:16 -07001811 // aa lines expand the endpoint vertices to encompass the AA boundary
1812 if (isAA) {
1813 vec2 abVector = (b - a);
1814 length = abVector.length();
1815 abVector.normalize();
Chet Haase99ecdc42011-05-06 12:06:34 -07001816 if (scaled) {
1817 abVector.x *= inverseScaleX;
1818 abVector.y *= inverseScaleY;
1819 float abLength = abVector.length();
1820 boundaryLengthProportion = abLength / (length + abLength);
1821 } else {
1822 boundaryLengthProportion = .5 / (length + 1);
1823 }
1824 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001825 a -= abVector;
1826 b += abVector;
1827 }
1828
Chet Haase5b0200b2011-04-13 17:58:08 -07001829 // Four corners of the rectangle defining a thick line
1830 vec2 p1 = a - n;
1831 vec2 p2 = a + n;
1832 vec2 p3 = b + n;
1833 vec2 p4 = b - n;
1834
Chet Haase99585ad2011-05-02 15:00:16 -07001835
Chet Haase5b0200b2011-04-13 17:58:08 -07001836 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1837 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1838 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1839 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1840
1841 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001842 if (!isAA) {
1843 if (prevVertex != NULL) {
1844 // Issue two repeat vertices to create degenerate triangles to bridge
1845 // between the previous line and the new one. This is necessary because
1846 // we are creating a single triangle_strip which will contain
1847 // potentially discontinuous line segments.
1848 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1849 Vertex::set(vertices++, p1.x, p1.y);
1850 generatedVerticesCount += 2;
1851 }
1852 Vertex::set(vertices++, p1.x, p1.y);
1853 Vertex::set(vertices++, p2.x, p2.y);
1854 Vertex::set(vertices++, p4.x, p4.y);
1855 Vertex::set(vertices++, p3.x, p3.y);
1856 prevVertex = vertices - 1;
1857 generatedVerticesCount += 4;
1858 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001859 if (!isHairLine && scaled) {
1860 // Must set width proportions per-segment for scaled non-hairlines to use the
1861 // correct AA boundary dimensions
1862 if (boundaryWidthSlot < 0) {
1863 boundaryWidthSlot =
1864 mCaches.currentProgram->getUniform("boundaryWidth");
1865 inverseBoundaryWidthSlot =
1866 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1867 }
1868 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1869 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1870 }
Chet Haase99585ad2011-05-02 15:00:16 -07001871 if (boundaryLengthSlot < 0) {
1872 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1873 inverseBoundaryLengthSlot =
1874 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1875 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001876 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
1877 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07001878
Chet Haase5b0200b2011-04-13 17:58:08 -07001879 if (prevAAVertex != NULL) {
1880 // Issue two repeat vertices to create degenerate triangles to bridge
1881 // between the previous line and the new one. This is necessary because
1882 // we are creating a single triangle_strip which will contain
1883 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07001884 AAVertex::set(aaVertices++,prevAAVertex->position[0],
1885 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
1886 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07001887 generatedVerticesCount += 2;
1888 }
Chet Haase99585ad2011-05-02 15:00:16 -07001889 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
1890 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
1891 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
1892 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Chet Haase5b0200b2011-04-13 17:58:08 -07001893 prevAAVertex = aaVertices - 1;
1894 generatedVerticesCount += 4;
1895 }
Chet Haase99585ad2011-05-02 15:00:16 -07001896 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
1897 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
1898 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07001899 }
1900 }
1901 if (generatedVerticesCount > 0) {
1902 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
1903 }
1904}
1905
Romain Guyed6fcb02011-03-21 13:11:28 -07001906void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
1907 if (mSnapshot->isIgnored()) return;
1908
1909 // TODO: The paint's cap style defines whether the points are square or circular
1910 // TODO: Handle AA for round points
1911
Chet Haase5b0200b2011-04-13 17:58:08 -07001912 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07001913 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07001914 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07001915 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001916 if (isHairLine) {
1917 // Now that we know it's hairline, we can set the effective width, to be used later
1918 strokeWidth = 1.0f;
1919 }
1920 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07001921 int alpha;
1922 SkXfermode::Mode mode;
1923 getAlphaAndMode(paint, &alpha, &mode);
1924
1925 int verticesCount = count >> 1;
1926 int generatedVerticesCount = 0;
1927
1928 TextureVertex pointsData[verticesCount];
1929 TextureVertex* vertex = &pointsData[0];
1930
1931 setupDraw();
Chet Haase8a5cc922011-04-26 07:28:09 -07001932 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07001933 setupDrawColor(paint->getColor(), alpha);
1934 setupDrawColorFilter();
1935 setupDrawShader();
1936 setupDrawBlending(mode);
1937 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07001938 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07001939 setupDrawColorUniforms();
1940 setupDrawColorFilterUniforms();
1941 setupDrawPointUniforms();
1942 setupDrawShaderIdentityUniforms();
1943 setupDrawMesh(vertex);
1944
1945 for (int i = 0; i < count; i += 2) {
1946 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1947 generatedVerticesCount++;
Chet Haase8a5cc922011-04-26 07:28:09 -07001948 float left = points[i] - halfWidth;
1949 float right = points[i] + halfWidth;
1950 float top = points[i + 1] - halfWidth;
1951 float bottom = points [i + 1] + halfWidth;
1952 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07001953 }
1954
1955 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
1956}
1957
Romain Guy85bf02f2010-06-22 13:11:24 -07001958void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001959 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001960 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001961
Romain Guyae88e5e2010-10-22 17:49:18 -07001962 Rect& clip(*mSnapshot->clipRect);
1963 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001964
Romain Guy3d58c032010-07-14 16:34:53 -07001965 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001966}
Romain Guy9d5316e2010-06-24 19:30:36 -07001967
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001968void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001969 if (!texture) return;
1970 const AutoTexture autoCleanup(texture);
1971
1972 const float x = left + texture->left - texture->offset;
1973 const float y = top + texture->top - texture->offset;
1974
1975 drawPathTexture(texture, x, y, paint);
1976}
1977
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001978void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1979 float rx, float ry, SkPaint* paint) {
1980 if (mSnapshot->isIgnored()) return;
1981
1982 glActiveTexture(gTextureUnits[0]);
1983 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1984 right - left, bottom - top, rx, ry, paint);
1985 drawShape(left, top, texture, paint);
1986}
1987
Romain Guy01d58e42011-01-19 21:54:02 -08001988void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1989 if (mSnapshot->isIgnored()) return;
1990
1991 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08001992 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001993 drawShape(x - radius, y - radius, texture, paint);
1994}
Romain Guy01d58e42011-01-19 21:54:02 -08001995
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001996void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
1997 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001998
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001999 glActiveTexture(gTextureUnits[0]);
2000 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2001 drawShape(left, top, texture, paint);
2002}
2003
Romain Guy8b2f5262011-01-23 16:15:02 -08002004void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2005 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2006 if (mSnapshot->isIgnored()) return;
2007
2008 if (fabs(sweepAngle) >= 360.0f) {
2009 drawOval(left, top, right, bottom, paint);
2010 return;
2011 }
2012
2013 glActiveTexture(gTextureUnits[0]);
2014 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2015 startAngle, sweepAngle, useCenter, paint);
2016 drawShape(left, top, texture, paint);
2017}
2018
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002019void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2020 SkPaint* paint) {
2021 if (mSnapshot->isIgnored()) return;
2022
2023 glActiveTexture(gTextureUnits[0]);
2024 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2025 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002026}
2027
Chet Haase5c13d892010-10-08 08:37:55 -07002028void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002029 if (p->getStyle() != SkPaint::kFill_Style) {
2030 drawRectAsShape(left, top, right, bottom, p);
2031 return;
2032 }
2033
Romain Guy6926c722010-07-12 20:20:03 -07002034 if (quickReject(left, top, right, bottom)) {
2035 return;
2036 }
2037
Romain Guy026c5e162010-06-28 17:12:22 -07002038 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002039 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002040 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2041 if (!isMode) {
2042 // Assume SRC_OVER
2043 mode = SkXfermode::kSrcOver_Mode;
2044 }
2045 } else {
2046 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002047 }
2048
Romain Guy026c5e162010-06-28 17:12:22 -07002049 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002050 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002051 drawAARect(left, top, right, bottom, color, mode);
2052 } else {
2053 drawColorRect(left, top, right, bottom, color, mode);
2054 }
Romain Guyc7d53492010-06-25 13:41:57 -07002055}
Romain Guy9d5316e2010-06-24 19:30:36 -07002056
Romain Guye8e62a42010-07-23 18:55:21 -07002057void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
2058 float x, float y, SkPaint* paint) {
Romain Guyb146b122010-12-15 17:06:45 -08002059 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07002060 return;
2061 }
Romain Guyaf636eb2010-12-09 17:47:21 -08002062 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07002063
Romain Guy67ffc362011-06-03 18:50:11 -07002064 // TODO: We should probably make a copy of the paint instead of modifying
2065 // it; modifying the paint will change its generationID the first
2066 // time, which might impact caches. More investigation needed to
2067 // see if it matters.
2068 // If we make a copy, then drawTextDecorations() should *not* make
2069 // its own copy as it does right now.
Romain Guyf607bdc2010-09-10 19:20:06 -07002070 paint->setAntiAlias(true);
Romain Guy67ffc362011-06-03 18:50:11 -07002071#if RENDER_TEXT_AS_GLYPHS
2072 paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding);
2073#endif
Romain Guye8e62a42010-07-23 18:55:21 -07002074
Romain Guya674ab72010-08-10 17:26:42 -07002075 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07002076 switch (paint->getTextAlign()) {
2077 case SkPaint::kCenter_Align:
2078 length = paint->measureText(text, bytesCount);
2079 x -= length / 2.0f;
2080 break;
2081 case SkPaint::kRight_Align:
2082 length = paint->measureText(text, bytesCount);
2083 x -= length;
2084 break;
2085 default:
2086 break;
2087 }
2088
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002089 const float oldX = x;
2090 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002091 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2092 if (pureTranslate) {
2093 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2094 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2095 }
2096
Romain Guyb45c0c92010-08-26 20:35:23 -07002097 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2098 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002099 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002100
Romain Guy86568192010-12-14 15:55:39 -08002101 int alpha;
2102 SkXfermode::Mode mode;
2103 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002104
Romain Guy1e45aae2010-08-13 19:39:53 -07002105 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07002106 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002107 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2108 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002109 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002110
Romain Guy740bf2b2011-04-26 15:33:10 -07002111 const float sx = oldX - shadow->left + mShadowDx;
2112 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002113
Romain Guy86568192010-12-14 15:55:39 -08002114 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002115 int shadowColor = mShadowColor;
2116 if (mShader) {
2117 shadowColor = 0xffffffff;
2118 }
Romain Guy86568192010-12-14 15:55:39 -08002119
2120 glActiveTexture(gTextureUnits[0]);
2121 setupDraw();
2122 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002123 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2124 setupDrawColorFilter();
2125 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002126 setupDrawBlending(true, mode);
2127 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002128 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002129 setupDrawTexture(shadow->id);
2130 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002131 setupDrawColorFilterUniforms();
2132 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002133 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2134
Romain Guy0a417492010-08-16 20:26:20 -07002135 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy740bf2b2011-04-26 15:33:10 -07002136
Romain Guy86568192010-12-14 15:55:39 -08002137 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07002138 }
2139
Romain Guyb146b122010-12-15 17:06:45 -08002140 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
2141 return;
2142 }
2143
Romain Guy6620c6d2010-12-06 18:07:02 -08002144 // Pick the appropriate texture filtering
2145 bool linearFilter = mSnapshot->transform->changesBounds();
2146 if (pureTranslate && !linearFilter) {
2147 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2148 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002149
Romain Guy86568192010-12-14 15:55:39 -08002150 glActiveTexture(gTextureUnits[0]);
2151 setupDraw();
2152 setupDrawDirtyRegionsDisabled();
2153 setupDrawWithTexture(true);
2154 setupDrawAlpha8Color(paint->getColor(), alpha);
2155 setupDrawColorFilter();
2156 setupDrawShader();
2157 setupDrawBlending(true, mode);
2158 setupDrawProgram();
2159 setupDrawModelView(x, y, x, y, pureTranslate, true);
2160 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2161 setupDrawPureColorUniforms();
2162 setupDrawColorFilterUniforms();
2163 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002164
Romain Guy6620c6d2010-12-06 18:07:02 -08002165 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002166 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2167
2168#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002169 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002170#else
Romain Guyf219da52011-01-16 12:54:25 -08002171 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002172#endif
Romain Guy03750a02010-10-18 14:06:08 -07002173 mCaches.unbindMeshBuffer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002174
2175 // Tell font renderer the locations of position and texture coord
2176 // attributes so it can bind its data properly
2177 int positionSlot = mCaches.currentProgram->position;
2178 fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot);
Romain Guy6620c6d2010-12-06 18:07:02 -08002179 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002180 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002181#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002182 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002183 if (!pureTranslate) {
2184 mSnapshot->transform->mapRect(bounds);
2185 }
Romain Guyf219da52011-01-16 12:54:25 -08002186 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002187 }
2188#endif
2189 }
Romain Guy694b5192010-07-21 21:33:20 -07002190
2191 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07002192 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07002193
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002194 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002195}
2196
Romain Guy7fbcc042010-08-04 15:40:07 -07002197void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002198 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002199
Romain Guy01d58e42011-01-19 21:54:02 -08002200 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07002201
Romain Guyfb8b7632010-08-23 21:05:08 -07002202 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -07002203 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002204 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002205
Romain Guy8b55f372010-08-18 17:10:07 -07002206 const float x = texture->left - texture->offset;
2207 const float y = texture->top - texture->offset;
2208
Romain Guy01d58e42011-01-19 21:54:02 -08002209 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002210}
2211
Romain Guyada830f2011-01-13 12:13:20 -08002212void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2213 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002214 return;
2215 }
2216
2217 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08002218
2219 int alpha;
2220 SkXfermode::Mode mode;
2221 getAlphaAndMode(paint, &alpha, &mode);
2222
Romain Guy9ace8f52011-07-07 20:50:11 -07002223 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002224
Romain Guyf219da52011-01-16 12:54:25 -08002225#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08002226 if (!layer->region.isEmpty()) {
2227 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002228 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002229 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002230 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002231 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002232
Romain Guyc88e3572011-01-22 00:32:12 -08002233 setupDraw();
2234 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002235 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002236 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002237 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002238 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002239 setupDrawPureColorUniforms();
2240 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002241 setupDrawTexture(layer->getTexture());
2242 if (mSnapshot->transform->isPureTranslate()) {
2243 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2244 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2245
2246 layer->setFilter(GL_NEAREST, GL_NEAREST);
2247 setupDrawModelViewTranslate(x, y,
2248 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2249 } else {
2250 layer->setFilter(GL_LINEAR, GL_LINEAR);
2251 setupDrawModelViewTranslate(x, y,
2252 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2253 }
Romain Guyc88e3572011-01-22 00:32:12 -08002254 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002255
Romain Guyc88e3572011-01-22 00:32:12 -08002256 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2257 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002258
Romain Guyc88e3572011-01-22 00:32:12 -08002259 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002260
2261#if DEBUG_LAYERS_AS_REGIONS
2262 drawRegionRects(layer->region);
2263#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002264 }
Romain Guyf219da52011-01-16 12:54:25 -08002265 }
2266#else
Romain Guyada830f2011-01-13 12:13:20 -08002267 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2268 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002269#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002270}
2271
Romain Guy6926c722010-07-12 20:20:03 -07002272///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002273// Shaders
2274///////////////////////////////////////////////////////////////////////////////
2275
2276void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002277 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002278}
2279
Romain Guy06f96e22010-07-30 19:18:16 -07002280void OpenGLRenderer::setupShader(SkiaShader* shader) {
2281 mShader = shader;
2282 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002283 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002284 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002285}
2286
Romain Guyd27977d2010-07-14 19:18:51 -07002287///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002288// Color filters
2289///////////////////////////////////////////////////////////////////////////////
2290
2291void OpenGLRenderer::resetColorFilter() {
2292 mColorFilter = NULL;
2293}
2294
2295void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2296 mColorFilter = filter;
2297}
2298
2299///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002300// Drop shadow
2301///////////////////////////////////////////////////////////////////////////////
2302
2303void OpenGLRenderer::resetShadow() {
2304 mHasShadow = false;
2305}
2306
2307void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2308 mHasShadow = true;
2309 mShadowRadius = radius;
2310 mShadowDx = dx;
2311 mShadowDy = dy;
2312 mShadowColor = color;
2313}
2314
2315///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002316// Drawing implementation
2317///////////////////////////////////////////////////////////////////////////////
2318
Romain Guy01d58e42011-01-19 21:54:02 -08002319void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2320 float x, float y, SkPaint* paint) {
2321 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2322 return;
2323 }
2324
2325 int alpha;
2326 SkXfermode::Mode mode;
2327 getAlphaAndMode(paint, &alpha, &mode);
2328
2329 setupDraw();
2330 setupDrawWithTexture(true);
2331 setupDrawAlpha8Color(paint->getColor(), alpha);
2332 setupDrawColorFilter();
2333 setupDrawShader();
2334 setupDrawBlending(true, mode);
2335 setupDrawProgram();
2336 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2337 setupDrawTexture(texture->id);
2338 setupDrawPureColorUniforms();
2339 setupDrawColorFilterUniforms();
2340 setupDrawShaderUniforms();
2341 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2342
2343 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2344
2345 finishDrawTexture();
2346}
2347
Romain Guyf607bdc2010-09-10 19:20:06 -07002348// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002349#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2350#define kStdUnderline_Offset (1.0f / 9.0f)
2351#define kStdUnderline_Thickness (1.0f / 18.0f)
2352
2353void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2354 float x, float y, SkPaint* paint) {
2355 // Handle underline and strike-through
2356 uint32_t flags = paint->getFlags();
2357 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002358 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002359 float underlineWidth = length;
2360 // If length is > 0.0f, we already measured the text for the text alignment
2361 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002362 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002363 }
2364
2365 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002366 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002367 case SkPaint::kCenter_Align:
2368 offsetX = underlineWidth * 0.5f;
2369 break;
2370 case SkPaint::kRight_Align:
2371 offsetX = underlineWidth;
2372 break;
2373 default:
2374 break;
2375 }
2376
2377 if (underlineWidth > 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002378 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002379 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002380
Romain Guye20ecbd2010-09-22 19:49:04 -07002381 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002382 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002383
Romain Guyf6834472011-01-23 13:32:12 -08002384 int linesCount = 0;
2385 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2386 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2387
2388 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002389 float points[pointsCount];
2390 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002391
2392 if (flags & SkPaint::kUnderlineText_Flag) {
2393 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002394 points[currentPoint++] = left;
2395 points[currentPoint++] = top;
2396 points[currentPoint++] = left + underlineWidth;
2397 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002398 }
2399
2400 if (flags & SkPaint::kStrikeThruText_Flag) {
2401 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002402 points[currentPoint++] = left;
2403 points[currentPoint++] = top;
2404 points[currentPoint++] = left + underlineWidth;
2405 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002406 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002407
Romain Guy726aeba2011-06-01 14:52:00 -07002408 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002409
Romain Guy726aeba2011-06-01 14:52:00 -07002410 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002411 }
2412 }
2413}
2414
Romain Guy026c5e162010-06-28 17:12:22 -07002415void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002416 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002417 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002418 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002419 color |= 0x00ffffff;
2420 }
2421
Romain Guy70ca14e2010-12-13 18:24:33 -08002422 setupDraw();
2423 setupDrawColor(color);
2424 setupDrawShader();
2425 setupDrawColorFilter();
2426 setupDrawBlending(mode);
2427 setupDrawProgram();
2428 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2429 setupDrawColorUniforms();
2430 setupDrawShaderUniforms(ignoreTransform);
2431 setupDrawColorFilterUniforms();
2432 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002433
Romain Guyc95c8d62010-09-17 15:31:32 -07002434 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2435}
2436
Romain Guy82ba8142010-07-09 13:25:56 -07002437void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002438 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002439 int alpha;
2440 SkXfermode::Mode mode;
2441 getAlphaAndMode(paint, &alpha, &mode);
2442
Romain Guye3c26852011-07-25 16:36:01 -07002443 texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002444
Romain Guy6620c6d2010-12-06 18:07:02 -08002445 if (mSnapshot->transform->isPureTranslate()) {
2446 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2447 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2448
Romain Guye3c26852011-07-25 16:36:01 -07002449 texture->setFilter(GL_NEAREST, GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002450 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2451 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2452 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2453 } else {
Romain Guye3c26852011-07-25 16:36:01 -07002454 texture->setFilter(GL_LINEAR, GL_LINEAR, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002455 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2456 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2457 GL_TRIANGLE_STRIP, gMeshCount);
2458 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002459}
2460
Romain Guybd6b79b2010-06-26 00:13:53 -07002461void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002462 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2463 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002464 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002465}
2466
2467void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002468 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002469 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002470 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002471
Romain Guy746b7402010-10-26 16:27:31 -07002472 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002473 setupDrawWithTexture();
2474 setupDrawColor(alpha, alpha, alpha, alpha);
2475 setupDrawColorFilter();
2476 setupDrawBlending(blend, mode, swapSrcDst);
2477 setupDrawProgram();
2478 if (!dirty) {
2479 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002480 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002481 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002482 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002483 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002484 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002485 }
Romain Guy86568192010-12-14 15:55:39 -08002486 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002487 setupDrawColorFilterUniforms();
2488 setupDrawTexture(texture);
2489 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002490
Romain Guy6820ac82010-09-15 18:11:50 -07002491 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002492
2493 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002494}
2495
Romain Guya5aed0d2010-09-09 14:42:43 -07002496void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002497 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002498 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2499 if (blend) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002500 if (mode <= SkXfermode::kScreen_Mode) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002501 if (!mCaches.blend) {
2502 glEnable(GL_BLEND);
2503 }
Romain Guy82ba8142010-07-09 13:25:56 -07002504
Romain Guyf607bdc2010-09-10 19:20:06 -07002505 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2506 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07002507
Romain Guya5aed0d2010-09-09 14:42:43 -07002508 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2509 glBlendFunc(sourceMode, destMode);
2510 mCaches.lastSrcMode = sourceMode;
2511 mCaches.lastDstMode = destMode;
2512 }
2513 } else {
2514 // These blend modes are not supported by OpenGL directly and have
2515 // to be implemented using shaders. Since the shader will perform
2516 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07002517 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002518 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002519 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002520 }
2521
2522 if (mCaches.blend) {
2523 glDisable(GL_BLEND);
2524 }
2525 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07002526 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002527 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002528 glDisable(GL_BLEND);
2529 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002530 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002531}
2532
Romain Guy889f8d12010-07-29 14:37:42 -07002533bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002534 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002535 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002536 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002537 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002538 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002539 }
Romain Guy6926c722010-07-12 20:20:03 -07002540 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002541}
2542
Romain Guy026c5e162010-06-28 17:12:22 -07002543void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002544 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002545 TextureVertex::setUV(v++, u1, v1);
2546 TextureVertex::setUV(v++, u2, v1);
2547 TextureVertex::setUV(v++, u1, v2);
2548 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002549}
2550
Chet Haase5c13d892010-10-08 08:37:55 -07002551void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002552 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002553 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002554
2555 // Skia draws using the color's alpha channel if < 255
2556 // Otherwise, it uses the paint's alpha
2557 int color = paint->getColor();
2558 *alpha = (color >> 24) & 0xFF;
2559 if (*alpha == 255) {
2560 *alpha = paint->getAlpha();
2561 }
2562 } else {
2563 *mode = SkXfermode::kSrcOver_Mode;
2564 *alpha = 255;
2565 }
Romain Guy026c5e162010-06-28 17:12:22 -07002566}
2567
Romain Guya5aed0d2010-09-09 14:42:43 -07002568SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002569 SkXfermode::Mode resultMode;
2570 if (!SkXfermode::AsMode(mode, &resultMode)) {
2571 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002572 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002573 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002574}
2575
Romain Guy9d5316e2010-06-24 19:30:36 -07002576}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002577}; // namespace android