blob: 1f5d13b8c59489420c3577eff5acceedfe96d085 [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 Guy7d7b5492011-01-24 16:33:45 -0800158 glScissor(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 Guydda570202010-07-06 11:39:32 -0700454 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700455 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700456 }
457
Romain Guy9ace8f52011-07-07 20:50:11 -0700458 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700459 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700460 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
461 bounds.getWidth() / float(layer->getWidth()), 0.0f);
462 layer->setColorFilter(mColorFilter);
Romain Guydda570202010-07-06 11:39:32 -0700463
Romain Guy8fb95422010-08-17 18:38:51 -0700464 // Save the layer in the snapshot
465 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700466 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700467
Romain Guyeb993562010-10-05 18:14:38 -0700468 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700469 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700470 } else {
471 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700472 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800473 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700474 if (layer->isEmpty()) {
475 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
476 bounds.left, snapshot->height - bounds.bottom,
477 layer->getWidth(), layer->getHeight(), 0);
478 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800479 } else {
480 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
481 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
482 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700483
Romain Guy54be1cd2011-06-13 19:04:27 -0700484 // Enqueue the buffer coordinates to clear the corresponding region later
485 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700486 }
Romain Guyeb993562010-10-05 18:14:38 -0700487 }
Romain Guyf86ef572010-07-01 11:05:42 -0700488
Romain Guyd55a8612010-06-28 17:42:46 -0700489 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700490}
491
Romain Guy5b3b3522010-10-27 18:57:51 -0700492bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
493 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700494 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700495
496#if RENDER_LAYERS_AS_REGIONS
497 snapshot->region = &snapshot->layer->region;
498 snapshot->flags |= Snapshot::kFlagFboTarget;
499#endif
500
501 Rect clip(bounds);
502 snapshot->transform->mapRect(clip);
503 clip.intersect(*snapshot->clipRect);
504 clip.snapToPixelBoundaries();
505 clip.intersect(snapshot->previous->viewport);
506
507 mat4 inverse;
508 inverse.loadInverse(*mSnapshot->transform);
509
510 inverse.mapRect(clip);
511 clip.snapToPixelBoundaries();
512 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700513 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700514
515 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700516 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700517 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700518 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
519 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
520 snapshot->height = bounds.getHeight();
521 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
522 snapshot->orthoMatrix.load(mOrthoMatrix);
523
524 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700525 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
526 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700527
528 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700529 if (layer->isEmpty()) {
530 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
531 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700532 }
533
534 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700535 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700536
537#if DEBUG_LAYERS_AS_REGIONS
538 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
539 if (status != GL_FRAMEBUFFER_COMPLETE) {
540 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
541
542 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700543 layer->deleteTexture();
544 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700545
546 delete layer;
547
548 return false;
549 }
550#endif
551
552 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
553 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
554 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
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 Guyeb993562010-10-05 18:14:38 -0700968 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700969 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700970}
971
972const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700973 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700974}
975
Romain Guyc7d53492010-06-25 13:41:57 -0700976bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800977 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700978 return true;
979 }
980
Romain Guy1d83e192010-08-17 11:37:00 -0700981 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700982 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700983 r.snapToPixelBoundaries();
984
985 Rect clipRect(*mSnapshot->clipRect);
986 clipRect.snapToPixelBoundaries();
987
988 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700989}
990
Romain Guy079ba2c2010-07-16 14:12:24 -0700991bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
992 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700993 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700994 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700995 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700996 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700997}
998
Romain Guyf6a11b82010-06-23 17:47:49 -0700999///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001000// Drawing commands
1001///////////////////////////////////////////////////////////////////////////////
1002
Romain Guy54be1cd2011-06-13 19:04:27 -07001003void OpenGLRenderer::setupDraw(bool clear) {
1004 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001005 if (mDirtyClip) {
1006 setScissorFromClip();
1007 }
1008 mDescription.reset();
1009 mSetShaderColor = false;
1010 mColorSet = false;
1011 mColorA = mColorR = mColorG = mColorB = 0.0f;
1012 mTextureUnit = 0;
1013 mTrackDirtyRegions = true;
1014}
1015
1016void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1017 mDescription.hasTexture = true;
1018 mDescription.hasAlpha8Texture = isAlpha8;
1019}
1020
Romain Guyaa6c24c2011-04-28 18:40:04 -07001021void OpenGLRenderer::setupDrawWithExternalTexture() {
1022 mDescription.hasExternalTexture = true;
1023}
1024
Romain Guy15bc6432011-12-13 13:11:32 -08001025void OpenGLRenderer::setupDrawNoTexture() {
1026 mCaches.disbaleTexCoordsVertexArray();
1027}
1028
Chet Haase5b0200b2011-04-13 17:58:08 -07001029void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001030 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001031}
1032
Romain Guyed6fcb02011-03-21 13:11:28 -07001033void OpenGLRenderer::setupDrawPoint(float pointSize) {
1034 mDescription.isPoint = true;
1035 mDescription.pointSize = pointSize;
1036}
1037
Romain Guy70ca14e2010-12-13 18:24:33 -08001038void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001039 setupDrawColor(color, (color >> 24) & 0xFF);
1040}
1041
1042void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1043 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001044 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1045 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001046 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001047 mColorR = a * ((color >> 16) & 0xFF);
1048 mColorG = a * ((color >> 8) & 0xFF);
1049 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001050 mColorSet = true;
1051 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1052}
1053
Romain Guy86568192010-12-14 15:55:39 -08001054void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1055 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001056 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1057 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001058 const float a = mColorA / 255.0f;
1059 mColorR = a * ((color >> 16) & 0xFF);
1060 mColorG = a * ((color >> 8) & 0xFF);
1061 mColorB = a * ((color ) & 0xFF);
1062 mColorSet = true;
1063 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1064}
1065
Romain Guy70ca14e2010-12-13 18:24:33 -08001066void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1067 mColorA = a;
1068 mColorR = r;
1069 mColorG = g;
1070 mColorB = b;
1071 mColorSet = true;
1072 mSetShaderColor = mDescription.setColor(r, g, b, a);
1073}
1074
Romain Guy86568192010-12-14 15:55:39 -08001075void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1076 mColorA = a;
1077 mColorR = r;
1078 mColorG = g;
1079 mColorB = b;
1080 mColorSet = true;
1081 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1082}
1083
Romain Guy70ca14e2010-12-13 18:24:33 -08001084void OpenGLRenderer::setupDrawShader() {
1085 if (mShader) {
1086 mShader->describe(mDescription, mCaches.extensions);
1087 }
1088}
1089
1090void OpenGLRenderer::setupDrawColorFilter() {
1091 if (mColorFilter) {
1092 mColorFilter->describe(mDescription, mCaches.extensions);
1093 }
1094}
1095
Romain Guyf09ef512011-05-27 11:43:46 -07001096void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1097 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1098 mColorA = 1.0f;
1099 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001100 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001101 }
1102}
1103
Romain Guy70ca14e2010-12-13 18:24:33 -08001104void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001105 // When the blending mode is kClear_Mode, we need to use a modulate color
1106 // argb=1,0,0,0
1107 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001108 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1109 mDescription, swapSrcDst);
1110}
1111
1112void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001113 // When the blending mode is kClear_Mode, we need to use a modulate color
1114 // argb=1,0,0,0
1115 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001116 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1117 mDescription, swapSrcDst);
1118}
1119
1120void OpenGLRenderer::setupDrawProgram() {
1121 useProgram(mCaches.programCache.get(mDescription));
1122}
1123
1124void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1125 mTrackDirtyRegions = false;
1126}
1127
1128void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1129 bool ignoreTransform) {
1130 mModelView.loadTranslate(left, top, 0.0f);
1131 if (!ignoreTransform) {
1132 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1133 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1134 } else {
1135 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1136 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1137 }
1138}
1139
Chet Haase8a5cc922011-04-26 07:28:09 -07001140void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1141 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001142}
1143
Romain Guy70ca14e2010-12-13 18:24:33 -08001144void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1145 bool ignoreTransform, bool ignoreModelView) {
1146 if (!ignoreModelView) {
1147 mModelView.loadTranslate(left, top, 0.0f);
1148 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001149 } else {
1150 mModelView.loadIdentity();
1151 }
Romain Guy86568192010-12-14 15:55:39 -08001152 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1153 if (!ignoreTransform) {
1154 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1155 if (mTrackDirtyRegions && dirty) {
1156 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1157 }
1158 } else {
1159 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1160 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1161 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001162}
1163
Romain Guyed6fcb02011-03-21 13:11:28 -07001164void OpenGLRenderer::setupDrawPointUniforms() {
1165 int slot = mCaches.currentProgram->getUniform("pointSize");
1166 glUniform1f(slot, mDescription.pointSize);
1167}
1168
Romain Guy70ca14e2010-12-13 18:24:33 -08001169void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001170 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001171 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1172 }
1173}
1174
Romain Guy86568192010-12-14 15:55:39 -08001175void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001176 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001177 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001178 }
1179}
1180
Romain Guy70ca14e2010-12-13 18:24:33 -08001181void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1182 if (mShader) {
1183 if (ignoreTransform) {
1184 mModelView.loadInverse(*mSnapshot->transform);
1185 }
1186 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1187 }
1188}
1189
Romain Guy8d0d4782010-12-14 20:13:35 -08001190void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1191 if (mShader) {
1192 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1193 }
1194}
1195
Romain Guy70ca14e2010-12-13 18:24:33 -08001196void OpenGLRenderer::setupDrawColorFilterUniforms() {
1197 if (mColorFilter) {
1198 mColorFilter->setupProgram(mCaches.currentProgram);
1199 }
1200}
1201
1202void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001203 bool force = mCaches.bindMeshBuffer();
1204 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001205 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001206}
1207
1208void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1209 bindTexture(texture);
1210 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
Romain Guy15bc6432011-12-13 13:11:32 -08001211 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001212}
1213
Romain Guyaa6c24c2011-04-28 18:40:04 -07001214void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1215 bindExternalTexture(texture);
1216 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
Romain Guy15bc6432011-12-13 13:11:32 -08001217 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001218}
1219
Romain Guy8f0095c2011-05-02 17:24:22 -07001220void OpenGLRenderer::setupDrawTextureTransform() {
1221 mDescription.hasTextureTransform = true;
1222}
1223
1224void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001225 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1226 GL_FALSE, &transform.data[0]);
1227}
1228
Romain Guy70ca14e2010-12-13 18:24:33 -08001229void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001230 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001231 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001232 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001233 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001234 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001235 }
Romain Guyd71dd362011-12-12 19:03:35 -08001236
Romain Guyf3a910b42011-12-12 20:35:21 -08001237 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001238 if (mCaches.currentProgram->texCoords >= 0) {
1239 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1240 }
1241
1242 mCaches.unbindIndicesBuffer();
1243}
1244
1245void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1246 bool force = mCaches.unbindMeshBuffer();
1247 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1248 if (mCaches.currentProgram->texCoords >= 0) {
1249 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001250 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001251}
1252
Chet Haase5b0200b2011-04-13 17:58:08 -07001253void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001254 bool force = mCaches.unbindMeshBuffer();
1255 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1256 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001257 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001258}
1259
1260/**
1261 * 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 -07001262 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1263 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1264 * attributes (one per vertex) are values from zero to one that tells the fragment
1265 * shader where the fragment is in relation to the line width/length overall; these values are
1266 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1267 * region of the line.
1268 * Note that we only pass down the width values in this setup function. The length coordinates
1269 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001270 */
Chet Haase99585ad2011-05-02 15:00:16 -07001271void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Chet Haase99ecdc42011-05-06 12:06:34 -07001272 GLvoid* lengthCoords, float boundaryWidthProportion) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001273 bool force = mCaches.unbindMeshBuffer();
1274 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1275 vertices, gAAVertexStride);
1276 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001277 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001278
Chet Haase99585ad2011-05-02 15:00:16 -07001279 int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
1280 glEnableVertexAttribArray(widthSlot);
1281 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001282
Chet Haase99585ad2011-05-02 15:00:16 -07001283 int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
1284 glEnableVertexAttribArray(lengthSlot);
1285 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001286
Chet Haase5b0200b2011-04-13 17:58:08 -07001287 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001288 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001289
Chet Haase99585ad2011-05-02 15:00:16 -07001290 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001291 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001292 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
Chet Haase5b0200b2011-04-13 17:58:08 -07001293}
1294
Romain Guy70ca14e2010-12-13 18:24:33 -08001295void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001296}
1297
1298///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001299// Drawing
1300///////////////////////////////////////////////////////////////////////////////
1301
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001302bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
1303 Rect& dirty, uint32_t level) {
1304 if (quickReject(0.0f, 0.0f, width, height)) {
1305 return false;
1306 }
1307
Romain Guy0fe478e2010-11-08 12:08:41 -08001308 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1309 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001310 if (displayList && displayList->isRenderable()) {
Romain Guycabfcc12011-03-07 18:06:46 -08001311 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001312 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001313
Chet Haasedaf98e92011-01-10 14:10:36 -08001314 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001315}
1316
Chet Haaseed30fd82011-04-22 16:18:45 -07001317void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1318 if (displayList) {
1319 displayList->output(*this, level);
1320 }
1321}
1322
Romain Guya168d732011-03-18 16:50:13 -07001323void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1324 int alpha;
1325 SkXfermode::Mode mode;
1326 getAlphaAndMode(paint, &alpha, &mode);
1327
Romain Guya168d732011-03-18 16:50:13 -07001328 float x = left;
1329 float y = top;
1330
Romain Guye3c26852011-07-25 16:36:01 -07001331 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001332 bool ignoreTransform = false;
1333 if (mSnapshot->transform->isPureTranslate()) {
1334 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1335 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1336 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001337 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001338 } else {
1339 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001340 }
1341
1342 setupDraw();
1343 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001344 if (paint) {
1345 setupDrawAlpha8Color(paint->getColor(), alpha);
1346 }
Romain Guya168d732011-03-18 16:50:13 -07001347 setupDrawColorFilter();
1348 setupDrawShader();
1349 setupDrawBlending(true, mode);
1350 setupDrawProgram();
1351 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001352
Romain Guya168d732011-03-18 16:50:13 -07001353 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001354 texture->setWrap(GL_CLAMP_TO_EDGE);
1355 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001356
Romain Guya168d732011-03-18 16:50:13 -07001357 setupDrawPureColorUniforms();
1358 setupDrawColorFilterUniforms();
1359 setupDrawShaderUniforms();
1360 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1361
1362 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1363
1364 finishDrawTexture();
1365}
1366
Chet Haase5c13d892010-10-08 08:37:55 -07001367void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001368 const float right = left + bitmap->width();
1369 const float bottom = top + bitmap->height();
1370
1371 if (quickReject(left, top, right, bottom)) {
1372 return;
1373 }
1374
Romain Guya1d3c912011-12-13 14:55:06 -08001375 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001376 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001377 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001378 const AutoTexture autoCleanup(texture);
1379
Romain Guya168d732011-03-18 16:50:13 -07001380 if (bitmap->getConfig() == SkBitmap::kA8_Config) {
1381 drawAlphaBitmap(texture, left, top, paint);
1382 } else {
1383 drawTextureRect(left, top, right, bottom, texture, paint);
1384 }
Romain Guyce0537b2010-06-29 21:05:21 -07001385}
1386
Chet Haase5c13d892010-10-08 08:37:55 -07001387void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001388 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1389 const mat4 transform(*matrix);
1390 transform.mapRect(r);
1391
Romain Guy6926c722010-07-12 20:20:03 -07001392 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1393 return;
1394 }
1395
Romain Guya1d3c912011-12-13 14:55:06 -08001396 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001397 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001398 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001399 const AutoTexture autoCleanup(texture);
1400
Romain Guy5b3b3522010-10-27 18:57:51 -07001401 // This could be done in a cheaper way, all we need is pass the matrix
1402 // to the vertex shader. The save/restore is a bit overkill.
1403 save(SkCanvas::kMatrix_SaveFlag);
1404 concatMatrix(matrix);
1405 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1406 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001407}
1408
Romain Guy5a7b4662011-01-20 19:09:30 -08001409void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1410 float* vertices, int* colors, SkPaint* paint) {
1411 // TODO: Do a quickReject
1412 if (!vertices || mSnapshot->isIgnored()) {
1413 return;
1414 }
1415
Romain Guya1d3c912011-12-13 14:55:06 -08001416 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001417 Texture* texture = mCaches.textureCache.get(bitmap);
1418 if (!texture) return;
1419 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001420
Romain Guyd21b6e12011-11-30 20:21:23 -08001421 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1422 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001423
1424 int alpha;
1425 SkXfermode::Mode mode;
1426 getAlphaAndMode(paint, &alpha, &mode);
1427
Romain Guy5a7b4662011-01-20 19:09:30 -08001428 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001429
Romain Guyb18d2d02011-02-10 15:52:54 -08001430 float left = FLT_MAX;
1431 float top = FLT_MAX;
1432 float right = FLT_MIN;
1433 float bottom = FLT_MIN;
1434
1435#if RENDER_LAYERS_AS_REGIONS
1436 bool hasActiveLayer = hasLayer();
1437#else
1438 bool hasActiveLayer = false;
1439#endif
1440
Romain Guya566b7c2011-01-23 16:36:11 -08001441 // TODO: Support the colors array
1442 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001443 TextureVertex* vertex = mesh;
1444 for (int32_t y = 0; y < meshHeight; y++) {
1445 for (int32_t x = 0; x < meshWidth; x++) {
1446 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1447
1448 float u1 = float(x) / meshWidth;
1449 float u2 = float(x + 1) / meshWidth;
1450 float v1 = float(y) / meshHeight;
1451 float v2 = float(y + 1) / meshHeight;
1452
1453 int ax = i + (meshWidth + 1) * 2;
1454 int ay = ax + 1;
1455 int bx = i;
1456 int by = bx + 1;
1457 int cx = i + 2;
1458 int cy = cx + 1;
1459 int dx = i + (meshWidth + 1) * 2 + 2;
1460 int dy = dx + 1;
1461
1462 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1463 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1464 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1465
1466 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1467 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1468 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001469
1470#if RENDER_LAYERS_AS_REGIONS
1471 if (hasActiveLayer) {
1472 // TODO: This could be optimized to avoid unnecessary ops
1473 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1474 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1475 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1476 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1477 }
1478#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001479 }
1480 }
1481
Romain Guyb18d2d02011-02-10 15:52:54 -08001482#if RENDER_LAYERS_AS_REGIONS
1483 if (hasActiveLayer) {
1484 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1485 }
1486#endif
1487
Romain Guy5a7b4662011-01-20 19:09:30 -08001488 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1489 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001490 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001491}
1492
Romain Guy8ba548f2010-06-30 19:21:21 -07001493void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1494 float srcLeft, float srcTop, float srcRight, float srcBottom,
1495 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001496 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001497 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1498 return;
1499 }
1500
Romain Guya1d3c912011-12-13 14:55:06 -08001501 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001502 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001503 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001504 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001505
Romain Guy8ba548f2010-06-30 19:21:21 -07001506 const float width = texture->width;
1507 const float height = texture->height;
1508
Romain Guy68169722011-08-22 17:33:33 -07001509 const float u1 = fmax(0.0f, srcLeft / width);
1510 const float v1 = fmax(0.0f, srcTop / height);
1511 const float u2 = fmin(1.0f, srcRight / width);
1512 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001513
Romain Guy03750a02010-10-18 14:06:08 -07001514 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001515 resetDrawTextureTexCoords(u1, v1, u2, v2);
1516
Romain Guy03750a02010-10-18 14:06:08 -07001517 int alpha;
1518 SkXfermode::Mode mode;
1519 getAlphaAndMode(paint, &alpha, &mode);
1520
Romain Guyd21b6e12011-11-30 20:21:23 -08001521 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1522
Romain Guy6620c6d2010-12-06 18:07:02 -08001523 if (mSnapshot->transform->isPureTranslate()) {
1524 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1525 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1526
Romain Guyb5014982011-07-28 15:39:12 -07001527 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001528 // Enable linear filtering if the source rectangle is scaled
1529 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001530 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001531 }
Romain Guyb5014982011-07-28 15:39:12 -07001532
Romain Guyd21b6e12011-11-30 20:21:23 -08001533 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001534 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1535 texture->id, alpha / 255.0f, mode, texture->blend,
1536 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1537 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1538 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001539 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001540 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1541 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1542 GL_TRIANGLE_STRIP, gMeshCount);
1543 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001544
1545 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1546}
1547
Romain Guy4aa90572010-09-26 18:40:37 -07001548void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001549 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001550 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001551 if (quickReject(left, top, right, bottom)) {
1552 return;
1553 }
1554
Romain Guya1d3c912011-12-13 14:55:06 -08001555 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001556 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001557 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001558 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001559 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1560 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001561
1562 int alpha;
1563 SkXfermode::Mode mode;
1564 getAlphaAndMode(paint, &alpha, &mode);
1565
Romain Guy2728f962010-10-08 18:36:15 -07001566 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001567 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001568
Romain Guya5ef39a2010-12-03 16:48:20 -08001569 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001570 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001571#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001572 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001573 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001574 const float offsetX = left + mSnapshot->transform->getTranslateX();
1575 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001576 const size_t count = mesh->quads.size();
1577 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001578 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001579 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001580 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1581 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1582 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001583 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001584 dirtyLayer(left + bounds.left, top + bounds.top,
1585 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001586 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001587 }
1588 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001589#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001590
Romain Guy6620c6d2010-12-06 18:07:02 -08001591 if (pureTranslate) {
1592 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1593 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1594
1595 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1596 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1597 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1598 true, !mesh->hasEmptyQuads);
1599 } else {
1600 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1601 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1602 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1603 true, !mesh->hasEmptyQuads);
1604 }
Romain Guy054dc182010-10-15 17:55:25 -07001605 }
Romain Guyf7f93552010-07-08 19:17:03 -07001606}
1607
Chet Haase99ecdc42011-05-06 12:06:34 -07001608/**
Chet Haase858aa932011-05-12 09:06:00 -07001609 * This function uses a similar approach to that of AA lines in the drawLines() function.
1610 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1611 * shader to compute the translucency of the color, determined by whether a given pixel is
1612 * within that boundary region and how far into the region it is.
1613 */
1614void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001615 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001616 float inverseScaleX = 1.0f;
1617 float inverseScaleY = 1.0f;
1618 // The quad that we use needs to account for scaling.
1619 if (!mSnapshot->transform->isPureTranslate()) {
1620 Matrix4 *mat = mSnapshot->transform;
1621 float m00 = mat->data[Matrix4::kScaleX];
1622 float m01 = mat->data[Matrix4::kSkewY];
1623 float m02 = mat->data[2];
1624 float m10 = mat->data[Matrix4::kSkewX];
1625 float m11 = mat->data[Matrix4::kScaleX];
1626 float m12 = mat->data[6];
1627 float scaleX = sqrt(m00 * m00 + m01 * m01);
1628 float scaleY = sqrt(m10 * m10 + m11 * m11);
1629 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1630 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1631 }
1632
1633 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001634 setupDrawNoTexture();
Chet Haase858aa932011-05-12 09:06:00 -07001635 setupDrawAALine();
1636 setupDrawColor(color);
1637 setupDrawColorFilter();
1638 setupDrawShader();
1639 setupDrawBlending(true, mode);
1640 setupDrawProgram();
1641 setupDrawModelViewIdentity(true);
1642 setupDrawColorUniforms();
1643 setupDrawColorFilterUniforms();
1644 setupDrawShaderIdentityUniforms();
1645
1646 AAVertex rects[4];
1647 AAVertex* aaVertices = &rects[0];
1648 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1649 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1650
1651 float boundarySizeX = .5 * inverseScaleX;
1652 float boundarySizeY = .5 * inverseScaleY;
1653
1654 // Adjust the rect by the AA boundary padding
1655 left -= boundarySizeX;
1656 right += boundarySizeX;
1657 top -= boundarySizeY;
1658 bottom += boundarySizeY;
1659
1660 float width = right - left;
1661 float height = bottom - top;
1662
1663 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1664 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
1665 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
1666 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1667 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1668 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
1669 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryHeightProportion));
1670
1671 if (!quickReject(left, top, right, bottom)) {
1672 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1673 AAVertex::set(aaVertices++, left, top, 1, 0);
1674 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1675 AAVertex::set(aaVertices++, right, top, 0, 0);
1676 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1677 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1678 }
1679}
1680
1681/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001682 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1683 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1684 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1685 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1686 * of the line. Hairlines are more involved because we need to account for transform scaling
1687 * to end up with a one-pixel-wide line in screen space..
1688 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1689 * in combination with values that we calculate and pass down in this method. The basic approach
1690 * is that the quad we create contains both the core line area plus a bounding area in which
1691 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1692 * proportion of the width and the length of a given segment is represented by the boundary
1693 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1694 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1695 * on the inside). This ends up giving the result we want, with pixels that are completely
1696 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1697 * how far into the boundary region they are, which is determined by shader interpolation.
1698 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001699void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1700 if (mSnapshot->isIgnored()) return;
1701
1702 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001703 // We use half the stroke width here because we're going to position the quad
1704 // corner vertices half of the width away from the line endpoints
1705 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001706 // A stroke width of 0 has a special meaning in Skia:
1707 // it draws a line 1 px wide regardless of current transform
1708 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase99ecdc42011-05-06 12:06:34 -07001709 float inverseScaleX = 1.0f;
1710 float inverseScaleY = 1.0f;
1711 bool scaled = false;
Chet Haase8a5cc922011-04-26 07:28:09 -07001712 int alpha;
1713 SkXfermode::Mode mode;
1714 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001715 int verticesCount = count;
1716 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001717 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001718 verticesCount += (count - 4);
1719 }
1720
1721 if (isHairLine || isAA) {
1722 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1723 // the line on the screen should always be one pixel wide regardless of scale. For
1724 // AA lines, we only want one pixel of translucent boundary around the quad.
1725 if (!mSnapshot->transform->isPureTranslate()) {
1726 Matrix4 *mat = mSnapshot->transform;
1727 float m00 = mat->data[Matrix4::kScaleX];
1728 float m01 = mat->data[Matrix4::kSkewY];
1729 float m02 = mat->data[2];
1730 float m10 = mat->data[Matrix4::kSkewX];
1731 float m11 = mat->data[Matrix4::kScaleX];
1732 float m12 = mat->data[6];
1733 float scaleX = sqrt(m00*m00 + m01*m01);
1734 float scaleY = sqrt(m10*m10 + m11*m11);
1735 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1736 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1737 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1738 scaled = true;
1739 }
1740 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001741 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001742
1743 getAlphaAndMode(paint, &alpha, &mode);
1744 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001745 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001746 if (isAA) {
1747 setupDrawAALine();
1748 }
1749 setupDrawColor(paint->getColor(), alpha);
1750 setupDrawColorFilter();
1751 setupDrawShader();
1752 if (isAA) {
1753 setupDrawBlending(true, mode);
1754 } else {
1755 setupDrawBlending(mode);
1756 }
1757 setupDrawProgram();
1758 setupDrawModelViewIdentity(true);
1759 setupDrawColorUniforms();
1760 setupDrawColorFilterUniforms();
1761 setupDrawShaderIdentityUniforms();
1762
1763 if (isHairLine) {
1764 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001765 halfStrokeWidth = isAA? 1 : .5;
1766 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001767 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001768 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001769 }
1770 Vertex lines[verticesCount];
1771 Vertex* vertices = &lines[0];
Chet Haase99585ad2011-05-02 15:00:16 -07001772 AAVertex wLines[verticesCount];
1773 AAVertex* aaVertices = &wLines[0];
Chet Haase5b0200b2011-04-13 17:58:08 -07001774 if (!isAA) {
1775 setupDrawVertices(vertices);
1776 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001777 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1778 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001779 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001780 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1781 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001782 // We will need to calculate the actual width proportion on each segment for
1783 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1784 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
1785 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
Chet Haase5b0200b2011-04-13 17:58:08 -07001786 }
1787
Chet Haase99ecdc42011-05-06 12:06:34 -07001788 AAVertex* prevAAVertex = NULL;
1789 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001790
Chet Haase99585ad2011-05-02 15:00:16 -07001791 int boundaryLengthSlot = -1;
1792 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001793 int boundaryWidthSlot = -1;
1794 int inverseBoundaryWidthSlot = -1;
Chet Haase5b0200b2011-04-13 17:58:08 -07001795 for (int i = 0; i < count; i += 4) {
1796 // a = start point, b = end point
1797 vec2 a(points[i], points[i + 1]);
1798 vec2 b(points[i + 2], points[i + 3]);
Chet Haase99585ad2011-05-02 15:00:16 -07001799 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001800 float boundaryLengthProportion = 0;
1801 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001802
Chet Haase5b0200b2011-04-13 17:58:08 -07001803 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001804 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001805 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001806 if (isAA) {
1807 float wideningFactor;
1808 if (fabs(n.x) >= fabs(n.y)) {
1809 wideningFactor = fabs(1.0f / n.x);
1810 } else {
1811 wideningFactor = fabs(1.0f / n.y);
1812 }
1813 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001814 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001815 if (scaled) {
1816 n.x *= inverseScaleX;
1817 n.y *= inverseScaleY;
1818 }
1819 } else if (scaled) {
1820 // Extend n by .5 pixel on each side, post-transform
1821 vec2 extendedN = n.copyNormalized();
1822 extendedN /= 2;
1823 extendedN.x *= inverseScaleX;
1824 extendedN.y *= inverseScaleY;
1825 float extendedNLength = extendedN.length();
1826 // We need to set this value on the shader prior to drawing
1827 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1828 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001829 }
1830 float x = n.x;
1831 n.x = -n.y;
1832 n.y = x;
1833
Chet Haase99585ad2011-05-02 15:00:16 -07001834 // aa lines expand the endpoint vertices to encompass the AA boundary
1835 if (isAA) {
1836 vec2 abVector = (b - a);
1837 length = abVector.length();
1838 abVector.normalize();
Chet Haase99ecdc42011-05-06 12:06:34 -07001839 if (scaled) {
1840 abVector.x *= inverseScaleX;
1841 abVector.y *= inverseScaleY;
1842 float abLength = abVector.length();
1843 boundaryLengthProportion = abLength / (length + abLength);
1844 } else {
1845 boundaryLengthProportion = .5 / (length + 1);
1846 }
1847 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001848 a -= abVector;
1849 b += abVector;
1850 }
1851
Chet Haase5b0200b2011-04-13 17:58:08 -07001852 // Four corners of the rectangle defining a thick line
1853 vec2 p1 = a - n;
1854 vec2 p2 = a + n;
1855 vec2 p3 = b + n;
1856 vec2 p4 = b - n;
1857
Chet Haase99585ad2011-05-02 15:00:16 -07001858
Chet Haase5b0200b2011-04-13 17:58:08 -07001859 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1860 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1861 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1862 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1863
1864 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001865 if (!isAA) {
1866 if (prevVertex != NULL) {
1867 // Issue two repeat vertices to create degenerate triangles to bridge
1868 // between the previous line and the new one. This is necessary because
1869 // we are creating a single triangle_strip which will contain
1870 // potentially discontinuous line segments.
1871 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1872 Vertex::set(vertices++, p1.x, p1.y);
1873 generatedVerticesCount += 2;
1874 }
1875 Vertex::set(vertices++, p1.x, p1.y);
1876 Vertex::set(vertices++, p2.x, p2.y);
1877 Vertex::set(vertices++, p4.x, p4.y);
1878 Vertex::set(vertices++, p3.x, p3.y);
1879 prevVertex = vertices - 1;
1880 generatedVerticesCount += 4;
1881 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001882 if (!isHairLine && scaled) {
1883 // Must set width proportions per-segment for scaled non-hairlines to use the
1884 // correct AA boundary dimensions
1885 if (boundaryWidthSlot < 0) {
1886 boundaryWidthSlot =
1887 mCaches.currentProgram->getUniform("boundaryWidth");
1888 inverseBoundaryWidthSlot =
1889 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1890 }
1891 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1892 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1893 }
Chet Haase99585ad2011-05-02 15:00:16 -07001894 if (boundaryLengthSlot < 0) {
1895 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1896 inverseBoundaryLengthSlot =
1897 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1898 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001899 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
1900 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07001901
Chet Haase5b0200b2011-04-13 17:58:08 -07001902 if (prevAAVertex != NULL) {
1903 // Issue two repeat vertices to create degenerate triangles to bridge
1904 // between the previous line and the new one. This is necessary because
1905 // we are creating a single triangle_strip which will contain
1906 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07001907 AAVertex::set(aaVertices++,prevAAVertex->position[0],
1908 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
1909 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07001910 generatedVerticesCount += 2;
1911 }
Chet Haase99585ad2011-05-02 15:00:16 -07001912 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
1913 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
1914 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
1915 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Chet Haase5b0200b2011-04-13 17:58:08 -07001916 prevAAVertex = aaVertices - 1;
1917 generatedVerticesCount += 4;
1918 }
Chet Haase99585ad2011-05-02 15:00:16 -07001919 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
1920 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
1921 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07001922 }
1923 }
1924 if (generatedVerticesCount > 0) {
1925 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
1926 }
1927}
1928
Romain Guyed6fcb02011-03-21 13:11:28 -07001929void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
1930 if (mSnapshot->isIgnored()) return;
1931
1932 // TODO: The paint's cap style defines whether the points are square or circular
1933 // TODO: Handle AA for round points
1934
Chet Haase5b0200b2011-04-13 17:58:08 -07001935 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07001936 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07001937 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07001938 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001939 if (isHairLine) {
1940 // Now that we know it's hairline, we can set the effective width, to be used later
1941 strokeWidth = 1.0f;
1942 }
1943 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07001944 int alpha;
1945 SkXfermode::Mode mode;
1946 getAlphaAndMode(paint, &alpha, &mode);
1947
1948 int verticesCount = count >> 1;
1949 int generatedVerticesCount = 0;
1950
1951 TextureVertex pointsData[verticesCount];
1952 TextureVertex* vertex = &pointsData[0];
1953
1954 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001955 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001956 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07001957 setupDrawColor(paint->getColor(), alpha);
1958 setupDrawColorFilter();
1959 setupDrawShader();
1960 setupDrawBlending(mode);
1961 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07001962 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07001963 setupDrawColorUniforms();
1964 setupDrawColorFilterUniforms();
1965 setupDrawPointUniforms();
1966 setupDrawShaderIdentityUniforms();
1967 setupDrawMesh(vertex);
1968
1969 for (int i = 0; i < count; i += 2) {
1970 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1971 generatedVerticesCount++;
Chet Haase8a5cc922011-04-26 07:28:09 -07001972 float left = points[i] - halfWidth;
1973 float right = points[i] + halfWidth;
1974 float top = points[i + 1] - halfWidth;
1975 float bottom = points [i + 1] + halfWidth;
1976 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07001977 }
1978
1979 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
1980}
1981
Romain Guy85bf02f2010-06-22 13:11:24 -07001982void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001983 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001984 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001985
Romain Guyae88e5e2010-10-22 17:49:18 -07001986 Rect& clip(*mSnapshot->clipRect);
1987 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001988
Romain Guy3d58c032010-07-14 16:34:53 -07001989 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001990}
Romain Guy9d5316e2010-06-24 19:30:36 -07001991
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001992void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001993 if (!texture) return;
1994 const AutoTexture autoCleanup(texture);
1995
1996 const float x = left + texture->left - texture->offset;
1997 const float y = top + texture->top - texture->offset;
1998
1999 drawPathTexture(texture, x, y, paint);
2000}
2001
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002002void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
2003 float rx, float ry, SkPaint* paint) {
2004 if (mSnapshot->isIgnored()) return;
2005
Romain Guya1d3c912011-12-13 14:55:06 -08002006 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002007 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2008 right - left, bottom - top, rx, ry, paint);
2009 drawShape(left, top, texture, paint);
2010}
2011
Romain Guy01d58e42011-01-19 21:54:02 -08002012void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2013 if (mSnapshot->isIgnored()) return;
2014
Romain Guya1d3c912011-12-13 14:55:06 -08002015 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002016 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002017 drawShape(x - radius, y - radius, texture, paint);
2018}
Romain Guy01d58e42011-01-19 21:54:02 -08002019
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002020void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2021 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002022
Romain Guya1d3c912011-12-13 14:55:06 -08002023 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002024 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2025 drawShape(left, top, texture, paint);
2026}
2027
Romain Guy8b2f5262011-01-23 16:15:02 -08002028void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2029 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2030 if (mSnapshot->isIgnored()) return;
2031
2032 if (fabs(sweepAngle) >= 360.0f) {
2033 drawOval(left, top, right, bottom, paint);
2034 return;
2035 }
2036
Romain Guya1d3c912011-12-13 14:55:06 -08002037 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002038 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2039 startAngle, sweepAngle, useCenter, paint);
2040 drawShape(left, top, texture, paint);
2041}
2042
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002043void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2044 SkPaint* paint) {
2045 if (mSnapshot->isIgnored()) return;
2046
Romain Guya1d3c912011-12-13 14:55:06 -08002047 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002048 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2049 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002050}
2051
Chet Haase5c13d892010-10-08 08:37:55 -07002052void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002053 if (p->getStyle() != SkPaint::kFill_Style) {
2054 drawRectAsShape(left, top, right, bottom, p);
2055 return;
2056 }
2057
Romain Guy6926c722010-07-12 20:20:03 -07002058 if (quickReject(left, top, right, bottom)) {
2059 return;
2060 }
2061
Romain Guy026c5e162010-06-28 17:12:22 -07002062 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002063 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002064 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2065 if (!isMode) {
2066 // Assume SRC_OVER
2067 mode = SkXfermode::kSrcOver_Mode;
2068 }
2069 } else {
2070 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002071 }
2072
Romain Guy026c5e162010-06-28 17:12:22 -07002073 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002074 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002075 drawAARect(left, top, right, bottom, color, mode);
2076 } else {
2077 drawColorRect(left, top, right, bottom, color, mode);
2078 }
Romain Guyc7d53492010-06-25 13:41:57 -07002079}
Romain Guy9d5316e2010-06-24 19:30:36 -07002080
Romain Guye8e62a42010-07-23 18:55:21 -07002081void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002082 float x, float y, SkPaint* paint, float length) {
Romain Guyb146b122010-12-15 17:06:45 -08002083 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07002084 return;
2085 }
Romain Guyaf636eb2010-12-09 17:47:21 -08002086 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07002087
Romain Guy8f9a9f62011-12-05 11:53:26 -08002088 // NOTE: AA and glyph id encoding are set in DisplayListRenderer.cpp
Romain Guye8e62a42010-07-23 18:55:21 -07002089
Romain Guye8e62a42010-07-23 18:55:21 -07002090 switch (paint->getTextAlign()) {
2091 case SkPaint::kCenter_Align:
Romain Guycac5fd32011-12-01 20:08:50 -08002092 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002093 x -= length / 2.0f;
2094 break;
2095 case SkPaint::kRight_Align:
Romain Guycac5fd32011-12-01 20:08:50 -08002096 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002097 x -= length;
2098 break;
2099 default:
2100 break;
2101 }
2102
Romain Guycac5fd32011-12-01 20:08:50 -08002103 SkPaint::FontMetrics metrics;
2104 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy8f9a9f62011-12-05 11:53:26 -08002105 // If no length was specified, just perform the hit test on the Y axis
Romain Guycac5fd32011-12-01 20:08:50 -08002106 if (quickReject(x, y + metrics.fTop,
2107 x + (length >= 0.0f ? length : INT_MAX / 2), y + metrics.fBottom)) {
2108 return;
2109 }
2110
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002111 const float oldX = x;
2112 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002113 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2114 if (pureTranslate) {
2115 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2116 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2117 }
2118
Romain Guyb45c0c92010-08-26 20:35:23 -07002119 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002120#if DEBUG_GLYPHS
2121 LOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
2122#endif
Romain Guyb45c0c92010-08-26 20:35:23 -07002123 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002124 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002125
Romain Guy86568192010-12-14 15:55:39 -08002126 int alpha;
2127 SkXfermode::Mode mode;
2128 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002129
Romain Guy1e45aae2010-08-13 19:39:53 -07002130 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07002131 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002132 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2133 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002134 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002135
Romain Guy740bf2b2011-04-26 15:33:10 -07002136 const float sx = oldX - shadow->left + mShadowDx;
2137 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002138
Romain Guy86568192010-12-14 15:55:39 -08002139 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002140 int shadowColor = mShadowColor;
2141 if (mShader) {
2142 shadowColor = 0xffffffff;
2143 }
Romain Guy86568192010-12-14 15:55:39 -08002144
Romain Guya1d3c912011-12-13 14:55:06 -08002145 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002146 setupDraw();
2147 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002148 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2149 setupDrawColorFilter();
2150 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002151 setupDrawBlending(true, mode);
2152 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002153 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002154 setupDrawTexture(shadow->id);
2155 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002156 setupDrawColorFilterUniforms();
2157 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002158 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2159
Romain Guy0a417492010-08-16 20:26:20 -07002160 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy740bf2b2011-04-26 15:33:10 -07002161
Romain Guy86568192010-12-14 15:55:39 -08002162 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07002163 }
2164
Romain Guyb146b122010-12-15 17:06:45 -08002165 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
2166 return;
2167 }
2168
Romain Guy6620c6d2010-12-06 18:07:02 -08002169 // Pick the appropriate texture filtering
2170 bool linearFilter = mSnapshot->transform->changesBounds();
2171 if (pureTranslate && !linearFilter) {
2172 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2173 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002174
Romain Guya1d3c912011-12-13 14:55:06 -08002175 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002176 setupDraw();
2177 setupDrawDirtyRegionsDisabled();
2178 setupDrawWithTexture(true);
2179 setupDrawAlpha8Color(paint->getColor(), alpha);
2180 setupDrawColorFilter();
2181 setupDrawShader();
2182 setupDrawBlending(true, mode);
2183 setupDrawProgram();
2184 setupDrawModelView(x, y, x, y, pureTranslate, true);
2185 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2186 setupDrawPureColorUniforms();
2187 setupDrawColorFilterUniforms();
2188 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002189
Romain Guy6620c6d2010-12-06 18:07:02 -08002190 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002191 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2192
2193#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002194 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002195#else
Romain Guyf219da52011-01-16 12:54:25 -08002196 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002197#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002198
Romain Guy6620c6d2010-12-06 18:07:02 -08002199 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002200 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002201#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002202 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002203 if (!pureTranslate) {
2204 mSnapshot->transform->mapRect(bounds);
2205 }
Romain Guyf219da52011-01-16 12:54:25 -08002206 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002207 }
2208#endif
2209 }
Romain Guy694b5192010-07-21 21:33:20 -07002210
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002211 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002212}
2213
Romain Guy7fbcc042010-08-04 15:40:07 -07002214void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002215 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002216
Romain Guya1d3c912011-12-13 14:55:06 -08002217 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002218
Romain Guyfb8b7632010-08-23 21:05:08 -07002219 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07002220 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002221 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002222
Romain Guy8b55f372010-08-18 17:10:07 -07002223 const float x = texture->left - texture->offset;
2224 const float y = texture->top - texture->offset;
2225
Romain Guy01d58e42011-01-19 21:54:02 -08002226 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002227}
2228
Romain Guyada830f2011-01-13 12:13:20 -08002229void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2230 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002231 return;
2232 }
2233
Romain Guya1d3c912011-12-13 14:55:06 -08002234 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002235
2236 int alpha;
2237 SkXfermode::Mode mode;
2238 getAlphaAndMode(paint, &alpha, &mode);
2239
Romain Guy9ace8f52011-07-07 20:50:11 -07002240 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002241
Romain Guyf219da52011-01-16 12:54:25 -08002242#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08002243 if (!layer->region.isEmpty()) {
2244 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002245 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002246 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002247 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002248 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002249
Romain Guyc88e3572011-01-22 00:32:12 -08002250 setupDraw();
2251 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002252 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002253 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002254 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002255 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002256 setupDrawPureColorUniforms();
2257 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002258 setupDrawTexture(layer->getTexture());
2259 if (mSnapshot->transform->isPureTranslate()) {
2260 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2261 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2262
Romain Guyd21b6e12011-11-30 20:21:23 -08002263 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002264 setupDrawModelViewTranslate(x, y,
2265 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2266 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002267 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002268 setupDrawModelViewTranslate(x, y,
2269 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2270 }
Romain Guyc88e3572011-01-22 00:32:12 -08002271 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002272
Romain Guyc88e3572011-01-22 00:32:12 -08002273 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2274 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002275
Romain Guyc88e3572011-01-22 00:32:12 -08002276 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002277
2278#if DEBUG_LAYERS_AS_REGIONS
2279 drawRegionRects(layer->region);
2280#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002281 }
Romain Guyf219da52011-01-16 12:54:25 -08002282 }
2283#else
Romain Guyada830f2011-01-13 12:13:20 -08002284 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2285 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002286#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002287}
2288
Romain Guy6926c722010-07-12 20:20:03 -07002289///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002290// Shaders
2291///////////////////////////////////////////////////////////////////////////////
2292
2293void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002294 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002295}
2296
Romain Guy06f96e22010-07-30 19:18:16 -07002297void OpenGLRenderer::setupShader(SkiaShader* shader) {
2298 mShader = shader;
2299 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002300 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002301 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002302}
2303
Romain Guyd27977d2010-07-14 19:18:51 -07002304///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002305// Color filters
2306///////////////////////////////////////////////////////////////////////////////
2307
2308void OpenGLRenderer::resetColorFilter() {
2309 mColorFilter = NULL;
2310}
2311
2312void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2313 mColorFilter = filter;
2314}
2315
2316///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002317// Drop shadow
2318///////////////////////////////////////////////////////////////////////////////
2319
2320void OpenGLRenderer::resetShadow() {
2321 mHasShadow = false;
2322}
2323
2324void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2325 mHasShadow = true;
2326 mShadowRadius = radius;
2327 mShadowDx = dx;
2328 mShadowDy = dy;
2329 mShadowColor = color;
2330}
2331
2332///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002333// Drawing implementation
2334///////////////////////////////////////////////////////////////////////////////
2335
Romain Guy01d58e42011-01-19 21:54:02 -08002336void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2337 float x, float y, SkPaint* paint) {
2338 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2339 return;
2340 }
2341
2342 int alpha;
2343 SkXfermode::Mode mode;
2344 getAlphaAndMode(paint, &alpha, &mode);
2345
2346 setupDraw();
2347 setupDrawWithTexture(true);
2348 setupDrawAlpha8Color(paint->getColor(), alpha);
2349 setupDrawColorFilter();
2350 setupDrawShader();
2351 setupDrawBlending(true, mode);
2352 setupDrawProgram();
2353 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2354 setupDrawTexture(texture->id);
2355 setupDrawPureColorUniforms();
2356 setupDrawColorFilterUniforms();
2357 setupDrawShaderUniforms();
2358 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2359
2360 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2361
2362 finishDrawTexture();
2363}
2364
Romain Guyf607bdc2010-09-10 19:20:06 -07002365// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002366#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2367#define kStdUnderline_Offset (1.0f / 9.0f)
2368#define kStdUnderline_Thickness (1.0f / 18.0f)
2369
2370void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2371 float x, float y, SkPaint* paint) {
2372 // Handle underline and strike-through
2373 uint32_t flags = paint->getFlags();
2374 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002375 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002376 float underlineWidth = length;
2377 // If length is > 0.0f, we already measured the text for the text alignment
2378 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002379 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002380 }
2381
2382 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002383 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002384 case SkPaint::kCenter_Align:
2385 offsetX = underlineWidth * 0.5f;
2386 break;
2387 case SkPaint::kRight_Align:
2388 offsetX = underlineWidth;
2389 break;
2390 default:
2391 break;
2392 }
2393
2394 if (underlineWidth > 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002395 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002396 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002397
Romain Guye20ecbd2010-09-22 19:49:04 -07002398 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002399 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002400
Romain Guyf6834472011-01-23 13:32:12 -08002401 int linesCount = 0;
2402 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2403 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2404
2405 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002406 float points[pointsCount];
2407 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002408
2409 if (flags & SkPaint::kUnderlineText_Flag) {
2410 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002411 points[currentPoint++] = left;
2412 points[currentPoint++] = top;
2413 points[currentPoint++] = left + underlineWidth;
2414 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002415 }
2416
2417 if (flags & SkPaint::kStrikeThruText_Flag) {
2418 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002419 points[currentPoint++] = left;
2420 points[currentPoint++] = top;
2421 points[currentPoint++] = left + underlineWidth;
2422 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002423 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002424
Romain Guy726aeba2011-06-01 14:52:00 -07002425 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002426
Romain Guy726aeba2011-06-01 14:52:00 -07002427 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002428 }
2429 }
2430}
2431
Romain Guy026c5e162010-06-28 17:12:22 -07002432void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002433 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002434 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002435 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002436 color |= 0x00ffffff;
2437 }
2438
Romain Guy70ca14e2010-12-13 18:24:33 -08002439 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002440 setupDrawNoTexture();
Romain Guy70ca14e2010-12-13 18:24:33 -08002441 setupDrawColor(color);
2442 setupDrawShader();
2443 setupDrawColorFilter();
2444 setupDrawBlending(mode);
2445 setupDrawProgram();
2446 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2447 setupDrawColorUniforms();
2448 setupDrawShaderUniforms(ignoreTransform);
2449 setupDrawColorFilterUniforms();
2450 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002451
Romain Guyc95c8d62010-09-17 15:31:32 -07002452 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2453}
2454
Romain Guy82ba8142010-07-09 13:25:56 -07002455void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002456 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002457 int alpha;
2458 SkXfermode::Mode mode;
2459 getAlphaAndMode(paint, &alpha, &mode);
2460
Romain Guyd21b6e12011-11-30 20:21:23 -08002461 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002462
Romain Guy6620c6d2010-12-06 18:07:02 -08002463 if (mSnapshot->transform->isPureTranslate()) {
2464 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2465 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2466
Romain Guyd21b6e12011-11-30 20:21:23 -08002467 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002468 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2469 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2470 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2471 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002472 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002473 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2474 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2475 GL_TRIANGLE_STRIP, gMeshCount);
2476 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002477}
2478
Romain Guybd6b79b2010-06-26 00:13:53 -07002479void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002480 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2481 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002482 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002483}
2484
2485void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002486 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002487 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002488 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002489
Romain Guy746b7402010-10-26 16:27:31 -07002490 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002491 setupDrawWithTexture();
2492 setupDrawColor(alpha, alpha, alpha, alpha);
2493 setupDrawColorFilter();
2494 setupDrawBlending(blend, mode, swapSrcDst);
2495 setupDrawProgram();
2496 if (!dirty) {
2497 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002498 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002499 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002500 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002501 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002502 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002503 }
Romain Guy86568192010-12-14 15:55:39 -08002504 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002505 setupDrawColorFilterUniforms();
2506 setupDrawTexture(texture);
2507 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002508
Romain Guy6820ac82010-09-15 18:11:50 -07002509 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002510
2511 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002512}
2513
Romain Guya5aed0d2010-09-09 14:42:43 -07002514void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002515 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002516 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2517 if (blend) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002518 if (mode <= SkXfermode::kScreen_Mode) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002519 if (!mCaches.blend) {
2520 glEnable(GL_BLEND);
2521 }
Romain Guy82ba8142010-07-09 13:25:56 -07002522
Romain Guyf607bdc2010-09-10 19:20:06 -07002523 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2524 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07002525
Romain Guya5aed0d2010-09-09 14:42:43 -07002526 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2527 glBlendFunc(sourceMode, destMode);
2528 mCaches.lastSrcMode = sourceMode;
2529 mCaches.lastDstMode = destMode;
2530 }
2531 } else {
2532 // These blend modes are not supported by OpenGL directly and have
2533 // to be implemented using shaders. Since the shader will perform
2534 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07002535 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002536 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002537 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002538 }
2539
2540 if (mCaches.blend) {
2541 glDisable(GL_BLEND);
2542 }
2543 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07002544 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002545 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002546 glDisable(GL_BLEND);
2547 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002548 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002549}
2550
Romain Guy889f8d12010-07-29 14:37:42 -07002551bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002552 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002553 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002554 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002555 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002556 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002557 }
Romain Guy6926c722010-07-12 20:20:03 -07002558 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002559}
2560
Romain Guy026c5e162010-06-28 17:12:22 -07002561void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002562 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002563 TextureVertex::setUV(v++, u1, v1);
2564 TextureVertex::setUV(v++, u2, v1);
2565 TextureVertex::setUV(v++, u1, v2);
2566 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002567}
2568
Chet Haase5c13d892010-10-08 08:37:55 -07002569void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002570 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002571 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002572
2573 // Skia draws using the color's alpha channel if < 255
2574 // Otherwise, it uses the paint's alpha
2575 int color = paint->getColor();
2576 *alpha = (color >> 24) & 0xFF;
2577 if (*alpha == 255) {
2578 *alpha = paint->getAlpha();
2579 }
2580 } else {
2581 *mode = SkXfermode::kSrcOver_Mode;
2582 *alpha = 255;
2583 }
Romain Guy026c5e162010-06-28 17:12:22 -07002584}
2585
Romain Guya5aed0d2010-09-09 14:42:43 -07002586SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002587 SkXfermode::Mode resultMode;
2588 if (!SkXfermode::AsMode(mode, &resultMode)) {
2589 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002590 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002591 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002592}
2593
Romain Guy9d5316e2010-06-24 19:30:36 -07002594}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002595}; // namespace android