blob: 59d3fcc2b0f6bf647fe7219a0cf54e07ecaed593 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy694b5192010-07-21 21:33:20 -070024#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070025
Romain Guye4d01122010-06-16 18:44:05 -070026#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070027#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070028
Romain Guy08aa2cb2011-03-17 11:06:57 -070029#include <private/hwui/DrawGlInfo.h>
30
Romain Guy5b3b3522010-10-27 18:57:51 -070031#include <ui/Rect.h>
32
Romain Guy85bf02f2010-06-22 13:11:24 -070033#include "OpenGLRenderer.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080034#include "DisplayListRenderer.h"
Romain Guya957eea2010-12-08 18:34:42 -080035#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070036
37namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070038namespace uirenderer {
39
40///////////////////////////////////////////////////////////////////////////////
41// Defines
42///////////////////////////////////////////////////////////////////////////////
43
Romain Guy759ea802010-09-16 20:49:46 -070044#define RAD_TO_DEG (180.0f / 3.14159265f)
45#define MIN_ANGLE 0.001f
46
Romain Guydbc26d22010-10-11 17:58:29 -070047// TODO: This should be set in properties
48#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH)
49
Romain Guyd21b6e12011-11-30 20:21:23 -080050#define FILTER(paint) (paint && paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
51
Romain Guy9d5316e2010-06-24 19:30:36 -070052///////////////////////////////////////////////////////////////////////////////
53// Globals
54///////////////////////////////////////////////////////////////////////////////
55
Romain Guy889f8d12010-07-29 14:37:42 -070056/**
57 * Structure mapping Skia xfermodes to OpenGL blending factors.
58 */
59struct Blender {
60 SkXfermode::Mode mode;
61 GLenum src;
62 GLenum dst;
63}; // struct Blender
64
Romain Guy026c5e162010-06-28 17:12:22 -070065// In this array, the index of each Blender equals the value of the first
66// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
67static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070068 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
69 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
70 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
71 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
72 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
73 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
74 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
75 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
76 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
77 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
78 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
79 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
81 { SkXfermode::kMultiply_Mode, GL_ZERO, GL_SRC_COLOR },
82 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070083};
Romain Guye4d01122010-06-16 18:44:05 -070084
Romain Guy87a76572010-09-13 18:11:21 -070085// This array contains the swapped version of each SkXfermode. For instance
86// this array's SrcOver blending mode is actually DstOver. You can refer to
87// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070088static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070089 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
90 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
91 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
92 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
93 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
94 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
95 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
96 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
97 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
98 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
99 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
100 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
101 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
102 { SkXfermode::kMultiply_Mode, GL_DST_COLOR, GL_ZERO },
103 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700104};
105
Romain Guy889f8d12010-07-29 14:37:42 -0700106static const GLenum gTextureUnits[] = {
Romain Guyb146b122010-12-15 17:06:45 -0800107 GL_TEXTURE0,
108 GL_TEXTURE1,
109 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -0700110};
111
Romain Guyf6a11b82010-06-23 17:47:49 -0700112///////////////////////////////////////////////////////////////////////////////
113// Constructors/destructor
114///////////////////////////////////////////////////////////////////////////////
115
Romain Guyfb8b7632010-08-23 21:05:08 -0700116OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700117 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700118 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700119 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700120
Romain Guyac670c02010-07-27 17:39:27 -0700121 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
122
Romain Guyae5575b2010-07-29 18:48:04 -0700123 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700124}
125
Romain Guy85bf02f2010-06-22 13:11:24 -0700126OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700127 // The context has already been destroyed at this point, do not call
128 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700129}
130
Romain Guyf6a11b82010-06-23 17:47:49 -0700131///////////////////////////////////////////////////////////////////////////////
132// Setup
133///////////////////////////////////////////////////////////////////////////////
134
Romain Guy85bf02f2010-06-22 13:11:24 -0700135void OpenGLRenderer::setViewport(int width, int height) {
Romain 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
Romain Guy3e263fa2011-12-12 16:47:48 -0800144 glDisable(GL_DITHER);
Romain Guy39d252a2011-12-12 18:14:06 -0800145 glEnable(GL_SCISSOR_TEST);
Romain Guy3e263fa2011-12-12 16:47:48 -0800146 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guy15bc6432011-12-13 13:11:32 -0800147
Romain Guy3e263fa2011-12-12 16:47:48 -0800148 glEnableVertexAttribArray(Program::kBindingPosition);
Romain Guye4d01122010-06-16 18:44:05 -0700149}
150
Romain Guy6b7bd242010-10-06 19:49:23 -0700151void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800152 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
153}
154
155void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800156 mCaches.clearGarbage();
157
Romain Guy8aef54f2010-09-01 15:13:49 -0700158 mSnapshot = new Snapshot(mFirstSnapshot,
159 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800160 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700161 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700162
Romain Guy39d252a2011-12-12 18:14:06 -0800163 glViewport(0, 0, mWidth, mHeight);
Romain Guy7d7b5492011-01-24 16:33:45 -0800164 glScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy39d252a2011-12-12 18:14:06 -0800165
Romain Guy7d7b5492011-01-24 16:33:45 -0800166 mSnapshot->setClip(left, top, right, bottom);
Romain Guy39d252a2011-12-12 18:14:06 -0800167 mDirtyClip = false;
Romain Guy7d7b5492011-01-24 16:33:45 -0800168
Romain Guy6b7bd242010-10-06 19:49:23 -0700169 if (!opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700170 glClear(GL_COLOR_BUFFER_BIT);
171 }
Romain Guybb9524b2010-06-22 18:56:38 -0700172}
173
Romain Guyb025b9c2010-09-16 14:16:48 -0700174void OpenGLRenderer::finish() {
175#if DEBUG_OPENGL
176 GLenum status = GL_NO_ERROR;
177 while ((status = glGetError()) != GL_NO_ERROR) {
178 LOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800179 switch (status) {
180 case GL_OUT_OF_MEMORY:
181 LOGE(" OpenGLRenderer is out of memory!");
182 break;
183 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700184 }
185#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800186#if DEBUG_MEMORY_USAGE
187 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800188#else
189 if (mCaches.getDebugLevel() & kDebugMemory) {
190 mCaches.dumpMemoryUsage();
191 }
Romain Guyc15008e2010-11-10 11:59:15 -0800192#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700193}
194
Romain Guy6c319ca2011-01-11 14:29:25 -0800195void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700196 if (mCaches.currentProgram) {
197 if (mCaches.currentProgram->isInUse()) {
198 mCaches.currentProgram->remove();
199 mCaches.currentProgram = NULL;
200 }
201 }
Romain Guy50c0f092010-10-19 11:42:22 -0700202 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800203 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800204 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800205 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700206}
207
Romain Guy6c319ca2011-01-11 14:29:25 -0800208void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800209 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
210
211 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy3e263fa2011-12-12 16:47:48 -0800212 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
213
Romain Guyda8532c2010-08-31 11:50:35 -0700214 glEnable(GL_SCISSOR_TEST);
Romain Guy746b7402010-10-26 16:27:31 -0700215 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700216
Chet Haase08837c22011-11-28 11:53:21 -0800217 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guyf607bdc2010-09-10 19:20:06 -0700218
Romain Guy50c0f092010-10-19 11:42:22 -0700219 mCaches.blend = true;
220 glEnable(GL_BLEND);
221 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
222 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700223}
224
Romain Guycabfcc12011-03-07 18:06:46 -0800225bool OpenGLRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800226 interrupt();
Romain Guyf90f8172011-01-25 22:53:24 -0800227 if (mDirtyClip) {
228 setScissorFromClip();
229 }
Romain Guyd643bb52011-03-01 14:55:21 -0800230
Romain Guy80911b82011-03-16 15:30:12 -0700231 Rect clip(*mSnapshot->clipRect);
232 clip.snapToPixelBoundaries();
233
Romain Guyd643bb52011-03-01 14:55:21 -0800234#if RENDER_LAYERS_AS_REGIONS
235 // Since we don't know what the functor will draw, let's dirty
236 // tne entire clip region
237 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800238 dirtyLayerUnchecked(clip, getRegion());
239 }
240#endif
241
Romain Guy08aa2cb2011-03-17 11:06:57 -0700242 DrawGlInfo info;
243 info.clipLeft = clip.left;
244 info.clipTop = clip.top;
245 info.clipRight = clip.right;
246 info.clipBottom = clip.bottom;
247 info.isLayer = hasLayer();
248 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700249
Romain Guy08aa2cb2011-03-17 11:06:57 -0700250 status_t result = (*functor)(0, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800251
252 if (result != 0) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700253 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800254 dirty.unionWith(localDirty);
255 }
256
Chet Haasedaf98e92011-01-10 14:10:36 -0800257 resume();
Romain Guycabfcc12011-03-07 18:06:46 -0800258 return result != 0;
Chet Haasedaf98e92011-01-10 14:10:36 -0800259}
260
Romain Guyf6a11b82010-06-23 17:47:49 -0700261///////////////////////////////////////////////////////////////////////////////
262// State management
263///////////////////////////////////////////////////////////////////////////////
264
Romain Guybb9524b2010-06-22 18:56:38 -0700265int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700266 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700267}
268
269int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700270 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700271}
272
273void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700274 if (mSaveCount > 1) {
275 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700276 }
Romain Guybb9524b2010-06-22 18:56:38 -0700277}
278
279void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700280 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700281
Romain Guy8fb95422010-08-17 18:38:51 -0700282 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700283 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700284 }
Romain Guybb9524b2010-06-22 18:56:38 -0700285}
286
Romain Guy8aef54f2010-09-01 15:13:49 -0700287int OpenGLRenderer::saveSnapshot(int flags) {
288 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700289 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700290}
291
292bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700293 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700294 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700295 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700296
Romain Guybd6b79b2010-06-26 00:13:53 -0700297 sp<Snapshot> current = mSnapshot;
298 sp<Snapshot> previous = mSnapshot->previous;
299
Romain Guyeb993562010-10-05 18:14:38 -0700300 if (restoreOrtho) {
301 Rect& r = previous->viewport;
302 glViewport(r.left, r.top, r.right, r.bottom);
303 mOrthoMatrix.load(current->orthoMatrix);
304 }
305
Romain Guy8b55f372010-08-18 17:10:07 -0700306 mSaveCount--;
307 mSnapshot = previous;
308
Romain Guy2542d192010-08-18 11:47:12 -0700309 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700310 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700311 }
Romain Guy2542d192010-08-18 11:47:12 -0700312
Romain Guy5ec99242010-11-03 16:19:08 -0700313 if (restoreLayer) {
314 composeLayer(current, previous);
315 }
316
Romain Guy2542d192010-08-18 11:47:12 -0700317 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700318}
319
Romain Guyf6a11b82010-06-23 17:47:49 -0700320///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700321// Layers
322///////////////////////////////////////////////////////////////////////////////
323
324int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700325 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700326 const GLuint previousFbo = mSnapshot->fbo;
327 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700328
Romain Guyaf636eb2010-12-09 17:47:21 -0800329 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700330 int alpha = 255;
331 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700332
Romain Guye45362c2010-11-03 19:58:32 -0700333 if (p) {
334 alpha = p->getAlpha();
335 if (!mCaches.extensions.hasFramebufferFetch()) {
336 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
337 if (!isMode) {
338 // Assume SRC_OVER
339 mode = SkXfermode::kSrcOver_Mode;
340 }
341 } else {
342 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700343 }
344 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700345 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700346 }
Romain Guyd55a8612010-06-28 17:42:46 -0700347
Romain Guydbc26d22010-10-11 17:58:29 -0700348 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
349 }
Romain Guyd55a8612010-06-28 17:42:46 -0700350
351 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700352}
353
354int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
355 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700356 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700357 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700358 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700359 SkPaint paint;
360 paint.setAlpha(alpha);
361 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700362 }
Romain Guyd55a8612010-06-28 17:42:46 -0700363}
Romain Guybd6b79b2010-06-26 00:13:53 -0700364
Romain Guy1c740bc2010-09-13 18:00:09 -0700365/**
366 * Layers are viewed by Skia are slightly different than layers in image editing
367 * programs (for instance.) When a layer is created, previously created layers
368 * and the frame buffer still receive every drawing command. For instance, if a
369 * layer is created and a shape intersecting the bounds of the layers and the
370 * framebuffer is draw, the shape will be drawn on both (unless the layer was
371 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
372 *
373 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
374 * texture. Unfortunately, this is inefficient as it requires every primitive to
375 * be drawn n + 1 times, where n is the number of active layers. In practice this
376 * means, for every primitive:
377 * - Switch active frame buffer
378 * - Change viewport, clip and projection matrix
379 * - Issue the drawing
380 *
381 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700382 * To avoid this, layers are implemented in a different way here, at least in the
383 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
384 * is set. When this flag is set we can redirect all drawing operations into a
385 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700386 *
387 * This implementation relies on the frame buffer being at least RGBA 8888. When
388 * a layer is created, only a texture is created, not an FBO. The content of the
389 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700390 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700391 * buffer and drawing continues as normal. This technique therefore treats the
392 * frame buffer as a scratch buffer for the layers.
393 *
394 * To compose the layers back onto the frame buffer, each layer texture
395 * (containing the original frame buffer data) is drawn as a simple quad over
396 * the frame buffer. The trick is that the quad is set as the composition
397 * destination in the blending equation, and the frame buffer becomes the source
398 * of the composition.
399 *
400 * Drawing layers with an alpha value requires an extra step before composition.
401 * An empty quad is drawn over the layer's region in the frame buffer. This quad
402 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
403 * quad is used to multiply the colors in the frame buffer. This is achieved by
404 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
405 * GL_ZERO, GL_SRC_ALPHA.
406 *
407 * Because glCopyTexImage2D() can be slow, an alternative implementation might
408 * be use to draw a single clipped layer. The implementation described above
409 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700410 *
411 * (1) The frame buffer is actually not cleared right away. To allow the GPU
412 * to potentially optimize series of calls to glCopyTexImage2D, the frame
413 * buffer is left untouched until the first drawing operation. Only when
414 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700415 */
Romain Guyd55a8612010-06-28 17:42:46 -0700416bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700417 float right, float bottom, int alpha, SkXfermode::Mode mode,
418 int flags, GLuint previousFbo) {
419 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700420 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700421
Romain Guyeb993562010-10-05 18:14:38 -0700422 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
423
Romain Guyf607bdc2010-09-10 19:20:06 -0700424 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700425 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700426 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700427 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700428
Romain Guyeb993562010-10-05 18:14:38 -0700429 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700430 if (bounds.intersect(*snapshot->clipRect)) {
431 // We cannot work with sub-pixels in this case
432 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700433
Romain Guyad37cd32011-03-15 11:12:25 -0700434 // When the layer is not an FBO, we may use glCopyTexImage so we
435 // need to make sure the layer does not extend outside the bounds
436 // of the framebuffer
437 if (!bounds.intersect(snapshot->previous->viewport)) {
438 bounds.setEmpty();
439 }
440 } else {
441 bounds.setEmpty();
442 }
Romain Guyeb993562010-10-05 18:14:38 -0700443 }
Romain Guybf434112010-09-16 14:40:17 -0700444
Romain Guy746b7402010-10-26 16:27:31 -0700445 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
446 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800447 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700448 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700449 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700450 }
451
452 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800453 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700454 return false;
455 }
Romain Guyf18fd992010-07-08 11:45:51 -0700456
Romain Guy5b3b3522010-10-27 18:57:51 -0700457 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700458 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700459 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700460 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700461 }
462
Romain Guy9ace8f52011-07-07 20:50:11 -0700463 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700464 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700465 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
466 bounds.getWidth() / float(layer->getWidth()), 0.0f);
467 layer->setColorFilter(mColorFilter);
Romain Guydda570202010-07-06 11:39:32 -0700468
Romain Guy8fb95422010-08-17 18:38:51 -0700469 // Save the layer in the snapshot
470 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700471 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700472
Romain Guyeb993562010-10-05 18:14:38 -0700473 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700474 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700475 } else {
476 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700477 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800478 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700479 if (layer->isEmpty()) {
480 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
481 bounds.left, snapshot->height - bounds.bottom,
482 layer->getWidth(), layer->getHeight(), 0);
483 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800484 } else {
485 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
486 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
487 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700488
Romain Guy54be1cd2011-06-13 19:04:27 -0700489 // Enqueue the buffer coordinates to clear the corresponding region later
490 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700491 }
Romain Guyeb993562010-10-05 18:14:38 -0700492 }
Romain Guyf86ef572010-07-01 11:05:42 -0700493
Romain Guyd55a8612010-06-28 17:42:46 -0700494 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700495}
496
Romain Guy5b3b3522010-10-27 18:57:51 -0700497bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
498 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700499 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700500
501#if RENDER_LAYERS_AS_REGIONS
502 snapshot->region = &snapshot->layer->region;
503 snapshot->flags |= Snapshot::kFlagFboTarget;
504#endif
505
506 Rect clip(bounds);
507 snapshot->transform->mapRect(clip);
508 clip.intersect(*snapshot->clipRect);
509 clip.snapToPixelBoundaries();
510 clip.intersect(snapshot->previous->viewport);
511
512 mat4 inverse;
513 inverse.loadInverse(*mSnapshot->transform);
514
515 inverse.mapRect(clip);
516 clip.snapToPixelBoundaries();
517 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700518 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700519
520 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700521 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700522 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700523 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
524 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
525 snapshot->height = bounds.getHeight();
526 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
527 snapshot->orthoMatrix.load(mOrthoMatrix);
528
529 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700530 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
531 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700532
533 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700534 if (layer->isEmpty()) {
535 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
536 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700537 }
538
539 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700540 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700541
542#if DEBUG_LAYERS_AS_REGIONS
543 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
544 if (status != GL_FRAMEBUFFER_COMPLETE) {
545 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
546
547 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700548 layer->deleteTexture();
549 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700550
551 delete layer;
552
553 return false;
554 }
555#endif
556
557 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
558 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
559 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700560 glClear(GL_COLOR_BUFFER_BIT);
561
562 dirtyClip();
563
564 // Change the ortho projection
565 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
566 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
567
568 return true;
569}
570
Romain Guy1c740bc2010-09-13 18:00:09 -0700571/**
572 * Read the documentation of createLayer() before doing anything in this method.
573 */
Romain Guy1d83e192010-08-17 11:37:00 -0700574void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
575 if (!current->layer) {
576 LOGE("Attempting to compose a layer that does not exist");
577 return;
578 }
579
Romain Guy5b3b3522010-10-27 18:57:51 -0700580 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700581
582 if (fboLayer) {
583 // Unbind current FBO and restore previous one
584 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
585 }
586
Romain Guy1d83e192010-08-17 11:37:00 -0700587 Layer* layer = current->layer;
588 const Rect& rect = layer->layer;
589
Romain Guy9ace8f52011-07-07 20:50:11 -0700590 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700591 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700592 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700593 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700594 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700595 }
596
Romain Guy03750a02010-10-18 14:06:08 -0700597 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700598
Romain Guy746b7402010-10-26 16:27:31 -0700599 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700600
Romain Guy5b3b3522010-10-27 18:57:51 -0700601 // When the layer is stored in an FBO, we can save a bit of fillrate by
602 // drawing only the dirty region
603 if (fboLayer) {
604 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700605 if (layer->getColorFilter()) {
606 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800607 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700608 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700609 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800610 resetColorFilter();
611 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700612 } else if (!rect.isEmpty()) {
613 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
614 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700615 }
Romain Guy8b55f372010-08-18 17:10:07 -0700616
Romain Guyeb993562010-10-05 18:14:38 -0700617 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800618 // Note: No need to use glDiscardFramebufferEXT() since we never
619 // create/compose layers that are not on screen with this
620 // code path
621 // See LayerRenderer::destroyLayer(Layer*)
622
Romain Guyeb993562010-10-05 18:14:38 -0700623 // Detach the texture from the FBO
624 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
625 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
626 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
627
628 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
629 mCaches.fboCache.put(current->fbo);
630 }
631
Romain Guy746b7402010-10-26 16:27:31 -0700632 dirtyClip();
633
Romain Guyeb993562010-10-05 18:14:38 -0700634 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700635 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700636 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700637 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700638 delete layer;
639 }
640}
641
Romain Guyaa6c24c2011-04-28 18:40:04 -0700642void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700643 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700644
Romain Guy302a9df2011-08-16 13:55:02 -0700645 mat4& transform = layer->getTransform();
646 if (!transform.isIdentity()) {
647 save(0);
648 mSnapshot->transform->multiply(transform);
649 }
650
Romain Guyaa6c24c2011-04-28 18:40:04 -0700651 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700652 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700653 setupDrawWithTexture();
654 } else {
655 setupDrawWithExternalTexture();
656 }
657 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700658 setupDrawColor(alpha, alpha, alpha, alpha);
659 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700660 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700661 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700662 setupDrawPureColorUniforms();
663 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700664 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
665 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700666 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700667 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700668 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700669 if (mSnapshot->transform->isPureTranslate() &&
670 layer->getWidth() == (uint32_t) rect.getWidth() &&
671 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700672 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
673 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
674
Romain Guyd21b6e12011-11-30 20:21:23 -0800675 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700676 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
677 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800678 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700679 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
680 }
681 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700682 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
683
684 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
685
686 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700687
688 if (!transform.isIdentity()) {
689 restore();
690 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700691}
692
Romain Guy5b3b3522010-10-27 18:57:51 -0700693void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700694 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700695 const Rect& texCoords = layer->texCoords;
696 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
697 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700698
Romain Guy9ace8f52011-07-07 20:50:11 -0700699 float x = rect.left;
700 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700701 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700702 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700703 layer->getHeight() == (uint32_t) rect.getHeight();
704
705 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700706 // When we're swapping, the layer is already in screen coordinates
707 if (!swap) {
708 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
709 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
710 }
711
Romain Guyd21b6e12011-11-30 20:21:23 -0800712 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700713 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800714 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700715 }
716
717 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
718 layer->getTexture(), layer->getAlpha() / 255.0f,
719 layer->getMode(), layer->isBlend(),
720 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
721 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700722
Romain Guyaa6c24c2011-04-28 18:40:04 -0700723 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
724 } else {
725 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
726 drawTextureLayer(layer, rect);
727 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
728 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700729}
730
731void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
732#if RENDER_LAYERS_AS_REGIONS
733 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700734 layer->setRegionAsRect();
735
Romain Guy40667672011-03-18 14:34:03 -0700736 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700737
Romain Guy5b3b3522010-10-27 18:57:51 -0700738 layer->region.clear();
739 return;
740 }
741
Romain Guy8a3957d2011-09-07 17:55:15 -0700742 // TODO: See LayerRenderer.cpp::generateMesh() for important
743 // information about this implementation
Romain Guy5b3b3522010-10-27 18:57:51 -0700744 if (!layer->region.isEmpty()) {
745 size_t count;
746 const android::Rect* rects = layer->region.getArray(&count);
747
Romain Guy9ace8f52011-07-07 20:50:11 -0700748 const float alpha = layer->getAlpha() / 255.0f;
749 const float texX = 1.0f / float(layer->getWidth());
750 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800751 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700752
753 TextureVertex* mesh = mCaches.getRegionMesh();
754 GLsizei numQuads = 0;
755
Romain Guy7230a742011-01-10 22:26:16 -0800756 setupDraw();
757 setupDrawWithTexture();
758 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800759 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700760 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800761 setupDrawProgram();
762 setupDrawDirtyRegionsDisabled();
763 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800764 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700765 setupDrawTexture(layer->getTexture());
766 if (mSnapshot->transform->isPureTranslate()) {
767 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
768 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
769
Romain Guyd21b6e12011-11-30 20:21:23 -0800770 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700771 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
772 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800773 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700774 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
775 }
Romain Guy15bc6432011-12-13 13:11:32 -0800776 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700777
778 for (size_t i = 0; i < count; i++) {
779 const android::Rect* r = &rects[i];
780
781 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800782 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700783 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800784 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700785
786 // TODO: Reject quads outside of the clip
787 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
788 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
789 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
790 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
791
792 numQuads++;
793
794 if (numQuads >= REGION_MESH_QUAD_COUNT) {
795 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
796 numQuads = 0;
797 mesh = mCaches.getRegionMesh();
798 }
799 }
800
801 if (numQuads > 0) {
802 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
803 }
804
Romain Guy7230a742011-01-10 22:26:16 -0800805 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700806
807#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800808 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700809#endif
810
811 layer->region.clear();
812 }
813#else
814 composeLayerRect(layer, rect);
815#endif
816}
817
Romain Guy3a3133d2011-02-01 22:59:58 -0800818void OpenGLRenderer::drawRegionRects(const Region& region) {
819#if DEBUG_LAYERS_AS_REGIONS
820 size_t count;
821 const android::Rect* rects = region.getArray(&count);
822
823 uint32_t colors[] = {
824 0x7fff0000, 0x7f00ff00,
825 0x7f0000ff, 0x7fff00ff,
826 };
827
828 int offset = 0;
829 int32_t top = rects[0].top;
830
831 for (size_t i = 0; i < count; i++) {
832 if (top != rects[i].top) {
833 offset ^= 0x2;
834 top = rects[i].top;
835 }
836
837 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
838 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
839 SkXfermode::kSrcOver_Mode);
840 }
841#endif
842}
843
Romain Guy5b3b3522010-10-27 18:57:51 -0700844void OpenGLRenderer::dirtyLayer(const float left, const float top,
845 const float right, const float bottom, const mat4 transform) {
846#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800847 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700848 Rect bounds(left, top, right, bottom);
849 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800850 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700851 }
852#endif
853}
854
855void OpenGLRenderer::dirtyLayer(const float left, const float top,
856 const float right, const float bottom) {
857#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800858 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700859 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800860 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800861 }
862#endif
863}
864
865void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
866#if RENDER_LAYERS_AS_REGIONS
867 if (bounds.intersect(*mSnapshot->clipRect)) {
868 bounds.snapToPixelBoundaries();
869 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
870 if (!dirty.isEmpty()) {
871 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700872 }
873 }
874#endif
875}
876
Romain Guy54be1cd2011-06-13 19:04:27 -0700877void OpenGLRenderer::clearLayerRegions() {
878 const size_t count = mLayers.size();
879 if (count == 0) return;
880
881 if (!mSnapshot->isIgnored()) {
882 // Doing several glScissor/glClear here can negatively impact
883 // GPUs with a tiler architecture, instead we draw quads with
884 // the Clear blending mode
885
886 // The list contains bounds that have already been clipped
887 // against their initial clip rect, and the current clip
888 // is likely different so we need to disable clipping here
889 glDisable(GL_SCISSOR_TEST);
890
891 Vertex mesh[count * 6];
892 Vertex* vertex = mesh;
893
894 for (uint32_t i = 0; i < count; i++) {
895 Rect* bounds = mLayers.itemAt(i);
896
897 Vertex::set(vertex++, bounds->left, bounds->bottom);
898 Vertex::set(vertex++, bounds->left, bounds->top);
899 Vertex::set(vertex++, bounds->right, bounds->top);
900 Vertex::set(vertex++, bounds->left, bounds->bottom);
901 Vertex::set(vertex++, bounds->right, bounds->top);
902 Vertex::set(vertex++, bounds->right, bounds->bottom);
903
904 delete bounds;
905 }
906
907 setupDraw(false);
908 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
909 setupDrawBlending(true, SkXfermode::kClear_Mode);
910 setupDrawProgram();
911 setupDrawPureColorUniforms();
912 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -0800913 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -0700914
Romain Guy54be1cd2011-06-13 19:04:27 -0700915 glDrawArrays(GL_TRIANGLES, 0, count * 6);
916
917 glEnable(GL_SCISSOR_TEST);
918 } else {
919 for (uint32_t i = 0; i < count; i++) {
920 delete mLayers.itemAt(i);
921 }
922 }
923
924 mLayers.clear();
925}
926
Romain Guybd6b79b2010-06-26 00:13:53 -0700927///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700928// Transforms
929///////////////////////////////////////////////////////////////////////////////
930
931void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700932 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700933}
934
935void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700936 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700937}
938
939void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700940 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700941}
942
Romain Guy807daf72011-01-18 11:19:19 -0800943void OpenGLRenderer::skew(float sx, float sy) {
944 mSnapshot->transform->skew(sx, sy);
945}
946
Romain Guyf6a11b82010-06-23 17:47:49 -0700947void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -0700948 if (matrix) {
949 mSnapshot->transform->load(*matrix);
950 } else {
951 mSnapshot->transform->loadIdentity();
952 }
Romain Guyf6a11b82010-06-23 17:47:49 -0700953}
954
955void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700956 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700957}
958
959void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700960 SkMatrix transform;
961 mSnapshot->transform->copyTo(transform);
962 transform.preConcat(*matrix);
963 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700964}
965
966///////////////////////////////////////////////////////////////////////////////
967// Clipping
968///////////////////////////////////////////////////////////////////////////////
969
Romain Guybb9524b2010-06-22 18:56:38 -0700970void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700971 Rect clip(*mSnapshot->clipRect);
972 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700973 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700974 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700975}
976
977const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700978 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700979}
980
Romain Guyc7d53492010-06-25 13:41:57 -0700981bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800982 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700983 return true;
984 }
985
Romain Guy1d83e192010-08-17 11:37:00 -0700986 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700987 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700988 r.snapToPixelBoundaries();
989
990 Rect clipRect(*mSnapshot->clipRect);
991 clipRect.snapToPixelBoundaries();
992
993 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700994}
995
Romain Guy079ba2c2010-07-16 14:12:24 -0700996bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
997 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700998 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700999 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001000 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001001 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001002}
1003
Romain Guyf6a11b82010-06-23 17:47:49 -07001004///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001005// Drawing commands
1006///////////////////////////////////////////////////////////////////////////////
1007
Romain Guy54be1cd2011-06-13 19:04:27 -07001008void OpenGLRenderer::setupDraw(bool clear) {
1009 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001010 if (mDirtyClip) {
1011 setScissorFromClip();
1012 }
1013 mDescription.reset();
1014 mSetShaderColor = false;
1015 mColorSet = false;
1016 mColorA = mColorR = mColorG = mColorB = 0.0f;
1017 mTextureUnit = 0;
1018 mTrackDirtyRegions = true;
1019}
1020
1021void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1022 mDescription.hasTexture = true;
1023 mDescription.hasAlpha8Texture = isAlpha8;
1024}
1025
Romain Guyaa6c24c2011-04-28 18:40:04 -07001026void OpenGLRenderer::setupDrawWithExternalTexture() {
1027 mDescription.hasExternalTexture = true;
1028}
1029
Romain Guy15bc6432011-12-13 13:11:32 -08001030void OpenGLRenderer::setupDrawNoTexture() {
1031 mCaches.disbaleTexCoordsVertexArray();
1032}
1033
Chet Haase5b0200b2011-04-13 17:58:08 -07001034void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001035 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001036}
1037
Romain Guyed6fcb02011-03-21 13:11:28 -07001038void OpenGLRenderer::setupDrawPoint(float pointSize) {
1039 mDescription.isPoint = true;
1040 mDescription.pointSize = pointSize;
1041}
1042
Romain Guy70ca14e2010-12-13 18:24:33 -08001043void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001044 setupDrawColor(color, (color >> 24) & 0xFF);
1045}
1046
1047void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1048 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001049 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1050 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001051 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001052 mColorR = a * ((color >> 16) & 0xFF);
1053 mColorG = a * ((color >> 8) & 0xFF);
1054 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001055 mColorSet = true;
1056 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1057}
1058
Romain Guy86568192010-12-14 15:55:39 -08001059void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1060 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001061 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1062 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001063 const float a = mColorA / 255.0f;
1064 mColorR = a * ((color >> 16) & 0xFF);
1065 mColorG = a * ((color >> 8) & 0xFF);
1066 mColorB = a * ((color ) & 0xFF);
1067 mColorSet = true;
1068 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1069}
1070
Romain Guy70ca14e2010-12-13 18:24:33 -08001071void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1072 mColorA = a;
1073 mColorR = r;
1074 mColorG = g;
1075 mColorB = b;
1076 mColorSet = true;
1077 mSetShaderColor = mDescription.setColor(r, g, b, a);
1078}
1079
Romain Guy86568192010-12-14 15:55:39 -08001080void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1081 mColorA = a;
1082 mColorR = r;
1083 mColorG = g;
1084 mColorB = b;
1085 mColorSet = true;
1086 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1087}
1088
Romain Guy70ca14e2010-12-13 18:24:33 -08001089void OpenGLRenderer::setupDrawShader() {
1090 if (mShader) {
1091 mShader->describe(mDescription, mCaches.extensions);
1092 }
1093}
1094
1095void OpenGLRenderer::setupDrawColorFilter() {
1096 if (mColorFilter) {
1097 mColorFilter->describe(mDescription, mCaches.extensions);
1098 }
1099}
1100
Romain Guyf09ef512011-05-27 11:43:46 -07001101void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1102 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1103 mColorA = 1.0f;
1104 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001105 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001106 }
1107}
1108
Romain Guy70ca14e2010-12-13 18:24:33 -08001109void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001110 // When the blending mode is kClear_Mode, we need to use a modulate color
1111 // argb=1,0,0,0
1112 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001113 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1114 mDescription, swapSrcDst);
1115}
1116
1117void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001118 // When the blending mode is kClear_Mode, we need to use a modulate color
1119 // argb=1,0,0,0
1120 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001121 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1122 mDescription, swapSrcDst);
1123}
1124
1125void OpenGLRenderer::setupDrawProgram() {
1126 useProgram(mCaches.programCache.get(mDescription));
1127}
1128
1129void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1130 mTrackDirtyRegions = false;
1131}
1132
1133void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1134 bool ignoreTransform) {
1135 mModelView.loadTranslate(left, top, 0.0f);
1136 if (!ignoreTransform) {
1137 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1138 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1139 } else {
1140 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1141 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1142 }
1143}
1144
Chet Haase8a5cc922011-04-26 07:28:09 -07001145void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1146 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001147}
1148
Romain Guy70ca14e2010-12-13 18:24:33 -08001149void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1150 bool ignoreTransform, bool ignoreModelView) {
1151 if (!ignoreModelView) {
1152 mModelView.loadTranslate(left, top, 0.0f);
1153 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001154 } else {
1155 mModelView.loadIdentity();
1156 }
Romain Guy86568192010-12-14 15:55:39 -08001157 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1158 if (!ignoreTransform) {
1159 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1160 if (mTrackDirtyRegions && dirty) {
1161 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1162 }
1163 } else {
1164 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1165 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1166 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001167}
1168
Romain Guyed6fcb02011-03-21 13:11:28 -07001169void OpenGLRenderer::setupDrawPointUniforms() {
1170 int slot = mCaches.currentProgram->getUniform("pointSize");
1171 glUniform1f(slot, mDescription.pointSize);
1172}
1173
Romain Guy70ca14e2010-12-13 18:24:33 -08001174void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001175 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001176 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1177 }
1178}
1179
Romain Guy86568192010-12-14 15:55:39 -08001180void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001181 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001182 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001183 }
1184}
1185
Romain Guy70ca14e2010-12-13 18:24:33 -08001186void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1187 if (mShader) {
1188 if (ignoreTransform) {
1189 mModelView.loadInverse(*mSnapshot->transform);
1190 }
1191 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1192 }
1193}
1194
Romain Guy8d0d4782010-12-14 20:13:35 -08001195void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1196 if (mShader) {
1197 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1198 }
1199}
1200
Romain Guy70ca14e2010-12-13 18:24:33 -08001201void OpenGLRenderer::setupDrawColorFilterUniforms() {
1202 if (mColorFilter) {
1203 mColorFilter->setupProgram(mCaches.currentProgram);
1204 }
1205}
1206
1207void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001208 bool force = mCaches.bindMeshBuffer();
1209 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001210 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001211}
1212
1213void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1214 bindTexture(texture);
1215 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
Romain Guy15bc6432011-12-13 13:11:32 -08001216 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001217}
1218
Romain Guyaa6c24c2011-04-28 18:40:04 -07001219void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1220 bindExternalTexture(texture);
1221 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
Romain Guy15bc6432011-12-13 13:11:32 -08001222 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001223}
1224
Romain Guy8f0095c2011-05-02 17:24:22 -07001225void OpenGLRenderer::setupDrawTextureTransform() {
1226 mDescription.hasTextureTransform = true;
1227}
1228
1229void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001230 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1231 GL_FALSE, &transform.data[0]);
1232}
1233
Romain Guy70ca14e2010-12-13 18:24:33 -08001234void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001235 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001236 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001237 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001238 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001239 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001240 }
Romain Guyd71dd362011-12-12 19:03:35 -08001241
Romain Guyf3a910b42011-12-12 20:35:21 -08001242 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001243 if (mCaches.currentProgram->texCoords >= 0) {
1244 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1245 }
1246
1247 mCaches.unbindIndicesBuffer();
1248}
1249
1250void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1251 bool force = mCaches.unbindMeshBuffer();
1252 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1253 if (mCaches.currentProgram->texCoords >= 0) {
1254 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001255 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001256}
1257
Chet Haase5b0200b2011-04-13 17:58:08 -07001258void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001259 bool force = mCaches.unbindMeshBuffer();
1260 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1261 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001262 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001263}
1264
1265/**
1266 * 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 -07001267 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1268 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1269 * attributes (one per vertex) are values from zero to one that tells the fragment
1270 * shader where the fragment is in relation to the line width/length overall; these values are
1271 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1272 * region of the line.
1273 * Note that we only pass down the width values in this setup function. The length coordinates
1274 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001275 */
Chet Haase99585ad2011-05-02 15:00:16 -07001276void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Chet Haase99ecdc42011-05-06 12:06:34 -07001277 GLvoid* lengthCoords, float boundaryWidthProportion) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001278 bool force = mCaches.unbindMeshBuffer();
1279 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1280 vertices, gAAVertexStride);
1281 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001282 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001283
Chet Haase99585ad2011-05-02 15:00:16 -07001284 int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
1285 glEnableVertexAttribArray(widthSlot);
1286 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001287
Chet Haase99585ad2011-05-02 15:00:16 -07001288 int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
1289 glEnableVertexAttribArray(lengthSlot);
1290 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001291
Chet Haase5b0200b2011-04-13 17:58:08 -07001292 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001293 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001294
Chet Haase99585ad2011-05-02 15:00:16 -07001295 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001296 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001297 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
Chet Haase5b0200b2011-04-13 17:58:08 -07001298}
1299
Romain Guy70ca14e2010-12-13 18:24:33 -08001300void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001301}
1302
1303///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001304// Drawing
1305///////////////////////////////////////////////////////////////////////////////
1306
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001307bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
1308 Rect& dirty, uint32_t level) {
1309 if (quickReject(0.0f, 0.0f, width, height)) {
1310 return false;
1311 }
1312
Romain Guy0fe478e2010-11-08 12:08:41 -08001313 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1314 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001315 if (displayList && displayList->isRenderable()) {
Romain Guycabfcc12011-03-07 18:06:46 -08001316 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001317 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001318
Chet Haasedaf98e92011-01-10 14:10:36 -08001319 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001320}
1321
Chet Haaseed30fd82011-04-22 16:18:45 -07001322void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1323 if (displayList) {
1324 displayList->output(*this, level);
1325 }
1326}
1327
Romain Guya168d732011-03-18 16:50:13 -07001328void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1329 int alpha;
1330 SkXfermode::Mode mode;
1331 getAlphaAndMode(paint, &alpha, &mode);
1332
Romain Guya168d732011-03-18 16:50:13 -07001333 float x = left;
1334 float y = top;
1335
Romain Guye3c26852011-07-25 16:36:01 -07001336 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001337 bool ignoreTransform = false;
1338 if (mSnapshot->transform->isPureTranslate()) {
1339 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1340 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1341 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001342 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001343 } else {
1344 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001345 }
1346
1347 setupDraw();
1348 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001349 if (paint) {
1350 setupDrawAlpha8Color(paint->getColor(), alpha);
1351 }
Romain Guya168d732011-03-18 16:50:13 -07001352 setupDrawColorFilter();
1353 setupDrawShader();
1354 setupDrawBlending(true, mode);
1355 setupDrawProgram();
1356 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001357
Romain Guya168d732011-03-18 16:50:13 -07001358 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001359 texture->setWrap(GL_CLAMP_TO_EDGE);
1360 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001361
Romain Guya168d732011-03-18 16:50:13 -07001362 setupDrawPureColorUniforms();
1363 setupDrawColorFilterUniforms();
1364 setupDrawShaderUniforms();
1365 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1366
1367 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1368
1369 finishDrawTexture();
1370}
1371
Chet Haase5c13d892010-10-08 08:37:55 -07001372void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001373 const float right = left + bitmap->width();
1374 const float bottom = top + bitmap->height();
1375
1376 if (quickReject(left, top, right, bottom)) {
1377 return;
1378 }
1379
Romain Guy86568192010-12-14 15:55:39 -08001380 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001381 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001382 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001383 const AutoTexture autoCleanup(texture);
1384
Romain Guya168d732011-03-18 16:50:13 -07001385 if (bitmap->getConfig() == SkBitmap::kA8_Config) {
1386 drawAlphaBitmap(texture, left, top, paint);
1387 } else {
1388 drawTextureRect(left, top, right, bottom, texture, paint);
1389 }
Romain Guyce0537b2010-06-29 21:05:21 -07001390}
1391
Chet Haase5c13d892010-10-08 08:37:55 -07001392void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001393 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1394 const mat4 transform(*matrix);
1395 transform.mapRect(r);
1396
Romain Guy6926c722010-07-12 20:20:03 -07001397 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1398 return;
1399 }
1400
Romain Guy86568192010-12-14 15:55:39 -08001401 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001402 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001403 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001404 const AutoTexture autoCleanup(texture);
1405
Romain Guy5b3b3522010-10-27 18:57:51 -07001406 // This could be done in a cheaper way, all we need is pass the matrix
1407 // to the vertex shader. The save/restore is a bit overkill.
1408 save(SkCanvas::kMatrix_SaveFlag);
1409 concatMatrix(matrix);
1410 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1411 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001412}
1413
Romain Guy5a7b4662011-01-20 19:09:30 -08001414void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1415 float* vertices, int* colors, SkPaint* paint) {
1416 // TODO: Do a quickReject
1417 if (!vertices || mSnapshot->isIgnored()) {
1418 return;
1419 }
1420
1421 glActiveTexture(gTextureUnits[0]);
1422 Texture* texture = mCaches.textureCache.get(bitmap);
1423 if (!texture) return;
1424 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001425
Romain Guyd21b6e12011-11-30 20:21:23 -08001426 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1427 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001428
1429 int alpha;
1430 SkXfermode::Mode mode;
1431 getAlphaAndMode(paint, &alpha, &mode);
1432
Romain Guy5a7b4662011-01-20 19:09:30 -08001433 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001434
Romain Guyb18d2d02011-02-10 15:52:54 -08001435 float left = FLT_MAX;
1436 float top = FLT_MAX;
1437 float right = FLT_MIN;
1438 float bottom = FLT_MIN;
1439
1440#if RENDER_LAYERS_AS_REGIONS
1441 bool hasActiveLayer = hasLayer();
1442#else
1443 bool hasActiveLayer = false;
1444#endif
1445
Romain Guya566b7c2011-01-23 16:36:11 -08001446 // TODO: Support the colors array
1447 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001448 TextureVertex* vertex = mesh;
1449 for (int32_t y = 0; y < meshHeight; y++) {
1450 for (int32_t x = 0; x < meshWidth; x++) {
1451 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1452
1453 float u1 = float(x) / meshWidth;
1454 float u2 = float(x + 1) / meshWidth;
1455 float v1 = float(y) / meshHeight;
1456 float v2 = float(y + 1) / meshHeight;
1457
1458 int ax = i + (meshWidth + 1) * 2;
1459 int ay = ax + 1;
1460 int bx = i;
1461 int by = bx + 1;
1462 int cx = i + 2;
1463 int cy = cx + 1;
1464 int dx = i + (meshWidth + 1) * 2 + 2;
1465 int dy = dx + 1;
1466
1467 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1468 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1469 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1470
1471 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1472 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1473 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001474
1475#if RENDER_LAYERS_AS_REGIONS
1476 if (hasActiveLayer) {
1477 // TODO: This could be optimized to avoid unnecessary ops
1478 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1479 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1480 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1481 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1482 }
1483#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001484 }
1485 }
1486
Romain Guyb18d2d02011-02-10 15:52:54 -08001487#if RENDER_LAYERS_AS_REGIONS
1488 if (hasActiveLayer) {
1489 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1490 }
1491#endif
1492
Romain Guy5a7b4662011-01-20 19:09:30 -08001493 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1494 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001495 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001496}
1497
Romain Guy8ba548f2010-06-30 19:21:21 -07001498void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1499 float srcLeft, float srcTop, float srcRight, float srcBottom,
1500 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001501 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001502 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1503 return;
1504 }
1505
Romain Guy746b7402010-10-26 16:27:31 -07001506 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001507 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001508 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001509 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001510
Romain Guy8ba548f2010-06-30 19:21:21 -07001511 const float width = texture->width;
1512 const float height = texture->height;
1513
Romain Guy68169722011-08-22 17:33:33 -07001514 const float u1 = fmax(0.0f, srcLeft / width);
1515 const float v1 = fmax(0.0f, srcTop / height);
1516 const float u2 = fmin(1.0f, srcRight / width);
1517 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001518
Romain Guy03750a02010-10-18 14:06:08 -07001519 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001520 resetDrawTextureTexCoords(u1, v1, u2, v2);
1521
Romain Guy03750a02010-10-18 14:06:08 -07001522 int alpha;
1523 SkXfermode::Mode mode;
1524 getAlphaAndMode(paint, &alpha, &mode);
1525
Romain Guyd21b6e12011-11-30 20:21:23 -08001526 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1527
Romain Guy6620c6d2010-12-06 18:07:02 -08001528 if (mSnapshot->transform->isPureTranslate()) {
1529 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1530 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1531
Romain Guyb5014982011-07-28 15:39:12 -07001532 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001533 // Enable linear filtering if the source rectangle is scaled
1534 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001535 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001536 }
Romain Guyb5014982011-07-28 15:39:12 -07001537
Romain Guyd21b6e12011-11-30 20:21:23 -08001538 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001539 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1540 texture->id, alpha / 255.0f, mode, texture->blend,
1541 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1542 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1543 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001544 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001545 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1546 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1547 GL_TRIANGLE_STRIP, gMeshCount);
1548 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001549
1550 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1551}
1552
Romain Guy4aa90572010-09-26 18:40:37 -07001553void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001554 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001555 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001556 if (quickReject(left, top, right, bottom)) {
1557 return;
1558 }
1559
Romain Guy746b7402010-10-26 16:27:31 -07001560 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001561 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001562 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001563 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001564 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1565 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001566
1567 int alpha;
1568 SkXfermode::Mode mode;
1569 getAlphaAndMode(paint, &alpha, &mode);
1570
Romain Guy2728f962010-10-08 18:36:15 -07001571 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001572 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001573
Romain Guya5ef39a2010-12-03 16:48:20 -08001574 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001575 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001576#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001577 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001578 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001579 const float offsetX = left + mSnapshot->transform->getTranslateX();
1580 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001581 const size_t count = mesh->quads.size();
1582 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001583 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001584 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001585 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1586 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1587 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001588 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001589 dirtyLayer(left + bounds.left, top + bounds.top,
1590 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001591 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001592 }
1593 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001594#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001595
Romain Guy6620c6d2010-12-06 18:07:02 -08001596 if (pureTranslate) {
1597 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1598 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1599
1600 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1601 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1602 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1603 true, !mesh->hasEmptyQuads);
1604 } else {
1605 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1606 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1607 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1608 true, !mesh->hasEmptyQuads);
1609 }
Romain Guy054dc182010-10-15 17:55:25 -07001610 }
Romain Guyf7f93552010-07-08 19:17:03 -07001611}
1612
Chet Haase99ecdc42011-05-06 12:06:34 -07001613/**
Chet Haase858aa932011-05-12 09:06:00 -07001614 * This function uses a similar approach to that of AA lines in the drawLines() function.
1615 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1616 * shader to compute the translucency of the color, determined by whether a given pixel is
1617 * within that boundary region and how far into the region it is.
1618 */
1619void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001620 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001621 float inverseScaleX = 1.0f;
1622 float inverseScaleY = 1.0f;
1623 // The quad that we use needs to account for scaling.
1624 if (!mSnapshot->transform->isPureTranslate()) {
1625 Matrix4 *mat = mSnapshot->transform;
1626 float m00 = mat->data[Matrix4::kScaleX];
1627 float m01 = mat->data[Matrix4::kSkewY];
1628 float m02 = mat->data[2];
1629 float m10 = mat->data[Matrix4::kSkewX];
1630 float m11 = mat->data[Matrix4::kScaleX];
1631 float m12 = mat->data[6];
1632 float scaleX = sqrt(m00 * m00 + m01 * m01);
1633 float scaleY = sqrt(m10 * m10 + m11 * m11);
1634 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1635 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1636 }
1637
1638 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001639 setupDrawNoTexture();
Chet Haase858aa932011-05-12 09:06:00 -07001640 setupDrawAALine();
1641 setupDrawColor(color);
1642 setupDrawColorFilter();
1643 setupDrawShader();
1644 setupDrawBlending(true, mode);
1645 setupDrawProgram();
1646 setupDrawModelViewIdentity(true);
1647 setupDrawColorUniforms();
1648 setupDrawColorFilterUniforms();
1649 setupDrawShaderIdentityUniforms();
1650
1651 AAVertex rects[4];
1652 AAVertex* aaVertices = &rects[0];
1653 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1654 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1655
1656 float boundarySizeX = .5 * inverseScaleX;
1657 float boundarySizeY = .5 * inverseScaleY;
1658
1659 // Adjust the rect by the AA boundary padding
1660 left -= boundarySizeX;
1661 right += boundarySizeX;
1662 top -= boundarySizeY;
1663 bottom += boundarySizeY;
1664
1665 float width = right - left;
1666 float height = bottom - top;
1667
1668 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1669 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
1670 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
1671 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1672 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1673 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
1674 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryHeightProportion));
1675
1676 if (!quickReject(left, top, right, bottom)) {
1677 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1678 AAVertex::set(aaVertices++, left, top, 1, 0);
1679 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1680 AAVertex::set(aaVertices++, right, top, 0, 0);
1681 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1682 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1683 }
1684}
1685
1686/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001687 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1688 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1689 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1690 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1691 * of the line. Hairlines are more involved because we need to account for transform scaling
1692 * to end up with a one-pixel-wide line in screen space..
1693 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1694 * in combination with values that we calculate and pass down in this method. The basic approach
1695 * is that the quad we create contains both the core line area plus a bounding area in which
1696 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1697 * proportion of the width and the length of a given segment is represented by the boundary
1698 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1699 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1700 * on the inside). This ends up giving the result we want, with pixels that are completely
1701 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1702 * how far into the boundary region they are, which is determined by shader interpolation.
1703 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001704void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1705 if (mSnapshot->isIgnored()) return;
1706
1707 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001708 // We use half the stroke width here because we're going to position the quad
1709 // corner vertices half of the width away from the line endpoints
1710 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001711 // A stroke width of 0 has a special meaning in Skia:
1712 // it draws a line 1 px wide regardless of current transform
1713 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase99ecdc42011-05-06 12:06:34 -07001714 float inverseScaleX = 1.0f;
1715 float inverseScaleY = 1.0f;
1716 bool scaled = false;
Chet Haase8a5cc922011-04-26 07:28:09 -07001717 int alpha;
1718 SkXfermode::Mode mode;
1719 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001720 int verticesCount = count;
1721 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001722 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001723 verticesCount += (count - 4);
1724 }
1725
1726 if (isHairLine || isAA) {
1727 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1728 // the line on the screen should always be one pixel wide regardless of scale. For
1729 // AA lines, we only want one pixel of translucent boundary around the quad.
1730 if (!mSnapshot->transform->isPureTranslate()) {
1731 Matrix4 *mat = mSnapshot->transform;
1732 float m00 = mat->data[Matrix4::kScaleX];
1733 float m01 = mat->data[Matrix4::kSkewY];
1734 float m02 = mat->data[2];
1735 float m10 = mat->data[Matrix4::kSkewX];
1736 float m11 = mat->data[Matrix4::kScaleX];
1737 float m12 = mat->data[6];
1738 float scaleX = sqrt(m00*m00 + m01*m01);
1739 float scaleY = sqrt(m10*m10 + m11*m11);
1740 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1741 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1742 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1743 scaled = true;
1744 }
1745 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001746 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001747
1748 getAlphaAndMode(paint, &alpha, &mode);
1749 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001750 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001751 if (isAA) {
1752 setupDrawAALine();
1753 }
1754 setupDrawColor(paint->getColor(), alpha);
1755 setupDrawColorFilter();
1756 setupDrawShader();
1757 if (isAA) {
1758 setupDrawBlending(true, mode);
1759 } else {
1760 setupDrawBlending(mode);
1761 }
1762 setupDrawProgram();
1763 setupDrawModelViewIdentity(true);
1764 setupDrawColorUniforms();
1765 setupDrawColorFilterUniforms();
1766 setupDrawShaderIdentityUniforms();
1767
1768 if (isHairLine) {
1769 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001770 halfStrokeWidth = isAA? 1 : .5;
1771 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001772 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001773 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001774 }
1775 Vertex lines[verticesCount];
1776 Vertex* vertices = &lines[0];
Chet Haase99585ad2011-05-02 15:00:16 -07001777 AAVertex wLines[verticesCount];
1778 AAVertex* aaVertices = &wLines[0];
Chet Haase5b0200b2011-04-13 17:58:08 -07001779 if (!isAA) {
1780 setupDrawVertices(vertices);
1781 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001782 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1783 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001784 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001785 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1786 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001787 // We will need to calculate the actual width proportion on each segment for
1788 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1789 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
1790 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
Chet Haase5b0200b2011-04-13 17:58:08 -07001791 }
1792
Chet Haase99ecdc42011-05-06 12:06:34 -07001793 AAVertex* prevAAVertex = NULL;
1794 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001795
Chet Haase99585ad2011-05-02 15:00:16 -07001796 int boundaryLengthSlot = -1;
1797 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001798 int boundaryWidthSlot = -1;
1799 int inverseBoundaryWidthSlot = -1;
Chet Haase5b0200b2011-04-13 17:58:08 -07001800 for (int i = 0; i < count; i += 4) {
1801 // a = start point, b = end point
1802 vec2 a(points[i], points[i + 1]);
1803 vec2 b(points[i + 2], points[i + 3]);
Chet Haase99585ad2011-05-02 15:00:16 -07001804 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001805 float boundaryLengthProportion = 0;
1806 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001807
Chet Haase5b0200b2011-04-13 17:58:08 -07001808 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001809 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001810 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001811 if (isAA) {
1812 float wideningFactor;
1813 if (fabs(n.x) >= fabs(n.y)) {
1814 wideningFactor = fabs(1.0f / n.x);
1815 } else {
1816 wideningFactor = fabs(1.0f / n.y);
1817 }
1818 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001819 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001820 if (scaled) {
1821 n.x *= inverseScaleX;
1822 n.y *= inverseScaleY;
1823 }
1824 } else if (scaled) {
1825 // Extend n by .5 pixel on each side, post-transform
1826 vec2 extendedN = n.copyNormalized();
1827 extendedN /= 2;
1828 extendedN.x *= inverseScaleX;
1829 extendedN.y *= inverseScaleY;
1830 float extendedNLength = extendedN.length();
1831 // We need to set this value on the shader prior to drawing
1832 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1833 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001834 }
1835 float x = n.x;
1836 n.x = -n.y;
1837 n.y = x;
1838
Chet Haase99585ad2011-05-02 15:00:16 -07001839 // aa lines expand the endpoint vertices to encompass the AA boundary
1840 if (isAA) {
1841 vec2 abVector = (b - a);
1842 length = abVector.length();
1843 abVector.normalize();
Chet Haase99ecdc42011-05-06 12:06:34 -07001844 if (scaled) {
1845 abVector.x *= inverseScaleX;
1846 abVector.y *= inverseScaleY;
1847 float abLength = abVector.length();
1848 boundaryLengthProportion = abLength / (length + abLength);
1849 } else {
1850 boundaryLengthProportion = .5 / (length + 1);
1851 }
1852 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001853 a -= abVector;
1854 b += abVector;
1855 }
1856
Chet Haase5b0200b2011-04-13 17:58:08 -07001857 // Four corners of the rectangle defining a thick line
1858 vec2 p1 = a - n;
1859 vec2 p2 = a + n;
1860 vec2 p3 = b + n;
1861 vec2 p4 = b - n;
1862
Chet Haase99585ad2011-05-02 15:00:16 -07001863
Chet Haase5b0200b2011-04-13 17:58:08 -07001864 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1865 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1866 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1867 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1868
1869 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001870 if (!isAA) {
1871 if (prevVertex != NULL) {
1872 // Issue two repeat vertices to create degenerate triangles to bridge
1873 // between the previous line and the new one. This is necessary because
1874 // we are creating a single triangle_strip which will contain
1875 // potentially discontinuous line segments.
1876 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1877 Vertex::set(vertices++, p1.x, p1.y);
1878 generatedVerticesCount += 2;
1879 }
1880 Vertex::set(vertices++, p1.x, p1.y);
1881 Vertex::set(vertices++, p2.x, p2.y);
1882 Vertex::set(vertices++, p4.x, p4.y);
1883 Vertex::set(vertices++, p3.x, p3.y);
1884 prevVertex = vertices - 1;
1885 generatedVerticesCount += 4;
1886 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001887 if (!isHairLine && scaled) {
1888 // Must set width proportions per-segment for scaled non-hairlines to use the
1889 // correct AA boundary dimensions
1890 if (boundaryWidthSlot < 0) {
1891 boundaryWidthSlot =
1892 mCaches.currentProgram->getUniform("boundaryWidth");
1893 inverseBoundaryWidthSlot =
1894 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1895 }
1896 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1897 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1898 }
Chet Haase99585ad2011-05-02 15:00:16 -07001899 if (boundaryLengthSlot < 0) {
1900 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1901 inverseBoundaryLengthSlot =
1902 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1903 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001904 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
1905 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07001906
Chet Haase5b0200b2011-04-13 17:58:08 -07001907 if (prevAAVertex != NULL) {
1908 // Issue two repeat vertices to create degenerate triangles to bridge
1909 // between the previous line and the new one. This is necessary because
1910 // we are creating a single triangle_strip which will contain
1911 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07001912 AAVertex::set(aaVertices++,prevAAVertex->position[0],
1913 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
1914 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07001915 generatedVerticesCount += 2;
1916 }
Chet Haase99585ad2011-05-02 15:00:16 -07001917 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
1918 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
1919 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
1920 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Chet Haase5b0200b2011-04-13 17:58:08 -07001921 prevAAVertex = aaVertices - 1;
1922 generatedVerticesCount += 4;
1923 }
Chet Haase99585ad2011-05-02 15:00:16 -07001924 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
1925 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
1926 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07001927 }
1928 }
1929 if (generatedVerticesCount > 0) {
1930 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
1931 }
1932}
1933
Romain Guyed6fcb02011-03-21 13:11:28 -07001934void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
1935 if (mSnapshot->isIgnored()) return;
1936
1937 // TODO: The paint's cap style defines whether the points are square or circular
1938 // TODO: Handle AA for round points
1939
Chet Haase5b0200b2011-04-13 17:58:08 -07001940 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07001941 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07001942 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07001943 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001944 if (isHairLine) {
1945 // Now that we know it's hairline, we can set the effective width, to be used later
1946 strokeWidth = 1.0f;
1947 }
1948 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07001949 int alpha;
1950 SkXfermode::Mode mode;
1951 getAlphaAndMode(paint, &alpha, &mode);
1952
1953 int verticesCount = count >> 1;
1954 int generatedVerticesCount = 0;
1955
1956 TextureVertex pointsData[verticesCount];
1957 TextureVertex* vertex = &pointsData[0];
1958
1959 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001960 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001961 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07001962 setupDrawColor(paint->getColor(), alpha);
1963 setupDrawColorFilter();
1964 setupDrawShader();
1965 setupDrawBlending(mode);
1966 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07001967 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07001968 setupDrawColorUniforms();
1969 setupDrawColorFilterUniforms();
1970 setupDrawPointUniforms();
1971 setupDrawShaderIdentityUniforms();
1972 setupDrawMesh(vertex);
1973
1974 for (int i = 0; i < count; i += 2) {
1975 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1976 generatedVerticesCount++;
Chet Haase8a5cc922011-04-26 07:28:09 -07001977 float left = points[i] - halfWidth;
1978 float right = points[i] + halfWidth;
1979 float top = points[i + 1] - halfWidth;
1980 float bottom = points [i + 1] + halfWidth;
1981 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07001982 }
1983
1984 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
1985}
1986
Romain Guy85bf02f2010-06-22 13:11:24 -07001987void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001988 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001989 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001990
Romain Guyae88e5e2010-10-22 17:49:18 -07001991 Rect& clip(*mSnapshot->clipRect);
1992 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001993
Romain Guy3d58c032010-07-14 16:34:53 -07001994 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001995}
Romain Guy9d5316e2010-06-24 19:30:36 -07001996
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001997void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001998 if (!texture) return;
1999 const AutoTexture autoCleanup(texture);
2000
2001 const float x = left + texture->left - texture->offset;
2002 const float y = top + texture->top - texture->offset;
2003
2004 drawPathTexture(texture, x, y, paint);
2005}
2006
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002007void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
2008 float rx, float ry, SkPaint* paint) {
2009 if (mSnapshot->isIgnored()) return;
2010
2011 glActiveTexture(gTextureUnits[0]);
2012 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2013 right - left, bottom - top, rx, ry, paint);
2014 drawShape(left, top, texture, paint);
2015}
2016
Romain Guy01d58e42011-01-19 21:54:02 -08002017void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2018 if (mSnapshot->isIgnored()) return;
2019
2020 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08002021 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002022 drawShape(x - radius, y - radius, texture, paint);
2023}
Romain Guy01d58e42011-01-19 21:54:02 -08002024
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002025void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2026 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002027
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002028 glActiveTexture(gTextureUnits[0]);
2029 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2030 drawShape(left, top, texture, paint);
2031}
2032
Romain Guy8b2f5262011-01-23 16:15:02 -08002033void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2034 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2035 if (mSnapshot->isIgnored()) return;
2036
2037 if (fabs(sweepAngle) >= 360.0f) {
2038 drawOval(left, top, right, bottom, paint);
2039 return;
2040 }
2041
2042 glActiveTexture(gTextureUnits[0]);
2043 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2044 startAngle, sweepAngle, useCenter, paint);
2045 drawShape(left, top, texture, paint);
2046}
2047
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002048void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2049 SkPaint* paint) {
2050 if (mSnapshot->isIgnored()) return;
2051
2052 glActiveTexture(gTextureUnits[0]);
2053 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2054 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002055}
2056
Chet Haase5c13d892010-10-08 08:37:55 -07002057void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002058 if (p->getStyle() != SkPaint::kFill_Style) {
2059 drawRectAsShape(left, top, right, bottom, p);
2060 return;
2061 }
2062
Romain Guy6926c722010-07-12 20:20:03 -07002063 if (quickReject(left, top, right, bottom)) {
2064 return;
2065 }
2066
Romain Guy026c5e162010-06-28 17:12:22 -07002067 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002068 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002069 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2070 if (!isMode) {
2071 // Assume SRC_OVER
2072 mode = SkXfermode::kSrcOver_Mode;
2073 }
2074 } else {
2075 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002076 }
2077
Romain Guy026c5e162010-06-28 17:12:22 -07002078 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002079 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002080 drawAARect(left, top, right, bottom, color, mode);
2081 } else {
2082 drawColorRect(left, top, right, bottom, color, mode);
2083 }
Romain Guyc7d53492010-06-25 13:41:57 -07002084}
Romain Guy9d5316e2010-06-24 19:30:36 -07002085
Romain Guye8e62a42010-07-23 18:55:21 -07002086void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002087 float x, float y, SkPaint* paint, float length) {
Romain Guyb146b122010-12-15 17:06:45 -08002088 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07002089 return;
2090 }
Romain Guyaf636eb2010-12-09 17:47:21 -08002091 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07002092
Romain Guy8f9a9f62011-12-05 11:53:26 -08002093 // NOTE: AA and glyph id encoding are set in DisplayListRenderer.cpp
Romain Guye8e62a42010-07-23 18:55:21 -07002094
Romain Guye8e62a42010-07-23 18:55:21 -07002095 switch (paint->getTextAlign()) {
2096 case SkPaint::kCenter_Align:
Romain Guycac5fd32011-12-01 20:08:50 -08002097 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002098 x -= length / 2.0f;
2099 break;
2100 case SkPaint::kRight_Align:
Romain Guycac5fd32011-12-01 20:08:50 -08002101 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002102 x -= length;
2103 break;
2104 default:
2105 break;
2106 }
2107
Romain Guycac5fd32011-12-01 20:08:50 -08002108 SkPaint::FontMetrics metrics;
2109 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy8f9a9f62011-12-05 11:53:26 -08002110 // If no length was specified, just perform the hit test on the Y axis
Romain Guycac5fd32011-12-01 20:08:50 -08002111 if (quickReject(x, y + metrics.fTop,
2112 x + (length >= 0.0f ? length : INT_MAX / 2), y + metrics.fBottom)) {
2113 return;
2114 }
2115
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002116 const float oldX = x;
2117 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002118 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2119 if (pureTranslate) {
2120 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2121 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2122 }
2123
Romain Guyb45c0c92010-08-26 20:35:23 -07002124 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002125#if DEBUG_GLYPHS
2126 LOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
2127#endif
Romain Guyb45c0c92010-08-26 20:35:23 -07002128 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002129 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002130
Romain Guy86568192010-12-14 15:55:39 -08002131 int alpha;
2132 SkXfermode::Mode mode;
2133 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002134
Romain Guy1e45aae2010-08-13 19:39:53 -07002135 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07002136 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002137 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2138 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002139 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002140
Romain Guy740bf2b2011-04-26 15:33:10 -07002141 const float sx = oldX - shadow->left + mShadowDx;
2142 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002143
Romain Guy86568192010-12-14 15:55:39 -08002144 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002145 int shadowColor = mShadowColor;
2146 if (mShader) {
2147 shadowColor = 0xffffffff;
2148 }
Romain Guy86568192010-12-14 15:55:39 -08002149
2150 glActiveTexture(gTextureUnits[0]);
2151 setupDraw();
2152 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002153 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2154 setupDrawColorFilter();
2155 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002156 setupDrawBlending(true, mode);
2157 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002158 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002159 setupDrawTexture(shadow->id);
2160 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002161 setupDrawColorFilterUniforms();
2162 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002163 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2164
Romain Guy0a417492010-08-16 20:26:20 -07002165 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy740bf2b2011-04-26 15:33:10 -07002166
Romain Guy86568192010-12-14 15:55:39 -08002167 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07002168 }
2169
Romain Guyb146b122010-12-15 17:06:45 -08002170 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
2171 return;
2172 }
2173
Romain Guy6620c6d2010-12-06 18:07:02 -08002174 // Pick the appropriate texture filtering
2175 bool linearFilter = mSnapshot->transform->changesBounds();
2176 if (pureTranslate && !linearFilter) {
2177 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2178 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002179
Romain Guy86568192010-12-14 15:55:39 -08002180 glActiveTexture(gTextureUnits[0]);
2181 setupDraw();
2182 setupDrawDirtyRegionsDisabled();
2183 setupDrawWithTexture(true);
2184 setupDrawAlpha8Color(paint->getColor(), alpha);
2185 setupDrawColorFilter();
2186 setupDrawShader();
2187 setupDrawBlending(true, mode);
2188 setupDrawProgram();
2189 setupDrawModelView(x, y, x, y, pureTranslate, true);
2190 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2191 setupDrawPureColorUniforms();
2192 setupDrawColorFilterUniforms();
2193 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002194
Romain Guy6620c6d2010-12-06 18:07:02 -08002195 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002196 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2197
2198#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002199 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002200#else
Romain Guyf219da52011-01-16 12:54:25 -08002201 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002202#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002203
Romain Guy6620c6d2010-12-06 18:07:02 -08002204 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002205 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002206#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002207 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002208 if (!pureTranslate) {
2209 mSnapshot->transform->mapRect(bounds);
2210 }
Romain Guyf219da52011-01-16 12:54:25 -08002211 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002212 }
2213#endif
2214 }
Romain Guy694b5192010-07-21 21:33:20 -07002215
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002216 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002217}
2218
Romain Guy7fbcc042010-08-04 15:40:07 -07002219void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002220 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002221
Romain Guy01d58e42011-01-19 21:54:02 -08002222 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07002223
Romain Guyfb8b7632010-08-23 21:05:08 -07002224 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07002225 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002226 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002227
Romain Guy8b55f372010-08-18 17:10:07 -07002228 const float x = texture->left - texture->offset;
2229 const float y = texture->top - texture->offset;
2230
Romain Guy01d58e42011-01-19 21:54:02 -08002231 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002232}
2233
Romain Guyada830f2011-01-13 12:13:20 -08002234void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2235 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002236 return;
2237 }
2238
2239 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08002240
2241 int alpha;
2242 SkXfermode::Mode mode;
2243 getAlphaAndMode(paint, &alpha, &mode);
2244
Romain Guy9ace8f52011-07-07 20:50:11 -07002245 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002246
Romain Guyf219da52011-01-16 12:54:25 -08002247#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08002248 if (!layer->region.isEmpty()) {
2249 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002250 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002251 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002252 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002253 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002254
Romain Guyc88e3572011-01-22 00:32:12 -08002255 setupDraw();
2256 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002257 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002258 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002259 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002260 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002261 setupDrawPureColorUniforms();
2262 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002263 setupDrawTexture(layer->getTexture());
2264 if (mSnapshot->transform->isPureTranslate()) {
2265 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2266 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2267
Romain Guyd21b6e12011-11-30 20:21:23 -08002268 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002269 setupDrawModelViewTranslate(x, y,
2270 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2271 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002272 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002273 setupDrawModelViewTranslate(x, y,
2274 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2275 }
Romain Guyc88e3572011-01-22 00:32:12 -08002276 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002277
Romain Guyc88e3572011-01-22 00:32:12 -08002278 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2279 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002280
Romain Guyc88e3572011-01-22 00:32:12 -08002281 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002282
2283#if DEBUG_LAYERS_AS_REGIONS
2284 drawRegionRects(layer->region);
2285#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002286 }
Romain Guyf219da52011-01-16 12:54:25 -08002287 }
2288#else
Romain Guyada830f2011-01-13 12:13:20 -08002289 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2290 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002291#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002292}
2293
Romain Guy6926c722010-07-12 20:20:03 -07002294///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002295// Shaders
2296///////////////////////////////////////////////////////////////////////////////
2297
2298void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002299 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002300}
2301
Romain Guy06f96e22010-07-30 19:18:16 -07002302void OpenGLRenderer::setupShader(SkiaShader* shader) {
2303 mShader = shader;
2304 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002305 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002306 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002307}
2308
Romain Guyd27977d2010-07-14 19:18:51 -07002309///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002310// Color filters
2311///////////////////////////////////////////////////////////////////////////////
2312
2313void OpenGLRenderer::resetColorFilter() {
2314 mColorFilter = NULL;
2315}
2316
2317void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2318 mColorFilter = filter;
2319}
2320
2321///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002322// Drop shadow
2323///////////////////////////////////////////////////////////////////////////////
2324
2325void OpenGLRenderer::resetShadow() {
2326 mHasShadow = false;
2327}
2328
2329void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2330 mHasShadow = true;
2331 mShadowRadius = radius;
2332 mShadowDx = dx;
2333 mShadowDy = dy;
2334 mShadowColor = color;
2335}
2336
2337///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002338// Drawing implementation
2339///////////////////////////////////////////////////////////////////////////////
2340
Romain Guy01d58e42011-01-19 21:54:02 -08002341void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2342 float x, float y, SkPaint* paint) {
2343 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2344 return;
2345 }
2346
2347 int alpha;
2348 SkXfermode::Mode mode;
2349 getAlphaAndMode(paint, &alpha, &mode);
2350
2351 setupDraw();
2352 setupDrawWithTexture(true);
2353 setupDrawAlpha8Color(paint->getColor(), alpha);
2354 setupDrawColorFilter();
2355 setupDrawShader();
2356 setupDrawBlending(true, mode);
2357 setupDrawProgram();
2358 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2359 setupDrawTexture(texture->id);
2360 setupDrawPureColorUniforms();
2361 setupDrawColorFilterUniforms();
2362 setupDrawShaderUniforms();
2363 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2364
2365 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2366
2367 finishDrawTexture();
2368}
2369
Romain Guyf607bdc2010-09-10 19:20:06 -07002370// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002371#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2372#define kStdUnderline_Offset (1.0f / 9.0f)
2373#define kStdUnderline_Thickness (1.0f / 18.0f)
2374
2375void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2376 float x, float y, SkPaint* paint) {
2377 // Handle underline and strike-through
2378 uint32_t flags = paint->getFlags();
2379 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002380 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002381 float underlineWidth = length;
2382 // If length is > 0.0f, we already measured the text for the text alignment
2383 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002384 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002385 }
2386
2387 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002388 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002389 case SkPaint::kCenter_Align:
2390 offsetX = underlineWidth * 0.5f;
2391 break;
2392 case SkPaint::kRight_Align:
2393 offsetX = underlineWidth;
2394 break;
2395 default:
2396 break;
2397 }
2398
2399 if (underlineWidth > 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002400 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002401 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002402
Romain Guye20ecbd2010-09-22 19:49:04 -07002403 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002404 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002405
Romain Guyf6834472011-01-23 13:32:12 -08002406 int linesCount = 0;
2407 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2408 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2409
2410 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002411 float points[pointsCount];
2412 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002413
2414 if (flags & SkPaint::kUnderlineText_Flag) {
2415 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002416 points[currentPoint++] = left;
2417 points[currentPoint++] = top;
2418 points[currentPoint++] = left + underlineWidth;
2419 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002420 }
2421
2422 if (flags & SkPaint::kStrikeThruText_Flag) {
2423 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002424 points[currentPoint++] = left;
2425 points[currentPoint++] = top;
2426 points[currentPoint++] = left + underlineWidth;
2427 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002428 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002429
Romain Guy726aeba2011-06-01 14:52:00 -07002430 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002431
Romain Guy726aeba2011-06-01 14:52:00 -07002432 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002433 }
2434 }
2435}
2436
Romain Guy026c5e162010-06-28 17:12:22 -07002437void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002438 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002439 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002440 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002441 color |= 0x00ffffff;
2442 }
2443
Romain Guy70ca14e2010-12-13 18:24:33 -08002444 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002445 setupDrawNoTexture();
Romain Guy70ca14e2010-12-13 18:24:33 -08002446 setupDrawColor(color);
2447 setupDrawShader();
2448 setupDrawColorFilter();
2449 setupDrawBlending(mode);
2450 setupDrawProgram();
2451 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2452 setupDrawColorUniforms();
2453 setupDrawShaderUniforms(ignoreTransform);
2454 setupDrawColorFilterUniforms();
2455 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002456
Romain Guyc95c8d62010-09-17 15:31:32 -07002457 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2458}
2459
Romain Guy82ba8142010-07-09 13:25:56 -07002460void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002461 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002462 int alpha;
2463 SkXfermode::Mode mode;
2464 getAlphaAndMode(paint, &alpha, &mode);
2465
Romain Guyd21b6e12011-11-30 20:21:23 -08002466 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002467
Romain Guy6620c6d2010-12-06 18:07:02 -08002468 if (mSnapshot->transform->isPureTranslate()) {
2469 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2470 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2471
Romain Guyd21b6e12011-11-30 20:21:23 -08002472 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002473 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2474 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2475 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2476 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002477 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002478 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2479 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2480 GL_TRIANGLE_STRIP, gMeshCount);
2481 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002482}
2483
Romain Guybd6b79b2010-06-26 00:13:53 -07002484void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002485 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2486 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002487 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002488}
2489
2490void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002491 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002492 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002493 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002494
Romain Guy746b7402010-10-26 16:27:31 -07002495 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002496 setupDrawWithTexture();
2497 setupDrawColor(alpha, alpha, alpha, alpha);
2498 setupDrawColorFilter();
2499 setupDrawBlending(blend, mode, swapSrcDst);
2500 setupDrawProgram();
2501 if (!dirty) {
2502 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002503 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002504 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002505 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002506 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002507 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002508 }
Romain Guy86568192010-12-14 15:55:39 -08002509 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002510 setupDrawColorFilterUniforms();
2511 setupDrawTexture(texture);
2512 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002513
Romain Guy6820ac82010-09-15 18:11:50 -07002514 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002515
2516 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002517}
2518
Romain Guya5aed0d2010-09-09 14:42:43 -07002519void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002520 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002521 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2522 if (blend) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002523 if (mode <= SkXfermode::kScreen_Mode) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002524 if (!mCaches.blend) {
2525 glEnable(GL_BLEND);
2526 }
Romain Guy82ba8142010-07-09 13:25:56 -07002527
Romain Guyf607bdc2010-09-10 19:20:06 -07002528 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2529 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07002530
Romain Guya5aed0d2010-09-09 14:42:43 -07002531 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2532 glBlendFunc(sourceMode, destMode);
2533 mCaches.lastSrcMode = sourceMode;
2534 mCaches.lastDstMode = destMode;
2535 }
2536 } else {
2537 // These blend modes are not supported by OpenGL directly and have
2538 // to be implemented using shaders. Since the shader will perform
2539 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07002540 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002541 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002542 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002543 }
2544
2545 if (mCaches.blend) {
2546 glDisable(GL_BLEND);
2547 }
2548 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07002549 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002550 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002551 glDisable(GL_BLEND);
2552 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002553 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002554}
2555
Romain Guy889f8d12010-07-29 14:37:42 -07002556bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002557 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002558 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002559 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002560 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002561 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002562 }
Romain Guy6926c722010-07-12 20:20:03 -07002563 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002564}
2565
Romain Guy026c5e162010-06-28 17:12:22 -07002566void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002567 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002568 TextureVertex::setUV(v++, u1, v1);
2569 TextureVertex::setUV(v++, u2, v1);
2570 TextureVertex::setUV(v++, u1, v2);
2571 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002572}
2573
Chet Haase5c13d892010-10-08 08:37:55 -07002574void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002575 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002576 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002577
2578 // Skia draws using the color's alpha channel if < 255
2579 // Otherwise, it uses the paint's alpha
2580 int color = paint->getColor();
2581 *alpha = (color >> 24) & 0xFF;
2582 if (*alpha == 255) {
2583 *alpha = paint->getAlpha();
2584 }
2585 } else {
2586 *mode = SkXfermode::kSrcOver_Mode;
2587 *alpha = 255;
2588 }
Romain Guy026c5e162010-06-28 17:12:22 -07002589}
2590
Romain Guya5aed0d2010-09-09 14:42:43 -07002591SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002592 SkXfermode::Mode resultMode;
2593 if (!SkXfermode::AsMode(mode, &resultMode)) {
2594 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002595 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002596 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002597}
2598
Romain Guy9d5316e2010-06-24 19:30:36 -07002599}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002600}; // namespace android