blob: cc0e05ef914b178b5488ebef19185bc90e2e86c1 [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 Guy85bf02f2010-06-22 13:11:24 -0700130void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700131 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700132
133 mWidth = width;
134 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700135
136 mFirstSnapshot->height = height;
137 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700138
Romain Guy3e263fa2011-12-12 16:47:48 -0800139 glDisable(GL_DITHER);
Romain Guy39d252a2011-12-12 18:14:06 -0800140 glEnable(GL_SCISSOR_TEST);
Romain Guy3e263fa2011-12-12 16:47:48 -0800141 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Romain Guy15bc6432011-12-13 13:11:32 -0800142
Romain Guy3e263fa2011-12-12 16:47:48 -0800143 glEnableVertexAttribArray(Program::kBindingPosition);
Romain Guye4d01122010-06-16 18:44:05 -0700144}
145
Romain Guy6b7bd242010-10-06 19:49:23 -0700146void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800147 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
148}
149
150void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800151 mCaches.clearGarbage();
152
Romain Guy8aef54f2010-09-01 15:13:49 -0700153 mSnapshot = new Snapshot(mFirstSnapshot,
154 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800155 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700156 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700157
Romain Guy39d252a2011-12-12 18:14:06 -0800158 glViewport(0, 0, mWidth, mHeight);
Romain Guy8f85e802011-12-14 19:23:32 -0800159 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy39d252a2011-12-12 18:14:06 -0800160
Romain Guy7d7b5492011-01-24 16:33:45 -0800161 mSnapshot->setClip(left, top, right, bottom);
Romain Guy39d252a2011-12-12 18:14:06 -0800162 mDirtyClip = false;
Romain Guy7d7b5492011-01-24 16:33:45 -0800163
Romain Guy6b7bd242010-10-06 19:49:23 -0700164 if (!opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700165 glClear(GL_COLOR_BUFFER_BIT);
166 }
Romain Guybb9524b2010-06-22 18:56:38 -0700167}
168
Romain Guyb025b9c2010-09-16 14:16:48 -0700169void OpenGLRenderer::finish() {
170#if DEBUG_OPENGL
171 GLenum status = GL_NO_ERROR;
172 while ((status = glGetError()) != GL_NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +0000173 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800174 switch (status) {
175 case GL_OUT_OF_MEMORY:
Steve Block3762c312012-01-06 19:20:56 +0000176 ALOGE(" OpenGLRenderer is out of memory!");
Romain Guya07105b2011-01-10 21:14:18 -0800177 break;
178 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700179 }
180#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800181#if DEBUG_MEMORY_USAGE
182 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800183#else
184 if (mCaches.getDebugLevel() & kDebugMemory) {
185 mCaches.dumpMemoryUsage();
186 }
Romain Guyc15008e2010-11-10 11:59:15 -0800187#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700188}
189
Romain Guy6c319ca2011-01-11 14:29:25 -0800190void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700191 if (mCaches.currentProgram) {
192 if (mCaches.currentProgram->isInUse()) {
193 mCaches.currentProgram->remove();
194 mCaches.currentProgram = NULL;
195 }
196 }
Romain Guy50c0f092010-10-19 11:42:22 -0700197 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800198 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800199 mCaches.resetVertexPointers();
Romain Guy15bc6432011-12-13 13:11:32 -0800200 mCaches.disbaleTexCoordsVertexArray();
Romain Guyda8532c2010-08-31 11:50:35 -0700201}
202
Romain Guy6c319ca2011-01-11 14:29:25 -0800203void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800204 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
205
206 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy3e263fa2011-12-12 16:47:48 -0800207 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
208
Romain Guyda8532c2010-08-31 11:50:35 -0700209 glEnable(GL_SCISSOR_TEST);
Romain Guy82bc7a72012-01-03 14:13:39 -0800210 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700211 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700212
Romain Guya1d3c912011-12-13 14:55:06 -0800213 mCaches.activeTexture(0);
Chet Haase08837c22011-11-28 11:53:21 -0800214 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guyf607bdc2010-09-10 19:20:06 -0700215
Romain Guy50c0f092010-10-19 11:42:22 -0700216 mCaches.blend = true;
217 glEnable(GL_BLEND);
218 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
219 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700220}
221
Romain Guycabfcc12011-03-07 18:06:46 -0800222bool OpenGLRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800223 interrupt();
Romain Guyf90f8172011-01-25 22:53:24 -0800224 if (mDirtyClip) {
225 setScissorFromClip();
226 }
Romain Guyd643bb52011-03-01 14:55:21 -0800227
Romain Guy80911b82011-03-16 15:30:12 -0700228 Rect clip(*mSnapshot->clipRect);
229 clip.snapToPixelBoundaries();
230
Romain Guyd643bb52011-03-01 14:55:21 -0800231#if RENDER_LAYERS_AS_REGIONS
232 // Since we don't know what the functor will draw, let's dirty
233 // tne entire clip region
234 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800235 dirtyLayerUnchecked(clip, getRegion());
236 }
237#endif
238
Romain Guy08aa2cb2011-03-17 11:06:57 -0700239 DrawGlInfo info;
240 info.clipLeft = clip.left;
241 info.clipTop = clip.top;
242 info.clipRight = clip.right;
243 info.clipBottom = clip.bottom;
244 info.isLayer = hasLayer();
245 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700246
Romain Guy08aa2cb2011-03-17 11:06:57 -0700247 status_t result = (*functor)(0, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800248
249 if (result != 0) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700250 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800251 dirty.unionWith(localDirty);
252 }
253
Chet Haasedaf98e92011-01-10 14:10:36 -0800254 resume();
Romain Guycabfcc12011-03-07 18:06:46 -0800255 return result != 0;
Chet Haasedaf98e92011-01-10 14:10:36 -0800256}
257
Romain Guyf6a11b82010-06-23 17:47:49 -0700258///////////////////////////////////////////////////////////////////////////////
259// State management
260///////////////////////////////////////////////////////////////////////////////
261
Romain Guybb9524b2010-06-22 18:56:38 -0700262int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700263 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700264}
265
266int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700267 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700268}
269
270void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700271 if (mSaveCount > 1) {
272 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700273 }
Romain Guybb9524b2010-06-22 18:56:38 -0700274}
275
276void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700277 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700278
Romain Guy8fb95422010-08-17 18:38:51 -0700279 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700280 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700281 }
Romain Guybb9524b2010-06-22 18:56:38 -0700282}
283
Romain Guy8aef54f2010-09-01 15:13:49 -0700284int OpenGLRenderer::saveSnapshot(int flags) {
285 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700286 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700287}
288
289bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700290 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700291 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700292 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700293
Romain Guybd6b79b2010-06-26 00:13:53 -0700294 sp<Snapshot> current = mSnapshot;
295 sp<Snapshot> previous = mSnapshot->previous;
296
Romain Guyeb993562010-10-05 18:14:38 -0700297 if (restoreOrtho) {
298 Rect& r = previous->viewport;
299 glViewport(r.left, r.top, r.right, r.bottom);
300 mOrthoMatrix.load(current->orthoMatrix);
301 }
302
Romain Guy8b55f372010-08-18 17:10:07 -0700303 mSaveCount--;
304 mSnapshot = previous;
305
Romain Guy2542d192010-08-18 11:47:12 -0700306 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700307 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700308 }
Romain Guy2542d192010-08-18 11:47:12 -0700309
Romain Guy5ec99242010-11-03 16:19:08 -0700310 if (restoreLayer) {
311 composeLayer(current, previous);
312 }
313
Romain Guy2542d192010-08-18 11:47:12 -0700314 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700315}
316
Romain Guyf6a11b82010-06-23 17:47:49 -0700317///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700318// Layers
319///////////////////////////////////////////////////////////////////////////////
320
321int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700322 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700323 const GLuint previousFbo = mSnapshot->fbo;
324 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700325
Romain Guyaf636eb2010-12-09 17:47:21 -0800326 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700327 int alpha = 255;
328 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700329
Romain Guye45362c2010-11-03 19:58:32 -0700330 if (p) {
331 alpha = p->getAlpha();
332 if (!mCaches.extensions.hasFramebufferFetch()) {
333 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
334 if (!isMode) {
335 // Assume SRC_OVER
336 mode = SkXfermode::kSrcOver_Mode;
337 }
338 } else {
339 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700340 }
341 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700342 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700343 }
Romain Guyd55a8612010-06-28 17:42:46 -0700344
Romain Guydbc26d22010-10-11 17:58:29 -0700345 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
346 }
Romain Guyd55a8612010-06-28 17:42:46 -0700347
348 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700349}
350
351int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
352 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700353 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700354 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700355 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700356 SkPaint paint;
357 paint.setAlpha(alpha);
358 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700359 }
Romain Guyd55a8612010-06-28 17:42:46 -0700360}
Romain Guybd6b79b2010-06-26 00:13:53 -0700361
Romain Guy1c740bc2010-09-13 18:00:09 -0700362/**
363 * Layers are viewed by Skia are slightly different than layers in image editing
364 * programs (for instance.) When a layer is created, previously created layers
365 * and the frame buffer still receive every drawing command. For instance, if a
366 * layer is created and a shape intersecting the bounds of the layers and the
367 * framebuffer is draw, the shape will be drawn on both (unless the layer was
368 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
369 *
370 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
371 * texture. Unfortunately, this is inefficient as it requires every primitive to
372 * be drawn n + 1 times, where n is the number of active layers. In practice this
373 * means, for every primitive:
374 * - Switch active frame buffer
375 * - Change viewport, clip and projection matrix
376 * - Issue the drawing
377 *
378 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700379 * To avoid this, layers are implemented in a different way here, at least in the
380 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
381 * is set. When this flag is set we can redirect all drawing operations into a
382 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700383 *
384 * This implementation relies on the frame buffer being at least RGBA 8888. When
385 * a layer is created, only a texture is created, not an FBO. The content of the
386 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700387 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700388 * buffer and drawing continues as normal. This technique therefore treats the
389 * frame buffer as a scratch buffer for the layers.
390 *
391 * To compose the layers back onto the frame buffer, each layer texture
392 * (containing the original frame buffer data) is drawn as a simple quad over
393 * the frame buffer. The trick is that the quad is set as the composition
394 * destination in the blending equation, and the frame buffer becomes the source
395 * of the composition.
396 *
397 * Drawing layers with an alpha value requires an extra step before composition.
398 * An empty quad is drawn over the layer's region in the frame buffer. This quad
399 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
400 * quad is used to multiply the colors in the frame buffer. This is achieved by
401 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
402 * GL_ZERO, GL_SRC_ALPHA.
403 *
404 * Because glCopyTexImage2D() can be slow, an alternative implementation might
405 * be use to draw a single clipped layer. The implementation described above
406 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700407 *
408 * (1) The frame buffer is actually not cleared right away. To allow the GPU
409 * to potentially optimize series of calls to glCopyTexImage2D, the frame
410 * buffer is left untouched until the first drawing operation. Only when
411 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700412 */
Romain Guyd55a8612010-06-28 17:42:46 -0700413bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700414 float right, float bottom, int alpha, SkXfermode::Mode mode,
415 int flags, GLuint previousFbo) {
416 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700417 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700418
Romain Guyeb993562010-10-05 18:14:38 -0700419 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
420
Romain Guyf607bdc2010-09-10 19:20:06 -0700421 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700422 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700423 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700424 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700425
Romain Guyeb993562010-10-05 18:14:38 -0700426 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700427 if (bounds.intersect(*snapshot->clipRect)) {
428 // We cannot work with sub-pixels in this case
429 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700430
Romain Guyad37cd32011-03-15 11:12:25 -0700431 // When the layer is not an FBO, we may use glCopyTexImage so we
432 // need to make sure the layer does not extend outside the bounds
433 // of the framebuffer
434 if (!bounds.intersect(snapshot->previous->viewport)) {
435 bounds.setEmpty();
436 }
437 } else {
438 bounds.setEmpty();
439 }
Romain Guyeb993562010-10-05 18:14:38 -0700440 }
Romain Guybf434112010-09-16 14:40:17 -0700441
Romain Guy746b7402010-10-26 16:27:31 -0700442 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
443 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800444 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700445 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700446 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700447 }
448
449 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800450 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700451 return false;
452 }
Romain Guyf18fd992010-07-08 11:45:51 -0700453
Romain Guya1d3c912011-12-13 14:55:06 -0800454 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700455 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700456 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700457 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700458 }
459
Romain Guy9ace8f52011-07-07 20:50:11 -0700460 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700461 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700462 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
463 bounds.getWidth() / float(layer->getWidth()), 0.0f);
464 layer->setColorFilter(mColorFilter);
Romain Guydda57022010-07-06 11:39:32 -0700465
Romain Guy8fb95422010-08-17 18:38:51 -0700466 // Save the layer in the snapshot
467 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700468 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700469
Romain Guyeb993562010-10-05 18:14:38 -0700470 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700471 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700472 } else {
473 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700474 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800475 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700476 if (layer->isEmpty()) {
477 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
478 bounds.left, snapshot->height - bounds.bottom,
479 layer->getWidth(), layer->getHeight(), 0);
480 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800481 } else {
482 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
483 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
484 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700485
Romain Guy54be1cd2011-06-13 19:04:27 -0700486 // Enqueue the buffer coordinates to clear the corresponding region later
487 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700488 }
Romain Guyeb993562010-10-05 18:14:38 -0700489 }
Romain Guyf86ef572010-07-01 11:05:42 -0700490
Romain Guyd55a8612010-06-28 17:42:46 -0700491 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700492}
493
Romain Guy5b3b3522010-10-27 18:57:51 -0700494bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
495 GLuint previousFbo) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700496 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700497
498#if RENDER_LAYERS_AS_REGIONS
499 snapshot->region = &snapshot->layer->region;
500 snapshot->flags |= Snapshot::kFlagFboTarget;
501#endif
502
503 Rect clip(bounds);
504 snapshot->transform->mapRect(clip);
505 clip.intersect(*snapshot->clipRect);
506 clip.snapToPixelBoundaries();
507 clip.intersect(snapshot->previous->viewport);
508
509 mat4 inverse;
510 inverse.loadInverse(*mSnapshot->transform);
511
512 inverse.mapRect(clip);
513 clip.snapToPixelBoundaries();
514 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700515 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700516
517 snapshot->flags |= Snapshot::kFlagIsFboLayer;
Romain Guy9ace8f52011-07-07 20:50:11 -0700518 snapshot->fbo = layer->getFbo();
Romain Guy5b3b3522010-10-27 18:57:51 -0700519 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700520 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
521 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
522 snapshot->height = bounds.getHeight();
523 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
524 snapshot->orthoMatrix.load(mOrthoMatrix);
525
526 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700527 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
528 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700529
530 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700531 if (layer->isEmpty()) {
532 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
533 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700534 }
535
536 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700537 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700538
539#if DEBUG_LAYERS_AS_REGIONS
540 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
541 if (status != GL_FRAMEBUFFER_COMPLETE) {
Steve Block3762c312012-01-06 19:20:56 +0000542 ALOGE("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guy5b3b3522010-10-27 18:57:51 -0700543
544 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
Romain Guy9ace8f52011-07-07 20:50:11 -0700545 layer->deleteTexture();
546 mCaches.fboCache.put(layer->getFbo());
Romain Guy5b3b3522010-10-27 18:57:51 -0700547
548 delete layer;
549
550 return false;
551 }
552#endif
553
554 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy8f85e802011-12-14 19:23:32 -0800555 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700556 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700557 glClear(GL_COLOR_BUFFER_BIT);
558
559 dirtyClip();
560
561 // Change the ortho projection
562 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
563 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
564
565 return true;
566}
567
Romain Guy1c740bc2010-09-13 18:00:09 -0700568/**
569 * Read the documentation of createLayer() before doing anything in this method.
570 */
Romain Guy1d83e192010-08-17 11:37:00 -0700571void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
572 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000573 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700574 return;
575 }
576
Romain Guy5b3b3522010-10-27 18:57:51 -0700577 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700578
579 if (fboLayer) {
580 // Unbind current FBO and restore previous one
581 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
582 }
583
Romain Guy1d83e192010-08-17 11:37:00 -0700584 Layer* layer = current->layer;
585 const Rect& rect = layer->layer;
586
Romain Guy9ace8f52011-07-07 20:50:11 -0700587 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700588 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700589 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700590 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700591 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700592 }
593
Romain Guy03750a02010-10-18 14:06:08 -0700594 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700595
Romain Guya1d3c912011-12-13 14:55:06 -0800596 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700597
Romain Guy5b3b3522010-10-27 18:57:51 -0700598 // When the layer is stored in an FBO, we can save a bit of fillrate by
599 // drawing only the dirty region
600 if (fboLayer) {
601 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700602 if (layer->getColorFilter()) {
603 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800604 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700605 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700606 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800607 resetColorFilter();
608 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700609 } else if (!rect.isEmpty()) {
610 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
611 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700612 }
Romain Guy8b55f372010-08-18 17:10:07 -0700613
Romain Guyeb993562010-10-05 18:14:38 -0700614 if (fboLayer) {
Romain Guy9c4b79a2011-11-10 19:23:58 -0800615 // Note: No need to use glDiscardFramebufferEXT() since we never
616 // create/compose layers that are not on screen with this
617 // code path
618 // See LayerRenderer::destroyLayer(Layer*)
619
Romain Guyeb993562010-10-05 18:14:38 -0700620 // Detach the texture from the FBO
621 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
622 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
623 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
624
625 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
626 mCaches.fboCache.put(current->fbo);
627 }
628
Romain Guy746b7402010-10-26 16:27:31 -0700629 dirtyClip();
630
Romain Guyeb993562010-10-05 18:14:38 -0700631 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700632 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700633 LAYER_LOGD("Deleting layer");
Romain Guy9ace8f52011-07-07 20:50:11 -0700634 layer->deleteTexture();
Romain Guy1d83e192010-08-17 11:37:00 -0700635 delete layer;
636 }
637}
638
Romain Guyaa6c24c2011-04-28 18:40:04 -0700639void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700640 float alpha = layer->getAlpha() / 255.0f;
Romain Guyaa6c24c2011-04-28 18:40:04 -0700641
Romain Guy302a9df2011-08-16 13:55:02 -0700642 mat4& transform = layer->getTransform();
643 if (!transform.isIdentity()) {
644 save(0);
645 mSnapshot->transform->multiply(transform);
646 }
647
Romain Guyaa6c24c2011-04-28 18:40:04 -0700648 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700649 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700650 setupDrawWithTexture();
651 } else {
652 setupDrawWithExternalTexture();
653 }
654 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700655 setupDrawColor(alpha, alpha, alpha, alpha);
656 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700657 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700658 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700659 setupDrawPureColorUniforms();
660 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700661 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
662 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700663 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700664 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700665 }
Romain Guyec19b4a2011-07-07 21:05:04 -0700666 if (mSnapshot->transform->isPureTranslate() &&
667 layer->getWidth() == (uint32_t) rect.getWidth() &&
668 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700669 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
670 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
671
Romain Guyd21b6e12011-11-30 20:21:23 -0800672 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700673 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
674 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800675 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700676 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
677 }
678 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -0700679 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
680
681 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
682
683 finishDrawTexture();
Romain Guy302a9df2011-08-16 13:55:02 -0700684
685 if (!transform.isIdentity()) {
686 restore();
687 }
Romain Guyaa6c24c2011-04-28 18:40:04 -0700688}
689
Romain Guy5b3b3522010-10-27 18:57:51 -0700690void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700691 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -0700692 const Rect& texCoords = layer->texCoords;
693 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
694 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700695
Romain Guy9ace8f52011-07-07 20:50:11 -0700696 float x = rect.left;
697 float y = rect.top;
Romain Guyb2479152011-07-08 11:57:29 -0700698 bool simpleTransform = mSnapshot->transform->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700699 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700700 layer->getHeight() == (uint32_t) rect.getHeight();
701
702 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700703 // When we're swapping, the layer is already in screen coordinates
704 if (!swap) {
705 x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
706 y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
707 }
708
Romain Guyd21b6e12011-11-30 20:21:23 -0800709 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700710 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800711 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700712 }
713
714 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
715 layer->getTexture(), layer->getAlpha() / 255.0f,
716 layer->getMode(), layer->isBlend(),
717 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
718 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700719
Romain Guyaa6c24c2011-04-28 18:40:04 -0700720 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
721 } else {
722 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
723 drawTextureLayer(layer, rect);
724 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
725 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700726}
727
728void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
729#if RENDER_LAYERS_AS_REGIONS
730 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -0700731 layer->setRegionAsRect();
732
Romain Guy40667672011-03-18 14:34:03 -0700733 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -0700734
Romain Guy5b3b3522010-10-27 18:57:51 -0700735 layer->region.clear();
736 return;
737 }
738
Romain Guy8a3957d2011-09-07 17:55:15 -0700739 // TODO: See LayerRenderer.cpp::generateMesh() for important
740 // information about this implementation
Romain Guy5b3b3522010-10-27 18:57:51 -0700741 if (!layer->region.isEmpty()) {
742 size_t count;
743 const android::Rect* rects = layer->region.getArray(&count);
744
Romain Guy9ace8f52011-07-07 20:50:11 -0700745 const float alpha = layer->getAlpha() / 255.0f;
746 const float texX = 1.0f / float(layer->getWidth());
747 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -0800748 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700749
750 TextureVertex* mesh = mCaches.getRegionMesh();
751 GLsizei numQuads = 0;
752
Romain Guy7230a742011-01-10 22:26:16 -0800753 setupDraw();
754 setupDrawWithTexture();
755 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800756 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -0700757 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -0800758 setupDrawProgram();
759 setupDrawDirtyRegionsDisabled();
760 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800761 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -0700762 setupDrawTexture(layer->getTexture());
763 if (mSnapshot->transform->isPureTranslate()) {
764 const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f);
765 const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f);
766
Romain Guyd21b6e12011-11-30 20:21:23 -0800767 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -0700768 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
769 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800770 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -0700771 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
772 }
Romain Guy15bc6432011-12-13 13:11:32 -0800773 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700774
775 for (size_t i = 0; i < count; i++) {
776 const android::Rect* r = &rects[i];
777
778 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800779 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700780 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800781 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700782
783 // TODO: Reject quads outside of the clip
784 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
785 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
786 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
787 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
788
789 numQuads++;
790
791 if (numQuads >= REGION_MESH_QUAD_COUNT) {
792 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
793 numQuads = 0;
794 mesh = mCaches.getRegionMesh();
795 }
796 }
797
798 if (numQuads > 0) {
799 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
800 }
801
Romain Guy7230a742011-01-10 22:26:16 -0800802 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700803
804#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800805 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700806#endif
807
808 layer->region.clear();
809 }
810#else
811 composeLayerRect(layer, rect);
812#endif
813}
814
Romain Guy3a3133d2011-02-01 22:59:58 -0800815void OpenGLRenderer::drawRegionRects(const Region& region) {
816#if DEBUG_LAYERS_AS_REGIONS
817 size_t count;
818 const android::Rect* rects = region.getArray(&count);
819
820 uint32_t colors[] = {
821 0x7fff0000, 0x7f00ff00,
822 0x7f0000ff, 0x7fff00ff,
823 };
824
825 int offset = 0;
826 int32_t top = rects[0].top;
827
828 for (size_t i = 0; i < count; i++) {
829 if (top != rects[i].top) {
830 offset ^= 0x2;
831 top = rects[i].top;
832 }
833
834 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
835 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
836 SkXfermode::kSrcOver_Mode);
837 }
838#endif
839}
840
Romain Guy5b3b3522010-10-27 18:57:51 -0700841void OpenGLRenderer::dirtyLayer(const float left, const float top,
842 const float right, const float bottom, const mat4 transform) {
843#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800844 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700845 Rect bounds(left, top, right, bottom);
846 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800847 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700848 }
849#endif
850}
851
852void OpenGLRenderer::dirtyLayer(const float left, const float top,
853 const float right, const float bottom) {
854#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800855 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700856 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800857 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800858 }
859#endif
860}
861
862void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
863#if RENDER_LAYERS_AS_REGIONS
864 if (bounds.intersect(*mSnapshot->clipRect)) {
865 bounds.snapToPixelBoundaries();
866 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
867 if (!dirty.isEmpty()) {
868 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700869 }
870 }
871#endif
872}
873
Romain Guy54be1cd2011-06-13 19:04:27 -0700874void OpenGLRenderer::clearLayerRegions() {
875 const size_t count = mLayers.size();
876 if (count == 0) return;
877
878 if (!mSnapshot->isIgnored()) {
879 // Doing several glScissor/glClear here can negatively impact
880 // GPUs with a tiler architecture, instead we draw quads with
881 // the Clear blending mode
882
883 // The list contains bounds that have already been clipped
884 // against their initial clip rect, and the current clip
885 // is likely different so we need to disable clipping here
886 glDisable(GL_SCISSOR_TEST);
887
888 Vertex mesh[count * 6];
889 Vertex* vertex = mesh;
890
891 for (uint32_t i = 0; i < count; i++) {
892 Rect* bounds = mLayers.itemAt(i);
893
894 Vertex::set(vertex++, bounds->left, bounds->bottom);
895 Vertex::set(vertex++, bounds->left, bounds->top);
896 Vertex::set(vertex++, bounds->right, bounds->top);
897 Vertex::set(vertex++, bounds->left, bounds->bottom);
898 Vertex::set(vertex++, bounds->right, bounds->top);
899 Vertex::set(vertex++, bounds->right, bounds->bottom);
900
901 delete bounds;
902 }
903
904 setupDraw(false);
905 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
906 setupDrawBlending(true, SkXfermode::kClear_Mode);
907 setupDrawProgram();
908 setupDrawPureColorUniforms();
909 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -0800910 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -0700911
Romain Guy54be1cd2011-06-13 19:04:27 -0700912 glDrawArrays(GL_TRIANGLES, 0, count * 6);
913
914 glEnable(GL_SCISSOR_TEST);
915 } else {
916 for (uint32_t i = 0; i < count; i++) {
917 delete mLayers.itemAt(i);
918 }
919 }
920
921 mLayers.clear();
922}
923
Romain Guybd6b79b2010-06-26 00:13:53 -0700924///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700925// Transforms
926///////////////////////////////////////////////////////////////////////////////
927
928void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700929 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700930}
931
932void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700933 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700934}
935
936void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700937 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700938}
939
Romain Guy807daf72011-01-18 11:19:19 -0800940void OpenGLRenderer::skew(float sx, float sy) {
941 mSnapshot->transform->skew(sx, sy);
942}
943
Romain Guyf6a11b82010-06-23 17:47:49 -0700944void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -0700945 if (matrix) {
946 mSnapshot->transform->load(*matrix);
947 } else {
948 mSnapshot->transform->loadIdentity();
949 }
Romain Guyf6a11b82010-06-23 17:47:49 -0700950}
951
952void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700953 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700954}
955
956void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700957 SkMatrix transform;
958 mSnapshot->transform->copyTo(transform);
959 transform.preConcat(*matrix);
960 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700961}
962
963///////////////////////////////////////////////////////////////////////////////
964// Clipping
965///////////////////////////////////////////////////////////////////////////////
966
Romain Guybb9524b2010-06-22 18:56:38 -0700967void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700968 Rect clip(*mSnapshot->clipRect);
969 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -0800970
971 mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
972 clip.getWidth(), clip.getHeight());
973
Romain Guy746b7402010-10-26 16:27:31 -0700974 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700975}
976
977const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700978 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700979}
980
Romain Guyc7d53492010-06-25 13:41:57 -0700981bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800982 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700983 return true;
984 }
985
Romain Guy1d83e192010-08-17 11:37:00 -0700986 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700987 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700988 r.snapToPixelBoundaries();
989
990 Rect clipRect(*mSnapshot->clipRect);
991 clipRect.snapToPixelBoundaries();
992
993 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700994}
995
Romain Guy079ba2c2010-07-16 14:12:24 -0700996bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
997 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700998 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700999 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001000 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001001 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001002}
1003
Romain Guyf6a11b82010-06-23 17:47:49 -07001004///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001005// Drawing commands
1006///////////////////////////////////////////////////////////////////////////////
1007
Romain Guy54be1cd2011-06-13 19:04:27 -07001008void OpenGLRenderer::setupDraw(bool clear) {
1009 if (clear) clearLayerRegions();
Romain Guy70ca14e2010-12-13 18:24:33 -08001010 if (mDirtyClip) {
1011 setScissorFromClip();
1012 }
1013 mDescription.reset();
1014 mSetShaderColor = false;
1015 mColorSet = false;
1016 mColorA = mColorR = mColorG = mColorB = 0.0f;
1017 mTextureUnit = 0;
1018 mTrackDirtyRegions = true;
1019}
1020
1021void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1022 mDescription.hasTexture = true;
1023 mDescription.hasAlpha8Texture = isAlpha8;
1024}
1025
Romain Guyaa6c24c2011-04-28 18:40:04 -07001026void OpenGLRenderer::setupDrawWithExternalTexture() {
1027 mDescription.hasExternalTexture = true;
1028}
1029
Romain Guy15bc6432011-12-13 13:11:32 -08001030void OpenGLRenderer::setupDrawNoTexture() {
1031 mCaches.disbaleTexCoordsVertexArray();
1032}
1033
Chet Haase5b0200b2011-04-13 17:58:08 -07001034void OpenGLRenderer::setupDrawAALine() {
Chet Haase99585ad2011-05-02 15:00:16 -07001035 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001036}
1037
Romain Guyed6fcb02011-03-21 13:11:28 -07001038void OpenGLRenderer::setupDrawPoint(float pointSize) {
1039 mDescription.isPoint = true;
1040 mDescription.pointSize = pointSize;
1041}
1042
Romain Guy70ca14e2010-12-13 18:24:33 -08001043void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001044 setupDrawColor(color, (color >> 24) & 0xFF);
1045}
1046
1047void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1048 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001049 // Second divide of a by 255 is an optimization, allowing us to simply multiply
1050 // the rgb values by a instead of also dividing by 255
Romain Guy70ca14e2010-12-13 18:24:33 -08001051 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -08001052 mColorR = a * ((color >> 16) & 0xFF);
1053 mColorG = a * ((color >> 8) & 0xFF);
1054 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -08001055 mColorSet = true;
1056 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1057}
1058
Romain Guy86568192010-12-14 15:55:39 -08001059void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1060 mColorA = alpha / 255.0f;
Chet Haase6cfdf452011-04-22 16:42:10 -07001061 // Double-divide of a by 255 is an optimization, allowing us to simply multiply
1062 // the rgb values by a instead of also dividing by 255
Romain Guy86568192010-12-14 15:55:39 -08001063 const float a = mColorA / 255.0f;
1064 mColorR = a * ((color >> 16) & 0xFF);
1065 mColorG = a * ((color >> 8) & 0xFF);
1066 mColorB = a * ((color ) & 0xFF);
1067 mColorSet = true;
1068 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1069}
1070
Romain Guy70ca14e2010-12-13 18:24:33 -08001071void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1072 mColorA = a;
1073 mColorR = r;
1074 mColorG = g;
1075 mColorB = b;
1076 mColorSet = true;
1077 mSetShaderColor = mDescription.setColor(r, g, b, a);
1078}
1079
Romain Guy86568192010-12-14 15:55:39 -08001080void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
1081 mColorA = a;
1082 mColorR = r;
1083 mColorG = g;
1084 mColorB = b;
1085 mColorSet = true;
1086 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
1087}
1088
Romain Guy70ca14e2010-12-13 18:24:33 -08001089void OpenGLRenderer::setupDrawShader() {
1090 if (mShader) {
1091 mShader->describe(mDescription, mCaches.extensions);
1092 }
1093}
1094
1095void OpenGLRenderer::setupDrawColorFilter() {
1096 if (mColorFilter) {
1097 mColorFilter->describe(mDescription, mCaches.extensions);
1098 }
1099}
1100
Romain Guyf09ef512011-05-27 11:43:46 -07001101void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1102 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1103 mColorA = 1.0f;
1104 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001105 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001106 }
1107}
1108
Romain Guy70ca14e2010-12-13 18:24:33 -08001109void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001110 // When the blending mode is kClear_Mode, we need to use a modulate color
1111 // argb=1,0,0,0
1112 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001113 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1114 mDescription, swapSrcDst);
1115}
1116
1117void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001118 // When the blending mode is kClear_Mode, we need to use a modulate color
1119 // argb=1,0,0,0
1120 accountForClear(mode);
Romain Guy70ca14e2010-12-13 18:24:33 -08001121 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
1122 mDescription, swapSrcDst);
1123}
1124
1125void OpenGLRenderer::setupDrawProgram() {
1126 useProgram(mCaches.programCache.get(mDescription));
1127}
1128
1129void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1130 mTrackDirtyRegions = false;
1131}
1132
1133void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1134 bool ignoreTransform) {
1135 mModelView.loadTranslate(left, top, 0.0f);
1136 if (!ignoreTransform) {
1137 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1138 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1139 } else {
1140 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1141 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1142 }
1143}
1144
Chet Haase8a5cc922011-04-26 07:28:09 -07001145void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
1146 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001147}
1148
Romain Guy70ca14e2010-12-13 18:24:33 -08001149void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1150 bool ignoreTransform, bool ignoreModelView) {
1151 if (!ignoreModelView) {
1152 mModelView.loadTranslate(left, top, 0.0f);
1153 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001154 } else {
1155 mModelView.loadIdentity();
1156 }
Romain Guy86568192010-12-14 15:55:39 -08001157 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1158 if (!ignoreTransform) {
1159 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1160 if (mTrackDirtyRegions && dirty) {
1161 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1162 }
1163 } else {
1164 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
1165 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1166 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001167}
1168
Romain Guyed6fcb02011-03-21 13:11:28 -07001169void OpenGLRenderer::setupDrawPointUniforms() {
1170 int slot = mCaches.currentProgram->getUniform("pointSize");
1171 glUniform1f(slot, mDescription.pointSize);
1172}
1173
Romain Guy70ca14e2010-12-13 18:24:33 -08001174void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001175 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001176 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1177 }
1178}
1179
Romain Guy86568192010-12-14 15:55:39 -08001180void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001181 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001182 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001183 }
1184}
1185
Romain Guy70ca14e2010-12-13 18:24:33 -08001186void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1187 if (mShader) {
1188 if (ignoreTransform) {
1189 mModelView.loadInverse(*mSnapshot->transform);
1190 }
1191 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1192 }
1193}
1194
Romain Guy8d0d4782010-12-14 20:13:35 -08001195void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1196 if (mShader) {
1197 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1198 }
1199}
1200
Romain Guy70ca14e2010-12-13 18:24:33 -08001201void OpenGLRenderer::setupDrawColorFilterUniforms() {
1202 if (mColorFilter) {
1203 mColorFilter->setupProgram(mCaches.currentProgram);
1204 }
1205}
1206
1207void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001208 bool force = mCaches.bindMeshBuffer();
1209 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001210 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001211}
1212
1213void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1214 bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001215 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001216 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001217}
1218
Romain Guyaa6c24c2011-04-28 18:40:04 -07001219void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1220 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001221 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001222 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001223}
1224
Romain Guy8f0095c2011-05-02 17:24:22 -07001225void OpenGLRenderer::setupDrawTextureTransform() {
1226 mDescription.hasTextureTransform = true;
1227}
1228
1229void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001230 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1231 GL_FALSE, &transform.data[0]);
1232}
1233
Romain Guy70ca14e2010-12-13 18:24:33 -08001234void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001235 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001236 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001237 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001238 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001239 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001240 }
Romain Guyd71dd362011-12-12 19:03:35 -08001241
Romain Guyf3a910b42011-12-12 20:35:21 -08001242 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001243 if (mCaches.currentProgram->texCoords >= 0) {
1244 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
1245 }
1246
1247 mCaches.unbindIndicesBuffer();
1248}
1249
1250void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1251 bool force = mCaches.unbindMeshBuffer();
1252 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
1253 if (mCaches.currentProgram->texCoords >= 0) {
1254 mCaches.bindTexCoordsVertexPointer(force, mCaches.currentProgram->texCoords, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001255 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001256}
1257
Chet Haase5b0200b2011-04-13 17:58:08 -07001258void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001259 bool force = mCaches.unbindMeshBuffer();
1260 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1261 vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001262 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001263}
1264
1265/**
1266 * Sets up the shader to draw an AA line. We draw AA lines with quads, where there is an
Chet Haase99585ad2011-05-02 15:00:16 -07001267 * outer boundary that fades out to 0. The variables set in the shader define the proportion of
1268 * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength
1269 * attributes (one per vertex) are values from zero to one that tells the fragment
1270 * shader where the fragment is in relation to the line width/length overall; these values are
1271 * then used to compute the proper color, based on whether the fragment lies in the fading AA
1272 * region of the line.
1273 * Note that we only pass down the width values in this setup function. The length coordinates
1274 * are set up for each individual segment.
Chet Haase5b0200b2011-04-13 17:58:08 -07001275 */
Chet Haase99585ad2011-05-02 15:00:16 -07001276void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
Chet Haase99ecdc42011-05-06 12:06:34 -07001277 GLvoid* lengthCoords, float boundaryWidthProportion) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001278 bool force = mCaches.unbindMeshBuffer();
1279 mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
1280 vertices, gAAVertexStride);
1281 mCaches.resetTexCoordsVertexPointer();
Romain Guy15bc6432011-12-13 13:11:32 -08001282 mCaches.unbindIndicesBuffer();
Romain Guyd71dd362011-12-12 19:03:35 -08001283
Chet Haase99585ad2011-05-02 15:00:16 -07001284 int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
1285 glEnableVertexAttribArray(widthSlot);
1286 glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001287
Chet Haase99585ad2011-05-02 15:00:16 -07001288 int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength");
1289 glEnableVertexAttribArray(lengthSlot);
1290 glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords);
Romain Guyd71dd362011-12-12 19:03:35 -08001291
Chet Haase5b0200b2011-04-13 17:58:08 -07001292 int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001293 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
Romain Guyd71dd362011-12-12 19:03:35 -08001294
Chet Haase99585ad2011-05-02 15:00:16 -07001295 // Setting the inverse value saves computations per-fragment in the shader
Chet Haase5b0200b2011-04-13 17:58:08 -07001296 int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth");
Chet Haase99ecdc42011-05-06 12:06:34 -07001297 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
Chet Haase5b0200b2011-04-13 17:58:08 -07001298}
1299
Romain Guy70ca14e2010-12-13 18:24:33 -08001300void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001301}
1302
1303///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001304// Drawing
1305///////////////////////////////////////////////////////////////////////////////
1306
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001307bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
1308 Rect& dirty, uint32_t level) {
1309 if (quickReject(0.0f, 0.0f, width, height)) {
1310 return false;
1311 }
1312
Romain Guy0fe478e2010-11-08 12:08:41 -08001313 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1314 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001315 if (displayList && displayList->isRenderable()) {
Romain Guycabfcc12011-03-07 18:06:46 -08001316 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001317 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001318
Chet Haasedaf98e92011-01-10 14:10:36 -08001319 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001320}
1321
Chet Haaseed30fd82011-04-22 16:18:45 -07001322void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) {
1323 if (displayList) {
1324 displayList->output(*this, level);
1325 }
1326}
1327
Romain Guya168d732011-03-18 16:50:13 -07001328void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1329 int alpha;
1330 SkXfermode::Mode mode;
1331 getAlphaAndMode(paint, &alpha, &mode);
1332
Romain Guya168d732011-03-18 16:50:13 -07001333 float x = left;
1334 float y = top;
1335
Romain Guye3c26852011-07-25 16:36:01 -07001336 GLenum filter = GL_LINEAR;
Romain Guya168d732011-03-18 16:50:13 -07001337 bool ignoreTransform = false;
1338 if (mSnapshot->transform->isPureTranslate()) {
1339 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1340 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1341 ignoreTransform = true;
Romain Guye3c26852011-07-25 16:36:01 -07001342 filter = GL_NEAREST;
Romain Guyd21b6e12011-11-30 20:21:23 -08001343 } else {
1344 filter = FILTER(paint);
Romain Guya168d732011-03-18 16:50:13 -07001345 }
1346
1347 setupDraw();
1348 setupDrawWithTexture(true);
Romain Guy5b7a3152011-03-23 17:15:38 -07001349 if (paint) {
1350 setupDrawAlpha8Color(paint->getColor(), alpha);
1351 }
Romain Guya168d732011-03-18 16:50:13 -07001352 setupDrawColorFilter();
1353 setupDrawShader();
1354 setupDrawBlending(true, mode);
1355 setupDrawProgram();
1356 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
Romain Guye3c26852011-07-25 16:36:01 -07001357
Romain Guya168d732011-03-18 16:50:13 -07001358 setupDrawTexture(texture->id);
Romain Guyd21b6e12011-11-30 20:21:23 -08001359 texture->setWrap(GL_CLAMP_TO_EDGE);
1360 texture->setFilter(filter);
Romain Guye3c26852011-07-25 16:36:01 -07001361
Romain Guya168d732011-03-18 16:50:13 -07001362 setupDrawPureColorUniforms();
1363 setupDrawColorFilterUniforms();
1364 setupDrawShaderUniforms();
1365 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1366
1367 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1368
1369 finishDrawTexture();
1370}
1371
Chet Haase5c13d892010-10-08 08:37:55 -07001372void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001373 const float right = left + bitmap->width();
1374 const float bottom = top + bitmap->height();
1375
1376 if (quickReject(left, top, right, bottom)) {
1377 return;
1378 }
1379
Romain Guya1d3c912011-12-13 14:55:06 -08001380 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001381 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001382 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001383 const AutoTexture autoCleanup(texture);
1384
Romain Guya168d732011-03-18 16:50:13 -07001385 if (bitmap->getConfig() == SkBitmap::kA8_Config) {
1386 drawAlphaBitmap(texture, left, top, paint);
1387 } else {
1388 drawTextureRect(left, top, right, bottom, texture, paint);
1389 }
Romain Guyce0537b2010-06-29 21:05:21 -07001390}
1391
Chet Haase5c13d892010-10-08 08:37:55 -07001392void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001393 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1394 const mat4 transform(*matrix);
1395 transform.mapRect(r);
1396
Romain Guy6926c722010-07-12 20:20:03 -07001397 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1398 return;
1399 }
1400
Romain Guya1d3c912011-12-13 14:55:06 -08001401 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001402 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001403 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001404 const AutoTexture autoCleanup(texture);
1405
Romain Guy5b3b3522010-10-27 18:57:51 -07001406 // This could be done in a cheaper way, all we need is pass the matrix
1407 // to the vertex shader. The save/restore is a bit overkill.
1408 save(SkCanvas::kMatrix_SaveFlag);
1409 concatMatrix(matrix);
1410 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1411 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001412}
1413
Romain Guy5a7b4662011-01-20 19:09:30 -08001414void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1415 float* vertices, int* colors, SkPaint* paint) {
1416 // TODO: Do a quickReject
1417 if (!vertices || mSnapshot->isIgnored()) {
1418 return;
1419 }
1420
Romain Guya1d3c912011-12-13 14:55:06 -08001421 mCaches.activeTexture(0);
Romain Guy5a7b4662011-01-20 19:09:30 -08001422 Texture* texture = mCaches.textureCache.get(bitmap);
1423 if (!texture) return;
1424 const AutoTexture autoCleanup(texture);
Romain Guye3c26852011-07-25 16:36:01 -07001425
Romain Guyd21b6e12011-11-30 20:21:23 -08001426 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1427 texture->setFilter(FILTER(paint), true);
Romain Guy5a7b4662011-01-20 19:09:30 -08001428
1429 int alpha;
1430 SkXfermode::Mode mode;
1431 getAlphaAndMode(paint, &alpha, &mode);
1432
Romain Guy5a7b4662011-01-20 19:09:30 -08001433 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001434
Romain Guyb18d2d02011-02-10 15:52:54 -08001435 float left = FLT_MAX;
1436 float top = FLT_MAX;
1437 float right = FLT_MIN;
1438 float bottom = FLT_MIN;
1439
1440#if RENDER_LAYERS_AS_REGIONS
1441 bool hasActiveLayer = hasLayer();
1442#else
1443 bool hasActiveLayer = false;
1444#endif
1445
Romain Guya566b7c2011-01-23 16:36:11 -08001446 // TODO: Support the colors array
1447 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001448 TextureVertex* vertex = mesh;
1449 for (int32_t y = 0; y < meshHeight; y++) {
1450 for (int32_t x = 0; x < meshWidth; x++) {
1451 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1452
1453 float u1 = float(x) / meshWidth;
1454 float u2 = float(x + 1) / meshWidth;
1455 float v1 = float(y) / meshHeight;
1456 float v2 = float(y + 1) / meshHeight;
1457
1458 int ax = i + (meshWidth + 1) * 2;
1459 int ay = ax + 1;
1460 int bx = i;
1461 int by = bx + 1;
1462 int cx = i + 2;
1463 int cy = cx + 1;
1464 int dx = i + (meshWidth + 1) * 2 + 2;
1465 int dy = dx + 1;
1466
1467 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1468 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1469 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1470
1471 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1472 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1473 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001474
1475#if RENDER_LAYERS_AS_REGIONS
1476 if (hasActiveLayer) {
1477 // TODO: This could be optimized to avoid unnecessary ops
1478 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1479 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1480 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1481 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1482 }
1483#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001484 }
1485 }
1486
Romain Guyb18d2d02011-02-10 15:52:54 -08001487#if RENDER_LAYERS_AS_REGIONS
1488 if (hasActiveLayer) {
1489 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1490 }
1491#endif
1492
Romain Guy5a7b4662011-01-20 19:09:30 -08001493 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1494 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001495 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001496}
1497
Romain Guy8ba548f2010-06-30 19:21:21 -07001498void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1499 float srcLeft, float srcTop, float srcRight, float srcBottom,
1500 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001501 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001502 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1503 return;
1504 }
1505
Romain Guya1d3c912011-12-13 14:55:06 -08001506 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001507 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001508 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001509 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07001510
Romain Guy8ba548f2010-06-30 19:21:21 -07001511 const float width = texture->width;
1512 const float height = texture->height;
1513
Romain Guy68169722011-08-22 17:33:33 -07001514 const float u1 = fmax(0.0f, srcLeft / width);
1515 const float v1 = fmax(0.0f, srcTop / height);
1516 const float u2 = fmin(1.0f, srcRight / width);
1517 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07001518
Romain Guy03750a02010-10-18 14:06:08 -07001519 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001520 resetDrawTextureTexCoords(u1, v1, u2, v2);
1521
Romain Guy03750a02010-10-18 14:06:08 -07001522 int alpha;
1523 SkXfermode::Mode mode;
1524 getAlphaAndMode(paint, &alpha, &mode);
1525
Romain Guyd21b6e12011-11-30 20:21:23 -08001526 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1527
Romain Guy6620c6d2010-12-06 18:07:02 -08001528 if (mSnapshot->transform->isPureTranslate()) {
1529 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1530 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1531
Romain Guyb5014982011-07-28 15:39:12 -07001532 GLenum filter = GL_NEAREST;
Romain Guy631582f2011-08-24 11:51:35 -07001533 // Enable linear filtering if the source rectangle is scaled
1534 if (srcRight - srcLeft != dstRight - dstLeft || srcBottom - srcTop != dstBottom - dstTop) {
Romain Guyd21b6e12011-11-30 20:21:23 -08001535 filter = FILTER(paint);
Romain Guyb5014982011-07-28 15:39:12 -07001536 }
Romain Guyb5014982011-07-28 15:39:12 -07001537
Romain Guyd21b6e12011-11-30 20:21:23 -08001538 texture->setFilter(filter, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001539 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1540 texture->id, alpha / 255.0f, mode, texture->blend,
1541 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1542 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1543 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001544 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08001545 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1546 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1547 GL_TRIANGLE_STRIP, gMeshCount);
1548 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001549
1550 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1551}
1552
Romain Guy4aa90572010-09-26 18:40:37 -07001553void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001554 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001555 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001556 if (quickReject(left, top, right, bottom)) {
1557 return;
1558 }
1559
Romain Guya1d3c912011-12-13 14:55:06 -08001560 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001561 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001562 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001563 const AutoTexture autoCleanup(texture);
Romain Guyd21b6e12011-11-30 20:21:23 -08001564 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1565 texture->setFilter(GL_LINEAR, true);
Romain Guyf7f93552010-07-08 19:17:03 -07001566
1567 int alpha;
1568 SkXfermode::Mode mode;
1569 getAlphaAndMode(paint, &alpha, &mode);
1570
Romain Guy2728f962010-10-08 18:36:15 -07001571 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001572 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001573
Romain Guya5ef39a2010-12-03 16:48:20 -08001574 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001575 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001576#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001577 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001578 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001579 const float offsetX = left + mSnapshot->transform->getTranslateX();
1580 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001581 const size_t count = mesh->quads.size();
1582 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001583 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001584 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001585 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1586 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1587 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001588 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001589 dirtyLayer(left + bounds.left, top + bounds.top,
1590 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001591 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001592 }
1593 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001594#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001595
Romain Guy6620c6d2010-12-06 18:07:02 -08001596 if (pureTranslate) {
1597 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1598 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1599
1600 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1601 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1602 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1603 true, !mesh->hasEmptyQuads);
1604 } else {
1605 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1606 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1607 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1608 true, !mesh->hasEmptyQuads);
1609 }
Romain Guy054dc182010-10-15 17:55:25 -07001610 }
Romain Guyf7f93552010-07-08 19:17:03 -07001611}
1612
Chet Haase99ecdc42011-05-06 12:06:34 -07001613/**
Chet Haase858aa932011-05-12 09:06:00 -07001614 * This function uses a similar approach to that of AA lines in the drawLines() function.
1615 * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment
1616 * shader to compute the translucency of the color, determined by whether a given pixel is
1617 * within that boundary region and how far into the region it is.
1618 */
1619void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom,
Romain Guy181d0a62011-06-09 18:52:38 -07001620 int color, SkXfermode::Mode mode) {
Chet Haase858aa932011-05-12 09:06:00 -07001621 float inverseScaleX = 1.0f;
1622 float inverseScaleY = 1.0f;
1623 // The quad that we use needs to account for scaling.
1624 if (!mSnapshot->transform->isPureTranslate()) {
1625 Matrix4 *mat = mSnapshot->transform;
1626 float m00 = mat->data[Matrix4::kScaleX];
1627 float m01 = mat->data[Matrix4::kSkewY];
1628 float m02 = mat->data[2];
1629 float m10 = mat->data[Matrix4::kSkewX];
1630 float m11 = mat->data[Matrix4::kScaleX];
1631 float m12 = mat->data[6];
1632 float scaleX = sqrt(m00 * m00 + m01 * m01);
1633 float scaleY = sqrt(m10 * m10 + m11 * m11);
1634 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1635 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1636 }
1637
1638 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001639 setupDrawNoTexture();
Chet Haase858aa932011-05-12 09:06:00 -07001640 setupDrawAALine();
1641 setupDrawColor(color);
1642 setupDrawColorFilter();
1643 setupDrawShader();
1644 setupDrawBlending(true, mode);
1645 setupDrawProgram();
1646 setupDrawModelViewIdentity(true);
1647 setupDrawColorUniforms();
1648 setupDrawColorFilterUniforms();
1649 setupDrawShaderIdentityUniforms();
1650
1651 AAVertex rects[4];
1652 AAVertex* aaVertices = &rects[0];
1653 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1654 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
1655
1656 float boundarySizeX = .5 * inverseScaleX;
1657 float boundarySizeY = .5 * inverseScaleY;
1658
1659 // Adjust the rect by the AA boundary padding
1660 left -= boundarySizeX;
1661 right += boundarySizeX;
1662 top -= boundarySizeY;
1663 bottom += boundarySizeY;
1664
1665 float width = right - left;
1666 float height = bottom - top;
1667
1668 float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0;
1669 float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0;
1670 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
1671 int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1672 int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength");
1673 glUniform1f(boundaryLengthSlot, boundaryHeightProportion);
1674 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryHeightProportion));
1675
1676 if (!quickReject(left, top, right, bottom)) {
1677 AAVertex::set(aaVertices++, left, bottom, 1, 1);
1678 AAVertex::set(aaVertices++, left, top, 1, 0);
1679 AAVertex::set(aaVertices++, right, bottom, 0, 1);
1680 AAVertex::set(aaVertices++, right, top, 0, 0);
1681 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1682 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1683 }
1684}
1685
1686/**
Chet Haase99ecdc42011-05-06 12:06:34 -07001687 * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization
1688 * rules for those lines produces some unexpected results, and may vary between hardware devices.
1689 * The basics of lines-as-quads is easy; we simply find the normal to the line and position the
1690 * corners of the quads on either side of each line endpoint, separated by the strokeWidth
1691 * of the line. Hairlines are more involved because we need to account for transform scaling
1692 * to end up with a one-pixel-wide line in screen space..
1693 * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader
1694 * in combination with values that we calculate and pass down in this method. The basic approach
1695 * is that the quad we create contains both the core line area plus a bounding area in which
1696 * the translucent/AA pixels are drawn. The values we calculate tell the shader what
1697 * proportion of the width and the length of a given segment is represented by the boundary
1698 * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad.
1699 * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel
1700 * on the inside). This ends up giving the result we want, with pixels that are completely
1701 * 'inside' the line area being filled opaquely and the other pixels being filled according to
1702 * how far into the boundary region they are, which is determined by shader interpolation.
1703 */
Chet Haase8a5cc922011-04-26 07:28:09 -07001704void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1705 if (mSnapshot->isIgnored()) return;
1706
1707 const bool isAA = paint->isAntiAlias();
Chet Haase99ecdc42011-05-06 12:06:34 -07001708 // We use half the stroke width here because we're going to position the quad
1709 // corner vertices half of the width away from the line endpoints
1710 float halfStrokeWidth = paint->getStrokeWidth() * 0.5f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001711 // A stroke width of 0 has a special meaning in Skia:
1712 // it draws a line 1 px wide regardless of current transform
1713 bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase99ecdc42011-05-06 12:06:34 -07001714 float inverseScaleX = 1.0f;
1715 float inverseScaleY = 1.0f;
1716 bool scaled = false;
Chet Haase8a5cc922011-04-26 07:28:09 -07001717 int alpha;
1718 SkXfermode::Mode mode;
1719 int generatedVerticesCount = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001720 int verticesCount = count;
1721 if (count > 4) {
Chet Haasec54ed962011-05-06 14:13:05 -07001722 // Polyline: account for extra vertices needed for continuous tri-strip
Chet Haase99ecdc42011-05-06 12:06:34 -07001723 verticesCount += (count - 4);
1724 }
1725
1726 if (isHairLine || isAA) {
1727 // The quad that we use for AA and hairlines needs to account for scaling. For hairlines
1728 // the line on the screen should always be one pixel wide regardless of scale. For
1729 // AA lines, we only want one pixel of translucent boundary around the quad.
1730 if (!mSnapshot->transform->isPureTranslate()) {
1731 Matrix4 *mat = mSnapshot->transform;
1732 float m00 = mat->data[Matrix4::kScaleX];
1733 float m01 = mat->data[Matrix4::kSkewY];
1734 float m02 = mat->data[2];
1735 float m10 = mat->data[Matrix4::kSkewX];
1736 float m11 = mat->data[Matrix4::kScaleX];
1737 float m12 = mat->data[6];
1738 float scaleX = sqrt(m00*m00 + m01*m01);
1739 float scaleY = sqrt(m10*m10 + m11*m11);
1740 inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
1741 inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
1742 if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
1743 scaled = true;
1744 }
1745 }
Chet Haase5b0200b2011-04-13 17:58:08 -07001746 }
Chet Haase8a5cc922011-04-26 07:28:09 -07001747
1748 getAlphaAndMode(paint, &alpha, &mode);
1749 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001750 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001751 if (isAA) {
1752 setupDrawAALine();
1753 }
1754 setupDrawColor(paint->getColor(), alpha);
1755 setupDrawColorFilter();
1756 setupDrawShader();
1757 if (isAA) {
1758 setupDrawBlending(true, mode);
1759 } else {
1760 setupDrawBlending(mode);
1761 }
1762 setupDrawProgram();
1763 setupDrawModelViewIdentity(true);
1764 setupDrawColorUniforms();
1765 setupDrawColorFilterUniforms();
1766 setupDrawShaderIdentityUniforms();
1767
1768 if (isHairLine) {
1769 // Set a real stroke width to be used in quad construction
Chet Haase99ecdc42011-05-06 12:06:34 -07001770 halfStrokeWidth = isAA? 1 : .5;
1771 } else if (isAA && !scaled) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001772 // Expand boundary to enable AA calculations on the quad border
Chet Haase99ecdc42011-05-06 12:06:34 -07001773 halfStrokeWidth += .5f;
Chet Haase5b0200b2011-04-13 17:58:08 -07001774 }
1775 Vertex lines[verticesCount];
1776 Vertex* vertices = &lines[0];
Chet Haase99585ad2011-05-02 15:00:16 -07001777 AAVertex wLines[verticesCount];
1778 AAVertex* aaVertices = &wLines[0];
Chet Haase5b0200b2011-04-13 17:58:08 -07001779 if (!isAA) {
1780 setupDrawVertices(vertices);
1781 } else {
Chet Haase99585ad2011-05-02 15:00:16 -07001782 void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
1783 void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset;
Chet Haase99ecdc42011-05-06 12:06:34 -07001784 // innerProportion is the ratio of the inner (non-AA) part of the line to the total
Chet Haase5b0200b2011-04-13 17:58:08 -07001785 // AA stroke width (the base stroke width expanded by a half pixel on either side).
1786 // This value is used in the fragment shader to determine how to fill fragments.
Chet Haase99ecdc42011-05-06 12:06:34 -07001787 // We will need to calculate the actual width proportion on each segment for
1788 // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled.
1789 float boundaryWidthProportion = 1 / (2 * halfStrokeWidth);
1790 setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion);
Chet Haase5b0200b2011-04-13 17:58:08 -07001791 }
1792
Chet Haase99ecdc42011-05-06 12:06:34 -07001793 AAVertex* prevAAVertex = NULL;
1794 Vertex* prevVertex = NULL;
Romain Guy740bf2b2011-04-26 15:33:10 -07001795
Chet Haase99585ad2011-05-02 15:00:16 -07001796 int boundaryLengthSlot = -1;
1797 int inverseBoundaryLengthSlot = -1;
Chet Haase99ecdc42011-05-06 12:06:34 -07001798 int boundaryWidthSlot = -1;
1799 int inverseBoundaryWidthSlot = -1;
Chet Haase5b0200b2011-04-13 17:58:08 -07001800 for (int i = 0; i < count; i += 4) {
1801 // a = start point, b = end point
1802 vec2 a(points[i], points[i + 1]);
1803 vec2 b(points[i + 2], points[i + 3]);
Chet Haase99585ad2011-05-02 15:00:16 -07001804 float length = 0;
Chet Haase99ecdc42011-05-06 12:06:34 -07001805 float boundaryLengthProportion = 0;
1806 float boundaryWidthProportion = 0;
Chet Haase5b0200b2011-04-13 17:58:08 -07001807
Chet Haase5b0200b2011-04-13 17:58:08 -07001808 // Find the normal to the line
Chet Haase99ecdc42011-05-06 12:06:34 -07001809 vec2 n = (b - a).copyNormalized() * halfStrokeWidth;
Chet Haase8a5cc922011-04-26 07:28:09 -07001810 if (isHairLine) {
Chet Haase8a5cc922011-04-26 07:28:09 -07001811 if (isAA) {
1812 float wideningFactor;
1813 if (fabs(n.x) >= fabs(n.y)) {
1814 wideningFactor = fabs(1.0f / n.x);
1815 } else {
1816 wideningFactor = fabs(1.0f / n.y);
1817 }
1818 n *= wideningFactor;
Chet Haase5b0200b2011-04-13 17:58:08 -07001819 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001820 if (scaled) {
1821 n.x *= inverseScaleX;
1822 n.y *= inverseScaleY;
1823 }
1824 } else if (scaled) {
1825 // Extend n by .5 pixel on each side, post-transform
1826 vec2 extendedN = n.copyNormalized();
1827 extendedN /= 2;
1828 extendedN.x *= inverseScaleX;
1829 extendedN.y *= inverseScaleY;
1830 float extendedNLength = extendedN.length();
1831 // We need to set this value on the shader prior to drawing
1832 boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength);
1833 n += extendedN;
Chet Haase5b0200b2011-04-13 17:58:08 -07001834 }
1835 float x = n.x;
1836 n.x = -n.y;
1837 n.y = x;
1838
Chet Haase99585ad2011-05-02 15:00:16 -07001839 // aa lines expand the endpoint vertices to encompass the AA boundary
1840 if (isAA) {
1841 vec2 abVector = (b - a);
1842 length = abVector.length();
1843 abVector.normalize();
Chet Haase99ecdc42011-05-06 12:06:34 -07001844 if (scaled) {
1845 abVector.x *= inverseScaleX;
1846 abVector.y *= inverseScaleY;
1847 float abLength = abVector.length();
1848 boundaryLengthProportion = abLength / (length + abLength);
1849 } else {
1850 boundaryLengthProportion = .5 / (length + 1);
1851 }
1852 abVector /= 2;
Chet Haase99585ad2011-05-02 15:00:16 -07001853 a -= abVector;
1854 b += abVector;
1855 }
1856
Chet Haase5b0200b2011-04-13 17:58:08 -07001857 // Four corners of the rectangle defining a thick line
1858 vec2 p1 = a - n;
1859 vec2 p2 = a + n;
1860 vec2 p3 = b + n;
1861 vec2 p4 = b - n;
1862
Chet Haase99585ad2011-05-02 15:00:16 -07001863
Chet Haase5b0200b2011-04-13 17:58:08 -07001864 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1865 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1866 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1867 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
1868
1869 if (!quickReject(left, top, right, bottom)) {
Chet Haase5b0200b2011-04-13 17:58:08 -07001870 if (!isAA) {
1871 if (prevVertex != NULL) {
1872 // Issue two repeat vertices to create degenerate triangles to bridge
1873 // between the previous line and the new one. This is necessary because
1874 // we are creating a single triangle_strip which will contain
1875 // potentially discontinuous line segments.
1876 Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]);
1877 Vertex::set(vertices++, p1.x, p1.y);
1878 generatedVerticesCount += 2;
1879 }
1880 Vertex::set(vertices++, p1.x, p1.y);
1881 Vertex::set(vertices++, p2.x, p2.y);
1882 Vertex::set(vertices++, p4.x, p4.y);
1883 Vertex::set(vertices++, p3.x, p3.y);
1884 prevVertex = vertices - 1;
1885 generatedVerticesCount += 4;
1886 } else {
Chet Haase99ecdc42011-05-06 12:06:34 -07001887 if (!isHairLine && scaled) {
1888 // Must set width proportions per-segment for scaled non-hairlines to use the
1889 // correct AA boundary dimensions
1890 if (boundaryWidthSlot < 0) {
1891 boundaryWidthSlot =
1892 mCaches.currentProgram->getUniform("boundaryWidth");
1893 inverseBoundaryWidthSlot =
1894 mCaches.currentProgram->getUniform("inverseBoundaryWidth");
1895 }
1896 glUniform1f(boundaryWidthSlot, boundaryWidthProportion);
1897 glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion));
1898 }
Chet Haase99585ad2011-05-02 15:00:16 -07001899 if (boundaryLengthSlot < 0) {
1900 boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength");
1901 inverseBoundaryLengthSlot =
1902 mCaches.currentProgram->getUniform("inverseBoundaryLength");
1903 }
Chet Haase99ecdc42011-05-06 12:06:34 -07001904 glUniform1f(boundaryLengthSlot, boundaryLengthProportion);
1905 glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion));
Chet Haase99585ad2011-05-02 15:00:16 -07001906
Chet Haase5b0200b2011-04-13 17:58:08 -07001907 if (prevAAVertex != NULL) {
1908 // Issue two repeat vertices to create degenerate triangles to bridge
1909 // between the previous line and the new one. This is necessary because
1910 // we are creating a single triangle_strip which will contain
1911 // potentially discontinuous line segments.
Chet Haase99585ad2011-05-02 15:00:16 -07001912 AAVertex::set(aaVertices++,prevAAVertex->position[0],
1913 prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length);
1914 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
Chet Haase5b0200b2011-04-13 17:58:08 -07001915 generatedVerticesCount += 2;
1916 }
Chet Haase99585ad2011-05-02 15:00:16 -07001917 AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1);
1918 AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0);
1919 AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1);
1920 AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0);
Chet Haase5b0200b2011-04-13 17:58:08 -07001921 prevAAVertex = aaVertices - 1;
1922 generatedVerticesCount += 4;
1923 }
Chet Haase99585ad2011-05-02 15:00:16 -07001924 dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top,
1925 a.x == b.x ? right: right, a.y == b.y ? bottom: bottom,
1926 *mSnapshot->transform);
Chet Haase5b0200b2011-04-13 17:58:08 -07001927 }
1928 }
1929 if (generatedVerticesCount > 0) {
1930 glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount);
1931 }
1932}
1933
Romain Guyed6fcb02011-03-21 13:11:28 -07001934void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
1935 if (mSnapshot->isIgnored()) return;
1936
1937 // TODO: The paint's cap style defines whether the points are square or circular
1938 // TODO: Handle AA for round points
1939
Chet Haase5b0200b2011-04-13 17:58:08 -07001940 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07001941 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07001942 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07001943 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07001944 if (isHairLine) {
1945 // Now that we know it's hairline, we can set the effective width, to be used later
1946 strokeWidth = 1.0f;
1947 }
1948 const float halfWidth = strokeWidth / 2;
Romain Guyed6fcb02011-03-21 13:11:28 -07001949 int alpha;
1950 SkXfermode::Mode mode;
1951 getAlphaAndMode(paint, &alpha, &mode);
1952
1953 int verticesCount = count >> 1;
1954 int generatedVerticesCount = 0;
1955
1956 TextureVertex pointsData[verticesCount];
1957 TextureVertex* vertex = &pointsData[0];
1958
1959 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08001960 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07001961 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07001962 setupDrawColor(paint->getColor(), alpha);
1963 setupDrawColorFilter();
1964 setupDrawShader();
1965 setupDrawBlending(mode);
1966 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07001967 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07001968 setupDrawColorUniforms();
1969 setupDrawColorFilterUniforms();
1970 setupDrawPointUniforms();
1971 setupDrawShaderIdentityUniforms();
1972 setupDrawMesh(vertex);
1973
1974 for (int i = 0; i < count; i += 2) {
1975 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1976 generatedVerticesCount++;
Chet Haase8a5cc922011-04-26 07:28:09 -07001977 float left = points[i] - halfWidth;
1978 float right = points[i] + halfWidth;
1979 float top = points[i + 1] - halfWidth;
1980 float bottom = points [i + 1] + halfWidth;
1981 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyed6fcb02011-03-21 13:11:28 -07001982 }
1983
1984 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
1985}
1986
Romain Guy85bf02f2010-06-22 13:11:24 -07001987void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001988 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001989 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001990
Romain Guyae88e5e2010-10-22 17:49:18 -07001991 Rect& clip(*mSnapshot->clipRect);
1992 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001993
Romain Guy3d58c032010-07-14 16:34:53 -07001994 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001995}
Romain Guy9d5316e2010-06-24 19:30:36 -07001996
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001997void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001998 if (!texture) return;
1999 const AutoTexture autoCleanup(texture);
2000
2001 const float x = left + texture->left - texture->offset;
2002 const float y = top + texture->top - texture->offset;
2003
2004 drawPathTexture(texture, x, y, paint);
2005}
2006
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002007void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
2008 float rx, float ry, SkPaint* paint) {
2009 if (mSnapshot->isIgnored()) return;
2010
Romain Guya1d3c912011-12-13 14:55:06 -08002011 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002012 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
2013 right - left, bottom - top, rx, ry, paint);
2014 drawShape(left, top, texture, paint);
2015}
2016
Romain Guy01d58e42011-01-19 21:54:02 -08002017void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
2018 if (mSnapshot->isIgnored()) return;
2019
Romain Guya1d3c912011-12-13 14:55:06 -08002020 mCaches.activeTexture(0);
Romain Guy01d58e42011-01-19 21:54:02 -08002021 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002022 drawShape(x - radius, y - radius, texture, paint);
2023}
Romain Guy01d58e42011-01-19 21:54:02 -08002024
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002025void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
2026 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002027
Romain Guya1d3c912011-12-13 14:55:06 -08002028 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002029 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
2030 drawShape(left, top, texture, paint);
2031}
2032
Romain Guy8b2f5262011-01-23 16:15:02 -08002033void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
2034 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
2035 if (mSnapshot->isIgnored()) return;
2036
2037 if (fabs(sweepAngle) >= 360.0f) {
2038 drawOval(left, top, right, bottom, paint);
2039 return;
2040 }
2041
Romain Guya1d3c912011-12-13 14:55:06 -08002042 mCaches.activeTexture(0);
Romain Guy8b2f5262011-01-23 16:15:02 -08002043 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
2044 startAngle, sweepAngle, useCenter, paint);
2045 drawShape(left, top, texture, paint);
2046}
2047
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002048void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
2049 SkPaint* paint) {
2050 if (mSnapshot->isIgnored()) return;
2051
Romain Guya1d3c912011-12-13 14:55:06 -08002052 mCaches.activeTexture(0);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002053 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
2054 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08002055}
2056
Chet Haase5c13d892010-10-08 08:37:55 -07002057void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002058 if (p->getStyle() != SkPaint::kFill_Style) {
2059 drawRectAsShape(left, top, right, bottom, p);
2060 return;
2061 }
2062
Romain Guy6926c722010-07-12 20:20:03 -07002063 if (quickReject(left, top, right, bottom)) {
2064 return;
2065 }
2066
Romain Guy026c5e162010-06-28 17:12:22 -07002067 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07002068 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002069 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
2070 if (!isMode) {
2071 // Assume SRC_OVER
2072 mode = SkXfermode::kSrcOver_Mode;
2073 }
2074 } else {
2075 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07002076 }
2077
Romain Guy026c5e162010-06-28 17:12:22 -07002078 int color = p->getColor();
Romain Guy181d0a62011-06-09 18:52:38 -07002079 if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) {
Chet Haase858aa932011-05-12 09:06:00 -07002080 drawAARect(left, top, right, bottom, color, mode);
2081 } else {
2082 drawColorRect(left, top, right, bottom, color, mode);
2083 }
Romain Guyc7d53492010-06-25 13:41:57 -07002084}
Romain Guy9d5316e2010-06-24 19:30:36 -07002085
Romain Guyeb9a5362012-01-17 17:39:26 -08002086void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
2087 const float* positions, SkPaint* paint) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002088 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2089 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guyeb9a5362012-01-17 17:39:26 -08002090 return;
2091 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002092
Romain Guy671d6cf2012-01-18 12:39:17 -08002093 // NOTE: Skia does not support perspective transform on drawPosText yet
2094 if (!mSnapshot->transform->isSimple()) {
2095 return;
2096 }
2097
2098 float x = 0.0f;
2099 float y = 0.0f;
2100 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2101 if (pureTranslate) {
2102 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2103 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2104 }
2105
2106 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
2107 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
2108 paint->getTextSize());
2109
2110 int alpha;
2111 SkXfermode::Mode mode;
2112 getAlphaAndMode(paint, &alpha, &mode);
2113
2114 // Pick the appropriate texture filtering
2115 bool linearFilter = mSnapshot->transform->changesBounds();
2116 if (pureTranslate && !linearFilter) {
2117 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2118 }
2119
2120 mCaches.activeTexture(0);
2121 setupDraw();
2122 setupDrawDirtyRegionsDisabled();
2123 setupDrawWithTexture(true);
2124 setupDrawAlpha8Color(paint->getColor(), alpha);
2125 setupDrawColorFilter();
2126 setupDrawShader();
2127 setupDrawBlending(true, mode);
2128 setupDrawProgram();
2129 setupDrawModelView(x, y, x, y, pureTranslate, true);
2130 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2131 setupDrawPureColorUniforms();
2132 setupDrawColorFilterUniforms();
2133 setupDrawShaderUniforms(pureTranslate);
2134
2135 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2136 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2137
2138#if RENDER_LAYERS_AS_REGIONS
2139 bool hasActiveLayer = hasLayer();
2140#else
2141 bool hasActiveLayer = false;
2142#endif
2143
2144 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
2145 positions, hasActiveLayer ? &bounds : NULL)) {
2146#if RENDER_LAYERS_AS_REGIONS
2147 if (hasActiveLayer) {
2148 if (!pureTranslate) {
2149 mSnapshot->transform->mapRect(bounds);
2150 }
2151 dirtyLayerUnchecked(bounds, getRegion());
2152 }
2153#endif
2154 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002155}
2156
Romain Guye8e62a42010-07-23 18:55:21 -07002157void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Romain Guycac5fd32011-12-01 20:08:50 -08002158 float x, float y, SkPaint* paint, float length) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002159 if (text == NULL || count == 0 || mSnapshot->isIgnored() ||
2160 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -07002161 return;
2162 }
2163
Romain Guye8e62a42010-07-23 18:55:21 -07002164 switch (paint->getTextAlign()) {
2165 case SkPaint::kCenter_Align:
Romain Guycac5fd32011-12-01 20:08:50 -08002166 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002167 x -= length / 2.0f;
2168 break;
2169 case SkPaint::kRight_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;
2172 break;
2173 default:
2174 break;
2175 }
2176
Romain Guycac5fd32011-12-01 20:08:50 -08002177 SkPaint::FontMetrics metrics;
2178 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy8f9a9f62011-12-05 11:53:26 -08002179 // If no length was specified, just perform the hit test on the Y axis
Romain Guycac5fd32011-12-01 20:08:50 -08002180 if (quickReject(x, y + metrics.fTop,
2181 x + (length >= 0.0f ? length : INT_MAX / 2), y + metrics.fBottom)) {
2182 return;
2183 }
2184
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002185 const float oldX = x;
2186 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08002187 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2188 if (pureTranslate) {
2189 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2190 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2191 }
2192
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002193#if DEBUG_GLYPHS
Steve Block5baa3a62011-12-20 16:23:08 +00002194 ALOGD("OpenGLRenderer drawText() with FontID=%d", SkTypeface::UniqueID(paint->getTypeface()));
Fabrice Di Meglioef9bb3c2011-10-17 11:06:46 -07002195#endif
Romain Guyeb9a5362012-01-17 17:39:26 -08002196
2197 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
Romain Guyb45c0c92010-08-26 20:35:23 -07002198 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07002199 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07002200
Romain Guy86568192010-12-14 15:55:39 -08002201 int alpha;
2202 SkXfermode::Mode mode;
2203 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002204
Romain Guy1e45aae2010-08-13 19:39:53 -07002205 if (mHasShadow) {
Romain Guy2d4fd362011-12-13 22:00:19 -08002206 mCaches.activeTexture(0);
2207
Romain Guyb45c0c92010-08-26 20:35:23 -07002208 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guy67ffc362011-06-03 18:50:11 -07002209 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
2210 paint, text, bytesCount, count, mShadowRadius);
Romain Guy1e45aae2010-08-13 19:39:53 -07002211 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07002212
Romain Guy740bf2b2011-04-26 15:33:10 -07002213 const float sx = oldX - shadow->left + mShadowDx;
2214 const float sy = oldY - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07002215
Romain Guy86568192010-12-14 15:55:39 -08002216 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
Romain Guy740bf2b2011-04-26 15:33:10 -07002217 int shadowColor = mShadowColor;
2218 if (mShader) {
2219 shadowColor = 0xffffffff;
2220 }
Romain Guy86568192010-12-14 15:55:39 -08002221
Romain Guy86568192010-12-14 15:55:39 -08002222 setupDraw();
2223 setupDrawWithTexture(true);
Romain Guy740bf2b2011-04-26 15:33:10 -07002224 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2225 setupDrawColorFilter();
2226 setupDrawShader();
Romain Guy86568192010-12-14 15:55:39 -08002227 setupDrawBlending(true, mode);
2228 setupDrawProgram();
Romain Guy740bf2b2011-04-26 15:33:10 -07002229 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
Romain Guy86568192010-12-14 15:55:39 -08002230 setupDrawTexture(shadow->id);
2231 setupDrawPureColorUniforms();
Romain Guy740bf2b2011-04-26 15:33:10 -07002232 setupDrawColorFilterUniforms();
2233 setupDrawShaderUniforms();
Romain Guy86568192010-12-14 15:55:39 -08002234 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2235
Romain Guy0a417492010-08-16 20:26:20 -07002236 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy1e45aae2010-08-13 19:39:53 -07002237 }
2238
Romain Guy6620c6d2010-12-06 18:07:02 -08002239 // Pick the appropriate texture filtering
2240 bool linearFilter = mSnapshot->transform->changesBounds();
2241 if (pureTranslate && !linearFilter) {
2242 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2243 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002244
Romain Guya1d3c912011-12-13 14:55:06 -08002245 mCaches.activeTexture(0);
Romain Guy86568192010-12-14 15:55:39 -08002246 setupDraw();
2247 setupDrawDirtyRegionsDisabled();
2248 setupDrawWithTexture(true);
2249 setupDrawAlpha8Color(paint->getColor(), alpha);
2250 setupDrawColorFilter();
2251 setupDrawShader();
2252 setupDrawBlending(true, mode);
2253 setupDrawProgram();
2254 setupDrawModelView(x, y, x, y, pureTranslate, true);
2255 setupDrawTexture(fontRenderer.getTexture(linearFilter));
2256 setupDrawPureColorUniforms();
2257 setupDrawColorFilterUniforms();
2258 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07002259
Romain Guy6620c6d2010-12-06 18:07:02 -08002260 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07002261 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2262
2263#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002264 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07002265#else
Romain Guyf219da52011-01-16 12:54:25 -08002266 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07002267#endif
Alex Sakhartchouk894df172011-02-17 16:45:37 -08002268
Romain Guy6620c6d2010-12-06 18:07:02 -08002269 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08002270 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07002271#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08002272 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08002273 if (!pureTranslate) {
2274 mSnapshot->transform->mapRect(bounds);
2275 }
Romain Guyf219da52011-01-16 12:54:25 -08002276 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002277 }
2278#endif
2279 }
Romain Guy694b5192010-07-21 21:33:20 -07002280
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002281 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07002282}
2283
Romain Guy7fbcc042010-08-04 15:40:07 -07002284void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08002285 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002286
Romain Guya1d3c912011-12-13 14:55:06 -08002287 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002288
Romain Guyfb8b7632010-08-23 21:05:08 -07002289 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -07002290 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002291 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002292
Romain Guy8b55f372010-08-18 17:10:07 -07002293 const float x = texture->left - texture->offset;
2294 const float y = texture->top - texture->offset;
2295
Romain Guy01d58e42011-01-19 21:54:02 -08002296 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07002297}
2298
Romain Guyada830f2011-01-13 12:13:20 -08002299void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
2300 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08002301 return;
2302 }
2303
Romain Guya1d3c912011-12-13 14:55:06 -08002304 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002305
2306 int alpha;
2307 SkXfermode::Mode mode;
2308 getAlphaAndMode(paint, &alpha, &mode);
2309
Romain Guy9ace8f52011-07-07 20:50:11 -07002310 layer->setAlpha(alpha, mode);
Romain Guy6c319ca2011-01-11 14:29:25 -08002311
Romain Guyf219da52011-01-16 12:54:25 -08002312#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08002313 if (!layer->region.isEmpty()) {
2314 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002315 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002316 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08002317 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08002318 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08002319
Romain Guyc88e3572011-01-22 00:32:12 -08002320 setupDraw();
2321 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002322 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002323 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002324 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002325 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002326 setupDrawPureColorUniforms();
2327 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002328 setupDrawTexture(layer->getTexture());
2329 if (mSnapshot->transform->isPureTranslate()) {
2330 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
2331 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
2332
Romain Guyd21b6e12011-11-30 20:21:23 -08002333 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07002334 setupDrawModelViewTranslate(x, y,
2335 x + layer->layer.getWidth(), y + layer->layer.getHeight(), true);
2336 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002337 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07002338 setupDrawModelViewTranslate(x, y,
2339 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2340 }
Romain Guyc88e3572011-01-22 00:32:12 -08002341 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08002342
Romain Guyc88e3572011-01-22 00:32:12 -08002343 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
2344 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08002345
Romain Guyc88e3572011-01-22 00:32:12 -08002346 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08002347
2348#if DEBUG_LAYERS_AS_REGIONS
2349 drawRegionRects(layer->region);
2350#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002351 }
Romain Guyf219da52011-01-16 12:54:25 -08002352 }
2353#else
Romain Guyada830f2011-01-13 12:13:20 -08002354 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
2355 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08002356#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08002357}
2358
Romain Guy6926c722010-07-12 20:20:03 -07002359///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002360// Shaders
2361///////////////////////////////////////////////////////////////////////////////
2362
2363void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07002364 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07002365}
2366
Romain Guy06f96e22010-07-30 19:18:16 -07002367void OpenGLRenderer::setupShader(SkiaShader* shader) {
2368 mShader = shader;
2369 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002370 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07002371 }
Romain Guy7fac2e12010-07-16 17:10:13 -07002372}
2373
Romain Guyd27977d2010-07-14 19:18:51 -07002374///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07002375// Color filters
2376///////////////////////////////////////////////////////////////////////////////
2377
2378void OpenGLRenderer::resetColorFilter() {
2379 mColorFilter = NULL;
2380}
2381
2382void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
2383 mColorFilter = filter;
2384}
2385
2386///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07002387// Drop shadow
2388///////////////////////////////////////////////////////////////////////////////
2389
2390void OpenGLRenderer::resetShadow() {
2391 mHasShadow = false;
2392}
2393
2394void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
2395 mHasShadow = true;
2396 mShadowRadius = radius;
2397 mShadowDx = dx;
2398 mShadowDy = dy;
2399 mShadowColor = color;
2400}
2401
2402///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08002403// Draw filters
2404///////////////////////////////////////////////////////////////////////////////
2405
2406void OpenGLRenderer::resetPaintFilter() {
2407 mHasDrawFilter = false;
2408}
2409
2410void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
2411 mHasDrawFilter = true;
2412 mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
2413 mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
2414}
2415
2416SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
2417 if (!mHasDrawFilter || !paint) return paint;
2418
2419 uint32_t flags = paint->getFlags();
2420
2421 mFilteredPaint = *paint;
2422 mFilteredPaint.setFlags((flags & ~mPaintFilterClearBits) | mPaintFilterSetBits);
2423
2424 return &mFilteredPaint;
2425}
2426
2427///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07002428// Drawing implementation
2429///////////////////////////////////////////////////////////////////////////////
2430
Romain Guy01d58e42011-01-19 21:54:02 -08002431void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
2432 float x, float y, SkPaint* paint) {
2433 if (quickReject(x, y, x + texture->width, y + texture->height)) {
2434 return;
2435 }
2436
2437 int alpha;
2438 SkXfermode::Mode mode;
2439 getAlphaAndMode(paint, &alpha, &mode);
2440
2441 setupDraw();
2442 setupDrawWithTexture(true);
2443 setupDrawAlpha8Color(paint->getColor(), alpha);
2444 setupDrawColorFilter();
2445 setupDrawShader();
2446 setupDrawBlending(true, mode);
2447 setupDrawProgram();
2448 setupDrawModelView(x, y, x + texture->width, y + texture->height);
2449 setupDrawTexture(texture->id);
2450 setupDrawPureColorUniforms();
2451 setupDrawColorFilterUniforms();
2452 setupDrawShaderUniforms();
2453 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2454
2455 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2456
2457 finishDrawTexture();
2458}
2459
Romain Guyf607bdc2010-09-10 19:20:06 -07002460// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07002461#define kStdStrikeThru_Offset (-6.0f / 21.0f)
2462#define kStdUnderline_Offset (1.0f / 9.0f)
2463#define kStdUnderline_Thickness (1.0f / 18.0f)
2464
2465void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
2466 float x, float y, SkPaint* paint) {
2467 // Handle underline and strike-through
2468 uint32_t flags = paint->getFlags();
2469 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07002470 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07002471 float underlineWidth = length;
2472 // If length is > 0.0f, we already measured the text for the text alignment
2473 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002474 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07002475 }
2476
2477 float offsetX = 0;
Romain Guy726aeba2011-06-01 14:52:00 -07002478 switch (paintCopy.getTextAlign()) {
Romain Guy0a417492010-08-16 20:26:20 -07002479 case SkPaint::kCenter_Align:
2480 offsetX = underlineWidth * 0.5f;
2481 break;
2482 case SkPaint::kRight_Align:
2483 offsetX = underlineWidth;
2484 break;
2485 default:
2486 break;
2487 }
2488
2489 if (underlineWidth > 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07002490 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08002491 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07002492
Romain Guye20ecbd2010-09-22 19:49:04 -07002493 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07002494 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07002495
Romain Guyf6834472011-01-23 13:32:12 -08002496 int linesCount = 0;
2497 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
2498 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
2499
2500 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07002501 float points[pointsCount];
2502 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07002503
2504 if (flags & SkPaint::kUnderlineText_Flag) {
2505 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002506 points[currentPoint++] = left;
2507 points[currentPoint++] = top;
2508 points[currentPoint++] = left + underlineWidth;
2509 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002510 }
2511
2512 if (flags & SkPaint::kStrikeThruText_Flag) {
2513 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07002514 points[currentPoint++] = left;
2515 points[currentPoint++] = top;
2516 points[currentPoint++] = left + underlineWidth;
2517 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07002518 }
Romain Guye20ecbd2010-09-22 19:49:04 -07002519
Romain Guy726aeba2011-06-01 14:52:00 -07002520 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07002521
Romain Guy726aeba2011-06-01 14:52:00 -07002522 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07002523 }
2524 }
2525}
2526
Romain Guy026c5e162010-06-28 17:12:22 -07002527void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07002528 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07002529 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07002530 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07002531 color |= 0x00ffffff;
2532 }
2533
Romain Guy70ca14e2010-12-13 18:24:33 -08002534 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002535 setupDrawNoTexture();
Romain Guy70ca14e2010-12-13 18:24:33 -08002536 setupDrawColor(color);
2537 setupDrawShader();
2538 setupDrawColorFilter();
2539 setupDrawBlending(mode);
2540 setupDrawProgram();
2541 setupDrawModelView(left, top, right, bottom, ignoreTransform);
2542 setupDrawColorUniforms();
2543 setupDrawShaderUniforms(ignoreTransform);
2544 setupDrawColorFilterUniforms();
2545 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07002546
Romain Guyc95c8d62010-09-17 15:31:32 -07002547 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2548}
2549
Romain Guy82ba8142010-07-09 13:25:56 -07002550void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07002551 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07002552 int alpha;
2553 SkXfermode::Mode mode;
2554 getAlphaAndMode(paint, &alpha, &mode);
2555
Romain Guyd21b6e12011-11-30 20:21:23 -08002556 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07002557
Romain Guy6620c6d2010-12-06 18:07:02 -08002558 if (mSnapshot->transform->isPureTranslate()) {
2559 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
2560 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
2561
Romain Guyd21b6e12011-11-30 20:21:23 -08002562 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002563 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
2564 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
2565 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
2566 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002567 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08002568 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
2569 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2570 GL_TRIANGLE_STRIP, gMeshCount);
2571 }
Romain Guy85bf02f2010-06-22 13:11:24 -07002572}
2573
Romain Guybd6b79b2010-06-26 00:13:53 -07002574void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002575 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
2576 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07002577 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07002578}
2579
2580void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07002581 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07002582 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07002583 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002584
Romain Guy746b7402010-10-26 16:27:31 -07002585 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08002586 setupDrawWithTexture();
2587 setupDrawColor(alpha, alpha, alpha, alpha);
2588 setupDrawColorFilter();
2589 setupDrawBlending(blend, mode, swapSrcDst);
2590 setupDrawProgram();
2591 if (!dirty) {
2592 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07002593 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002594 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08002595 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002596 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08002597 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07002598 }
Romain Guy86568192010-12-14 15:55:39 -08002599 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08002600 setupDrawColorFilterUniforms();
2601 setupDrawTexture(texture);
2602 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07002603
Romain Guy6820ac82010-09-15 18:11:50 -07002604 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08002605
2606 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07002607}
2608
Romain Guya5aed0d2010-09-09 14:42:43 -07002609void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07002610 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07002611 blend = blend || mode != SkXfermode::kSrcOver_Mode;
2612 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08002613 // These blend modes are not supported by OpenGL directly and have
2614 // to be implemented using shaders. Since the shader will perform
2615 // the blending, turn blending off here
2616 // If the blend mode cannot be implemented using shaders, fall
2617 // back to the default SrcOver blend mode instead
2618 if (mode > SkXfermode::kScreen_Mode) {
Romain Guy746b7402010-10-26 16:27:31 -07002619 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002620 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07002621 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07002622
Romain Guy82bc7a72012-01-03 14:13:39 -08002623 if (mCaches.blend) {
2624 glDisable(GL_BLEND);
2625 mCaches.blend = false;
2626 }
2627
2628 return;
2629 } else {
2630 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002631 }
Romain Guy82bc7a72012-01-03 14:13:39 -08002632 }
2633
2634 if (!mCaches.blend) {
2635 glEnable(GL_BLEND);
2636 }
2637
2638 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
2639 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
2640
2641 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
2642 glBlendFunc(sourceMode, destMode);
2643 mCaches.lastSrcMode = sourceMode;
2644 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07002645 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002646 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07002647 glDisable(GL_BLEND);
2648 }
Romain Guyfb8b7632010-08-23 21:05:08 -07002649 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07002650}
2651
Romain Guy889f8d12010-07-29 14:37:42 -07002652bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07002653 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002654 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07002655 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07002656 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07002657 return false;
Romain Guy260e1022010-07-12 14:41:06 -07002658 }
Romain Guy6926c722010-07-12 20:20:03 -07002659 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002660}
2661
Romain Guy026c5e162010-06-28 17:12:22 -07002662void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002663 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002664 TextureVertex::setUV(v++, u1, v1);
2665 TextureVertex::setUV(v++, u2, v1);
2666 TextureVertex::setUV(v++, u1, v2);
2667 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002668}
2669
Chet Haase5c13d892010-10-08 08:37:55 -07002670void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002671 if (paint) {
Romain Guy2ffefd42011-09-08 15:33:03 -07002672 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002673
2674 // Skia draws using the color's alpha channel if < 255
2675 // Otherwise, it uses the paint's alpha
2676 int color = paint->getColor();
2677 *alpha = (color >> 24) & 0xFF;
2678 if (*alpha == 255) {
2679 *alpha = paint->getAlpha();
2680 }
2681 } else {
2682 *mode = SkXfermode::kSrcOver_Mode;
2683 *alpha = 255;
2684 }
Romain Guy026c5e162010-06-28 17:12:22 -07002685}
2686
Romain Guya5aed0d2010-09-09 14:42:43 -07002687SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002688 SkXfermode::Mode resultMode;
2689 if (!SkXfermode::AsMode(mode, &resultMode)) {
2690 resultMode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002691 }
Derek Sollenbergerd39d1af2011-05-16 13:09:42 -04002692 return resultMode;
Romain Guya5aed0d2010-09-09 14:42:43 -07002693}
2694
Romain Guy9d5316e2010-06-24 19:30:36 -07002695}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002696}; // namespace android