blob: 75c6d0aebf494aec32a5ba35c2f24a4de0199d78 [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 Guyf6a11b82010-06-23 17:47:49 -0700106///////////////////////////////////////////////////////////////////////////////
107// Constructors/destructor
108///////////////////////////////////////////////////////////////////////////////
109
Romain Guyfb8b7632010-08-23 21:05:08 -0700110OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700111 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700112 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700113 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700114
Romain Guyac670c02010-07-27 17:39:27 -0700115 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
116
Romain Guyae5575b2010-07-29 18:48:04 -0700117 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700118}
119
Romain Guy85bf02f2010-06-22 13:11:24 -0700120OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700121 // The context has already been destroyed at this point, do not call
122 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700123}
124
Romain Guyf6a11b82010-06-23 17:47:49 -0700125///////////////////////////////////////////////////////////////////////////////
126// Setup
127///////////////////////////////////////////////////////////////////////////////
128
Romain Guy85bf02f2010-06-22 13:11:24 -0700129void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700130 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700131
132 mWidth = width;
133 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700134
135 mFirstSnapshot->height = height;
136 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700137
Romain Guy3e263fa2011-12-12 16:47:48 -0800138 glDisable(GL_DITHER);
Romain Guy39d252a2011-12-12 18:14:06 -0800139 glEnable(GL_SCISSOR_TEST);
Romain Guy3e263fa2011-12-12 16:47:48 -0800140 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guy15bc6432011-12-13 13:11:32 -0800141
Romain Guy3e263fa2011-12-12 16:47:48 -0800142 glEnableVertexAttribArray(Program::kBindingPosition);
Romain Guye4d01122010-06-16 18:44:05 -0700143}
144
Romain Guy6b7bd242010-10-06 19:49:23 -0700145void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800146 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
147}
148
149void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800150 mCaches.clearGarbage();
151
Romain Guy8aef54f2010-09-01 15:13:49 -0700152 mSnapshot = new Snapshot(mFirstSnapshot,
153 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800154 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700155 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700156
Romain Guy39d252a2011-12-12 18:14:06 -0800157 glViewport(0, 0, mWidth, mHeight);
Romain Guy8f85e802011-12-14 19:23:32 -0800158 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy39d252a2011-12-12 18:14:06 -0800159
Romain Guy7d7b5492011-01-24 16:33:45 -0800160 mSnapshot->setClip(left, top, right, bottom);
Romain Guy39d252a2011-12-12 18:14:06 -0800161 mDirtyClip = false;
Romain Guy7d7b5492011-01-24 16:33:45 -0800162
Romain Guy6b7bd242010-10-06 19:49:23 -0700163 if (!opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700164 glClear(GL_COLOR_BUFFER_BIT);
165 }
Romain Guybb9524b2010-06-22 18:56:38 -0700166}
167
Romain Guyb025b9c2010-09-16 14:16:48 -0700168void OpenGLRenderer::finish() {
169#if DEBUG_OPENGL
170 GLenum status = GL_NO_ERROR;
171 while ((status = glGetError()) != GL_NO_ERROR) {
172 LOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800173 switch (status) {
174 case GL_OUT_OF_MEMORY:
175 LOGE(" OpenGLRenderer is out of memory!");
176 break;
177 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700178 }
179#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800180#if DEBUG_MEMORY_USAGE
181 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800182#else
183 if (mCaches.getDebugLevel() & kDebugMemory) {
184 mCaches.dumpMemoryUsage();
185 }
Romain Guyc15008e2010-11-10 11:59:15 -0800186#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700187}
188
Romain Guy6c319ca2011-01-11 14:29:25 -0800189void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700190 if (mCaches.currentProgram) {
191 if (mCaches.currentProgram->isInUse()) {
192 mCaches.currentProgram->remove();
193 mCaches.currentProgram = NULL;
194 }
195 }
Romain Guy50c0f092010-10-19 11:42:22 -0700196 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800197 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800198 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800199 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700200}
201
Romain Guy6c319ca2011-01-11 14:29:25 -0800202void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800203 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
204
205 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy3e263fa2011-12-12 16:47:48 -0800206 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
207
Romain Guyda8532c2010-08-31 11:50:35 -0700208 glEnable(GL_SCISSOR_TEST);
Romain Guy746b7402010-10-26 16:27:31 -0700209 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700210
Romain Guya1d3c912011-12-13 14:55:06 -0800211 mCaches.activeTexture(0);
Chet Haase08837c22011-11-28 11:53:21 -0800212 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guyf607bdc2010-09-10 19:20:06 -0700213
Romain Guy50c0f092010-10-19 11:42:22 -0700214 mCaches.blend = true;
215 glEnable(GL_BLEND);
216 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
217 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700218}
219
Romain Guycabfcc12011-03-07 18:06:46 -0800220bool OpenGLRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800221 interrupt();
Romain Guyf90f8172011-01-25 22:53:24 -0800222 if (mDirtyClip) {
223 setScissorFromClip();
224 }
Romain Guyd643bb52011-03-01 14:55:21 -0800225
Romain Guy80911b82011-03-16 15:30:12 -0700226 Rect clip(*mSnapshot->clipRect);
227 clip.snapToPixelBoundaries();
228
Romain Guyd643bb52011-03-01 14:55:21 -0800229#if RENDER_LAYERS_AS_REGIONS
230 // Since we don't know what the functor will draw, let's dirty
231 // tne entire clip region
232 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800233 dirtyLayerUnchecked(clip, getRegion());
234 }
235#endif
236
Romain Guy08aa2cb2011-03-17 11:06:57 -0700237 DrawGlInfo info;
238 info.clipLeft = clip.left;
239 info.clipTop = clip.top;
240 info.clipRight = clip.right;
241 info.clipBottom = clip.bottom;
242 info.isLayer = hasLayer();
243 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700244
Romain Guy08aa2cb2011-03-17 11:06:57 -0700245 status_t result = (*functor)(0, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800246
247 if (result != 0) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700248 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800249 dirty.unionWith(localDirty);
250 }
251
Chet Haasedaf98e92011-01-10 14:10:36 -0800252 resume();
Romain Guycabfcc12011-03-07 18:06:46 -0800253 return result != 0;
Chet Haasedaf98e92011-01-10 14:10:36 -0800254}
255
Romain Guyf6a11b82010-06-23 17:47:49 -0700256///////////////////////////////////////////////////////////////////////////////
257// State management
258///////////////////////////////////////////////////////////////////////////////
259
Romain Guybb9524b2010-06-22 18:56:38 -0700260int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700261 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700262}
263
264int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700265 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700266}
267
268void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700269 if (mSaveCount > 1) {
270 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700271 }
Romain Guybb9524b2010-06-22 18:56:38 -0700272}
273
274void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700275 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700276
Romain Guy8fb95422010-08-17 18:38:51 -0700277 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700278 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700279 }
Romain Guybb9524b2010-06-22 18:56:38 -0700280}
281
Romain Guy8aef54f2010-09-01 15:13:49 -0700282int OpenGLRenderer::saveSnapshot(int flags) {
283 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700284 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700285}
286
287bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700288 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700289 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700290 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700291
Romain Guybd6b79b2010-06-26 00:13:53 -0700292 sp<Snapshot> current = mSnapshot;
293 sp<Snapshot> previous = mSnapshot->previous;
294
Romain Guyeb993562010-10-05 18:14:38 -0700295 if (restoreOrtho) {
296 Rect& r = previous->viewport;
297 glViewport(r.left, r.top, r.right, r.bottom);
298 mOrthoMatrix.load(current->orthoMatrix);
299 }
300
Romain Guy8b55f372010-08-18 17:10:07 -0700301 mSaveCount--;
302 mSnapshot = previous;
303
Romain Guy2542d192010-08-18 11:47:12 -0700304 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700305 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700306 }
Romain Guy2542d192010-08-18 11:47:12 -0700307
Romain Guy5ec99242010-11-03 16:19:08 -0700308 if (restoreLayer) {
309 composeLayer(current, previous);
310 }
311
Romain Guy2542d192010-08-18 11:47:12 -0700312 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700313}
314
Romain Guyf6a11b82010-06-23 17:47:49 -0700315///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700316// Layers
317///////////////////////////////////////////////////////////////////////////////
318
319int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700320 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700321 const GLuint previousFbo = mSnapshot->fbo;
322 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700323
Romain Guyaf636eb2010-12-09 17:47:21 -0800324 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700325 int alpha = 255;
326 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700327
Romain Guye45362c2010-11-03 19:58:32 -0700328 if (p) {
329 alpha = p->getAlpha();
330 if (!mCaches.extensions.hasFramebufferFetch()) {
331 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
332 if (!isMode) {
333 // Assume SRC_OVER
334 mode = SkXfermode::kSrcOver_Mode;
335 }
336 } else {
337 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700338 }
339 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700340 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700341 }
Romain Guyd55a8612010-06-28 17:42:46 -0700342
Romain Guydbc26d22010-10-11 17:58:29 -0700343 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
344 }
Romain Guyd55a8612010-06-28 17:42:46 -0700345
346 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700347}
348
349int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
350 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700351 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700352 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700353 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700354 SkPaint paint;
355 paint.setAlpha(alpha);
356 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700357 }
Romain Guyd55a8612010-06-28 17:42:46 -0700358}
Romain Guybd6b79b2010-06-26 00:13:53 -0700359
Romain Guy1c740bc2010-09-13 18:00:09 -0700360/**
361 * Layers are viewed by Skia are slightly different than layers in image editing
362 * programs (for instance.) When a layer is created, previously created layers
363 * and the frame buffer still receive every drawing command. For instance, if a
364 * layer is created and a shape intersecting the bounds of the layers and the
365 * framebuffer is draw, the shape will be drawn on both (unless the layer was
366 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
367 *
368 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
369 * texture. Unfortunately, this is inefficient as it requires every primitive to
370 * be drawn n + 1 times, where n is the number of active layers. In practice this
371 * means, for every primitive:
372 * - Switch active frame buffer
373 * - Change viewport, clip and projection matrix
374 * - Issue the drawing
375 *
376 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700377 * To avoid this, layers are implemented in a different way here, at least in the
378 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
379 * is set. When this flag is set we can redirect all drawing operations into a
380 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700381 *
382 * This implementation relies on the frame buffer being at least RGBA 8888. When
383 * a layer is created, only a texture is created, not an FBO. The content of the
384 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700385 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700386 * buffer and drawing continues as normal. This technique therefore treats the
387 * frame buffer as a scratch buffer for the layers.
388 *
389 * To compose the layers back onto the frame buffer, each layer texture
390 * (containing the original frame buffer data) is drawn as a simple quad over
391 * the frame buffer. The trick is that the quad is set as the composition
392 * destination in the blending equation, and the frame buffer becomes the source
393 * of the composition.
394 *
395 * Drawing layers with an alpha value requires an extra step before composition.
396 * An empty quad is drawn over the layer's region in the frame buffer. This quad
397 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
398 * quad is used to multiply the colors in the frame buffer. This is achieved by
399 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
400 * GL_ZERO, GL_SRC_ALPHA.
401 *
402 * Because glCopyTexImage2D() can be slow, an alternative implementation might
403 * be use to draw a single clipped layer. The implementation described above
404 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700405 *
406 * (1) The frame buffer is actually not cleared right away. To allow the GPU
407 * to potentially optimize series of calls to glCopyTexImage2D, the frame
408 * buffer is left untouched until the first drawing operation. Only when
409 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700410 */
Romain Guyd55a8612010-06-28 17:42:46 -0700411bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700412 float right, float bottom, int alpha, SkXfermode::Mode mode,
413 int flags, GLuint previousFbo) {
414 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700415 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700416
Romain Guyeb993562010-10-05 18:14:38 -0700417 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
418
Romain Guyf607bdc2010-09-10 19:20:06 -0700419 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700420 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700421 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700422 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700423
Romain Guyeb993562010-10-05 18:14:38 -0700424 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700425 if (bounds.intersect(*snapshot->clipRect)) {
426 // We cannot work with sub-pixels in this case
427 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700428
Romain Guyad37cd32011-03-15 11:12:25 -0700429 // When the layer is not an FBO, we may use glCopyTexImage so we
430 // need to make sure the layer does not extend outside the bounds
431 // of the framebuffer
432 if (!bounds.intersect(snapshot->previous->viewport)) {
433 bounds.setEmpty();
434 }
435 } else {
436 bounds.setEmpty();
437 }
Romain Guyeb993562010-10-05 18:14:38 -0700438 }
Romain Guybf434112010-09-16 14:40:17 -0700439
Romain Guy746b7402010-10-26 16:27:31 -0700440 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
441 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800442 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700443 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700444 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700445 }
446
447 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800448 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700449 return false;
450 }
Romain Guyf18fd992010-07-08 11:45:51 -0700451
Romain Guya1d3c912011-12-13 14:55:06 -0800452 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700453 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700454 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700455 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700456 }
457
Romain Guy9ace8f52011-07-07 20:50:11 -0700458 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700459 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700460 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
461 bounds.getWidth() / float(layer->getWidth()), 0.0f);
462 layer->setColorFilter(mColorFilter);
Romain Guydda57022010-07-06 11:39:32 -0700463
Romain Guy8fb95422010-08-17 18:38:51 -0700464 // Save the layer in the snapshot
465 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700466 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700467
Romain Guyeb993562010-10-05 18:14:38 -0700468 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700469 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700470 } else {
471 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700472 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800473 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700474 if (layer->isEmpty()) {
475 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
476 bounds.left, snapshot->height - bounds.bottom,
477 layer->getWidth(), layer->getHeight(), 0);
478 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800479 } else {
480 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
481 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
482 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700483
Romain Guy54be1cd2011-06-13 19:04:27 -0700484 // Enqueue the buffer coordinates to clear the corresponding region later
485 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700486 }
Romain Guyeb993562010-10-05 18:14:38 -0700487 }
Romain Guyf86ef572010-07-01 11:05:42 -0700488
Romain Guyd55a8612010-06-28 17:42:46 -0700489 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700490}
491
Romain Guy5b3b3522010-10-27 18:57:51 -0700492bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
493 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700494 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700495
496#if RENDER_LAYERS_AS_REGIONS
497 snapshot->region = &snapshot->layer->region;
498 snapshot->flags |= Snapshot::kFlagFboTarget;
499#endif
500
501 Rect clip(bounds);
502 snapshot->transform->mapRect(clip);
503 clip.intersect(*snapshot->clipRect);
504 clip.snapToPixelBoundaries();
505 clip.intersect(snapshot->previous->viewport);
506
507 mat4 inverse;
508 inverse.loadInverse(*mSnapshot->transform);
509
510 inverse.mapRect(clip);
511 clip.snapToPixelBoundaries();
512 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700513 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700514
515 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700516 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700517 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700518 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
519 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
520 snapshot->height = bounds.getHeight();
521 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
522 snapshot->orthoMatrix.load(mOrthoMatrix);
523
524 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700525 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
526 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700527
528 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700529 if (layer->isEmpty()) {
530 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
531 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700532 }
533
534 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700535 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700536
537#if DEBUG_LAYERS_AS_REGIONS
538 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
539 if (status != GL_FRAMEBUFFER_COMPLETE) {
540 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
541
542 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700543 layer->deleteTexture();
544 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700545
546 delete layer;
547
548 return false;
549 }
550#endif
551
552 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy8f85e802011-12-14 19:23:32 -0800553 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700554 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700555 glClear(GL_COLOR_BUFFER_BIT);
556
557 dirtyClip();
558
559 // Change the ortho projection
560 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
561 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
562
563 return true;
564}
565
Romain Guy1c740bc2010-09-13 18:00:09 -0700566/**
567 * Read the documentation of createLayer() before doing anything in this method.
568 */
Romain Guy1d83e192010-08-17 11:37:00 -0700569void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
570 if (!current->layer) {
571 LOGE("Attempting to compose a layer that does not exist");
572 return;
573 }
574
Romain Guy5b3b3522010-10-27 18:57:51 -0700575 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700576
577 if (fboLayer) {
578 // Unbind current FBO and restore previous one
579 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
580 }
581
Romain Guy1d83e192010-08-17 11:37:00 -0700582 Layer* layer = current->layer;
583 const Rect& rect = layer->layer;
584
Romain Guy9ace8f52011-07-07 20:50:11 -0700585 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700586 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700587 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700588 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700589 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700590 }
591
Romain Guy03750a02010-10-18 14:06:08 -0700592 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700593
Romain Guya1d3c912011-12-13 14:55:06 -0800594 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700595
Romain Guy5b3b3522010-10-27 18:57:51 -0700596 // When the layer is stored in an FBO, we can save a bit of fillrate by
597 // drawing only the dirty region
598 if (fboLayer) {
599 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700600 if (layer->getColorFilter()) {
601 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800602 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700603 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700604 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800605 resetColorFilter();
606 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700607 } else if (!rect.isEmpty()) {
608 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
609 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700610 }
Romain Guy8b55f372010-08-18 17:10:07 -0700611
Romain Guyeb993562010-10-05 18:14:38 -0700612 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800613 // Note: No need to use glDiscardFramebufferEXT() since we never
614 // create/compose layers that are not on screen with this
615 // code path
616 // See LayerRenderer::destroyLayer(Layer*)
617
Romain Guyeb993562010-10-05 18:14:38 -0700618 // Detach the texture from the FBO
619 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
620 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
621 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
622
623 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
624 mCaches.fboCache.put(current->fbo);
625 }
626
Romain Guy746b7402010-10-26 16:27:31 -0700627 dirtyClip();
628
Romain Guyeb993562010-10-05 18:14:38 -0700629 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700630 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700631 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700632 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700633 delete layer;
634 }
635}
636
Romain Guyaa6c24c2011-04-28 18:40:04 -0700637void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700638 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700639
Romain Guy302a9df2011-08-16 13:55:02 -0700640 mat4& transform = layer->getTransform();
641 if (!transform.isIdentity()) {
642 save(0);
643 mSnapshot->transform->multiply(transform);
644 }
645
Romain Guyaa6c24c2011-04-28 18:40:04 -0700646 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700647 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700648 setupDrawWithTexture();
649 } else {
650 setupDrawWithExternalTexture();
651 }
652 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700653 setupDrawColor(alpha, alpha, alpha, alpha);
654 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700655 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700656 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700657 setupDrawPureColorUniforms();
658 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700659 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
660 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700661 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700662 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700663 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700664 if (mSnapshot->transform->isPureTranslate() &&
665 layer->getWidth() == (uint32_t) rect.getWidth() &&
666 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700667 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
668 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
669
Romain Guyd21b6e12011-11-30 20:21:23 -0800670 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700671 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
672 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800673 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700674 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
675 }
676 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700677 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
678
679 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
680
681 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700682
683 if (!transform.isIdentity()) {
684 restore();
685 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700686}
687
Romain Guy5b3b3522010-10-27 18:57:51 -0700688void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700689 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700690 const Rect& texCoords = layer->texCoords;
691 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
692 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700693
Romain Guy9ace8f52011-07-07 20:50:11 -0700694 float x = rect.left;
695 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700696 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700697 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700698 layer->getHeight() == (uint32_t) rect.getHeight();
699
700 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700701 // When we're swapping, the layer is already in screen coordinates
702 if (!swap) {
703 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
704 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
705 }
706
Romain Guyd21b6e12011-11-30 20:21:23 -0800707 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700708 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800709 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700710 }
711
712 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
713 layer->getTexture(), layer->getAlpha() / 255.0f,
714 layer->getMode(), layer->isBlend(),
715 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
716 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700717
Romain Guyaa6c24c2011-04-28 18:40:04 -0700718 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
719 } else {
720 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
721 drawTextureLayer(layer, rect);
722 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
723 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700724}
725
726void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
727#if RENDER_LAYERS_AS_REGIONS
728 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700729 layer->setRegionAsRect();
730
Romain Guy40667672011-03-18 14:34:03 -0700731 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700732
Romain Guy5b3b3522010-10-27 18:57:51 -0700733 layer->region.clear();
734 return;
735 }
736
Romain Guy8a3957d2011-09-07 17:55:15 -0700737 // TODO: See LayerRenderer.cpp::generateMesh() for important
738 // information about this implementation
Romain Guy5b3b3522010-10-27 18:57:51 -0700739 if (!layer->region.isEmpty()) {
740 size_t count;
741 const android::Rect* rects = layer->region.getArray(&count);
742
Romain Guy9ace8f52011-07-07 20:50:11 -0700743 const float alpha = layer->getAlpha() / 255.0f;
744 const float texX = 1.0f / float(layer->getWidth());
745 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800746 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700747
748 TextureVertex* mesh = mCaches.getRegionMesh();
749 GLsizei numQuads = 0;
750
Romain Guy7230a742011-01-10 22:26:16 -0800751 setupDraw();
752 setupDrawWithTexture();
753 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800754 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700755 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800756 setupDrawProgram();
757 setupDrawDirtyRegionsDisabled();
758 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800759 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700760 setupDrawTexture(layer->getTexture());
761 if (mSnapshot->transform->isPureTranslate()) {
762 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
763 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
764
Romain Guyd21b6e12011-11-30 20:21:23 -0800765 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700766 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
767 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800768 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700769 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
770 }
Romain Guy15bc6432011-12-13 13:11:32 -0800771 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700772
773 for (size_t i = 0; i < count; i++) {
774 const android::Rect* r = &rects[i];
775
776 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800777 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700778 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800779 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700780
781 // TODO: Reject quads outside of the clip
782 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
783 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
784 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
785 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
786
787 numQuads++;
788
789 if (numQuads >= REGION_MESH_QUAD_COUNT) {
790 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
791 numQuads = 0;
792 mesh = mCaches.getRegionMesh();
793 }
794 }
795
796 if (numQuads > 0) {
797 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
798 }
799
Romain Guy7230a742011-01-10 22:26:16 -0800800 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700801
802#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800803 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700804#endif
805
806 layer->region.clear();
807 }
808#else
809 composeLayerRect(layer, rect);
810#endif
811}
812
Romain Guy3a3133d2011-02-01 22:59:58 -0800813void OpenGLRenderer::drawRegionRects(const Region& region) {
814#if DEBUG_LAYERS_AS_REGIONS
815 size_t count;
816 const android::Rect* rects = region.getArray(&count);
817
818 uint32_t colors[] = {
819 0x7fff0000, 0x7f00ff00,
820 0x7f0000ff, 0x7fff00ff,
821 };
822
823 int offset = 0;
824 int32_t top = rects[0].top;
825
826 for (size_t i = 0; i < count; i++) {
827 if (top != rects[i].top) {
828 offset ^= 0x2;
829 top = rects[i].top;
830 }
831
832 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
833 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
834 SkXfermode::kSrcOver_Mode);
835 }
836#endif
837}
838
Romain Guy5b3b3522010-10-27 18:57:51 -0700839void OpenGLRenderer::dirtyLayer(const float left, const float top,
840 const float right, const float bottom, const mat4 transform) {
841#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800842 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700843 Rect bounds(left, top, right, bottom);
844 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800845 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700846 }
847#endif
848}
849
850void OpenGLRenderer::dirtyLayer(const float left, const float top,
851 const float right, const float bottom) {
852#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800853 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700854 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800855 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800856 }
857#endif
858}
859
860void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
861#if RENDER_LAYERS_AS_REGIONS
862 if (bounds.intersect(*mSnapshot->clipRect)) {
863 bounds.snapToPixelBoundaries();
864 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
865 if (!dirty.isEmpty()) {
866 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700867 }
868 }
869#endif
870}
871
Romain Guy54be1cd2011-06-13 19:04:27 -0700872void OpenGLRenderer::clearLayerRegions() {
873 const size_t count = mLayers.size();
874 if (count == 0) return;
875
876 if (!mSnapshot->isIgnored()) {
877 // Doing several glScissor/glClear here can negatively impact
878 // GPUs with a tiler architecture, instead we draw quads with
879 // the Clear blending mode
880
881 // The list contains bounds that have already been clipped
882 // against their initial clip rect, and the current clip
883 // is likely different so we need to disable clipping here
884 glDisable(GL_SCISSOR_TEST);
885
886 Vertex mesh[count * 6];
887 Vertex* vertex = mesh;
888
889 for (uint32_t i = 0; i < count; i++) {
890 Rect* bounds = mLayers.itemAt(i);
891
892 Vertex::set(vertex++, bounds->left, bounds->bottom);
893 Vertex::set(vertex++, bounds->left, bounds->top);
894 Vertex::set(vertex++, bounds->right, bounds->top);
895 Vertex::set(vertex++, bounds->left, bounds->bottom);
896 Vertex::set(vertex++, bounds->right, bounds->top);
897 Vertex::set(vertex++, bounds->right, bounds->bottom);
898
899 delete bounds;
900 }
901
902 setupDraw(false);
903 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
904 setupDrawBlending(true, SkXfermode::kClear_Mode);
905 setupDrawProgram();
906 setupDrawPureColorUniforms();
907 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -0800908 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -0700909
Romain Guy54be1cd2011-06-13 19:04:27 -0700910 glDrawArrays(GL_TRIANGLES, 0, count * 6);
911
912 glEnable(GL_SCISSOR_TEST);
913 } else {
914 for (uint32_t i = 0; i < count; i++) {
915 delete mLayers.itemAt(i);
916 }
917 }
918
919 mLayers.clear();
920}
921
Romain Guybd6b79b2010-06-26 00:13:53 -0700922///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700923// Transforms
924///////////////////////////////////////////////////////////////////////////////
925
926void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700927 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700928}
929
930void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700931 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700932}
933
934void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700935 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700936}
937
Romain Guy807daf72011-01-18 11:19:19 -0800938void OpenGLRenderer::skew(float sx, float sy) {
939 mSnapshot->transform->skew(sx, sy);
940}
941
Romain Guyf6a11b82010-06-23 17:47:49 -0700942void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -0700943 if (matrix) {
944 mSnapshot->transform->load(*matrix);
945 } else {
946 mSnapshot->transform->loadIdentity();
947 }
Romain Guyf6a11b82010-06-23 17:47:49 -0700948}
949
950void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700951 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700952}
953
954void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700955 SkMatrix transform;
956 mSnapshot->transform->copyTo(transform);
957 transform.preConcat(*matrix);
958 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700959}
960
961///////////////////////////////////////////////////////////////////////////////
962// Clipping
963///////////////////////////////////////////////////////////////////////////////
964
Romain Guybb9524b2010-06-22 18:56:38 -0700965void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700966 Rect clip(*mSnapshot->clipRect);
967 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -0800968
969 mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
970 clip.getWidth(), clip.getHeight());
971
Romain Guy746b7402010-10-26 16:27:31 -0700972 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700973}
974
975const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700976 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700977}
978
Romain Guyc7d53492010-06-25 13:41:57 -0700979bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800980 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700981 return true;
982 }
983
Romain Guy1d83e192010-08-17 11:37:00 -0700984 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700985 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700986 r.snapToPixelBoundaries();
987
988 Rect clipRect(*mSnapshot->clipRect);
989 clipRect.snapToPixelBoundaries();
990
991 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700992}
993
Romain Guy079ba2c2010-07-16 14:12:24 -0700994bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
995 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700996 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700997 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700998 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700999 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001000}
1001
Romain Guyf6a11b82010-06-23 17:47:49 -07001002///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001003// Drawing commands
1004///////////////////////////////////////////////////////////////////////////////
1005
Romain Guy54be1cd2011-06-13 19:04:27 -07001006void OpenGLRenderer::setupDraw(bool clear) {
1007 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001008 if (mDirtyClip) {
1009 setScissorFromClip();
1010 }
1011 mDescription.reset();
1012 mSetShaderColor = false;
1013 mColorSet = false;
1014 mColorA = mColorR = mColorG = mColorB = 0.0f;
1015 mTextureUnit = 0;
1016 mTrackDirtyRegions = true;
1017}
1018
1019void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1020 mDescription.hasTexture = true;
1021 mDescription.hasAlpha8Texture = isAlpha8;
1022}
1023
Romain Guyaa6c24c2011-04-28 18:40:04 -07001024void OpenGLRenderer::setupDrawWithExternalTexture() {
1025 mDescription.hasExternalTexture = true;
1026}
1027
Romain Guy15bc6432011-12-13 13:11:32 -08001028void OpenGLRenderer::setupDrawNoTexture() {
1029 mCaches.disbaleTexCoordsVertexArray();
1030}
1031
Chet Haase5b0200b2011-04-13 17:58:08 -07001032void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001033 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001034}
1035
Romain Guyed6fcb02011-03-21 13:11:28 -07001036void OpenGLRenderer::setupDrawPoint(float pointSize) {
1037 mDescription.isPoint = true;
1038 mDescription.pointSize = pointSize;
1039}
1040
Romain Guy70ca14e2010-12-13 18:24:33 -08001041void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001042 setupDrawColor(color, (color >> 24) & 0xFF);
1043}
1044
1045void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1046 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001047 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1048 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001049 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001050 mColorR = a * ((color >> 16) & 0xFF);
1051 mColorG = a * ((color >> 8) & 0xFF);
1052 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001053 mColorSet = true;
1054 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1055}
1056
Romain Guy86568192010-12-14 15:55:39 -08001057void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1058 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001059 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1060 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001061 const float a = mColorA / 255.0f;
1062 mColorR = a * ((color >> 16) & 0xFF);
1063 mColorG = a * ((color >> 8) & 0xFF);
1064 mColorB = a * ((color ) & 0xFF);
1065 mColorSet = true;
1066 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1067}
1068
Romain Guy70ca14e2010-12-13 18:24:33 -08001069void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1070 mColorA = a;
1071 mColorR = r;
1072 mColorG = g;
1073 mColorB = b;
1074 mColorSet = true;
1075 mSetShaderColor = mDescription.setColor(r, g, b, a);
1076}
1077
Romain Guy86568192010-12-14 15:55:39 -08001078void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1079 mColorA = a;
1080 mColorR = r;
1081 mColorG = g;
1082 mColorB = b;
1083 mColorSet = true;
1084 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1085}
1086
Romain Guy70ca14e2010-12-13 18:24:33 -08001087void OpenGLRenderer::setupDrawShader() {
1088 if (mShader) {
1089 mShader->describe(mDescription, mCaches.extensions);
1090 }
1091}
1092
1093void OpenGLRenderer::setupDrawColorFilter() {
1094 if (mColorFilter) {
1095 mColorFilter->describe(mDescription, mCaches.extensions);
1096 }
1097}
1098
Romain Guyf09ef512011-05-27 11:43:46 -07001099void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1100 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1101 mColorA = 1.0f;
1102 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001103 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001104 }
1105}
1106
Romain Guy70ca14e2010-12-13 18:24:33 -08001107void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001108 // When the blending mode is kClear_Mode, we need to use a modulate color
1109 // argb=1,0,0,0
1110 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001111 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1112 mDescription, swapSrcDst);
1113}
1114
1115void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001116 // When the blending mode is kClear_Mode, we need to use a modulate color
1117 // argb=1,0,0,0
1118 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001119 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1120 mDescription, swapSrcDst);
1121}
1122
1123void OpenGLRenderer::setupDrawProgram() {
1124 useProgram(mCaches.programCache.get(mDescription));
1125}
1126
1127void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1128 mTrackDirtyRegions = false;
1129}
1130
1131void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1132 bool ignoreTransform) {
1133 mModelView.loadTranslate(left, top, 0.0f);
1134 if (!ignoreTransform) {
1135 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1136 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1137 } else {
1138 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1139 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1140 }
1141}
1142
Chet Haase8a5cc922011-04-26 07:28:09 -07001143void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1144 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001145}
1146
Romain Guy70ca14e2010-12-13 18:24:33 -08001147void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1148 bool ignoreTransform, bool ignoreModelView) {
1149 if (!ignoreModelView) {
1150 mModelView.loadTranslate(left, top, 0.0f);
1151 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001152 } else {
1153 mModelView.loadIdentity();
1154 }
Romain Guy86568192010-12-14 15:55:39 -08001155 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1156 if (!ignoreTransform) {
1157 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1158 if (mTrackDirtyRegions && dirty) {
1159 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1160 }
1161 } else {
1162 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1163 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1164 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001165}
1166
Romain Guyed6fcb02011-03-21 13:11:28 -07001167void OpenGLRenderer::setupDrawPointUniforms() {
1168 int slot = mCaches.currentProgram->getUniform("pointSize");
1169 glUniform1f(slot, mDescription.pointSize);
1170}
1171
Romain Guy70ca14e2010-12-13 18:24:33 -08001172void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001173 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001174 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1175 }
1176}
1177
Romain Guy86568192010-12-14 15:55:39 -08001178void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001179 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001180 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001181 }
1182}
1183
Romain Guy70ca14e2010-12-13 18:24:33 -08001184void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1185 if (mShader) {
1186 if (ignoreTransform) {
1187 mModelView.loadInverse(*mSnapshot->transform);
1188 }
1189 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1190 }
1191}
1192
Romain Guy8d0d4782010-12-14 20:13:35 -08001193void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1194 if (mShader) {
1195 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1196 }
1197}
1198
Romain Guy70ca14e2010-12-13 18:24:33 -08001199void OpenGLRenderer::setupDrawColorFilterUniforms() {
1200 if (mColorFilter) {
1201 mColorFilter->setupProgram(mCaches.currentProgram);
1202 }
1203}
1204
1205void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001206 bool force = mCaches.bindMeshBuffer();
1207 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001208 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001209}
1210
1211void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1212 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001213 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001214 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001215}
1216
Romain Guyaa6c24c2011-04-28 18:40:04 -07001217void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1218 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001219 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001220 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001221}
1222
Romain Guy8f0095c2011-05-02 17:24:22 -07001223void OpenGLRenderer::setupDrawTextureTransform() {
1224 mDescription.hasTextureTransform = true;
1225}
1226
1227void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001228 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1229 GL_FALSE, &transform.data[0]);
1230}
1231
Romain Guy70ca14e2010-12-13 18:24:33 -08001232void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001233 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001234 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001235 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001236 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001237 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001238 }
Romain Guyd71dd362011-12-12 19:03:35 -08001239
Romain Guyf3a910b42011-12-12 20:35:21 -08001240 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001241 if (mCaches.currentProgram->texCoords >= 0) {
1242 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1243 }
1244
1245 mCaches.unbindIndicesBuffer();
1246}
1247
1248void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1249 bool force = mCaches.unbindMeshBuffer();
1250 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1251 if (mCaches.currentProgram->texCoords >= 0) {
1252 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001253 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001254}
1255
Chet Haase5b0200b2011-04-13 17:58:08 -07001256void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001257 bool force = mCaches.unbindMeshBuffer();
1258 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1259 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001260 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001261}
1262
1263/**
1264 * 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 -07001265 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1266 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1267 * attributes (one per vertex) are values from zero to one that tells the fragment
1268 * shader where the fragment is in relation to the line width/length overall; these values are
1269 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1270 * region of the line.
1271 * Note that we only pass down the width values in this setup function. The length coordinates
1272 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001273 */
Chet Haase99585ad2011-05-02 15:00:16 -07001274void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Chet Haase99ecdc42011-05-06 12:06:34 -07001275 GLvoid* lengthCoords, float boundaryWidthProportion) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001276 bool force = mCaches.unbindMeshBuffer();
1277 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1278 vertices, gAAVertexStride);
1279 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001280 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001281
Chet Haase99585ad2011-05-02 15:00:16 -07001282 int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
1283 glEnableVertexAttribArray(widthSlot);
1284 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001285
Chet Haase99585ad2011-05-02 15:00:16 -07001286 int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
1287 glEnableVertexAttribArray(lengthSlot);
1288 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001289
Chet Haase5b0200b2011-04-13 17:58:08 -07001290 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001291 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001292
Chet Haase99585ad2011-05-02 15:00:16 -07001293 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001294 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001295 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
Chet Haase5b0200b2011-04-13 17:58:08 -07001296}
1297
Romain Guy70ca14e2010-12-13 18:24:33 -08001298void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001299}
1300
1301///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001302// Drawing
1303///////////////////////////////////////////////////////////////////////////////
1304
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001305bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
1306 Rect& dirty, uint32_t level) {
1307 if (quickReject(0.0f, 0.0f, width, height)) {
1308 return false;
1309 }
1310
Romain Guy0fe478e2010-11-08 12:08:41 -08001311 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1312 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001313 if (displayList && displayList->isRenderable()) {
Romain Guycabfcc12011-03-07 18:06:46 -08001314 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001315 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001316
Chet Haasedaf98e92011-01-10 14:10:36 -08001317 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001318}
1319
Chet Haaseed30fd82011-04-22 16:18:45 -07001320void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1321 if (displayList) {
1322 displayList->output(*this, level);
1323 }
1324}
1325
Romain Guya168d732011-03-18 16:50:13 -07001326void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1327 int alpha;
1328 SkXfermode::Mode mode;
1329 getAlphaAndMode(paint, &alpha, &mode);
1330
Romain Guya168d732011-03-18 16:50:13 -07001331 float x = left;
1332 float y = top;
1333
Romain Guye3c26852011-07-25 16:36:01 -07001334 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001335 bool ignoreTransform = false;
1336 if (mSnapshot->transform->isPureTranslate()) {
1337 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1338 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1339 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001340 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001341 } else {
1342 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001343 }
1344
1345 setupDraw();
1346 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001347 if (paint) {
1348 setupDrawAlpha8Color(paint->getColor(), alpha);
1349 }
Romain Guya168d732011-03-18 16:50:13 -07001350 setupDrawColorFilter();
1351 setupDrawShader();
1352 setupDrawBlending(true, mode);
1353 setupDrawProgram();
1354 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001355
Romain Guya168d732011-03-18 16:50:13 -07001356 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001357 texture->setWrap(GL_CLAMP_TO_EDGE);
1358 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001359
Romain Guya168d732011-03-18 16:50:13 -07001360 setupDrawPureColorUniforms();
1361 setupDrawColorFilterUniforms();
1362 setupDrawShaderUniforms();
1363 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1364
1365 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1366
1367 finishDrawTexture();
1368}
1369
Chet Haase5c13d892010-10-08 08:37:55 -07001370void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001371 const float right = left + bitmap->width();
1372 const float bottom = top + bitmap->height();
1373
1374 if (quickReject(left, top, right, bottom)) {
1375 return;
1376 }
1377
Romain Guya1d3c912011-12-13 14:55:06 -08001378 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001379 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001380 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001381 const AutoTexture autoCleanup(texture);
1382
Romain Guya168d732011-03-18 16:50:13 -07001383 if (bitmap->getConfig() == SkBitmap::kA8_Config) {
1384 drawAlphaBitmap(texture, left, top, paint);
1385 } else {
1386 drawTextureRect(left, top, right, bottom, texture, paint);
1387 }
Romain Guyce0537b2010-06-29 21:05:21 -07001388}
1389
Chet Haase5c13d892010-10-08 08:37:55 -07001390void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001391 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1392 const mat4 transform(*matrix);
1393 transform.mapRect(r);
1394
Romain Guy6926c722010-07-12 20:20:03 -07001395 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1396 return;
1397 }
1398
Romain Guya1d3c912011-12-13 14:55:06 -08001399 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001400 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001401 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001402 const AutoTexture autoCleanup(texture);
1403
Romain Guy5b3b3522010-10-27 18:57:51 -07001404 // This could be done in a cheaper way, all we need is pass the matrix
1405 // to the vertex shader. The save/restore is a bit overkill.
1406 save(SkCanvas::kMatrix_SaveFlag);
1407 concatMatrix(matrix);
1408 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1409 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001410}
1411
Romain Guy5a7b4662011-01-20 19:09:30 -08001412void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1413 float* vertices, int* colors, SkPaint* paint) {
1414 // TODO: Do a quickReject
1415 if (!vertices || mSnapshot->isIgnored()) {
1416 return;
1417 }
1418
Romain Guya1d3c912011-12-13 14:55:06 -08001419 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001420 Texture* texture = mCaches.textureCache.get(bitmap);
1421 if (!texture) return;
1422 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001423
Romain Guyd21b6e12011-11-30 20:21:23 -08001424 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1425 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001426
1427 int alpha;
1428 SkXfermode::Mode mode;
1429 getAlphaAndMode(paint, &alpha, &mode);
1430
Romain Guy5a7b4662011-01-20 19:09:30 -08001431 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001432
Romain Guyb18d2d02011-02-10 15:52:54 -08001433 float left = FLT_MAX;
1434 float top = FLT_MAX;
1435 float right = FLT_MIN;
1436 float bottom = FLT_MIN;
1437
1438#if RENDER_LAYERS_AS_REGIONS
1439 bool hasActiveLayer = hasLayer();
1440#else
1441 bool hasActiveLayer = false;
1442#endif
1443
Romain Guya566b7c2011-01-23 16:36:11 -08001444 // TODO: Support the colors array
1445 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001446 TextureVertex* vertex = mesh;
1447 for (int32_t y = 0; y < meshHeight; y++) {
1448 for (int32_t x = 0; x < meshWidth; x++) {
1449 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1450
1451 float u1 = float(x) / meshWidth;
1452 float u2 = float(x + 1) / meshWidth;
1453 float v1 = float(y) / meshHeight;
1454 float v2 = float(y + 1) / meshHeight;
1455
1456 int ax = i + (meshWidth + 1) * 2;
1457 int ay = ax + 1;
1458 int bx = i;
1459 int by = bx + 1;
1460 int cx = i + 2;
1461 int cy = cx + 1;
1462 int dx = i + (meshWidth + 1) * 2 + 2;
1463 int dy = dx + 1;
1464
1465 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1466 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1467 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1468
1469 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1470 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1471 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001472
1473#if RENDER_LAYERS_AS_REGIONS
1474 if (hasActiveLayer) {
1475 // TODO: This could be optimized to avoid unnecessary ops
1476 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1477 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1478 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1479 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1480 }
1481#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001482 }
1483 }
1484
Romain Guyb18d2d02011-02-10 15:52:54 -08001485#if RENDER_LAYERS_AS_REGIONS
1486 if (hasActiveLayer) {
1487 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1488 }
1489#endif
1490
Romain Guy5a7b4662011-01-20 19:09:30 -08001491 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1492 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001493 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001494}
1495
Romain Guy8ba548f2010-06-30 19:21:21 -07001496void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1497 float srcLeft, float srcTop, float srcRight, float srcBottom,
1498 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001499 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001500 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1501 return;
1502 }
1503
Romain Guya1d3c912011-12-13 14:55:06 -08001504 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001505 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001506 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001507 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001508
Romain Guy8ba548f2010-06-30 19:21:21 -07001509 const float width = texture->width;
1510 const float height = texture->height;
1511
Romain Guy68169722011-08-22 17:33:33 -07001512 const float u1 = fmax(0.0f, srcLeft / width);
1513 const float v1 = fmax(0.0f, srcTop / height);
1514 const float u2 = fmin(1.0f, srcRight / width);
1515 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001516
Romain Guy03750a02010-10-18 14:06:08 -07001517 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001518 resetDrawTextureTexCoords(u1, v1, u2, v2);
1519
Romain Guy03750a02010-10-18 14:06:08 -07001520 int alpha;
1521 SkXfermode::Mode mode;
1522 getAlphaAndMode(paint, &alpha, &mode);
1523
Romain Guyd21b6e12011-11-30 20:21:23 -08001524 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1525
Romain Guy6620c6d2010-12-06 18:07:02 -08001526 if (mSnapshot->transform->isPureTranslate()) {
1527 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1528 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1529
Romain Guyb5014982011-07-28 15:39:12 -07001530 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001531 // Enable linear filtering if the source rectangle is scaled
1532 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001533 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001534 }
Romain Guyb5014982011-07-28 15:39:12 -07001535
Romain Guyd21b6e12011-11-30 20:21:23 -08001536 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001537 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1538 texture->id, alpha / 255.0f, mode, texture->blend,
1539 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1540 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1541 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001542 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001543 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1544 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1545 GL_TRIANGLE_STRIP, gMeshCount);
1546 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001547
1548 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1549}
1550
Romain Guy4aa90572010-09-26 18:40:37 -07001551void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001552 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001553 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001554 if (quickReject(left, top, right, bottom)) {
1555 return;
1556 }
1557
Romain Guya1d3c912011-12-13 14:55:06 -08001558 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001559 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001560 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001561 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001562 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1563 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001564
1565 int alpha;
1566 SkXfermode::Mode mode;
1567 getAlphaAndMode(paint, &alpha, &mode);
1568
Romain Guy2728f962010-10-08 18:36:15 -07001569 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001570 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001571
Romain Guya5ef39a2010-12-03 16:48:20 -08001572 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001573 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001574#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001575 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001576 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001577 const float offsetX = left + mSnapshot->transform->getTranslateX();
1578 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001579 const size_t count = mesh->quads.size();
1580 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001581 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001582 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001583 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1584 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1585 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001586 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001587 dirtyLayer(left + bounds.left, top + bounds.top,
1588 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001589 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001590 }
1591 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001592#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001593
Romain Guy6620c6d2010-12-06 18:07:02 -08001594 if (pureTranslate) {
1595 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1596 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1597
1598 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1599 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1600 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1601 true, !mesh->hasEmptyQuads);
1602 } else {
1603 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1604 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1605 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1606 true, !mesh->hasEmptyQuads);
1607 }
Romain Guy054dc182010-10-15 17:55:25 -07001608 }
Romain Guyf7f93552010-07-08 19:17:03 -07001609}
1610
Chet Haase99ecdc42011-05-06 12:06:34 -07001611/**
Chet Haase858aa932011-05-12 09:06:00 -07001612 * This function uses a similar approach to that of AA lines in the drawLines() function.
1613 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1614 * shader to compute the translucency of the color, determined by whether a given pixel is
1615 * within that boundary region and how far into the region it is.
1616 */
1617void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001618 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001619 float inverseScaleX = 1.0f;
1620 float inverseScaleY = 1.0f;
1621 // The quad that we use needs to account for scaling.
1622 if (!mSnapshot->transform->isPureTranslate()) {
1623 Matrix4 *mat = mSnapshot->transform;
1624 float m00 = mat->data[Matrix4::kScaleX];
1625 float m01 = mat->data[Matrix4::kSkewY];
1626 float m02 = mat->data[2];
1627 float m10 = mat->data[Matrix4::kSkewX];
1628 float m11 = mat->data[Matrix4::kScaleX];
1629 float m12 = mat->data[6];
1630 float scaleX = sqrt(m00 * m00 + m01 * m01);
1631 float scaleY = sqrt(m10 * m10 + m11 * m11);
1632 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1633 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1634 }
1635
1636 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001637 setupDrawNoTexture();
Chet Haase858aa932011-05-12 09:06:00 -07001638 setupDrawAALine();
1639 setupDrawColor(color);
1640 setupDrawColorFilter();
1641 setupDrawShader();
1642 setupDrawBlending(true, mode);
1643 setupDrawProgram();
1644 setupDrawModelViewIdentity(true);
1645 setupDrawColorUniforms();
1646 setupDrawColorFilterUniforms();
1647 setupDrawShaderIdentityUniforms();
1648
1649 AAVertex rects[4];
1650 AAVertex* aaVertices = &rects[0];
1651 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1652 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1653
1654 float boundarySizeX = .5 * inverseScaleX;
1655 float boundarySizeY = .5 * inverseScaleY;
1656
1657 // Adjust the rect by the AA boundary padding
1658 left -= boundarySizeX;
1659 right += boundarySizeX;
1660 top -= boundarySizeY;
1661 bottom += boundarySizeY;
1662
1663 float width = right - left;
1664 float height = bottom - top;
1665
1666 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1667 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
1668 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
1669 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1670 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1671 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
1672 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryHeightProportion));
1673
1674 if (!quickReject(left, top, right, bottom)) {
1675 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1676 AAVertex::set(aaVertices++, left, top, 1, 0);
1677 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1678 AAVertex::set(aaVertices++, right, top, 0, 0);
1679 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1680 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1681 }
1682}
1683
1684/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001685 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1686 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1687 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1688 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1689 * of the line. Hairlines are more involved because we need to account for transform scaling
1690 * to end up with a one-pixel-wide line in screen space..
1691 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1692 * in combination with values that we calculate and pass down in this method. The basic approach
1693 * is that the quad we create contains both the core line area plus a bounding area in which
1694 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1695 * proportion of the width and the length of a given segment is represented by the boundary
1696 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1697 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1698 * on the inside). This ends up giving the result we want, with pixels that are completely
1699 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1700 * how far into the boundary region they are, which is determined by shader interpolation.
1701 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001702void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1703 if (mSnapshot->isIgnored()) return;
1704
1705 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001706 // We use half the stroke width here because we're going to position the quad
1707 // corner vertices half of the width away from the line endpoints
1708 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001709 // A stroke width of 0 has a special meaning in Skia:
1710 // it draws a line 1 px wide regardless of current transform
1711 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase99ecdc42011-05-06 12:06:34 -07001712 float inverseScaleX = 1.0f;
1713 float inverseScaleY = 1.0f;
1714 bool scaled = false;
Chet Haase8a5cc922011-04-26 07:28:09 -07001715 int alpha;
1716 SkXfermode::Mode mode;
1717 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001718 int verticesCount = count;
1719 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001720 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001721 verticesCount += (count - 4);
1722 }
1723
1724 if (isHairLine || isAA) {
1725 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1726 // the line on the screen should always be one pixel wide regardless of scale. For
1727 // AA lines, we only want one pixel of translucent boundary around the quad.
1728 if (!mSnapshot->transform->isPureTranslate()) {
1729 Matrix4 *mat = mSnapshot->transform;
1730 float m00 = mat->data[Matrix4::kScaleX];
1731 float m01 = mat->data[Matrix4::kSkewY];
1732 float m02 = mat->data[2];
1733 float m10 = mat->data[Matrix4::kSkewX];
1734 float m11 = mat->data[Matrix4::kScaleX];
1735 float m12 = mat->data[6];
1736 float scaleX = sqrt(m00*m00 + m01*m01);
1737 float scaleY = sqrt(m10*m10 + m11*m11);
1738 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1739 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1740 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1741 scaled = true;
1742 }
1743 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001744 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001745
1746 getAlphaAndMode(paint, &alpha, &mode);
1747 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001748 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001749 if (isAA) {
1750 setupDrawAALine();
1751 }
1752 setupDrawColor(paint->getColor(), alpha);
1753 setupDrawColorFilter();
1754 setupDrawShader();
1755 if (isAA) {
1756 setupDrawBlending(true, mode);
1757 } else {
1758 setupDrawBlending(mode);
1759 }
1760 setupDrawProgram();
1761 setupDrawModelViewIdentity(true);
1762 setupDrawColorUniforms();
1763 setupDrawColorFilterUniforms();
1764 setupDrawShaderIdentityUniforms();
1765
1766 if (isHairLine) {
1767 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001768 halfStrokeWidth = isAA? 1 : .5;
1769 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001770 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001771 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001772 }
1773 Vertex lines[verticesCount];
1774 Vertex* vertices = &lines[0];
Chet Haase99585ad2011-05-02 15:00:16 -07001775 AAVertex wLines[verticesCount];
1776 AAVertex* aaVertices = &wLines[0];
Chet Haase5b0200b2011-04-13 17:58:08 -07001777 if (!isAA) {
1778 setupDrawVertices(vertices);
1779 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001780 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1781 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001782 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001783 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1784 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001785 // We will need to calculate the actual width proportion on each segment for
1786 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1787 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
1788 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
Chet Haase5b0200b2011-04-13 17:58:08 -07001789 }
1790
Chet Haase99ecdc42011-05-06 12:06:34 -07001791 AAVertex* prevAAVertex = NULL;
1792 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001793
Chet Haase99585ad2011-05-02 15:00:16 -07001794 int boundaryLengthSlot = -1;
1795 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001796 int boundaryWidthSlot = -1;
1797 int inverseBoundaryWidthSlot = -1;
Chet Haase5b0200b2011-04-13 17:58:08 -07001798 for (int i = 0; i < count; i += 4) {
1799 // a = start point, b = end point
1800 vec2 a(points[i], points[i + 1]);
1801 vec2 b(points[i + 2], points[i + 3]);
Chet Haase99585ad2011-05-02 15:00:16 -07001802 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001803 float boundaryLengthProportion = 0;
1804 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001805
Chet Haase5b0200b2011-04-13 17:58:08 -07001806 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001807 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001808 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001809 if (isAA) {
1810 float wideningFactor;
1811 if (fabs(n.x) >= fabs(n.y)) {
1812 wideningFactor = fabs(1.0f / n.x);
1813 } else {
1814 wideningFactor = fabs(1.0f / n.y);
1815 }
1816 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001817 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001818 if (scaled) {
1819 n.x *= inverseScaleX;
1820 n.y *= inverseScaleY;
1821 }
1822 } else if (scaled) {
1823 // Extend n by .5 pixel on each side, post-transform
1824 vec2 extendedN = n.copyNormalized();
1825 extendedN /= 2;
1826 extendedN.x *= inverseScaleX;
1827 extendedN.y *= inverseScaleY;
1828 float extendedNLength = extendedN.length();
1829 // We need to set this value on the shader prior to drawing
1830 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1831 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001832 }
1833 float x = n.x;
1834 n.x = -n.y;
1835 n.y = x;
1836
Chet Haase99585ad2011-05-02 15:00:16 -07001837 // aa lines expand the endpoint vertices to encompass the AA boundary
1838 if (isAA) {
1839 vec2 abVector = (b - a);
1840 length = abVector.length();
1841 abVector.normalize();
Chet Haase99ecdc42011-05-06 12:06:34 -07001842 if (scaled) {
1843 abVector.x *= inverseScaleX;
1844 abVector.y *= inverseScaleY;
1845 float abLength = abVector.length();
1846 boundaryLengthProportion = abLength / (length + abLength);
1847 } else {
1848 boundaryLengthProportion = .5 / (length + 1);
1849 }
1850 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001851 a -= abVector;
1852 b += abVector;
1853 }
1854
Chet Haase5b0200b2011-04-13 17:58:08 -07001855 // Four corners of the rectangle defining a thick line
1856 vec2 p1 = a - n;
1857 vec2 p2 = a + n;
1858 vec2 p3 = b + n;
1859 vec2 p4 = b - n;
1860
Chet Haase99585ad2011-05-02 15:00:16 -07001861
Chet Haase5b0200b2011-04-13 17:58:08 -07001862 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1863 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1864 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1865 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1866
1867 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001868 if (!isAA) {
1869 if (prevVertex != NULL) {
1870 // Issue two repeat vertices to create degenerate triangles to bridge
1871 // between the previous line and the new one. This is necessary because
1872 // we are creating a single triangle_strip which will contain
1873 // potentially discontinuous line segments.
1874 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1875 Vertex::set(vertices++, p1.x, p1.y);
1876 generatedVerticesCount += 2;
1877 }
1878 Vertex::set(vertices++, p1.x, p1.y);
1879 Vertex::set(vertices++, p2.x, p2.y);
1880 Vertex::set(vertices++, p4.x, p4.y);
1881 Vertex::set(vertices++, p3.x, p3.y);
1882 prevVertex = vertices - 1;
1883 generatedVerticesCount += 4;
1884 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001885 if (!isHairLine && scaled) {
1886 // Must set width proportions per-segment for scaled non-hairlines to use the
1887 // correct AA boundary dimensions
1888 if (boundaryWidthSlot < 0) {
1889 boundaryWidthSlot =
1890 mCaches.currentProgram->getUniform("boundaryWidth");
1891 inverseBoundaryWidthSlot =
1892 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1893 }
1894 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1895 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1896 }
Chet Haase99585ad2011-05-02 15:00:16 -07001897 if (boundaryLengthSlot < 0) {
1898 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1899 inverseBoundaryLengthSlot =
1900 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1901 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001902 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
1903 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07001904
Chet Haase5b0200b2011-04-13 17:58:08 -07001905 if (prevAAVertex != NULL) {
1906 // Issue two repeat vertices to create degenerate triangles to bridge
1907 // between the previous line and the new one. This is necessary because
1908 // we are creating a single triangle_strip which will contain
1909 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07001910 AAVertex::set(aaVertices++,prevAAVertex->position[0],
1911 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
1912 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07001913 generatedVerticesCount += 2;
1914 }
Chet Haase99585ad2011-05-02 15:00:16 -07001915 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
1916 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
1917 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
1918 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Chet Haase5b0200b2011-04-13 17:58:08 -07001919 prevAAVertex = aaVertices - 1;
1920 generatedVerticesCount += 4;
1921 }
Chet Haase99585ad2011-05-02 15:00:16 -07001922 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
1923 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
1924 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07001925 }
1926 }
1927 if (generatedVerticesCount > 0) {
1928 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
1929 }
1930}
1931
Romain Guyed6fcb02011-03-21 13:11:28 -07001932void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
1933 if (mSnapshot->isIgnored()) return;
1934
1935 // TODO: The paint's cap style defines whether the points are square or circular
1936 // TODO: Handle AA for round points
1937
Chet Haase5b0200b2011-04-13 17:58:08 -07001938 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07001939 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07001940 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07001941 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001942 if (isHairLine) {
1943 // Now that we know it's hairline, we can set the effective width, to be used later
1944 strokeWidth = 1.0f;
1945 }
1946 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07001947 int alpha;
1948 SkXfermode::Mode mode;
1949 getAlphaAndMode(paint, &alpha, &mode);
1950
1951 int verticesCount = count >> 1;
1952 int generatedVerticesCount = 0;
1953
1954 TextureVertex pointsData[verticesCount];
1955 TextureVertex* vertex = &pointsData[0];
1956
1957 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001958 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001959 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07001960 setupDrawColor(paint->getColor(), alpha);
1961 setupDrawColorFilter();
1962 setupDrawShader();
1963 setupDrawBlending(mode);
1964 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07001965 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07001966 setupDrawColorUniforms();
1967 setupDrawColorFilterUniforms();
1968 setupDrawPointUniforms();
1969 setupDrawShaderIdentityUniforms();
1970 setupDrawMesh(vertex);
1971
1972 for (int i = 0; i < count; i += 2) {
1973 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1974 generatedVerticesCount++;
Chet Haase8a5cc922011-04-26 07:28:09 -07001975 float left = points[i] - halfWidth;
1976 float right = points[i] + halfWidth;
1977 float top = points[i + 1] - halfWidth;
1978 float bottom = points [i + 1] + halfWidth;
1979 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07001980 }
1981
1982 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
1983}
1984
Romain Guy85bf02f2010-06-22 13:11:24 -07001985void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001986 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001987 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001988
Romain Guyae88e5e2010-10-22 17:49:18 -07001989 Rect& clip(*mSnapshot->clipRect);
1990 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001991
Romain Guy3d58c032010-07-14 16:34:53 -07001992 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001993}
Romain Guy9d5316e2010-06-24 19:30:36 -07001994
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001995void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001996 if (!texture) return;
1997 const AutoTexture autoCleanup(texture);
1998
1999 const float x = left + texture->left - texture->offset;
2000 const float y = top + texture->top - texture->offset;
2001
2002 drawPathTexture(texture, x, y, paint);
2003}
2004
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002005void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
2006 float rx, float ry, SkPaint* paint) {
2007 if (mSnapshot->isIgnored()) return;
2008
Romain Guya1d3c912011-12-13 14:55:06 -08002009 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002010 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2011 right - left, bottom - top, rx, ry, paint);
2012 drawShape(left, top, texture, paint);
2013}
2014
Romain Guy01d58e42011-01-19 21:54:02 -08002015void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2016 if (mSnapshot->isIgnored()) return;
2017
Romain Guya1d3c912011-12-13 14:55:06 -08002018 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002019 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002020 drawShape(x - radius, y - radius, texture, paint);
2021}
Romain Guy01d58e42011-01-19 21:54:02 -08002022
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002023void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2024 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002025
Romain Guya1d3c912011-12-13 14:55:06 -08002026 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002027 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2028 drawShape(left, top, texture, paint);
2029}
2030
Romain Guy8b2f5262011-01-23 16:15:02 -08002031void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2032 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2033 if (mSnapshot->isIgnored()) return;
2034
2035 if (fabs(sweepAngle) >= 360.0f) {
2036 drawOval(left, top, right, bottom, paint);
2037 return;
2038 }
2039
Romain Guya1d3c912011-12-13 14:55:06 -08002040 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002041 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2042 startAngle, sweepAngle, useCenter, paint);
2043 drawShape(left, top, texture, paint);
2044}
2045
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002046void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2047 SkPaint* paint) {
2048 if (mSnapshot->isIgnored()) return;
2049
Romain Guya1d3c912011-12-13 14:55:06 -08002050 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002051 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2052 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002053}
2054
Chet Haase5c13d892010-10-08 08:37:55 -07002055void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002056 if (p->getStyle() != SkPaint::kFill_Style) {
2057 drawRectAsShape(left, top, right, bottom, p);
2058 return;
2059 }
2060
Romain Guy6926c722010-07-12 20:20:03 -07002061 if (quickReject(left, top, right, bottom)) {
2062 return;
2063 }
2064
Romain Guy026c5e162010-06-28 17:12:22 -07002065 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002066 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002067 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2068 if (!isMode) {
2069 // Assume SRC_OVER
2070 mode = SkXfermode::kSrcOver_Mode;
2071 }
2072 } else {
2073 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002074 }
2075
Romain Guy026c5e162010-06-28 17:12:22 -07002076 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002077 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002078 drawAARect(left, top, right, bottom, color, mode);
2079 } else {
2080 drawColorRect(left, top, right, bottom, color, mode);
2081 }
Romain Guyc7d53492010-06-25 13:41:57 -07002082}
Romain Guy9d5316e2010-06-24 19:30:36 -07002083
Romain Guye8e62a42010-07-23 18:55:21 -07002084void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002085 float x, float y, SkPaint* paint, float length) {
Romain Guyb146b122010-12-15 17:06:45 -08002086 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07002087 return;
2088 }
Romain Guyaf636eb2010-12-09 17:47:21 -08002089 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07002090
Romain Guy8f9a9f62011-12-05 11:53:26 -08002091 // NOTE: AA and glyph id encoding are set in DisplayListRenderer.cpp
Romain Guye8e62a42010-07-23 18:55:21 -07002092
Romain Guye8e62a42010-07-23 18:55:21 -07002093 switch (paint->getTextAlign()) {
2094 case SkPaint::kCenter_Align:
Romain Guycac5fd32011-12-01 20:08:50 -08002095 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002096 x -= length / 2.0f;
2097 break;
2098 case SkPaint::kRight_Align:
Romain Guycac5fd32011-12-01 20:08:50 -08002099 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002100 x -= length;
2101 break;
2102 default:
2103 break;
2104 }
2105
Romain Guycac5fd32011-12-01 20:08:50 -08002106 SkPaint::FontMetrics metrics;
2107 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy8f9a9f62011-12-05 11:53:26 -08002108 // If no length was specified, just perform the hit test on the Y axis
Romain Guycac5fd32011-12-01 20:08:50 -08002109 if (quickReject(x, y + metrics.fTop,
2110 x + (length >= 0.0f ? length : INT_MAX / 2), y + metrics.fBottom)) {
2111 return;
2112 }
2113
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002114 const float oldX = x;
2115 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002116 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2117 if (pureTranslate) {
2118 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2119 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2120 }
2121
Romain Guyb45c0c92010-08-26 20:35:23 -07002122 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002123#if DEBUG_GLYPHS
2124 LOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
2125#endif
Romain Guyb45c0c92010-08-26 20:35:23 -07002126 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002127 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002128
Romain Guy86568192010-12-14 15:55:39 -08002129 int alpha;
2130 SkXfermode::Mode mode;
2131 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002132
Romain Guy1e45aae2010-08-13 19:39:53 -07002133 if (mHasShadow) {
Romain Guy2d4fd362011-12-13 22:00:19 -08002134 mCaches.activeTexture(0);
2135
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
Romain Guy86568192010-12-14 15:55:39 -08002150 setupDraw();
2151 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002152 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2153 setupDrawColorFilter();
2154 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002155 setupDrawBlending(true, mode);
2156 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002157 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002158 setupDrawTexture(shadow->id);
2159 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002160 setupDrawColorFilterUniforms();
2161 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002162 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2163
Romain Guy0a417492010-08-16 20:26:20 -07002164 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy1e45aae2010-08-13 19:39:53 -07002165 }
2166
Romain Guyb146b122010-12-15 17:06:45 -08002167 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
2168 return;
2169 }
2170
Romain Guy6620c6d2010-12-06 18:07:02 -08002171 // Pick the appropriate texture filtering
2172 bool linearFilter = mSnapshot->transform->changesBounds();
2173 if (pureTranslate && !linearFilter) {
2174 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2175 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002176
Romain Guya1d3c912011-12-13 14:55:06 -08002177 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002178 setupDraw();
2179 setupDrawDirtyRegionsDisabled();
2180 setupDrawWithTexture(true);
2181 setupDrawAlpha8Color(paint->getColor(), alpha);
2182 setupDrawColorFilter();
2183 setupDrawShader();
2184 setupDrawBlending(true, mode);
2185 setupDrawProgram();
2186 setupDrawModelView(x, y, x, y, pureTranslate, true);
2187 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2188 setupDrawPureColorUniforms();
2189 setupDrawColorFilterUniforms();
2190 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002191
Romain Guy6620c6d2010-12-06 18:07:02 -08002192 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002193 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2194
2195#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002196 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002197#else
Romain Guyf219da52011-01-16 12:54:25 -08002198 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002199#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002200
Romain Guy6620c6d2010-12-06 18:07:02 -08002201 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002202 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002203#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002204 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002205 if (!pureTranslate) {
2206 mSnapshot->transform->mapRect(bounds);
2207 }
Romain Guyf219da52011-01-16 12:54:25 -08002208 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002209 }
2210#endif
2211 }
Romain Guy694b5192010-07-21 21:33:20 -07002212
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002213 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002214}
2215
Romain Guy7fbcc042010-08-04 15:40:07 -07002216void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002217 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002218
Romain Guya1d3c912011-12-13 14:55:06 -08002219 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002220
Romain Guyfb8b7632010-08-23 21:05:08 -07002221 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -07002222 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002223 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002224
Romain Guy8b55f372010-08-18 17:10:07 -07002225 const float x = texture->left - texture->offset;
2226 const float y = texture->top - texture->offset;
2227
Romain Guy01d58e42011-01-19 21:54:02 -08002228 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002229}
2230
Romain Guyada830f2011-01-13 12:13:20 -08002231void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2232 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002233 return;
2234 }
2235
Romain Guya1d3c912011-12-13 14:55:06 -08002236 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002237
2238 int alpha;
2239 SkXfermode::Mode mode;
2240 getAlphaAndMode(paint, &alpha, &mode);
2241
Romain Guy9ace8f52011-07-07 20:50:11 -07002242 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002243
Romain Guyf219da52011-01-16 12:54:25 -08002244#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08002245 if (!layer->region.isEmpty()) {
2246 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002247 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002248 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002249 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002250 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002251
Romain Guyc88e3572011-01-22 00:32:12 -08002252 setupDraw();
2253 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002254 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002255 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002256 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002257 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002258 setupDrawPureColorUniforms();
2259 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002260 setupDrawTexture(layer->getTexture());
2261 if (mSnapshot->transform->isPureTranslate()) {
2262 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2263 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2264
Romain Guyd21b6e12011-11-30 20:21:23 -08002265 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002266 setupDrawModelViewTranslate(x, y,
2267 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2268 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002269 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002270 setupDrawModelViewTranslate(x, y,
2271 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2272 }
Romain Guyc88e3572011-01-22 00:32:12 -08002273 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002274
Romain Guyc88e3572011-01-22 00:32:12 -08002275 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2276 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002277
Romain Guyc88e3572011-01-22 00:32:12 -08002278 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002279
2280#if DEBUG_LAYERS_AS_REGIONS
2281 drawRegionRects(layer->region);
2282#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002283 }
Romain Guyf219da52011-01-16 12:54:25 -08002284 }
2285#else
Romain Guyada830f2011-01-13 12:13:20 -08002286 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2287 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002288#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002289}
2290
Romain Guy6926c722010-07-12 20:20:03 -07002291///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002292// Shaders
2293///////////////////////////////////////////////////////////////////////////////
2294
2295void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002296 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002297}
2298
Romain Guy06f96e22010-07-30 19:18:16 -07002299void OpenGLRenderer::setupShader(SkiaShader* shader) {
2300 mShader = shader;
2301 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002302 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002303 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002304}
2305
Romain Guyd27977d2010-07-14 19:18:51 -07002306///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002307// Color filters
2308///////////////////////////////////////////////////////////////////////////////
2309
2310void OpenGLRenderer::resetColorFilter() {
2311 mColorFilter = NULL;
2312}
2313
2314void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2315 mColorFilter = filter;
2316}
2317
2318///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002319// Drop shadow
2320///////////////////////////////////////////////////////////////////////////////
2321
2322void OpenGLRenderer::resetShadow() {
2323 mHasShadow = false;
2324}
2325
2326void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2327 mHasShadow = true;
2328 mShadowRadius = radius;
2329 mShadowDx = dx;
2330 mShadowDy = dy;
2331 mShadowColor = color;
2332}
2333
2334///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002335// Drawing implementation
2336///////////////////////////////////////////////////////////////////////////////
2337
Romain Guy01d58e42011-01-19 21:54:02 -08002338void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2339 float x, float y, SkPaint* paint) {
2340 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2341 return;
2342 }
2343
2344 int alpha;
2345 SkXfermode::Mode mode;
2346 getAlphaAndMode(paint, &alpha, &mode);
2347
2348 setupDraw();
2349 setupDrawWithTexture(true);
2350 setupDrawAlpha8Color(paint->getColor(), alpha);
2351 setupDrawColorFilter();
2352 setupDrawShader();
2353 setupDrawBlending(true, mode);
2354 setupDrawProgram();
2355 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2356 setupDrawTexture(texture->id);
2357 setupDrawPureColorUniforms();
2358 setupDrawColorFilterUniforms();
2359 setupDrawShaderUniforms();
2360 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2361
2362 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2363
2364 finishDrawTexture();
2365}
2366
Romain Guyf607bdc2010-09-10 19:20:06 -07002367// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002368#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2369#define kStdUnderline_Offset (1.0f / 9.0f)
2370#define kStdUnderline_Thickness (1.0f / 18.0f)
2371
2372void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2373 float x, float y, SkPaint* paint) {
2374 // Handle underline and strike-through
2375 uint32_t flags = paint->getFlags();
2376 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002377 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002378 float underlineWidth = length;
2379 // If length is > 0.0f, we already measured the text for the text alignment
2380 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002381 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002382 }
2383
2384 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002385 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002386 case SkPaint::kCenter_Align:
2387 offsetX = underlineWidth * 0.5f;
2388 break;
2389 case SkPaint::kRight_Align:
2390 offsetX = underlineWidth;
2391 break;
2392 default:
2393 break;
2394 }
2395
2396 if (underlineWidth > 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002397 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002398 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002399
Romain Guye20ecbd2010-09-22 19:49:04 -07002400 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002401 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002402
Romain Guyf6834472011-01-23 13:32:12 -08002403 int linesCount = 0;
2404 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2405 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2406
2407 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002408 float points[pointsCount];
2409 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002410
2411 if (flags & SkPaint::kUnderlineText_Flag) {
2412 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002413 points[currentPoint++] = left;
2414 points[currentPoint++] = top;
2415 points[currentPoint++] = left + underlineWidth;
2416 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002417 }
2418
2419 if (flags & SkPaint::kStrikeThruText_Flag) {
2420 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002421 points[currentPoint++] = left;
2422 points[currentPoint++] = top;
2423 points[currentPoint++] = left + underlineWidth;
2424 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002425 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002426
Romain Guy726aeba2011-06-01 14:52:00 -07002427 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002428
Romain Guy726aeba2011-06-01 14:52:00 -07002429 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002430 }
2431 }
2432}
2433
Romain Guy026c5e162010-06-28 17:12:22 -07002434void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002435 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002436 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002437 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002438 color |= 0x00ffffff;
2439 }
2440
Romain Guy70ca14e2010-12-13 18:24:33 -08002441 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002442 setupDrawNoTexture();
Romain Guy70ca14e2010-12-13 18:24:33 -08002443 setupDrawColor(color);
2444 setupDrawShader();
2445 setupDrawColorFilter();
2446 setupDrawBlending(mode);
2447 setupDrawProgram();
2448 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2449 setupDrawColorUniforms();
2450 setupDrawShaderUniforms(ignoreTransform);
2451 setupDrawColorFilterUniforms();
2452 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002453
Romain Guyc95c8d62010-09-17 15:31:32 -07002454 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2455}
2456
Romain Guy82ba8142010-07-09 13:25:56 -07002457void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002458 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002459 int alpha;
2460 SkXfermode::Mode mode;
2461 getAlphaAndMode(paint, &alpha, &mode);
2462
Romain Guyd21b6e12011-11-30 20:21:23 -08002463 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002464
Romain Guy6620c6d2010-12-06 18:07:02 -08002465 if (mSnapshot->transform->isPureTranslate()) {
2466 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2467 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2468
Romain Guyd21b6e12011-11-30 20:21:23 -08002469 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002470 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2471 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2472 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2473 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002474 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002475 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2476 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2477 GL_TRIANGLE_STRIP, gMeshCount);
2478 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002479}
2480
Romain Guybd6b79b2010-06-26 00:13:53 -07002481void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002482 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2483 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002484 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002485}
2486
2487void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002488 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002489 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002490 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002491
Romain Guy746b7402010-10-26 16:27:31 -07002492 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002493 setupDrawWithTexture();
2494 setupDrawColor(alpha, alpha, alpha, alpha);
2495 setupDrawColorFilter();
2496 setupDrawBlending(blend, mode, swapSrcDst);
2497 setupDrawProgram();
2498 if (!dirty) {
2499 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002500 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002501 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002502 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002503 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002504 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002505 }
Romain Guy86568192010-12-14 15:55:39 -08002506 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002507 setupDrawColorFilterUniforms();
2508 setupDrawTexture(texture);
2509 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002510
Romain Guy6820ac82010-09-15 18:11:50 -07002511 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002512
2513 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002514}
2515
Romain Guya5aed0d2010-09-09 14:42:43 -07002516void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002517 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002518 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2519 if (blend) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002520 if (mode <= SkXfermode::kScreen_Mode) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002521 if (!mCaches.blend) {
2522 glEnable(GL_BLEND);
2523 }
Romain Guy82ba8142010-07-09 13:25:56 -07002524
Romain Guyf607bdc2010-09-10 19:20:06 -07002525 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2526 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07002527
Romain Guya5aed0d2010-09-09 14:42:43 -07002528 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2529 glBlendFunc(sourceMode, destMode);
2530 mCaches.lastSrcMode = sourceMode;
2531 mCaches.lastDstMode = destMode;
2532 }
2533 } else {
2534 // These blend modes are not supported by OpenGL directly and have
2535 // to be implemented using shaders. Since the shader will perform
2536 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07002537 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002538 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002539 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002540 }
2541
2542 if (mCaches.blend) {
2543 glDisable(GL_BLEND);
2544 }
2545 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07002546 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002547 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002548 glDisable(GL_BLEND);
2549 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002550 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002551}
2552
Romain Guy889f8d12010-07-29 14:37:42 -07002553bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002554 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002555 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002556 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002557 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002558 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002559 }
Romain Guy6926c722010-07-12 20:20:03 -07002560 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002561}
2562
Romain Guy026c5e162010-06-28 17:12:22 -07002563void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002564 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002565 TextureVertex::setUV(v++, u1, v1);
2566 TextureVertex::setUV(v++, u2, v1);
2567 TextureVertex::setUV(v++, u1, v2);
2568 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002569}
2570
Chet Haase5c13d892010-10-08 08:37:55 -07002571void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002572 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002573 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002574
2575 // Skia draws using the color's alpha channel if < 255
2576 // Otherwise, it uses the paint's alpha
2577 int color = paint->getColor();
2578 *alpha = (color >> 24) & 0xFF;
2579 if (*alpha == 255) {
2580 *alpha = paint->getAlpha();
2581 }
2582 } else {
2583 *mode = SkXfermode::kSrcOver_Mode;
2584 *alpha = 255;
2585 }
Romain Guy026c5e162010-06-28 17:12:22 -07002586}
2587
Romain Guya5aed0d2010-09-09 14:42:43 -07002588SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002589 SkXfermode::Mode resultMode;
2590 if (!SkXfermode::AsMode(mode, &resultMode)) {
2591 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002592 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002593 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002594}
2595
Romain Guy9d5316e2010-06-24 19:30:36 -07002596}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002597}; // namespace android