blob: f7d9040108e85198600cc7d14b48f4a11938bf27 [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 Guy5ff9df62012-01-23 17:09:05 -0800114 mHasDrawFilter = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700115
Romain Guyac670c02010-07-27 17:39:27 -0700116 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
117
Romain Guyae5575b2010-07-29 18:48:04 -0700118 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700119}
120
Romain Guy85bf02f2010-06-22 13:11:24 -0700121OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700122 // The context has already been destroyed at this point, do not call
123 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700124}
125
Romain Guyf6a11b82010-06-23 17:47:49 -0700126///////////////////////////////////////////////////////////////////////////////
127// Setup
128///////////////////////////////////////////////////////////////////////////////
129
Romain Guy530041d2012-01-25 18:56:29 -0800130uint32_t OpenGLRenderer::getStencilSize() {
131 return STENCIL_BUFFER_SIZE;
132}
133
Romain Guy85bf02f2010-06-22 13:11:24 -0700134void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700135 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700136
137 mWidth = width;
138 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700139
140 mFirstSnapshot->height = height;
141 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700142
Romain Guy3e263fa2011-12-12 16:47:48 -0800143 glDisable(GL_DITHER);
Romain Guy39d252a2011-12-12 18:14:06 -0800144 glEnable(GL_SCISSOR_TEST);
Romain Guy3e263fa2011-12-12 16:47:48 -0800145 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guy15bc6432011-12-13 13:11:32 -0800146
Romain Guy3e263fa2011-12-12 16:47:48 -0800147 glEnableVertexAttribArray(Program::kBindingPosition);
Romain Guye4d01122010-06-16 18:44:05 -0700148}
149
Romain Guy6b7bd242010-10-06 19:49:23 -0700150void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800151 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
152}
153
154void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800155 mCaches.clearGarbage();
156
Romain Guy8aef54f2010-09-01 15:13:49 -0700157 mSnapshot = new Snapshot(mFirstSnapshot,
158 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800159 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700160 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700161
Romain Guy39d252a2011-12-12 18:14:06 -0800162 glViewport(0, 0, mWidth, mHeight);
Romain Guy8f85e802011-12-14 19:23:32 -0800163 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy39d252a2011-12-12 18:14:06 -0800164
Romain Guy7d7b5492011-01-24 16:33:45 -0800165 mSnapshot->setClip(left, top, right, bottom);
Romain Guy39d252a2011-12-12 18:14:06 -0800166 mDirtyClip = false;
Romain Guy7d7b5492011-01-24 16:33:45 -0800167
Romain Guy6b7bd242010-10-06 19:49:23 -0700168 if (!opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700169 glClear(GL_COLOR_BUFFER_BIT);
170 }
Romain Guybb9524b2010-06-22 18:56:38 -0700171}
172
Romain Guyb025b9c2010-09-16 14:16:48 -0700173void OpenGLRenderer::finish() {
174#if DEBUG_OPENGL
175 GLenum status = GL_NO_ERROR;
176 while ((status = glGetError()) != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000177 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800178 switch (status) {
179 case GL_OUT_OF_MEMORY:
Steve Block3762c312012-01-06 19:20:56 +0000180 ALOGE(" OpenGLRenderer is out of memory!");
Romain Guya07105b2011-01-10 21:14:18 -0800181 break;
182 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700183 }
184#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800185#if DEBUG_MEMORY_USAGE
186 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800187#else
188 if (mCaches.getDebugLevel() & kDebugMemory) {
189 mCaches.dumpMemoryUsage();
190 }
Romain Guyc15008e2010-11-10 11:59:15 -0800191#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700192}
193
Romain Guy6c319ca2011-01-11 14:29:25 -0800194void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700195 if (mCaches.currentProgram) {
196 if (mCaches.currentProgram->isInUse()) {
197 mCaches.currentProgram->remove();
198 mCaches.currentProgram = NULL;
199 }
200 }
Romain Guy50c0f092010-10-19 11:42:22 -0700201 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800202 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800203 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800204 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700205}
206
Romain Guy6c319ca2011-01-11 14:29:25 -0800207void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800208 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
209
210 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy3e263fa2011-12-12 16:47:48 -0800211 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
212
Romain Guyda8532c2010-08-31 11:50:35 -0700213 glEnable(GL_SCISSOR_TEST);
Romain Guy82bc7a72012-01-03 14:13:39 -0800214 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700215 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700216
Romain Guya1d3c912011-12-13 14:55:06 -0800217 mCaches.activeTexture(0);
Chet Haase08837c22011-11-28 11:53:21 -0800218 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guyf607bdc2010-09-10 19:20:06 -0700219
Romain Guy50c0f092010-10-19 11:42:22 -0700220 mCaches.blend = true;
221 glEnable(GL_BLEND);
222 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
223 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700224}
225
Romain Guycabfcc12011-03-07 18:06:46 -0800226bool OpenGLRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800227 interrupt();
Romain Guyf90f8172011-01-25 22:53:24 -0800228 if (mDirtyClip) {
229 setScissorFromClip();
230 }
Romain Guyd643bb52011-03-01 14:55:21 -0800231
Romain Guy80911b82011-03-16 15:30:12 -0700232 Rect clip(*mSnapshot->clipRect);
233 clip.snapToPixelBoundaries();
234
Romain Guyd643bb52011-03-01 14:55:21 -0800235#if RENDER_LAYERS_AS_REGIONS
236 // Since we don't know what the functor will draw, let's dirty
237 // tne entire clip region
238 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800239 dirtyLayerUnchecked(clip, getRegion());
240 }
241#endif
242
Romain Guy08aa2cb2011-03-17 11:06:57 -0700243 DrawGlInfo info;
244 info.clipLeft = clip.left;
245 info.clipTop = clip.top;
246 info.clipRight = clip.right;
247 info.clipBottom = clip.bottom;
248 info.isLayer = hasLayer();
249 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700250
Romain Guy08aa2cb2011-03-17 11:06:57 -0700251 status_t result = (*functor)(0, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800252
253 if (result != 0) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700254 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800255 dirty.unionWith(localDirty);
256 }
257
Chet Haasedaf98e92011-01-10 14:10:36 -0800258 resume();
Romain Guycabfcc12011-03-07 18:06:46 -0800259 return result != 0;
Chet Haasedaf98e92011-01-10 14:10:36 -0800260}
261
Romain Guyf6a11b82010-06-23 17:47:49 -0700262///////////////////////////////////////////////////////////////////////////////
263// State management
264///////////////////////////////////////////////////////////////////////////////
265
Romain Guybb9524b2010-06-22 18:56:38 -0700266int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700267 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700268}
269
270int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700271 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700272}
273
274void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700275 if (mSaveCount > 1) {
276 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700277 }
Romain Guybb9524b2010-06-22 18:56:38 -0700278}
279
280void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700281 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700282
Romain Guy8fb95422010-08-17 18:38:51 -0700283 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700284 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700285 }
Romain Guybb9524b2010-06-22 18:56:38 -0700286}
287
Romain Guy8aef54f2010-09-01 15:13:49 -0700288int OpenGLRenderer::saveSnapshot(int flags) {
289 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700290 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700291}
292
293bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700294 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700295 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700296 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700297
Romain Guybd6b79b2010-06-26 00:13:53 -0700298 sp<Snapshot> current = mSnapshot;
299 sp<Snapshot> previous = mSnapshot->previous;
300
Romain Guyeb993562010-10-05 18:14:38 -0700301 if (restoreOrtho) {
302 Rect& r = previous->viewport;
303 glViewport(r.left, r.top, r.right, r.bottom);
304 mOrthoMatrix.load(current->orthoMatrix);
305 }
306
Romain Guy8b55f372010-08-18 17:10:07 -0700307 mSaveCount--;
308 mSnapshot = previous;
309
Romain Guy2542d192010-08-18 11:47:12 -0700310 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700311 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700312 }
Romain Guy2542d192010-08-18 11:47:12 -0700313
Romain Guy5ec99242010-11-03 16:19:08 -0700314 if (restoreLayer) {
315 composeLayer(current, previous);
316 }
317
Romain Guy2542d192010-08-18 11:47:12 -0700318 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700319}
320
Romain Guyf6a11b82010-06-23 17:47:49 -0700321///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700322// Layers
323///////////////////////////////////////////////////////////////////////////////
324
325int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700326 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700327 const GLuint previousFbo = mSnapshot->fbo;
328 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700329
Romain Guyaf636eb2010-12-09 17:47:21 -0800330 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700331 int alpha = 255;
332 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700333
Romain Guye45362c2010-11-03 19:58:32 -0700334 if (p) {
335 alpha = p->getAlpha();
336 if (!mCaches.extensions.hasFramebufferFetch()) {
337 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
338 if (!isMode) {
339 // Assume SRC_OVER
340 mode = SkXfermode::kSrcOver_Mode;
341 }
342 } else {
343 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700344 }
345 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700346 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700347 }
Romain Guyd55a8612010-06-28 17:42:46 -0700348
Romain Guydbc26d22010-10-11 17:58:29 -0700349 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
350 }
Romain Guyd55a8612010-06-28 17:42:46 -0700351
352 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700353}
354
355int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
356 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700357 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700358 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700359 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700360 SkPaint paint;
361 paint.setAlpha(alpha);
362 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700363 }
Romain Guyd55a8612010-06-28 17:42:46 -0700364}
Romain Guybd6b79b2010-06-26 00:13:53 -0700365
Romain Guy1c740bc2010-09-13 18:00:09 -0700366/**
367 * Layers are viewed by Skia are slightly different than layers in image editing
368 * programs (for instance.) When a layer is created, previously created layers
369 * and the frame buffer still receive every drawing command. For instance, if a
370 * layer is created and a shape intersecting the bounds of the layers and the
371 * framebuffer is draw, the shape will be drawn on both (unless the layer was
372 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
373 *
374 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
375 * texture. Unfortunately, this is inefficient as it requires every primitive to
376 * be drawn n + 1 times, where n is the number of active layers. In practice this
377 * means, for every primitive:
378 * - Switch active frame buffer
379 * - Change viewport, clip and projection matrix
380 * - Issue the drawing
381 *
382 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700383 * To avoid this, layers are implemented in a different way here, at least in the
384 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
385 * is set. When this flag is set we can redirect all drawing operations into a
386 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700387 *
388 * This implementation relies on the frame buffer being at least RGBA 8888. When
389 * a layer is created, only a texture is created, not an FBO. The content of the
390 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700391 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700392 * buffer and drawing continues as normal. This technique therefore treats the
393 * frame buffer as a scratch buffer for the layers.
394 *
395 * To compose the layers back onto the frame buffer, each layer texture
396 * (containing the original frame buffer data) is drawn as a simple quad over
397 * the frame buffer. The trick is that the quad is set as the composition
398 * destination in the blending equation, and the frame buffer becomes the source
399 * of the composition.
400 *
401 * Drawing layers with an alpha value requires an extra step before composition.
402 * An empty quad is drawn over the layer's region in the frame buffer. This quad
403 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
404 * quad is used to multiply the colors in the frame buffer. This is achieved by
405 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
406 * GL_ZERO, GL_SRC_ALPHA.
407 *
408 * Because glCopyTexImage2D() can be slow, an alternative implementation might
409 * be use to draw a single clipped layer. The implementation described above
410 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700411 *
412 * (1) The frame buffer is actually not cleared right away. To allow the GPU
413 * to potentially optimize series of calls to glCopyTexImage2D, the frame
414 * buffer is left untouched until the first drawing operation. Only when
415 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700416 */
Romain Guyd55a8612010-06-28 17:42:46 -0700417bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700418 float right, float bottom, int alpha, SkXfermode::Mode mode,
419 int flags, GLuint previousFbo) {
420 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700421 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700422
Romain Guyeb993562010-10-05 18:14:38 -0700423 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
424
Romain Guyf607bdc2010-09-10 19:20:06 -0700425 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700426 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700427 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700428 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700429
Romain Guyeb993562010-10-05 18:14:38 -0700430 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700431 if (bounds.intersect(*snapshot->clipRect)) {
432 // We cannot work with sub-pixels in this case
433 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700434
Romain Guyad37cd32011-03-15 11:12:25 -0700435 // When the layer is not an FBO, we may use glCopyTexImage so we
436 // need to make sure the layer does not extend outside the bounds
437 // of the framebuffer
438 if (!bounds.intersect(snapshot->previous->viewport)) {
439 bounds.setEmpty();
440 }
441 } else {
442 bounds.setEmpty();
443 }
Romain Guyeb993562010-10-05 18:14:38 -0700444 }
Romain Guybf434112010-09-16 14:40:17 -0700445
Romain Guy746b7402010-10-26 16:27:31 -0700446 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
447 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800448 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700449 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700450 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700451 }
452
453 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800454 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700455 return false;
456 }
Romain Guyf18fd992010-07-08 11:45:51 -0700457
Romain Guya1d3c912011-12-13 14:55:06 -0800458 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700459 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700460 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700461 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700462 }
463
Romain Guy9ace8f52011-07-07 20:50:11 -0700464 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700465 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700466 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
467 bounds.getWidth() / float(layer->getWidth()), 0.0f);
468 layer->setColorFilter(mColorFilter);
Romain Guydda570202010-07-06 11:39:32 -0700469
Romain Guy8fb95422010-08-17 18:38:51 -0700470 // Save the layer in the snapshot
471 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700472 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700473
Romain Guyeb993562010-10-05 18:14:38 -0700474 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700475 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700476 } else {
477 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700478 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800479 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700480 if (layer->isEmpty()) {
481 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
482 bounds.left, snapshot->height - bounds.bottom,
483 layer->getWidth(), layer->getHeight(), 0);
484 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800485 } else {
486 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
487 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
488 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700489
Romain Guy54be1cd2011-06-13 19:04:27 -0700490 // Enqueue the buffer coordinates to clear the corresponding region later
491 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700492 }
Romain Guyeb993562010-10-05 18:14:38 -0700493 }
Romain Guyf86ef572010-07-01 11:05:42 -0700494
Romain Guyd55a8612010-06-28 17:42:46 -0700495 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700496}
497
Romain Guy5b3b3522010-10-27 18:57:51 -0700498bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
499 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700500 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700501
502#if RENDER_LAYERS_AS_REGIONS
503 snapshot->region = &snapshot->layer->region;
504 snapshot->flags |= Snapshot::kFlagFboTarget;
505#endif
506
507 Rect clip(bounds);
508 snapshot->transform->mapRect(clip);
509 clip.intersect(*snapshot->clipRect);
510 clip.snapToPixelBoundaries();
511 clip.intersect(snapshot->previous->viewport);
512
513 mat4 inverse;
514 inverse.loadInverse(*mSnapshot->transform);
515
516 inverse.mapRect(clip);
517 clip.snapToPixelBoundaries();
518 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700519 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700520
521 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700522 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700523 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700524 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
525 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
526 snapshot->height = bounds.getHeight();
527 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
528 snapshot->orthoMatrix.load(mOrthoMatrix);
529
530 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700531 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
532 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700533
534 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700535 if (layer->isEmpty()) {
536 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
537 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700538 }
539
540 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700541 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700542
543#if DEBUG_LAYERS_AS_REGIONS
544 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
545 if (status != GL_FRAMEBUFFER_COMPLETE) {
Steve Block3762c312012-01-06 19:20:56 +0000546 ALOGE("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guy5b3b3522010-10-27 18:57:51 -0700547
548 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700549 layer->deleteTexture();
550 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700551
552 delete layer;
553
554 return false;
555 }
556#endif
557
558 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy8f85e802011-12-14 19:23:32 -0800559 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700560 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700561 glClear(GL_COLOR_BUFFER_BIT);
562
563 dirtyClip();
564
565 // Change the ortho projection
566 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
567 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
568
569 return true;
570}
571
Romain Guy1c740bc2010-09-13 18:00:09 -0700572/**
573 * Read the documentation of createLayer() before doing anything in this method.
574 */
Romain Guy1d83e192010-08-17 11:37:00 -0700575void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
576 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000577 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700578 return;
579 }
580
Romain Guy5b3b3522010-10-27 18:57:51 -0700581 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700582
583 if (fboLayer) {
584 // Unbind current FBO and restore previous one
585 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
586 }
587
Romain Guy1d83e192010-08-17 11:37:00 -0700588 Layer* layer = current->layer;
589 const Rect& rect = layer->layer;
590
Romain Guy9ace8f52011-07-07 20:50:11 -0700591 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700592 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700593 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700594 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700595 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700596 }
597
Romain Guy03750a02010-10-18 14:06:08 -0700598 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700599
Romain Guya1d3c912011-12-13 14:55:06 -0800600 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700601
Romain Guy5b3b3522010-10-27 18:57:51 -0700602 // When the layer is stored in an FBO, we can save a bit of fillrate by
603 // drawing only the dirty region
604 if (fboLayer) {
605 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700606 if (layer->getColorFilter()) {
607 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800608 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700609 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700610 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800611 resetColorFilter();
612 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700613 } else if (!rect.isEmpty()) {
614 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
615 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700616 }
Romain Guy8b55f372010-08-18 17:10:07 -0700617
Romain Guyeb993562010-10-05 18:14:38 -0700618 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800619 // Note: No need to use glDiscardFramebufferEXT() since we never
620 // create/compose layers that are not on screen with this
621 // code path
622 // See LayerRenderer::destroyLayer(Layer*)
623
Romain Guyeb993562010-10-05 18:14:38 -0700624 // Detach the texture from the FBO
625 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
626 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
627 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
628
629 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
630 mCaches.fboCache.put(current->fbo);
631 }
632
Romain Guy746b7402010-10-26 16:27:31 -0700633 dirtyClip();
634
Romain Guyeb993562010-10-05 18:14:38 -0700635 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700636 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700637 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700638 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700639 delete layer;
640 }
641}
642
Romain Guyaa6c24c2011-04-28 18:40:04 -0700643void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700644 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700645
Romain Guy302a9df2011-08-16 13:55:02 -0700646 mat4& transform = layer->getTransform();
647 if (!transform.isIdentity()) {
648 save(0);
649 mSnapshot->transform->multiply(transform);
650 }
651
Romain Guyaa6c24c2011-04-28 18:40:04 -0700652 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700653 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700654 setupDrawWithTexture();
655 } else {
656 setupDrawWithExternalTexture();
657 }
658 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700659 setupDrawColor(alpha, alpha, alpha, alpha);
660 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700661 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700662 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700663 setupDrawPureColorUniforms();
664 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700665 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
666 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700667 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700668 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700669 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700670 if (mSnapshot->transform->isPureTranslate() &&
671 layer->getWidth() == (uint32_t) rect.getWidth() &&
672 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700673 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
674 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
675
Romain Guyd21b6e12011-11-30 20:21:23 -0800676 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700677 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
678 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800679 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700680 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
681 }
682 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700683 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
684
685 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
686
687 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700688
689 if (!transform.isIdentity()) {
690 restore();
691 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700692}
693
Romain Guy5b3b3522010-10-27 18:57:51 -0700694void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700695 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700696 const Rect& texCoords = layer->texCoords;
697 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
698 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700699
Romain Guy9ace8f52011-07-07 20:50:11 -0700700 float x = rect.left;
701 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700702 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700703 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700704 layer->getHeight() == (uint32_t) rect.getHeight();
705
706 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700707 // When we're swapping, the layer is already in screen coordinates
708 if (!swap) {
709 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
710 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
711 }
712
Romain Guyd21b6e12011-11-30 20:21:23 -0800713 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700714 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800715 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700716 }
717
718 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
719 layer->getTexture(), layer->getAlpha() / 255.0f,
720 layer->getMode(), layer->isBlend(),
721 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
722 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700723
Romain Guyaa6c24c2011-04-28 18:40:04 -0700724 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
725 } else {
726 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
727 drawTextureLayer(layer, rect);
728 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
729 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700730}
731
732void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
733#if RENDER_LAYERS_AS_REGIONS
734 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700735 layer->setRegionAsRect();
736
Romain Guy40667672011-03-18 14:34:03 -0700737 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700738
Romain Guy5b3b3522010-10-27 18:57:51 -0700739 layer->region.clear();
740 return;
741 }
742
Romain Guy8a3957d2011-09-07 17:55:15 -0700743 // TODO: See LayerRenderer.cpp::generateMesh() for important
744 // information about this implementation
Romain Guy5b3b3522010-10-27 18:57:51 -0700745 if (!layer->region.isEmpty()) {
746 size_t count;
747 const android::Rect* rects = layer->region.getArray(&count);
748
Romain Guy9ace8f52011-07-07 20:50:11 -0700749 const float alpha = layer->getAlpha() / 255.0f;
750 const float texX = 1.0f / float(layer->getWidth());
751 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800752 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700753
754 TextureVertex* mesh = mCaches.getRegionMesh();
755 GLsizei numQuads = 0;
756
Romain Guy7230a742011-01-10 22:26:16 -0800757 setupDraw();
758 setupDrawWithTexture();
759 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800760 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700761 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800762 setupDrawProgram();
763 setupDrawDirtyRegionsDisabled();
764 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800765 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700766 setupDrawTexture(layer->getTexture());
767 if (mSnapshot->transform->isPureTranslate()) {
768 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
769 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
770
Romain Guyd21b6e12011-11-30 20:21:23 -0800771 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700772 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
773 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800774 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700775 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
776 }
Romain Guy15bc6432011-12-13 13:11:32 -0800777 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700778
779 for (size_t i = 0; i < count; i++) {
780 const android::Rect* r = &rects[i];
781
782 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800783 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700784 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800785 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700786
787 // TODO: Reject quads outside of the clip
788 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
789 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
790 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
791 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
792
793 numQuads++;
794
795 if (numQuads >= REGION_MESH_QUAD_COUNT) {
796 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
797 numQuads = 0;
798 mesh = mCaches.getRegionMesh();
799 }
800 }
801
802 if (numQuads > 0) {
803 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
804 }
805
Romain Guy7230a742011-01-10 22:26:16 -0800806 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700807
808#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800809 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700810#endif
811
812 layer->region.clear();
813 }
814#else
815 composeLayerRect(layer, rect);
816#endif
817}
818
Romain Guy3a3133d2011-02-01 22:59:58 -0800819void OpenGLRenderer::drawRegionRects(const Region& region) {
820#if DEBUG_LAYERS_AS_REGIONS
821 size_t count;
822 const android::Rect* rects = region.getArray(&count);
823
824 uint32_t colors[] = {
825 0x7fff0000, 0x7f00ff00,
826 0x7f0000ff, 0x7fff00ff,
827 };
828
829 int offset = 0;
830 int32_t top = rects[0].top;
831
832 for (size_t i = 0; i < count; i++) {
833 if (top != rects[i].top) {
834 offset ^= 0x2;
835 top = rects[i].top;
836 }
837
838 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
839 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
840 SkXfermode::kSrcOver_Mode);
841 }
842#endif
843}
844
Romain Guy5b3b3522010-10-27 18:57:51 -0700845void OpenGLRenderer::dirtyLayer(const float left, const float top,
846 const float right, const float bottom, const mat4 transform) {
847#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800848 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700849 Rect bounds(left, top, right, bottom);
850 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800851 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700852 }
853#endif
854}
855
856void OpenGLRenderer::dirtyLayer(const float left, const float top,
857 const float right, const float bottom) {
858#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800859 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700860 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800861 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800862 }
863#endif
864}
865
866void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
867#if RENDER_LAYERS_AS_REGIONS
868 if (bounds.intersect(*mSnapshot->clipRect)) {
869 bounds.snapToPixelBoundaries();
870 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
871 if (!dirty.isEmpty()) {
872 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700873 }
874 }
875#endif
876}
877
Romain Guy54be1cd2011-06-13 19:04:27 -0700878void OpenGLRenderer::clearLayerRegions() {
879 const size_t count = mLayers.size();
880 if (count == 0) return;
881
882 if (!mSnapshot->isIgnored()) {
883 // Doing several glScissor/glClear here can negatively impact
884 // GPUs with a tiler architecture, instead we draw quads with
885 // the Clear blending mode
886
887 // The list contains bounds that have already been clipped
888 // against their initial clip rect, and the current clip
889 // is likely different so we need to disable clipping here
890 glDisable(GL_SCISSOR_TEST);
891
892 Vertex mesh[count * 6];
893 Vertex* vertex = mesh;
894
895 for (uint32_t i = 0; i < count; i++) {
896 Rect* bounds = mLayers.itemAt(i);
897
898 Vertex::set(vertex++, bounds->left, bounds->bottom);
899 Vertex::set(vertex++, bounds->left, bounds->top);
900 Vertex::set(vertex++, bounds->right, bounds->top);
901 Vertex::set(vertex++, bounds->left, bounds->bottom);
902 Vertex::set(vertex++, bounds->right, bounds->top);
903 Vertex::set(vertex++, bounds->right, bounds->bottom);
904
905 delete bounds;
906 }
907
908 setupDraw(false);
909 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
910 setupDrawBlending(true, SkXfermode::kClear_Mode);
911 setupDrawProgram();
912 setupDrawPureColorUniforms();
913 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -0800914 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -0700915
Romain Guy54be1cd2011-06-13 19:04:27 -0700916 glDrawArrays(GL_TRIANGLES, 0, count * 6);
917
918 glEnable(GL_SCISSOR_TEST);
919 } else {
920 for (uint32_t i = 0; i < count; i++) {
921 delete mLayers.itemAt(i);
922 }
923 }
924
925 mLayers.clear();
926}
927
Romain Guybd6b79b2010-06-26 00:13:53 -0700928///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700929// Transforms
930///////////////////////////////////////////////////////////////////////////////
931
932void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700933 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700934}
935
936void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700937 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700938}
939
940void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700941 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700942}
943
Romain Guy807daf72011-01-18 11:19:19 -0800944void OpenGLRenderer::skew(float sx, float sy) {
945 mSnapshot->transform->skew(sx, sy);
946}
947
Romain Guyf6a11b82010-06-23 17:47:49 -0700948void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -0700949 if (matrix) {
950 mSnapshot->transform->load(*matrix);
951 } else {
952 mSnapshot->transform->loadIdentity();
953 }
Romain Guyf6a11b82010-06-23 17:47:49 -0700954}
955
956void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700957 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700958}
959
960void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700961 SkMatrix transform;
962 mSnapshot->transform->copyTo(transform);
963 transform.preConcat(*matrix);
964 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700965}
966
967///////////////////////////////////////////////////////////////////////////////
968// Clipping
969///////////////////////////////////////////////////////////////////////////////
970
Romain Guybb9524b2010-06-22 18:56:38 -0700971void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700972 Rect clip(*mSnapshot->clipRect);
973 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -0800974
975 mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
976 clip.getWidth(), clip.getHeight());
977
Romain Guy746b7402010-10-26 16:27:31 -0700978 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700979}
980
981const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700982 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700983}
984
Romain Guyc7d53492010-06-25 13:41:57 -0700985bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800986 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700987 return true;
988 }
989
Romain Guy1d83e192010-08-17 11:37:00 -0700990 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700991 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700992 r.snapToPixelBoundaries();
993
994 Rect clipRect(*mSnapshot->clipRect);
995 clipRect.snapToPixelBoundaries();
996
997 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700998}
999
Romain Guy079ba2c2010-07-16 14:12:24 -07001000bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
1001 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001002 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001003 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001004 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001005 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001006}
1007
Romain Guyf6a11b82010-06-23 17:47:49 -07001008///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001009// Drawing commands
1010///////////////////////////////////////////////////////////////////////////////
1011
Romain Guy54be1cd2011-06-13 19:04:27 -07001012void OpenGLRenderer::setupDraw(bool clear) {
1013 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001014 if (mDirtyClip) {
1015 setScissorFromClip();
1016 }
1017 mDescription.reset();
1018 mSetShaderColor = false;
1019 mColorSet = false;
1020 mColorA = mColorR = mColorG = mColorB = 0.0f;
1021 mTextureUnit = 0;
1022 mTrackDirtyRegions = true;
1023}
1024
1025void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1026 mDescription.hasTexture = true;
1027 mDescription.hasAlpha8Texture = isAlpha8;
1028}
1029
Romain Guyaa6c24c2011-04-28 18:40:04 -07001030void OpenGLRenderer::setupDrawWithExternalTexture() {
1031 mDescription.hasExternalTexture = true;
1032}
1033
Romain Guy15bc6432011-12-13 13:11:32 -08001034void OpenGLRenderer::setupDrawNoTexture() {
1035 mCaches.disbaleTexCoordsVertexArray();
1036}
1037
Chet Haase5b0200b2011-04-13 17:58:08 -07001038void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001039 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001040}
1041
Romain Guyed6fcb02011-03-21 13:11:28 -07001042void OpenGLRenderer::setupDrawPoint(float pointSize) {
1043 mDescription.isPoint = true;
1044 mDescription.pointSize = pointSize;
1045}
1046
Romain Guy70ca14e2010-12-13 18:24:33 -08001047void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001048 setupDrawColor(color, (color >> 24) & 0xFF);
1049}
1050
1051void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1052 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001053 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1054 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001055 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001056 mColorR = a * ((color >> 16) & 0xFF);
1057 mColorG = a * ((color >> 8) & 0xFF);
1058 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001059 mColorSet = true;
1060 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1061}
1062
Romain Guy86568192010-12-14 15:55:39 -08001063void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1064 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001065 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1066 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001067 const float a = mColorA / 255.0f;
1068 mColorR = a * ((color >> 16) & 0xFF);
1069 mColorG = a * ((color >> 8) & 0xFF);
1070 mColorB = a * ((color ) & 0xFF);
1071 mColorSet = true;
1072 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1073}
1074
Romain Guy70ca14e2010-12-13 18:24:33 -08001075void OpenGLRenderer::setupDrawColor(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.setColor(r, g, b, a);
1082}
1083
Romain Guy86568192010-12-14 15:55:39 -08001084void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1085 mColorA = a;
1086 mColorR = r;
1087 mColorG = g;
1088 mColorB = b;
1089 mColorSet = true;
1090 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1091}
1092
Romain Guy70ca14e2010-12-13 18:24:33 -08001093void OpenGLRenderer::setupDrawShader() {
1094 if (mShader) {
1095 mShader->describe(mDescription, mCaches.extensions);
1096 }
1097}
1098
1099void OpenGLRenderer::setupDrawColorFilter() {
1100 if (mColorFilter) {
1101 mColorFilter->describe(mDescription, mCaches.extensions);
1102 }
1103}
1104
Romain Guyf09ef512011-05-27 11:43:46 -07001105void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1106 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1107 mColorA = 1.0f;
1108 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001109 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001110 }
1111}
1112
Romain Guy70ca14e2010-12-13 18:24:33 -08001113void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001114 // When the blending mode is kClear_Mode, we need to use a modulate color
1115 // argb=1,0,0,0
1116 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001117 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1118 mDescription, swapSrcDst);
1119}
1120
1121void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001122 // When the blending mode is kClear_Mode, we need to use a modulate color
1123 // argb=1,0,0,0
1124 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001125 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1126 mDescription, swapSrcDst);
1127}
1128
1129void OpenGLRenderer::setupDrawProgram() {
1130 useProgram(mCaches.programCache.get(mDescription));
1131}
1132
1133void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1134 mTrackDirtyRegions = false;
1135}
1136
1137void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1138 bool ignoreTransform) {
1139 mModelView.loadTranslate(left, top, 0.0f);
1140 if (!ignoreTransform) {
1141 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1142 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1143 } else {
1144 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1145 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1146 }
1147}
1148
Chet Haase8a5cc922011-04-26 07:28:09 -07001149void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1150 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001151}
1152
Romain Guy70ca14e2010-12-13 18:24:33 -08001153void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1154 bool ignoreTransform, bool ignoreModelView) {
1155 if (!ignoreModelView) {
1156 mModelView.loadTranslate(left, top, 0.0f);
1157 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001158 } else {
1159 mModelView.loadIdentity();
1160 }
Romain Guy86568192010-12-14 15:55:39 -08001161 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1162 if (!ignoreTransform) {
1163 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1164 if (mTrackDirtyRegions && dirty) {
1165 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1166 }
1167 } else {
1168 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1169 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1170 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001171}
1172
Romain Guyed6fcb02011-03-21 13:11:28 -07001173void OpenGLRenderer::setupDrawPointUniforms() {
1174 int slot = mCaches.currentProgram->getUniform("pointSize");
1175 glUniform1f(slot, mDescription.pointSize);
1176}
1177
Romain Guy70ca14e2010-12-13 18:24:33 -08001178void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001179 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001180 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1181 }
1182}
1183
Romain Guy86568192010-12-14 15:55:39 -08001184void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001185 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001186 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001187 }
1188}
1189
Romain Guy70ca14e2010-12-13 18:24:33 -08001190void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1191 if (mShader) {
1192 if (ignoreTransform) {
1193 mModelView.loadInverse(*mSnapshot->transform);
1194 }
1195 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1196 }
1197}
1198
Romain Guy8d0d4782010-12-14 20:13:35 -08001199void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1200 if (mShader) {
1201 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1202 }
1203}
1204
Romain Guy70ca14e2010-12-13 18:24:33 -08001205void OpenGLRenderer::setupDrawColorFilterUniforms() {
1206 if (mColorFilter) {
1207 mColorFilter->setupProgram(mCaches.currentProgram);
1208 }
1209}
1210
1211void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001212 bool force = mCaches.bindMeshBuffer();
1213 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001214 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001215}
1216
1217void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1218 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001219 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001220 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001221}
1222
Romain Guyaa6c24c2011-04-28 18:40:04 -07001223void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1224 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001225 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001226 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001227}
1228
Romain Guy8f0095c2011-05-02 17:24:22 -07001229void OpenGLRenderer::setupDrawTextureTransform() {
1230 mDescription.hasTextureTransform = true;
1231}
1232
1233void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001234 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1235 GL_FALSE, &transform.data[0]);
1236}
1237
Romain Guy70ca14e2010-12-13 18:24:33 -08001238void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001239 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001240 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001241 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001242 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001243 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001244 }
Romain Guyd71dd362011-12-12 19:03:35 -08001245
Romain Guyf3a910b42011-12-12 20:35:21 -08001246 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001247 if (mCaches.currentProgram->texCoords >= 0) {
1248 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1249 }
1250
1251 mCaches.unbindIndicesBuffer();
1252}
1253
1254void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1255 bool force = mCaches.unbindMeshBuffer();
1256 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1257 if (mCaches.currentProgram->texCoords >= 0) {
1258 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001259 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001260}
1261
Chet Haase5b0200b2011-04-13 17:58:08 -07001262void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001263 bool force = mCaches.unbindMeshBuffer();
1264 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1265 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001266 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001267}
1268
1269/**
1270 * 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 -07001271 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1272 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1273 * attributes (one per vertex) are values from zero to one that tells the fragment
1274 * shader where the fragment is in relation to the line width/length overall; these values are
1275 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1276 * region of the line.
1277 * Note that we only pass down the width values in this setup function. The length coordinates
1278 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001279 */
Chet Haase99585ad2011-05-02 15:00:16 -07001280void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Chet Haase99ecdc42011-05-06 12:06:34 -07001281 GLvoid* lengthCoords, float boundaryWidthProportion) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001282 bool force = mCaches.unbindMeshBuffer();
1283 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1284 vertices, gAAVertexStride);
1285 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001286 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001287
Chet Haase99585ad2011-05-02 15:00:16 -07001288 int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
1289 glEnableVertexAttribArray(widthSlot);
1290 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001291
Chet Haase99585ad2011-05-02 15:00:16 -07001292 int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
1293 glEnableVertexAttribArray(lengthSlot);
1294 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001295
Chet Haase5b0200b2011-04-13 17:58:08 -07001296 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001297 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001298
Chet Haase99585ad2011-05-02 15:00:16 -07001299 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001300 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001301 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
Chet Haase5b0200b2011-04-13 17:58:08 -07001302}
1303
Romain Guy70ca14e2010-12-13 18:24:33 -08001304void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001305}
1306
1307///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001308// Drawing
1309///////////////////////////////////////////////////////////////////////////////
1310
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001311bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
1312 Rect& dirty, uint32_t level) {
1313 if (quickReject(0.0f, 0.0f, width, height)) {
1314 return false;
1315 }
1316
Romain Guy0fe478e2010-11-08 12:08:41 -08001317 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1318 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001319 if (displayList && displayList->isRenderable()) {
Romain Guycabfcc12011-03-07 18:06:46 -08001320 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001321 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001322
Chet Haasedaf98e92011-01-10 14:10:36 -08001323 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001324}
1325
Chet Haaseed30fd82011-04-22 16:18:45 -07001326void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1327 if (displayList) {
1328 displayList->output(*this, level);
1329 }
1330}
1331
Romain Guya168d732011-03-18 16:50:13 -07001332void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1333 int alpha;
1334 SkXfermode::Mode mode;
1335 getAlphaAndMode(paint, &alpha, &mode);
1336
Romain Guya168d732011-03-18 16:50:13 -07001337 float x = left;
1338 float y = top;
1339
Romain Guye3c26852011-07-25 16:36:01 -07001340 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001341 bool ignoreTransform = false;
1342 if (mSnapshot->transform->isPureTranslate()) {
1343 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1344 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1345 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001346 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001347 } else {
1348 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001349 }
1350
1351 setupDraw();
1352 setupDrawWithTexture(true);
Romain Guy5b7a31502011-03-23 17:15:38 -07001353 if (paint) {
1354 setupDrawAlpha8Color(paint->getColor(), alpha);
1355 }
Romain Guya168d732011-03-18 16:50:13 -07001356 setupDrawColorFilter();
1357 setupDrawShader();
1358 setupDrawBlending(true, mode);
1359 setupDrawProgram();
1360 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001361
Romain Guya168d732011-03-18 16:50:13 -07001362 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001363 texture->setWrap(GL_CLAMP_TO_EDGE);
1364 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001365
Romain Guya168d732011-03-18 16:50:13 -07001366 setupDrawPureColorUniforms();
1367 setupDrawColorFilterUniforms();
1368 setupDrawShaderUniforms();
1369 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1370
1371 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1372
1373 finishDrawTexture();
1374}
1375
Chet Haase5c13d892010-10-08 08:37:55 -07001376void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001377 const float right = left + bitmap->width();
1378 const float bottom = top + bitmap->height();
1379
1380 if (quickReject(left, top, right, bottom)) {
1381 return;
1382 }
1383
Romain Guya1d3c912011-12-13 14:55:06 -08001384 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001385 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001386 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001387 const AutoTexture autoCleanup(texture);
1388
Romain Guya168d732011-03-18 16:50:13 -07001389 if (bitmap->getConfig() == SkBitmap::kA8_Config) {
1390 drawAlphaBitmap(texture, left, top, paint);
1391 } else {
1392 drawTextureRect(left, top, right, bottom, texture, paint);
1393 }
Romain Guyce0537b2010-06-29 21:05:21 -07001394}
1395
Chet Haase5c13d892010-10-08 08:37:55 -07001396void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001397 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1398 const mat4 transform(*matrix);
1399 transform.mapRect(r);
1400
Romain Guy6926c722010-07-12 20:20:03 -07001401 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1402 return;
1403 }
1404
Romain Guya1d3c912011-12-13 14:55:06 -08001405 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001406 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001407 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001408 const AutoTexture autoCleanup(texture);
1409
Romain Guy5b3b3522010-10-27 18:57:51 -07001410 // This could be done in a cheaper way, all we need is pass the matrix
1411 // to the vertex shader. The save/restore is a bit overkill.
1412 save(SkCanvas::kMatrix_SaveFlag);
1413 concatMatrix(matrix);
1414 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1415 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001416}
1417
Romain Guy5a7b4662011-01-20 19:09:30 -08001418void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1419 float* vertices, int* colors, SkPaint* paint) {
1420 // TODO: Do a quickReject
1421 if (!vertices || mSnapshot->isIgnored()) {
1422 return;
1423 }
1424
Romain Guya1d3c912011-12-13 14:55:06 -08001425 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001426 Texture* texture = mCaches.textureCache.get(bitmap);
1427 if (!texture) return;
1428 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001429
Romain Guyd21b6e12011-11-30 20:21:23 -08001430 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1431 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001432
1433 int alpha;
1434 SkXfermode::Mode mode;
1435 getAlphaAndMode(paint, &alpha, &mode);
1436
Romain Guy5a7b4662011-01-20 19:09:30 -08001437 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001438
Romain Guyb18d2d02011-02-10 15:52:54 -08001439 float left = FLT_MAX;
1440 float top = FLT_MAX;
1441 float right = FLT_MIN;
1442 float bottom = FLT_MIN;
1443
1444#if RENDER_LAYERS_AS_REGIONS
1445 bool hasActiveLayer = hasLayer();
1446#else
1447 bool hasActiveLayer = false;
1448#endif
1449
Romain Guya566b7c2011-01-23 16:36:11 -08001450 // TODO: Support the colors array
1451 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001452 TextureVertex* vertex = mesh;
1453 for (int32_t y = 0; y < meshHeight; y++) {
1454 for (int32_t x = 0; x < meshWidth; x++) {
1455 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1456
1457 float u1 = float(x) / meshWidth;
1458 float u2 = float(x + 1) / meshWidth;
1459 float v1 = float(y) / meshHeight;
1460 float v2 = float(y + 1) / meshHeight;
1461
1462 int ax = i + (meshWidth + 1) * 2;
1463 int ay = ax + 1;
1464 int bx = i;
1465 int by = bx + 1;
1466 int cx = i + 2;
1467 int cy = cx + 1;
1468 int dx = i + (meshWidth + 1) * 2 + 2;
1469 int dy = dx + 1;
1470
1471 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1472 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1473 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1474
1475 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1476 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1477 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001478
1479#if RENDER_LAYERS_AS_REGIONS
1480 if (hasActiveLayer) {
1481 // TODO: This could be optimized to avoid unnecessary ops
1482 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1483 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1484 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1485 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1486 }
1487#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001488 }
1489 }
1490
Romain Guyb18d2d02011-02-10 15:52:54 -08001491#if RENDER_LAYERS_AS_REGIONS
1492 if (hasActiveLayer) {
1493 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1494 }
1495#endif
1496
Romain Guy5a7b4662011-01-20 19:09:30 -08001497 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1498 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001499 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001500}
1501
Romain Guy8ba548f2010-06-30 19:21:21 -07001502void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1503 float srcLeft, float srcTop, float srcRight, float srcBottom,
1504 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001505 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001506 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1507 return;
1508 }
1509
Romain Guya1d3c912011-12-13 14:55:06 -08001510 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001511 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001512 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001513 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001514
Romain Guy8ba548f2010-06-30 19:21:21 -07001515 const float width = texture->width;
1516 const float height = texture->height;
1517
Romain Guy68169722011-08-22 17:33:33 -07001518 const float u1 = fmax(0.0f, srcLeft / width);
1519 const float v1 = fmax(0.0f, srcTop / height);
1520 const float u2 = fmin(1.0f, srcRight / width);
1521 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001522
Romain Guy03750a02010-10-18 14:06:08 -07001523 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001524 resetDrawTextureTexCoords(u1, v1, u2, v2);
1525
Romain Guy03750a02010-10-18 14:06:08 -07001526 int alpha;
1527 SkXfermode::Mode mode;
1528 getAlphaAndMode(paint, &alpha, &mode);
1529
Romain Guyd21b6e12011-11-30 20:21:23 -08001530 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1531
Romain Guy6620c6d2010-12-06 18:07:02 -08001532 if (mSnapshot->transform->isPureTranslate()) {
1533 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1534 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1535
Romain Guyb5014982011-07-28 15:39:12 -07001536 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001537 // Enable linear filtering if the source rectangle is scaled
1538 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001539 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001540 }
Romain Guyb5014982011-07-28 15:39:12 -07001541
Romain Guyd21b6e12011-11-30 20:21:23 -08001542 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001543 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1544 texture->id, alpha / 255.0f, mode, texture->blend,
1545 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1546 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1547 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001548 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001549 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1550 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1551 GL_TRIANGLE_STRIP, gMeshCount);
1552 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001553
1554 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1555}
1556
Romain Guy4aa90572010-09-26 18:40:37 -07001557void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001558 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001559 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001560 if (quickReject(left, top, right, bottom)) {
1561 return;
1562 }
1563
Romain Guya1d3c912011-12-13 14:55:06 -08001564 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001565 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001566 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001567 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001568 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1569 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001570
1571 int alpha;
1572 SkXfermode::Mode mode;
1573 getAlphaAndMode(paint, &alpha, &mode);
1574
Romain Guy2728f962010-10-08 18:36:15 -07001575 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001576 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001577
Romain Guya5ef39a2010-12-03 16:48:20 -08001578 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001579 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001580#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001581 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001582 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001583 const float offsetX = left + mSnapshot->transform->getTranslateX();
1584 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001585 const size_t count = mesh->quads.size();
1586 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001587 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001588 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001589 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1590 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1591 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001592 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001593 dirtyLayer(left + bounds.left, top + bounds.top,
1594 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001595 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001596 }
1597 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001598#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001599
Romain Guy6620c6d2010-12-06 18:07:02 -08001600 if (pureTranslate) {
1601 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1602 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1603
1604 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1605 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1606 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1607 true, !mesh->hasEmptyQuads);
1608 } else {
1609 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1610 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1611 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1612 true, !mesh->hasEmptyQuads);
1613 }
Romain Guy054dc182010-10-15 17:55:25 -07001614 }
Romain Guyf7f93552010-07-08 19:17:03 -07001615}
1616
Chet Haase99ecdc42011-05-06 12:06:34 -07001617/**
Chet Haase858aa932011-05-12 09:06:00 -07001618 * This function uses a similar approach to that of AA lines in the drawLines() function.
1619 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1620 * shader to compute the translucency of the color, determined by whether a given pixel is
1621 * within that boundary region and how far into the region it is.
1622 */
1623void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001624 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001625 float inverseScaleX = 1.0f;
1626 float inverseScaleY = 1.0f;
1627 // The quad that we use needs to account for scaling.
1628 if (!mSnapshot->transform->isPureTranslate()) {
1629 Matrix4 *mat = mSnapshot->transform;
1630 float m00 = mat->data[Matrix4::kScaleX];
1631 float m01 = mat->data[Matrix4::kSkewY];
1632 float m02 = mat->data[2];
1633 float m10 = mat->data[Matrix4::kSkewX];
1634 float m11 = mat->data[Matrix4::kScaleX];
1635 float m12 = mat->data[6];
1636 float scaleX = sqrt(m00 * m00 + m01 * m01);
1637 float scaleY = sqrt(m10 * m10 + m11 * m11);
1638 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1639 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1640 }
1641
1642 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001643 setupDrawNoTexture();
Chet Haase858aa932011-05-12 09:06:00 -07001644 setupDrawAALine();
1645 setupDrawColor(color);
1646 setupDrawColorFilter();
1647 setupDrawShader();
1648 setupDrawBlending(true, mode);
1649 setupDrawProgram();
1650 setupDrawModelViewIdentity(true);
1651 setupDrawColorUniforms();
1652 setupDrawColorFilterUniforms();
1653 setupDrawShaderIdentityUniforms();
1654
1655 AAVertex rects[4];
1656 AAVertex* aaVertices = &rects[0];
1657 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1658 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1659
1660 float boundarySizeX = .5 * inverseScaleX;
1661 float boundarySizeY = .5 * inverseScaleY;
1662
1663 // Adjust the rect by the AA boundary padding
1664 left -= boundarySizeX;
1665 right += boundarySizeX;
1666 top -= boundarySizeY;
1667 bottom += boundarySizeY;
1668
1669 float width = right - left;
1670 float height = bottom - top;
1671
1672 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1673 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
1674 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
1675 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1676 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1677 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
1678 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryHeightProportion));
1679
1680 if (!quickReject(left, top, right, bottom)) {
1681 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1682 AAVertex::set(aaVertices++, left, top, 1, 0);
1683 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1684 AAVertex::set(aaVertices++, right, top, 0, 0);
1685 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1686 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1687 }
1688}
1689
1690/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001691 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1692 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1693 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1694 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1695 * of the line. Hairlines are more involved because we need to account for transform scaling
1696 * to end up with a one-pixel-wide line in screen space..
1697 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1698 * in combination with values that we calculate and pass down in this method. The basic approach
1699 * is that the quad we create contains both the core line area plus a bounding area in which
1700 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1701 * proportion of the width and the length of a given segment is represented by the boundary
1702 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1703 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1704 * on the inside). This ends up giving the result we want, with pixels that are completely
1705 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1706 * how far into the boundary region they are, which is determined by shader interpolation.
1707 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001708void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1709 if (mSnapshot->isIgnored()) return;
1710
1711 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001712 // We use half the stroke width here because we're going to position the quad
1713 // corner vertices half of the width away from the line endpoints
1714 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001715 // A stroke width of 0 has a special meaning in Skia:
1716 // it draws a line 1 px wide regardless of current transform
1717 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase99ecdc42011-05-06 12:06:34 -07001718 float inverseScaleX = 1.0f;
1719 float inverseScaleY = 1.0f;
1720 bool scaled = false;
Chet Haase8a5cc922011-04-26 07:28:09 -07001721 int alpha;
1722 SkXfermode::Mode mode;
1723 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001724 int verticesCount = count;
1725 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001726 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001727 verticesCount += (count - 4);
1728 }
1729
1730 if (isHairLine || isAA) {
1731 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1732 // the line on the screen should always be one pixel wide regardless of scale. For
1733 // AA lines, we only want one pixel of translucent boundary around the quad.
1734 if (!mSnapshot->transform->isPureTranslate()) {
1735 Matrix4 *mat = mSnapshot->transform;
1736 float m00 = mat->data[Matrix4::kScaleX];
1737 float m01 = mat->data[Matrix4::kSkewY];
1738 float m02 = mat->data[2];
1739 float m10 = mat->data[Matrix4::kSkewX];
1740 float m11 = mat->data[Matrix4::kScaleX];
1741 float m12 = mat->data[6];
1742 float scaleX = sqrt(m00*m00 + m01*m01);
1743 float scaleY = sqrt(m10*m10 + m11*m11);
1744 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1745 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1746 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1747 scaled = true;
1748 }
1749 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001750 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001751
1752 getAlphaAndMode(paint, &alpha, &mode);
1753 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001754 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001755 if (isAA) {
1756 setupDrawAALine();
1757 }
1758 setupDrawColor(paint->getColor(), alpha);
1759 setupDrawColorFilter();
1760 setupDrawShader();
1761 if (isAA) {
1762 setupDrawBlending(true, mode);
1763 } else {
1764 setupDrawBlending(mode);
1765 }
1766 setupDrawProgram();
1767 setupDrawModelViewIdentity(true);
1768 setupDrawColorUniforms();
1769 setupDrawColorFilterUniforms();
1770 setupDrawShaderIdentityUniforms();
1771
1772 if (isHairLine) {
1773 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001774 halfStrokeWidth = isAA? 1 : .5;
1775 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001776 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001777 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001778 }
1779 Vertex lines[verticesCount];
1780 Vertex* vertices = &lines[0];
Chet Haase99585ad2011-05-02 15:00:16 -07001781 AAVertex wLines[verticesCount];
1782 AAVertex* aaVertices = &wLines[0];
Chet Haase5b0200b2011-04-13 17:58:08 -07001783 if (!isAA) {
1784 setupDrawVertices(vertices);
1785 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001786 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1787 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001788 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001789 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1790 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001791 // We will need to calculate the actual width proportion on each segment for
1792 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1793 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
1794 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
Chet Haase5b0200b2011-04-13 17:58:08 -07001795 }
1796
Chet Haase99ecdc42011-05-06 12:06:34 -07001797 AAVertex* prevAAVertex = NULL;
1798 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001799
Chet Haase99585ad2011-05-02 15:00:16 -07001800 int boundaryLengthSlot = -1;
1801 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001802 int boundaryWidthSlot = -1;
1803 int inverseBoundaryWidthSlot = -1;
Chet Haase5b0200b2011-04-13 17:58:08 -07001804 for (int i = 0; i < count; i += 4) {
1805 // a = start point, b = end point
1806 vec2 a(points[i], points[i + 1]);
1807 vec2 b(points[i + 2], points[i + 3]);
Chet Haase99585ad2011-05-02 15:00:16 -07001808 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001809 float boundaryLengthProportion = 0;
1810 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001811
Chet Haase5b0200b2011-04-13 17:58:08 -07001812 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001813 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001814 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001815 if (isAA) {
1816 float wideningFactor;
1817 if (fabs(n.x) >= fabs(n.y)) {
1818 wideningFactor = fabs(1.0f / n.x);
1819 } else {
1820 wideningFactor = fabs(1.0f / n.y);
1821 }
1822 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001823 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001824 if (scaled) {
1825 n.x *= inverseScaleX;
1826 n.y *= inverseScaleY;
1827 }
1828 } else if (scaled) {
1829 // Extend n by .5 pixel on each side, post-transform
1830 vec2 extendedN = n.copyNormalized();
1831 extendedN /= 2;
1832 extendedN.x *= inverseScaleX;
1833 extendedN.y *= inverseScaleY;
1834 float extendedNLength = extendedN.length();
1835 // We need to set this value on the shader prior to drawing
1836 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1837 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001838 }
1839 float x = n.x;
1840 n.x = -n.y;
1841 n.y = x;
1842
Chet Haase99585ad2011-05-02 15:00:16 -07001843 // aa lines expand the endpoint vertices to encompass the AA boundary
1844 if (isAA) {
1845 vec2 abVector = (b - a);
1846 length = abVector.length();
1847 abVector.normalize();
Chet Haase99ecdc42011-05-06 12:06:34 -07001848 if (scaled) {
1849 abVector.x *= inverseScaleX;
1850 abVector.y *= inverseScaleY;
1851 float abLength = abVector.length();
1852 boundaryLengthProportion = abLength / (length + abLength);
1853 } else {
1854 boundaryLengthProportion = .5 / (length + 1);
1855 }
1856 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001857 a -= abVector;
1858 b += abVector;
1859 }
1860
Chet Haase5b0200b2011-04-13 17:58:08 -07001861 // Four corners of the rectangle defining a thick line
1862 vec2 p1 = a - n;
1863 vec2 p2 = a + n;
1864 vec2 p3 = b + n;
1865 vec2 p4 = b - n;
1866
Chet Haase99585ad2011-05-02 15:00:16 -07001867
Chet Haase5b0200b2011-04-13 17:58:08 -07001868 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1869 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1870 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1871 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1872
1873 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001874 if (!isAA) {
1875 if (prevVertex != NULL) {
1876 // Issue two repeat vertices to create degenerate triangles to bridge
1877 // between the previous line and the new one. This is necessary because
1878 // we are creating a single triangle_strip which will contain
1879 // potentially discontinuous line segments.
1880 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1881 Vertex::set(vertices++, p1.x, p1.y);
1882 generatedVerticesCount += 2;
1883 }
1884 Vertex::set(vertices++, p1.x, p1.y);
1885 Vertex::set(vertices++, p2.x, p2.y);
1886 Vertex::set(vertices++, p4.x, p4.y);
1887 Vertex::set(vertices++, p3.x, p3.y);
1888 prevVertex = vertices - 1;
1889 generatedVerticesCount += 4;
1890 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001891 if (!isHairLine && scaled) {
1892 // Must set width proportions per-segment for scaled non-hairlines to use the
1893 // correct AA boundary dimensions
1894 if (boundaryWidthSlot < 0) {
1895 boundaryWidthSlot =
1896 mCaches.currentProgram->getUniform("boundaryWidth");
1897 inverseBoundaryWidthSlot =
1898 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1899 }
1900 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1901 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1902 }
Chet Haase99585ad2011-05-02 15:00:16 -07001903 if (boundaryLengthSlot < 0) {
1904 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1905 inverseBoundaryLengthSlot =
1906 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1907 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001908 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
1909 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07001910
Chet Haase5b0200b2011-04-13 17:58:08 -07001911 if (prevAAVertex != NULL) {
1912 // Issue two repeat vertices to create degenerate triangles to bridge
1913 // between the previous line and the new one. This is necessary because
1914 // we are creating a single triangle_strip which will contain
1915 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07001916 AAVertex::set(aaVertices++,prevAAVertex->position[0],
1917 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
1918 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07001919 generatedVerticesCount += 2;
1920 }
Chet Haase99585ad2011-05-02 15:00:16 -07001921 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
1922 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
1923 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
1924 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Chet Haase5b0200b2011-04-13 17:58:08 -07001925 prevAAVertex = aaVertices - 1;
1926 generatedVerticesCount += 4;
1927 }
Chet Haase99585ad2011-05-02 15:00:16 -07001928 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
1929 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
1930 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07001931 }
1932 }
1933 if (generatedVerticesCount > 0) {
1934 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
1935 }
1936}
1937
Romain Guyed6fcb02011-03-21 13:11:28 -07001938void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
1939 if (mSnapshot->isIgnored()) return;
1940
1941 // TODO: The paint's cap style defines whether the points are square or circular
1942 // TODO: Handle AA for round points
1943
Chet Haase5b0200b2011-04-13 17:58:08 -07001944 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07001945 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07001946 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07001947 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001948 if (isHairLine) {
1949 // Now that we know it's hairline, we can set the effective width, to be used later
1950 strokeWidth = 1.0f;
1951 }
1952 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07001953 int alpha;
1954 SkXfermode::Mode mode;
1955 getAlphaAndMode(paint, &alpha, &mode);
1956
1957 int verticesCount = count >> 1;
1958 int generatedVerticesCount = 0;
1959
1960 TextureVertex pointsData[verticesCount];
1961 TextureVertex* vertex = &pointsData[0];
1962
1963 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001964 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001965 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07001966 setupDrawColor(paint->getColor(), alpha);
1967 setupDrawColorFilter();
1968 setupDrawShader();
1969 setupDrawBlending(mode);
1970 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07001971 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07001972 setupDrawColorUniforms();
1973 setupDrawColorFilterUniforms();
1974 setupDrawPointUniforms();
1975 setupDrawShaderIdentityUniforms();
1976 setupDrawMesh(vertex);
1977
1978 for (int i = 0; i < count; i += 2) {
1979 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1980 generatedVerticesCount++;
Chet Haase8a5cc922011-04-26 07:28:09 -07001981 float left = points[i] - halfWidth;
1982 float right = points[i] + halfWidth;
1983 float top = points[i + 1] - halfWidth;
1984 float bottom = points [i + 1] + halfWidth;
1985 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07001986 }
1987
1988 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
1989}
1990
Romain Guy85bf02f2010-06-22 13:11:24 -07001991void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001992 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001993 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001994
Romain Guyae88e5e2010-10-22 17:49:18 -07001995 Rect& clip(*mSnapshot->clipRect);
1996 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001997
Romain Guy3d58c032010-07-14 16:34:53 -07001998 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001999}
Romain Guy9d5316e2010-06-24 19:30:36 -07002000
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002001void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08002002 if (!texture) return;
2003 const AutoTexture autoCleanup(texture);
2004
2005 const float x = left + texture->left - texture->offset;
2006 const float y = top + texture->top - texture->offset;
2007
2008 drawPathTexture(texture, x, y, paint);
2009}
2010
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002011void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
2012 float rx, float ry, SkPaint* paint) {
2013 if (mSnapshot->isIgnored()) return;
2014
Romain Guya1d3c912011-12-13 14:55:06 -08002015 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002016 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2017 right - left, bottom - top, rx, ry, paint);
2018 drawShape(left, top, texture, paint);
2019}
2020
Romain Guy01d58e42011-01-19 21:54:02 -08002021void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2022 if (mSnapshot->isIgnored()) return;
2023
Romain Guya1d3c912011-12-13 14:55:06 -08002024 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002025 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002026 drawShape(x - radius, y - radius, texture, paint);
2027}
Romain Guy01d58e42011-01-19 21:54:02 -08002028
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002029void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2030 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002031
Romain Guya1d3c912011-12-13 14:55:06 -08002032 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002033 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2034 drawShape(left, top, texture, paint);
2035}
2036
Romain Guy8b2f5262011-01-23 16:15:02 -08002037void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2038 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2039 if (mSnapshot->isIgnored()) return;
2040
2041 if (fabs(sweepAngle) >= 360.0f) {
2042 drawOval(left, top, right, bottom, paint);
2043 return;
2044 }
2045
Romain Guya1d3c912011-12-13 14:55:06 -08002046 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002047 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2048 startAngle, sweepAngle, useCenter, paint);
2049 drawShape(left, top, texture, paint);
2050}
2051
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002052void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2053 SkPaint* paint) {
2054 if (mSnapshot->isIgnored()) return;
2055
Romain Guya1d3c912011-12-13 14:55:06 -08002056 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002057 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2058 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002059}
2060
Chet Haase5c13d892010-10-08 08:37:55 -07002061void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002062 if (p->getStyle() != SkPaint::kFill_Style) {
2063 drawRectAsShape(left, top, right, bottom, p);
2064 return;
2065 }
2066
Romain Guy6926c722010-07-12 20:20:03 -07002067 if (quickReject(left, top, right, bottom)) {
2068 return;
2069 }
2070
Romain Guy026c5e162010-06-28 17:12:22 -07002071 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002072 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002073 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2074 if (!isMode) {
2075 // Assume SRC_OVER
2076 mode = SkXfermode::kSrcOver_Mode;
2077 }
2078 } else {
2079 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002080 }
2081
Romain Guy026c5e162010-06-28 17:12:22 -07002082 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002083 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002084 drawAARect(left, top, right, bottom, color, mode);
2085 } else {
2086 drawColorRect(left, top, right, bottom, color, mode);
2087 }
Romain Guyc7d53492010-06-25 13:41:57 -07002088}
Romain Guy9d5316e2010-06-24 19:30:36 -07002089
Romain Guyeb9a5362012-01-17 17:39:26 -08002090void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
2091 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002092 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2093 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guyeb9a5362012-01-17 17:39:26 -08002094 return;
2095 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002096
Romain Guy671d6cf2012-01-18 12:39:17 -08002097 // NOTE: Skia does not support perspective transform on drawPosText yet
2098 if (!mSnapshot->transform->isSimple()) {
2099 return;
2100 }
2101
2102 float x = 0.0f;
2103 float y = 0.0f;
2104 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2105 if (pureTranslate) {
2106 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2107 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2108 }
2109
2110 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2111 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2112 paint->getTextSize());
2113
2114 int alpha;
2115 SkXfermode::Mode mode;
2116 getAlphaAndMode(paint, &alpha, &mode);
2117
2118 // Pick the appropriate texture filtering
2119 bool linearFilter = mSnapshot->transform->changesBounds();
2120 if (pureTranslate && !linearFilter) {
2121 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2122 }
2123
2124 mCaches.activeTexture(0);
2125 setupDraw();
2126 setupDrawDirtyRegionsDisabled();
2127 setupDrawWithTexture(true);
2128 setupDrawAlpha8Color(paint->getColor(), alpha);
2129 setupDrawColorFilter();
2130 setupDrawShader();
2131 setupDrawBlending(true, mode);
2132 setupDrawProgram();
2133 setupDrawModelView(x, y, x, y, pureTranslate, true);
2134 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2135 setupDrawPureColorUniforms();
2136 setupDrawColorFilterUniforms();
2137 setupDrawShaderUniforms(pureTranslate);
2138
2139 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2140 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2141
2142#if RENDER_LAYERS_AS_REGIONS
2143 bool hasActiveLayer = hasLayer();
2144#else
2145 bool hasActiveLayer = false;
2146#endif
2147
2148 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2149 positions, hasActiveLayer ? &bounds : NULL)) {
2150#if RENDER_LAYERS_AS_REGIONS
2151 if (hasActiveLayer) {
2152 if (!pureTranslate) {
2153 mSnapshot->transform->mapRect(bounds);
2154 }
2155 dirtyLayerUnchecked(bounds, getRegion());
2156 }
2157#endif
2158 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002159}
2160
Romain Guye8e62a42010-07-23 18:55:21 -07002161void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002162 float x, float y, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002163 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2164 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -07002165 return;
2166 }
2167
Romain Guye8e62a42010-07-23 18:55:21 -07002168 switch (paint->getTextAlign()) {
2169 case SkPaint::kCenter_Align:
Romain Guycac5fd32011-12-01 20:08:50 -08002170 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002171 x -= length / 2.0f;
2172 break;
2173 case SkPaint::kRight_Align:
Romain Guycac5fd32011-12-01 20:08:50 -08002174 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002175 x -= length;
2176 break;
2177 default:
2178 break;
2179 }
2180
Romain Guycac5fd32011-12-01 20:08:50 -08002181 SkPaint::FontMetrics metrics;
2182 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy8f9a9f62011-12-05 11:53:26 -08002183 // If no length was specified, just perform the hit test on the Y axis
Romain Guycac5fd32011-12-01 20:08:50 -08002184 if (quickReject(x, y + metrics.fTop,
2185 x + (length >= 0.0f ? length : INT_MAX / 2), y + metrics.fBottom)) {
2186 return;
2187 }
2188
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002189 const float oldX = x;
2190 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002191 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2192 if (pureTranslate) {
2193 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2194 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2195 }
2196
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002197#if DEBUG_GLYPHS
Steve Block5baa3a62011-12-20 16:23:08 +00002198 ALOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002199#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002200
2201 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002202 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002203 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002204
Romain Guy86568192010-12-14 15:55:39 -08002205 int alpha;
2206 SkXfermode::Mode mode;
2207 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002208
Romain Guy1e45aae2010-08-13 19:39:53 -07002209 if (mHasShadow) {
Romain Guy2d4fd362011-12-13 22:00:19 -08002210 mCaches.activeTexture(0);
2211
Romain Guyb45c0c92010-08-26 20:35:23 -07002212 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002213 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2214 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002215 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002216
Romain Guy740bf2b2011-04-26 15:33:10 -07002217 const float sx = oldX - shadow->left + mShadowDx;
2218 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002219
Romain Guy86568192010-12-14 15:55:39 -08002220 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002221 int shadowColor = mShadowColor;
2222 if (mShader) {
2223 shadowColor = 0xffffffff;
2224 }
Romain Guy86568192010-12-14 15:55:39 -08002225
Romain Guy86568192010-12-14 15:55:39 -08002226 setupDraw();
2227 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002228 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2229 setupDrawColorFilter();
2230 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002231 setupDrawBlending(true, mode);
2232 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002233 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002234 setupDrawTexture(shadow->id);
2235 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002236 setupDrawColorFilterUniforms();
2237 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002238 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2239
Romain Guy0a417492010-08-16 20:26:20 -07002240 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy1e45aae2010-08-13 19:39:53 -07002241 }
2242
Romain Guy6620c6d2010-12-06 18:07:02 -08002243 // Pick the appropriate texture filtering
2244 bool linearFilter = mSnapshot->transform->changesBounds();
2245 if (pureTranslate && !linearFilter) {
2246 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2247 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002248
Romain Guya1d3c912011-12-13 14:55:06 -08002249 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002250 setupDraw();
2251 setupDrawDirtyRegionsDisabled();
2252 setupDrawWithTexture(true);
2253 setupDrawAlpha8Color(paint->getColor(), alpha);
2254 setupDrawColorFilter();
2255 setupDrawShader();
2256 setupDrawBlending(true, mode);
2257 setupDrawProgram();
2258 setupDrawModelView(x, y, x, y, pureTranslate, true);
2259 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2260 setupDrawPureColorUniforms();
2261 setupDrawColorFilterUniforms();
2262 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002263
Romain Guy6620c6d2010-12-06 18:07:02 -08002264 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002265 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2266
2267#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002268 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002269#else
Romain Guyf219da52011-01-16 12:54:25 -08002270 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002271#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002272
Romain Guy6620c6d2010-12-06 18:07:02 -08002273 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002274 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002275#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002276 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002277 if (!pureTranslate) {
2278 mSnapshot->transform->mapRect(bounds);
2279 }
Romain Guyf219da52011-01-16 12:54:25 -08002280 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002281 }
2282#endif
2283 }
Romain Guy694b5192010-07-21 21:33:20 -07002284
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002285 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002286}
2287
Romain Guy7fbcc042010-08-04 15:40:07 -07002288void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002289 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002290
Romain Guya1d3c912011-12-13 14:55:06 -08002291 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002292
Romain Guyfb8b7632010-08-23 21:05:08 -07002293 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07002294 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002295 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002296
Romain Guy8b55f372010-08-18 17:10:07 -07002297 const float x = texture->left - texture->offset;
2298 const float y = texture->top - texture->offset;
2299
Romain Guy01d58e42011-01-19 21:54:02 -08002300 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002301}
2302
Romain Guyada830f2011-01-13 12:13:20 -08002303void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2304 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002305 return;
2306 }
2307
Romain Guya1d3c912011-12-13 14:55:06 -08002308 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002309
2310 int alpha;
2311 SkXfermode::Mode mode;
2312 getAlphaAndMode(paint, &alpha, &mode);
2313
Romain Guy9ace8f52011-07-07 20:50:11 -07002314 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002315
Romain Guyf219da52011-01-16 12:54:25 -08002316#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08002317 if (!layer->region.isEmpty()) {
2318 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002319 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002320 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002321 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002322 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002323
Romain Guyc88e3572011-01-22 00:32:12 -08002324 setupDraw();
2325 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002326 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002327 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002328 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002329 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002330 setupDrawPureColorUniforms();
2331 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002332 setupDrawTexture(layer->getTexture());
2333 if (mSnapshot->transform->isPureTranslate()) {
2334 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2335 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2336
Romain Guyd21b6e12011-11-30 20:21:23 -08002337 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002338 setupDrawModelViewTranslate(x, y,
2339 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2340 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002341 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002342 setupDrawModelViewTranslate(x, y,
2343 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2344 }
Romain Guyc88e3572011-01-22 00:32:12 -08002345 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002346
Romain Guyc88e3572011-01-22 00:32:12 -08002347 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2348 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002349
Romain Guyc88e3572011-01-22 00:32:12 -08002350 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002351
2352#if DEBUG_LAYERS_AS_REGIONS
2353 drawRegionRects(layer->region);
2354#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002355 }
Romain Guyf219da52011-01-16 12:54:25 -08002356 }
2357#else
Romain Guyada830f2011-01-13 12:13:20 -08002358 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2359 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002360#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002361}
2362
Romain Guy6926c722010-07-12 20:20:03 -07002363///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002364// Shaders
2365///////////////////////////////////////////////////////////////////////////////
2366
2367void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002368 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002369}
2370
Romain Guy06f96e22010-07-30 19:18:16 -07002371void OpenGLRenderer::setupShader(SkiaShader* shader) {
2372 mShader = shader;
2373 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002374 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002375 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002376}
2377
Romain Guyd27977d2010-07-14 19:18:51 -07002378///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002379// Color filters
2380///////////////////////////////////////////////////////////////////////////////
2381
2382void OpenGLRenderer::resetColorFilter() {
2383 mColorFilter = NULL;
2384}
2385
2386void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2387 mColorFilter = filter;
2388}
2389
2390///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002391// Drop shadow
2392///////////////////////////////////////////////////////////////////////////////
2393
2394void OpenGLRenderer::resetShadow() {
2395 mHasShadow = false;
2396}
2397
2398void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2399 mHasShadow = true;
2400 mShadowRadius = radius;
2401 mShadowDx = dx;
2402 mShadowDy = dy;
2403 mShadowColor = color;
2404}
2405
2406///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002407// Draw filters
2408///////////////////////////////////////////////////////////////////////////////
2409
2410void OpenGLRenderer::resetPaintFilter() {
2411 mHasDrawFilter = false;
2412}
2413
2414void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2415 mHasDrawFilter = true;
2416 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2417 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2418}
2419
2420SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
2421 if (!mHasDrawFilter || !paint) return paint;
2422
2423 uint32_t flags = paint->getFlags();
2424
2425 mFilteredPaint = *paint;
2426 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2427
2428 return &mFilteredPaint;
2429}
2430
2431///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002432// Drawing implementation
2433///////////////////////////////////////////////////////////////////////////////
2434
Romain Guy01d58e42011-01-19 21:54:02 -08002435void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2436 float x, float y, SkPaint* paint) {
2437 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2438 return;
2439 }
2440
2441 int alpha;
2442 SkXfermode::Mode mode;
2443 getAlphaAndMode(paint, &alpha, &mode);
2444
2445 setupDraw();
2446 setupDrawWithTexture(true);
2447 setupDrawAlpha8Color(paint->getColor(), alpha);
2448 setupDrawColorFilter();
2449 setupDrawShader();
2450 setupDrawBlending(true, mode);
2451 setupDrawProgram();
2452 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2453 setupDrawTexture(texture->id);
2454 setupDrawPureColorUniforms();
2455 setupDrawColorFilterUniforms();
2456 setupDrawShaderUniforms();
2457 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2458
2459 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2460
2461 finishDrawTexture();
2462}
2463
Romain Guyf607bdc2010-09-10 19:20:06 -07002464// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002465#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2466#define kStdUnderline_Offset (1.0f / 9.0f)
2467#define kStdUnderline_Thickness (1.0f / 18.0f)
2468
2469void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2470 float x, float y, SkPaint* paint) {
2471 // Handle underline and strike-through
2472 uint32_t flags = paint->getFlags();
2473 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002474 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002475 float underlineWidth = length;
2476 // If length is > 0.0f, we already measured the text for the text alignment
2477 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002478 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002479 }
2480
2481 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002482 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002483 case SkPaint::kCenter_Align:
2484 offsetX = underlineWidth * 0.5f;
2485 break;
2486 case SkPaint::kRight_Align:
2487 offsetX = underlineWidth;
2488 break;
2489 default:
2490 break;
2491 }
2492
2493 if (underlineWidth > 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002494 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002495 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002496
Romain Guye20ecbd2010-09-22 19:49:04 -07002497 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002498 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002499
Romain Guyf6834472011-01-23 13:32:12 -08002500 int linesCount = 0;
2501 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2502 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2503
2504 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002505 float points[pointsCount];
2506 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002507
2508 if (flags & SkPaint::kUnderlineText_Flag) {
2509 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002510 points[currentPoint++] = left;
2511 points[currentPoint++] = top;
2512 points[currentPoint++] = left + underlineWidth;
2513 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002514 }
2515
2516 if (flags & SkPaint::kStrikeThruText_Flag) {
2517 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002518 points[currentPoint++] = left;
2519 points[currentPoint++] = top;
2520 points[currentPoint++] = left + underlineWidth;
2521 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002522 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002523
Romain Guy726aeba2011-06-01 14:52:00 -07002524 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002525
Romain Guy726aeba2011-06-01 14:52:00 -07002526 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002527 }
2528 }
2529}
2530
Romain Guy026c5e162010-06-28 17:12:22 -07002531void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002532 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002533 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002534 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002535 color |= 0x00ffffff;
2536 }
2537
Romain Guy70ca14e2010-12-13 18:24:33 -08002538 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002539 setupDrawNoTexture();
Romain Guy70ca14e2010-12-13 18:24:33 -08002540 setupDrawColor(color);
2541 setupDrawShader();
2542 setupDrawColorFilter();
2543 setupDrawBlending(mode);
2544 setupDrawProgram();
2545 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2546 setupDrawColorUniforms();
2547 setupDrawShaderUniforms(ignoreTransform);
2548 setupDrawColorFilterUniforms();
2549 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002550
Romain Guyc95c8d62010-09-17 15:31:32 -07002551 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2552}
2553
Romain Guy82ba8142010-07-09 13:25:56 -07002554void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002555 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002556 int alpha;
2557 SkXfermode::Mode mode;
2558 getAlphaAndMode(paint, &alpha, &mode);
2559
Romain Guyd21b6e12011-11-30 20:21:23 -08002560 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002561
Romain Guy6620c6d2010-12-06 18:07:02 -08002562 if (mSnapshot->transform->isPureTranslate()) {
2563 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2564 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2565
Romain Guyd21b6e12011-11-30 20:21:23 -08002566 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002567 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2568 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2569 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2570 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002571 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002572 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2573 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2574 GL_TRIANGLE_STRIP, gMeshCount);
2575 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002576}
2577
Romain Guybd6b79b2010-06-26 00:13:53 -07002578void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002579 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2580 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002581 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002582}
2583
2584void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002585 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002586 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002587 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002588
Romain Guy746b7402010-10-26 16:27:31 -07002589 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002590 setupDrawWithTexture();
2591 setupDrawColor(alpha, alpha, alpha, alpha);
2592 setupDrawColorFilter();
2593 setupDrawBlending(blend, mode, swapSrcDst);
2594 setupDrawProgram();
2595 if (!dirty) {
2596 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002597 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002598 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002599 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002600 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002601 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002602 }
Romain Guy86568192010-12-14 15:55:39 -08002603 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002604 setupDrawColorFilterUniforms();
2605 setupDrawTexture(texture);
2606 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002607
Romain Guy6820ac82010-09-15 18:11:50 -07002608 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002609
2610 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002611}
2612
Romain Guya5aed0d2010-09-09 14:42:43 -07002613void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002614 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002615 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2616 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002617 // These blend modes are not supported by OpenGL directly and have
2618 // to be implemented using shaders. Since the shader will perform
2619 // the blending, turn blending off here
2620 // If the blend mode cannot be implemented using shaders, fall
2621 // back to the default SrcOver blend mode instead
2622 if (mode > SkXfermode::kScreen_Mode) {
Romain Guy746b7402010-10-26 16:27:31 -07002623 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002624 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002625 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002626
Romain Guy82bc7a72012-01-03 14:13:39 -08002627 if (mCaches.blend) {
2628 glDisable(GL_BLEND);
2629 mCaches.blend = false;
2630 }
2631
2632 return;
2633 } else {
2634 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002635 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002636 }
2637
2638 if (!mCaches.blend) {
2639 glEnable(GL_BLEND);
2640 }
2641
2642 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2643 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2644
2645 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2646 glBlendFunc(sourceMode, destMode);
2647 mCaches.lastSrcMode = sourceMode;
2648 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002649 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002650 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002651 glDisable(GL_BLEND);
2652 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002653 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002654}
2655
Romain Guy889f8d12010-07-29 14:37:42 -07002656bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002657 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002658 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002659 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002660 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002661 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002662 }
Romain Guy6926c722010-07-12 20:20:03 -07002663 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002664}
2665
Romain Guy026c5e162010-06-28 17:12:22 -07002666void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002667 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002668 TextureVertex::setUV(v++, u1, v1);
2669 TextureVertex::setUV(v++, u2, v1);
2670 TextureVertex::setUV(v++, u1, v2);
2671 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002672}
2673
Chet Haase5c13d892010-10-08 08:37:55 -07002674void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002675 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002676 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002677
2678 // Skia draws using the color's alpha channel if < 255
2679 // Otherwise, it uses the paint's alpha
2680 int color = paint->getColor();
2681 *alpha = (color >> 24) & 0xFF;
2682 if (*alpha == 255) {
2683 *alpha = paint->getAlpha();
2684 }
2685 } else {
2686 *mode = SkXfermode::kSrcOver_Mode;
2687 *alpha = 255;
2688 }
Romain Guy026c5e162010-06-28 17:12:22 -07002689}
2690
Romain Guya5aed0d2010-09-09 14:42:43 -07002691SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002692 SkXfermode::Mode resultMode;
2693 if (!SkXfermode::AsMode(mode, &resultMode)) {
2694 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002695 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002696 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002697}
2698
Romain Guy9d5316e2010-06-24 19:30:36 -07002699}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002700}; // namespace android