blob: a711289dbd8e5e5b745ad840aac5644568bd5bb0 [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 Guy9d5316e2010-06-24 19:30:36 -070050///////////////////////////////////////////////////////////////////////////////
51// Globals
52///////////////////////////////////////////////////////////////////////////////
53
Romain Guy889f8d12010-07-29 14:37:42 -070054/**
55 * Structure mapping Skia xfermodes to OpenGL blending factors.
56 */
57struct Blender {
58 SkXfermode::Mode mode;
59 GLenum src;
60 GLenum dst;
61}; // struct Blender
62
Romain Guy026c5e162010-06-28 17:12:22 -070063// In this array, the index of each Blender equals the value of the first
64// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
65static const Blender gBlends[] = {
Romain Guyb146b122010-12-15 17:06:45 -080066 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
67 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
68 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
69 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
70 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
71 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
72 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
73 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
74 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
75 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
76 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
77 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
Romain Guy026c5e162010-06-28 17:12:22 -070078};
Romain Guye4d01122010-06-16 18:44:05 -070079
Romain Guy87a76572010-09-13 18:11:21 -070080// This array contains the swapped version of each SkXfermode. For instance
81// this array's SrcOver blending mode is actually DstOver. You can refer to
82// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070083static const Blender gBlendsSwap[] = {
Romain Guyb146b122010-12-15 17:06:45 -080084 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
85 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
86 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
87 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
88 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
89 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
90 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
91 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
92 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
93 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
94 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
95 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
Romain Guyf607bdc2010-09-10 19:20:06 -070096};
97
Romain Guy889f8d12010-07-29 14:37:42 -070098static const GLenum gTextureUnits[] = {
Romain Guyb146b122010-12-15 17:06:45 -080099 GL_TEXTURE0,
100 GL_TEXTURE1,
101 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -0700102};
103
Romain Guyf6a11b82010-06-23 17:47:49 -0700104///////////////////////////////////////////////////////////////////////////////
105// Constructors/destructor
106///////////////////////////////////////////////////////////////////////////////
107
Romain Guyfb8b7632010-08-23 21:05:08 -0700108OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700109 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700110 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700111 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700112
Romain Guyac670c02010-07-27 17:39:27 -0700113 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
114
Romain Guyae5575b2010-07-29 18:48:04 -0700115 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700116}
117
Romain Guy85bf02f2010-06-22 13:11:24 -0700118OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700119 // The context has already been destroyed at this point, do not call
120 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700121}
122
Romain Guyf6a11b82010-06-23 17:47:49 -0700123///////////////////////////////////////////////////////////////////////////////
124// Setup
125///////////////////////////////////////////////////////////////////////////////
126
Romain Guy85bf02f2010-06-22 13:11:24 -0700127void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700128 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700129 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700130
131 mWidth = width;
132 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700133
134 mFirstSnapshot->height = height;
135 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700136
137 mDirtyClip = false;
Romain Guye4d01122010-06-16 18:44:05 -0700138}
139
Romain Guy6b7bd242010-10-06 19:49:23 -0700140void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800141 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
142}
143
144void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800145 mCaches.clearGarbage();
146
Romain Guy8aef54f2010-09-01 15:13:49 -0700147 mSnapshot = new Snapshot(mFirstSnapshot,
148 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800149 mSnapshot->fbo = getTargetFbo();
150
Romain Guy8fb95422010-08-17 18:38:51 -0700151 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700152
Romain Guyfb8b7632010-08-23 21:05:08 -0700153 glViewport(0, 0, mWidth, mHeight);
154
Romain Guyd90f23e2010-09-09 11:47:54 -0700155 glDisable(GL_DITHER);
Romain Guybb9524b2010-06-22 18:56:38 -0700156
Romain Guy7d7b5492011-01-24 16:33:45 -0800157 glEnable(GL_SCISSOR_TEST);
158 glScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
159 mSnapshot->setClip(left, top, right, bottom);
160
Romain Guy6b7bd242010-10-06 19:49:23 -0700161 if (!opaque) {
162 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
163 glClear(GL_COLOR_BUFFER_BIT);
164 }
Romain Guybb9524b2010-06-22 18:56:38 -0700165}
166
Romain Guyb025b9c2010-09-16 14:16:48 -0700167void OpenGLRenderer::finish() {
168#if DEBUG_OPENGL
169 GLenum status = GL_NO_ERROR;
170 while ((status = glGetError()) != GL_NO_ERROR) {
171 LOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800172 switch (status) {
173 case GL_OUT_OF_MEMORY:
174 LOGE(" OpenGLRenderer is out of memory!");
175 break;
176 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700177 }
178#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800179#if DEBUG_MEMORY_USAGE
180 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800181#else
182 if (mCaches.getDebugLevel() & kDebugMemory) {
183 mCaches.dumpMemoryUsage();
184 }
Romain Guyc15008e2010-11-10 11:59:15 -0800185#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700186}
187
Romain Guy6c319ca2011-01-11 14:29:25 -0800188void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700189 if (mCaches.currentProgram) {
190 if (mCaches.currentProgram->isInUse()) {
191 mCaches.currentProgram->remove();
192 mCaches.currentProgram = NULL;
193 }
194 }
Romain Guy50c0f092010-10-19 11:42:22 -0700195 mCaches.unbindMeshBuffer();
Romain Guyda8532c2010-08-31 11:50:35 -0700196}
197
Romain Guy6c319ca2011-01-11 14:29:25 -0800198void OpenGLRenderer::resume() {
Romain Guyeb993562010-10-05 18:14:38 -0700199 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700200
201 glEnable(GL_SCISSOR_TEST);
Romain Guy746b7402010-10-26 16:27:31 -0700202 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700203
Romain Guyf607bdc2010-09-10 19:20:06 -0700204 glDisable(GL_DITHER);
205
Romain Guy42f3a4b2011-01-19 13:42:26 -0800206 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy03750a02010-10-18 14:06:08 -0700207 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700208
Romain Guy50c0f092010-10-19 11:42:22 -0700209 mCaches.blend = true;
210 glEnable(GL_BLEND);
211 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
212 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700213}
214
Romain Guycabfcc12011-03-07 18:06:46 -0800215bool OpenGLRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) {
Chet Haasedaf98e92011-01-10 14:10:36 -0800216 interrupt();
Romain Guyf90f8172011-01-25 22:53:24 -0800217 if (mDirtyClip) {
218 setScissorFromClip();
219 }
Romain Guyd643bb52011-03-01 14:55:21 -0800220
Romain Guy80911b82011-03-16 15:30:12 -0700221 Rect clip(*mSnapshot->clipRect);
222 clip.snapToPixelBoundaries();
223
Romain Guyd643bb52011-03-01 14:55:21 -0800224#if RENDER_LAYERS_AS_REGIONS
225 // Since we don't know what the functor will draw, let's dirty
226 // tne entire clip region
227 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800228 dirtyLayerUnchecked(clip, getRegion());
229 }
230#endif
231
Romain Guy08aa2cb2011-03-17 11:06:57 -0700232 DrawGlInfo info;
233 info.clipLeft = clip.left;
234 info.clipTop = clip.top;
235 info.clipRight = clip.right;
236 info.clipBottom = clip.bottom;
237 info.isLayer = hasLayer();
238 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700239
Romain Guy08aa2cb2011-03-17 11:06:57 -0700240 status_t result = (*functor)(0, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800241
242 if (result != 0) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700243 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800244 dirty.unionWith(localDirty);
245 }
246
Chet Haasedaf98e92011-01-10 14:10:36 -0800247 resume();
Romain Guycabfcc12011-03-07 18:06:46 -0800248 return result != 0;
Chet Haasedaf98e92011-01-10 14:10:36 -0800249}
250
Romain Guyf6a11b82010-06-23 17:47:49 -0700251///////////////////////////////////////////////////////////////////////////////
252// State management
253///////////////////////////////////////////////////////////////////////////////
254
Romain Guybb9524b2010-06-22 18:56:38 -0700255int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700256 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700257}
258
259int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700260 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700261}
262
263void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700264 if (mSaveCount > 1) {
265 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700266 }
Romain Guybb9524b2010-06-22 18:56:38 -0700267}
268
269void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700270 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700271
Romain Guy8fb95422010-08-17 18:38:51 -0700272 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700273 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700274 }
Romain Guybb9524b2010-06-22 18:56:38 -0700275}
276
Romain Guy8aef54f2010-09-01 15:13:49 -0700277int OpenGLRenderer::saveSnapshot(int flags) {
278 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700279 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700280}
281
282bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700283 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700284 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700285 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700286
Romain Guybd6b79b2010-06-26 00:13:53 -0700287 sp<Snapshot> current = mSnapshot;
288 sp<Snapshot> previous = mSnapshot->previous;
289
Romain Guyeb993562010-10-05 18:14:38 -0700290 if (restoreOrtho) {
291 Rect& r = previous->viewport;
292 glViewport(r.left, r.top, r.right, r.bottom);
293 mOrthoMatrix.load(current->orthoMatrix);
294 }
295
Romain Guy8b55f372010-08-18 17:10:07 -0700296 mSaveCount--;
297 mSnapshot = previous;
298
Romain Guy2542d192010-08-18 11:47:12 -0700299 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700300 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700301 }
Romain Guy2542d192010-08-18 11:47:12 -0700302
Romain Guy5ec99242010-11-03 16:19:08 -0700303 if (restoreLayer) {
304 composeLayer(current, previous);
305 }
306
Romain Guy2542d192010-08-18 11:47:12 -0700307 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700308}
309
Romain Guyf6a11b82010-06-23 17:47:49 -0700310///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700311// Layers
312///////////////////////////////////////////////////////////////////////////////
313
314int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700315 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700316 const GLuint previousFbo = mSnapshot->fbo;
317 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700318
Romain Guyaf636eb2010-12-09 17:47:21 -0800319 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700320 int alpha = 255;
321 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700322
Romain Guye45362c2010-11-03 19:58:32 -0700323 if (p) {
324 alpha = p->getAlpha();
325 if (!mCaches.extensions.hasFramebufferFetch()) {
326 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
327 if (!isMode) {
328 // Assume SRC_OVER
329 mode = SkXfermode::kSrcOver_Mode;
330 }
331 } else {
332 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700333 }
334 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700335 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700336 }
Romain Guyd55a8612010-06-28 17:42:46 -0700337
Romain Guydbc26d22010-10-11 17:58:29 -0700338 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
339 }
Romain Guyd55a8612010-06-28 17:42:46 -0700340
341 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700342}
343
344int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
345 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700346 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700347 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700348 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700349 SkPaint paint;
350 paint.setAlpha(alpha);
351 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700352 }
Romain Guyd55a8612010-06-28 17:42:46 -0700353}
Romain Guybd6b79b2010-06-26 00:13:53 -0700354
Romain Guy1c740bc2010-09-13 18:00:09 -0700355/**
356 * Layers are viewed by Skia are slightly different than layers in image editing
357 * programs (for instance.) When a layer is created, previously created layers
358 * and the frame buffer still receive every drawing command. For instance, if a
359 * layer is created and a shape intersecting the bounds of the layers and the
360 * framebuffer is draw, the shape will be drawn on both (unless the layer was
361 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
362 *
363 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
364 * texture. Unfortunately, this is inefficient as it requires every primitive to
365 * be drawn n + 1 times, where n is the number of active layers. In practice this
366 * means, for every primitive:
367 * - Switch active frame buffer
368 * - Change viewport, clip and projection matrix
369 * - Issue the drawing
370 *
371 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700372 * To avoid this, layers are implemented in a different way here, at least in the
373 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
374 * is set. When this flag is set we can redirect all drawing operations into a
375 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700376 *
377 * This implementation relies on the frame buffer being at least RGBA 8888. When
378 * a layer is created, only a texture is created, not an FBO. The content of the
379 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700380 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700381 * buffer and drawing continues as normal. This technique therefore treats the
382 * frame buffer as a scratch buffer for the layers.
383 *
384 * To compose the layers back onto the frame buffer, each layer texture
385 * (containing the original frame buffer data) is drawn as a simple quad over
386 * the frame buffer. The trick is that the quad is set as the composition
387 * destination in the blending equation, and the frame buffer becomes the source
388 * of the composition.
389 *
390 * Drawing layers with an alpha value requires an extra step before composition.
391 * An empty quad is drawn over the layer's region in the frame buffer. This quad
392 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
393 * quad is used to multiply the colors in the frame buffer. This is achieved by
394 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
395 * GL_ZERO, GL_SRC_ALPHA.
396 *
397 * Because glCopyTexImage2D() can be slow, an alternative implementation might
398 * be use to draw a single clipped layer. The implementation described above
399 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700400 *
401 * (1) The frame buffer is actually not cleared right away. To allow the GPU
402 * to potentially optimize series of calls to glCopyTexImage2D, the frame
403 * buffer is left untouched until the first drawing operation. Only when
404 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700405 */
Romain Guyd55a8612010-06-28 17:42:46 -0700406bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700407 float right, float bottom, int alpha, SkXfermode::Mode mode,
408 int flags, GLuint previousFbo) {
409 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700410 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700411
Romain Guyeb993562010-10-05 18:14:38 -0700412 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
413
Romain Guyf607bdc2010-09-10 19:20:06 -0700414 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700415 Rect bounds(left, top, right, bottom);
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700416 if (!fboLayer) {
Romain Guyeb993562010-10-05 18:14:38 -0700417 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700418
Romain Guyeb993562010-10-05 18:14:38 -0700419 // Layers only make sense if they are in the framebuffer's bounds
Romain Guyad37cd32011-03-15 11:12:25 -0700420 if (bounds.intersect(*snapshot->clipRect)) {
421 // We cannot work with sub-pixels in this case
422 bounds.snapToPixelBoundaries();
Romain Guyae517592010-10-22 10:40:27 -0700423
Romain Guyad37cd32011-03-15 11:12:25 -0700424 // When the layer is not an FBO, we may use glCopyTexImage so we
425 // need to make sure the layer does not extend outside the bounds
426 // of the framebuffer
427 if (!bounds.intersect(snapshot->previous->viewport)) {
428 bounds.setEmpty();
429 }
430 } else {
431 bounds.setEmpty();
432 }
Romain Guyeb993562010-10-05 18:14:38 -0700433 }
Romain Guybf434112010-09-16 14:40:17 -0700434
Romain Guy746b7402010-10-26 16:27:31 -0700435 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
436 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800437 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700438 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700439 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700440 }
441
442 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800443 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700444 return false;
445 }
Romain Guyf18fd992010-07-08 11:45:51 -0700446
Romain Guy5b3b3522010-10-27 18:57:51 -0700447 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700448 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700449 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700450 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700451 }
452
Romain Guydda570202010-07-06 11:39:32 -0700453 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700454 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700455 layer->layer.set(bounds);
Romain Guy8550c4c2010-10-08 15:49:53 -0700456 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
457 bounds.getWidth() / float(layer->width), 0.0f);
Romain Guy171c5922011-01-06 10:04:23 -0800458 layer->colorFilter = mColorFilter;
Romain Guydda570202010-07-06 11:39:32 -0700459
Romain Guy8fb95422010-08-17 18:38:51 -0700460 // Save the layer in the snapshot
461 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700462 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700463
Romain Guyeb993562010-10-05 18:14:38 -0700464 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700465 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700466 } else {
467 // Copy the framebuffer into the layer
468 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy514fb182011-01-19 14:38:29 -0800469 if (!bounds.isEmpty()) {
470 if (layer->empty) {
471 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left,
472 snapshot->height - bounds.bottom, layer->width, layer->height, 0);
473 layer->empty = false;
474 } else {
475 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
476 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
477 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700478
479 // Clear the framebuffer where the layer will draw
480 glScissor(bounds.left, mSnapshot->height - bounds.bottom,
481 bounds.getWidth(), bounds.getHeight());
482 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
483 glClear(GL_COLOR_BUFFER_BIT);
484
485 dirtyClip();
Romain Guyae88e5e2010-10-22 17:49:18 -0700486 }
Romain Guyeb993562010-10-05 18:14:38 -0700487 }
Romain Guyf86ef572010-07-01 11:05:42 -0700488
Romain Guyd55a8612010-06-28 17:42:46 -0700489 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700490}
491
Romain Guy5b3b3522010-10-27 18:57:51 -0700492bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
493 GLuint previousFbo) {
494 layer->fbo = mCaches.fboCache.get();
495
496#if RENDER_LAYERS_AS_REGIONS
497 snapshot->region = &snapshot->layer->region;
498 snapshot->flags |= Snapshot::kFlagFboTarget;
499#endif
500
501 Rect clip(bounds);
502 snapshot->transform->mapRect(clip);
503 clip.intersect(*snapshot->clipRect);
504 clip.snapToPixelBoundaries();
505 clip.intersect(snapshot->previous->viewport);
506
507 mat4 inverse;
508 inverse.loadInverse(*mSnapshot->transform);
509
510 inverse.mapRect(clip);
511 clip.snapToPixelBoundaries();
512 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700513 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700514
515 snapshot->flags |= Snapshot::kFlagIsFboLayer;
516 snapshot->fbo = layer->fbo;
517 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700518 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
519 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
520 snapshot->height = bounds.getHeight();
521 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
522 snapshot->orthoMatrix.load(mOrthoMatrix);
523
524 // Bind texture to FBO
525 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
526 glBindTexture(GL_TEXTURE_2D, layer->texture);
527
528 // Initialize the texture if needed
529 if (layer->empty) {
530 layer->empty = false;
531 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
532 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
533 }
534
535 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
536 layer->texture, 0);
537
538#if DEBUG_LAYERS_AS_REGIONS
539 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
540 if (status != GL_FRAMEBUFFER_COMPLETE) {
541 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
542
543 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
544 glDeleteTextures(1, &layer->texture);
545 mCaches.fboCache.put(layer->fbo);
546
547 delete layer;
548
549 return false;
550 }
551#endif
552
553 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
554 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
555 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
556 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
557 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) {
573 LOGE("Attempting to compose a layer that does not exist");
574 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 Guyeb993562010-10-05 18:14:38 -0700587 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700588 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700589 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700590 // Required below, composeLayerRect() will divide by 255
591 layer->alpha = 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 Guy746b7402010-10-26 16:27:31 -0700596 glActiveTexture(gTextureUnits[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 Guy171c5922011-01-06 10:04:23 -0800602 if (layer->colorFilter) {
603 setupColorFilter(layer->colorFilter);
604 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700605 composeLayerRegion(layer, rect);
Romain Guy171c5922011-01-06 10:04:23 -0800606 if (layer->colorFilter) {
607 resetColorFilter();
608 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700609 } else {
Romain Guy514fb182011-01-19 14:38:29 -0800610 if (!rect.isEmpty()) {
611 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
612 composeLayerRect(layer, rect, true);
613 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700614 }
Romain Guy8b55f372010-08-18 17:10:07 -0700615
Romain Guyeb993562010-10-05 18:14:38 -0700616 if (fboLayer) {
617 // Detach the texture from the FBO
618 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
619 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
620 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
621
622 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
623 mCaches.fboCache.put(current->fbo);
624 }
625
Romain Guy746b7402010-10-26 16:27:31 -0700626 dirtyClip();
627
Romain Guyeb993562010-10-05 18:14:38 -0700628 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700629 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700630 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700631 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700632 delete layer;
633 }
634}
635
Romain Guy5b3b3522010-10-27 18:57:51 -0700636void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
637 const Rect& texCoords = layer->texCoords;
638 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
639
640 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
641 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
642 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap);
643
644 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
645}
646
647void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
648#if RENDER_LAYERS_AS_REGIONS
649 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -0700650 composeLayerRect(layer, layer->regionRect);
Romain Guy5b3b3522010-10-27 18:57:51 -0700651 layer->region.clear();
652 return;
653 }
654
655 if (!layer->region.isEmpty()) {
656 size_t count;
657 const android::Rect* rects = layer->region.getArray(&count);
658
Romain Guy5b3b3522010-10-27 18:57:51 -0700659 const float alpha = layer->alpha / 255.0f;
Romain Guy5b3b3522010-10-27 18:57:51 -0700660 const float texX = 1.0f / float(layer->width);
661 const float texY = 1.0f / float(layer->height);
Romain Guyf219da52011-01-16 12:54:25 -0800662 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700663
664 TextureVertex* mesh = mCaches.getRegionMesh();
665 GLsizei numQuads = 0;
666
Romain Guy7230a742011-01-10 22:26:16 -0800667 setupDraw();
668 setupDrawWithTexture();
669 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800670 setupDrawColorFilter();
Romain Guy7230a742011-01-10 22:26:16 -0800671 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
672 setupDrawProgram();
673 setupDrawDirtyRegionsDisabled();
674 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800675 setupDrawColorFilterUniforms();
Romain Guy7230a742011-01-10 22:26:16 -0800676 setupDrawTexture(layer->texture);
Romain Guyc038ea32011-01-12 15:08:47 -0800677 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
Romain Guy7230a742011-01-10 22:26:16 -0800678 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700679
680 for (size_t i = 0; i < count; i++) {
681 const android::Rect* r = &rects[i];
682
683 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800684 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700685 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800686 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700687
688 // TODO: Reject quads outside of the clip
689 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
690 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
691 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
692 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
693
694 numQuads++;
695
696 if (numQuads >= REGION_MESH_QUAD_COUNT) {
697 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
698 numQuads = 0;
699 mesh = mCaches.getRegionMesh();
700 }
701 }
702
703 if (numQuads > 0) {
704 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
705 }
706
707 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800708 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700709
710#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800711 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700712#endif
713
714 layer->region.clear();
715 }
716#else
717 composeLayerRect(layer, rect);
718#endif
719}
720
Romain Guy3a3133d2011-02-01 22:59:58 -0800721void OpenGLRenderer::drawRegionRects(const Region& region) {
722#if DEBUG_LAYERS_AS_REGIONS
723 size_t count;
724 const android::Rect* rects = region.getArray(&count);
725
726 uint32_t colors[] = {
727 0x7fff0000, 0x7f00ff00,
728 0x7f0000ff, 0x7fff00ff,
729 };
730
731 int offset = 0;
732 int32_t top = rects[0].top;
733
734 for (size_t i = 0; i < count; i++) {
735 if (top != rects[i].top) {
736 offset ^= 0x2;
737 top = rects[i].top;
738 }
739
740 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
741 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
742 SkXfermode::kSrcOver_Mode);
743 }
744#endif
745}
746
Romain Guy5b3b3522010-10-27 18:57:51 -0700747void OpenGLRenderer::dirtyLayer(const float left, const float top,
748 const float right, const float bottom, const mat4 transform) {
749#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800750 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700751 Rect bounds(left, top, right, bottom);
752 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800753 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700754 }
755#endif
756}
757
758void OpenGLRenderer::dirtyLayer(const float left, const float top,
759 const float right, const float bottom) {
760#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800761 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700762 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800763 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800764 }
765#endif
766}
767
768void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
769#if RENDER_LAYERS_AS_REGIONS
770 if (bounds.intersect(*mSnapshot->clipRect)) {
771 bounds.snapToPixelBoundaries();
772 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
773 if (!dirty.isEmpty()) {
774 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700775 }
776 }
777#endif
778}
779
Romain Guybd6b79b2010-06-26 00:13:53 -0700780///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700781// Transforms
782///////////////////////////////////////////////////////////////////////////////
783
784void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700785 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700786}
787
788void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700789 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700790}
791
792void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700793 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700794}
795
Romain Guy807daf72011-01-18 11:19:19 -0800796void OpenGLRenderer::skew(float sx, float sy) {
797 mSnapshot->transform->skew(sx, sy);
798}
799
Romain Guyf6a11b82010-06-23 17:47:49 -0700800void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700801 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700802}
803
Romain Guy41030da2010-10-13 13:40:37 -0700804const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700805 if (mSnapshot->fbo != 0) {
806 return &mSnapshot->transform->data[0];
807 }
808 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700809}
810
Romain Guyf6a11b82010-06-23 17:47:49 -0700811void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700812 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700813}
814
815void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700816 SkMatrix transform;
817 mSnapshot->transform->copyTo(transform);
818 transform.preConcat(*matrix);
819 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700820}
821
822///////////////////////////////////////////////////////////////////////////////
823// Clipping
824///////////////////////////////////////////////////////////////////////////////
825
Romain Guybb9524b2010-06-22 18:56:38 -0700826void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700827 Rect clip(*mSnapshot->clipRect);
828 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700829 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700830 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700831}
832
833const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700834 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700835}
836
Romain Guyc7d53492010-06-25 13:41:57 -0700837bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800838 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700839 return true;
840 }
841
Romain Guy1d83e192010-08-17 11:37:00 -0700842 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700843 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700844 r.snapToPixelBoundaries();
845
846 Rect clipRect(*mSnapshot->clipRect);
847 clipRect.snapToPixelBoundaries();
848
849 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700850}
851
Romain Guy079ba2c2010-07-16 14:12:24 -0700852bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
853 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700854 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700855 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700856 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700857 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700858}
859
Romain Guyf6a11b82010-06-23 17:47:49 -0700860///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -0800861// Drawing commands
862///////////////////////////////////////////////////////////////////////////////
863
864void OpenGLRenderer::setupDraw() {
Romain Guy70ca14e2010-12-13 18:24:33 -0800865 if (mDirtyClip) {
866 setScissorFromClip();
867 }
868 mDescription.reset();
869 mSetShaderColor = false;
870 mColorSet = false;
871 mColorA = mColorR = mColorG = mColorB = 0.0f;
872 mTextureUnit = 0;
873 mTrackDirtyRegions = true;
Romain Guy8d0d4782010-12-14 20:13:35 -0800874 mTexCoordsSlot = -1;
Romain Guy70ca14e2010-12-13 18:24:33 -0800875}
876
877void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
878 mDescription.hasTexture = true;
879 mDescription.hasAlpha8Texture = isAlpha8;
880}
881
882void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -0800883 setupDrawColor(color, (color >> 24) & 0xFF);
884}
885
886void OpenGLRenderer::setupDrawColor(int color, int alpha) {
887 mColorA = alpha / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -0800888 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -0800889 mColorR = a * ((color >> 16) & 0xFF);
890 mColorG = a * ((color >> 8) & 0xFF);
891 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -0800892 mColorSet = true;
893 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
894}
895
Romain Guy86568192010-12-14 15:55:39 -0800896void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
897 mColorA = alpha / 255.0f;
898 const float a = mColorA / 255.0f;
899 mColorR = a * ((color >> 16) & 0xFF);
900 mColorG = a * ((color >> 8) & 0xFF);
901 mColorB = a * ((color ) & 0xFF);
902 mColorSet = true;
903 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
904}
905
Romain Guy70ca14e2010-12-13 18:24:33 -0800906void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
907 mColorA = a;
908 mColorR = r;
909 mColorG = g;
910 mColorB = b;
911 mColorSet = true;
912 mSetShaderColor = mDescription.setColor(r, g, b, a);
913}
914
Romain Guy86568192010-12-14 15:55:39 -0800915void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
916 mColorA = a;
917 mColorR = r;
918 mColorG = g;
919 mColorB = b;
920 mColorSet = true;
921 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
922}
923
Romain Guy70ca14e2010-12-13 18:24:33 -0800924void OpenGLRenderer::setupDrawShader() {
925 if (mShader) {
926 mShader->describe(mDescription, mCaches.extensions);
927 }
928}
929
930void OpenGLRenderer::setupDrawColorFilter() {
931 if (mColorFilter) {
932 mColorFilter->describe(mDescription, mCaches.extensions);
933 }
934}
935
936void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
937 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
938 mDescription, swapSrcDst);
939}
940
941void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
942 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
943 mDescription, swapSrcDst);
944}
945
946void OpenGLRenderer::setupDrawProgram() {
947 useProgram(mCaches.programCache.get(mDescription));
948}
949
950void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
951 mTrackDirtyRegions = false;
952}
953
954void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
955 bool ignoreTransform) {
956 mModelView.loadTranslate(left, top, 0.0f);
957 if (!ignoreTransform) {
958 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
959 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
960 } else {
961 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
962 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
963 }
964}
965
Romain Guy8d0d4782010-12-14 20:13:35 -0800966void OpenGLRenderer::setupDrawModelViewIdentity() {
967 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform);
968}
969
Romain Guy70ca14e2010-12-13 18:24:33 -0800970void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
971 bool ignoreTransform, bool ignoreModelView) {
972 if (!ignoreModelView) {
973 mModelView.loadTranslate(left, top, 0.0f);
974 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -0800975 } else {
976 mModelView.loadIdentity();
977 }
Romain Guy86568192010-12-14 15:55:39 -0800978 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
979 if (!ignoreTransform) {
980 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
981 if (mTrackDirtyRegions && dirty) {
982 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
983 }
984 } else {
985 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
986 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
987 }
Romain Guy70ca14e2010-12-13 18:24:33 -0800988}
989
990void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -0800991 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -0800992 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
993 }
994}
995
Romain Guy86568192010-12-14 15:55:39 -0800996void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -0800997 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -0800998 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -0800999 }
1000}
1001
Romain Guy70ca14e2010-12-13 18:24:33 -08001002void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
1003 if (mShader) {
1004 if (ignoreTransform) {
1005 mModelView.loadInverse(*mSnapshot->transform);
1006 }
1007 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
1008 }
1009}
1010
Romain Guy8d0d4782010-12-14 20:13:35 -08001011void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
1012 if (mShader) {
1013 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1014 }
1015}
1016
Romain Guy70ca14e2010-12-13 18:24:33 -08001017void OpenGLRenderer::setupDrawColorFilterUniforms() {
1018 if (mColorFilter) {
1019 mColorFilter->setupProgram(mCaches.currentProgram);
1020 }
1021}
1022
1023void OpenGLRenderer::setupDrawSimpleMesh() {
1024 mCaches.bindMeshBuffer();
1025 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1026 gMeshStride, 0);
1027}
1028
1029void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1030 bindTexture(texture);
1031 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1032
1033 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1034 glEnableVertexAttribArray(mTexCoordsSlot);
1035}
1036
1037void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1038 if (!vertices) {
1039 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1040 } else {
1041 mCaches.unbindMeshBuffer();
1042 }
1043 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1044 gMeshStride, vertices);
David Licf289572011-02-25 12:05:44 -08001045 if (mTexCoordsSlot >= 0) {
Romain Guy8d0d4782010-12-14 20:13:35 -08001046 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1047 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001048}
1049
1050void OpenGLRenderer::finishDrawTexture() {
1051 glDisableVertexAttribArray(mTexCoordsSlot);
1052}
1053
1054///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001055// Drawing
1056///////////////////////////////////////////////////////////////////////////////
1057
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001058bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
1059 Rect& dirty, uint32_t level) {
1060 if (quickReject(0.0f, 0.0f, width, height)) {
1061 return false;
1062 }
1063
Romain Guy0fe478e2010-11-08 12:08:41 -08001064 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1065 // will be performed by the display list itself
1066 if (displayList) {
Romain Guycabfcc12011-03-07 18:06:46 -08001067 return displayList->replay(*this, dirty, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001068 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001069
Chet Haasedaf98e92011-01-10 14:10:36 -08001070 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001071}
1072
Romain Guya168d732011-03-18 16:50:13 -07001073void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1074 int alpha;
1075 SkXfermode::Mode mode;
1076 getAlphaAndMode(paint, &alpha, &mode);
1077
1078 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1079
1080 float x = left;
1081 float y = top;
1082
1083 bool ignoreTransform = false;
1084 if (mSnapshot->transform->isPureTranslate()) {
1085 x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1086 y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1087 ignoreTransform = true;
1088 }
1089
1090 setupDraw();
1091 setupDrawWithTexture(true);
1092 setupDrawAlpha8Color(paint->getColor(), alpha);
1093 setupDrawColorFilter();
1094 setupDrawShader();
1095 setupDrawBlending(true, mode);
1096 setupDrawProgram();
1097 setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform);
1098 setupDrawTexture(texture->id);
1099 setupDrawPureColorUniforms();
1100 setupDrawColorFilterUniforms();
1101 setupDrawShaderUniforms();
1102 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1103
1104 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1105
1106 finishDrawTexture();
1107}
1108
Chet Haase5c13d892010-10-08 08:37:55 -07001109void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001110 const float right = left + bitmap->width();
1111 const float bottom = top + bitmap->height();
1112
1113 if (quickReject(left, top, right, bottom)) {
1114 return;
1115 }
1116
Romain Guy86568192010-12-14 15:55:39 -08001117 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001118 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001119 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001120 const AutoTexture autoCleanup(texture);
1121
Romain Guya168d732011-03-18 16:50:13 -07001122 if (bitmap->getConfig() == SkBitmap::kA8_Config) {
1123 drawAlphaBitmap(texture, left, top, paint);
1124 } else {
1125 drawTextureRect(left, top, right, bottom, texture, paint);
1126 }
Romain Guyce0537b2010-06-29 21:05:21 -07001127}
1128
Chet Haase5c13d892010-10-08 08:37:55 -07001129void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001130 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1131 const mat4 transform(*matrix);
1132 transform.mapRect(r);
1133
Romain Guy6926c722010-07-12 20:20:03 -07001134 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1135 return;
1136 }
1137
Romain Guy86568192010-12-14 15:55:39 -08001138 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001139 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001140 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001141 const AutoTexture autoCleanup(texture);
1142
Romain Guy5b3b3522010-10-27 18:57:51 -07001143 // This could be done in a cheaper way, all we need is pass the matrix
1144 // to the vertex shader. The save/restore is a bit overkill.
1145 save(SkCanvas::kMatrix_SaveFlag);
1146 concatMatrix(matrix);
1147 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1148 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001149}
1150
Romain Guy5a7b4662011-01-20 19:09:30 -08001151void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1152 float* vertices, int* colors, SkPaint* paint) {
1153 // TODO: Do a quickReject
1154 if (!vertices || mSnapshot->isIgnored()) {
1155 return;
1156 }
1157
1158 glActiveTexture(gTextureUnits[0]);
1159 Texture* texture = mCaches.textureCache.get(bitmap);
1160 if (!texture) return;
1161 const AutoTexture autoCleanup(texture);
1162 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1163
1164 int alpha;
1165 SkXfermode::Mode mode;
1166 getAlphaAndMode(paint, &alpha, &mode);
1167
Romain Guy5a7b4662011-01-20 19:09:30 -08001168 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001169
Romain Guyb18d2d02011-02-10 15:52:54 -08001170 float left = FLT_MAX;
1171 float top = FLT_MAX;
1172 float right = FLT_MIN;
1173 float bottom = FLT_MIN;
1174
1175#if RENDER_LAYERS_AS_REGIONS
1176 bool hasActiveLayer = hasLayer();
1177#else
1178 bool hasActiveLayer = false;
1179#endif
1180
Romain Guya566b7c2011-01-23 16:36:11 -08001181 // TODO: Support the colors array
1182 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001183 TextureVertex* vertex = mesh;
1184 for (int32_t y = 0; y < meshHeight; y++) {
1185 for (int32_t x = 0; x < meshWidth; x++) {
1186 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1187
1188 float u1 = float(x) / meshWidth;
1189 float u2 = float(x + 1) / meshWidth;
1190 float v1 = float(y) / meshHeight;
1191 float v2 = float(y + 1) / meshHeight;
1192
1193 int ax = i + (meshWidth + 1) * 2;
1194 int ay = ax + 1;
1195 int bx = i;
1196 int by = bx + 1;
1197 int cx = i + 2;
1198 int cy = cx + 1;
1199 int dx = i + (meshWidth + 1) * 2 + 2;
1200 int dy = dx + 1;
1201
1202 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1203 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1204 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1205
1206 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1207 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1208 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001209
1210#if RENDER_LAYERS_AS_REGIONS
1211 if (hasActiveLayer) {
1212 // TODO: This could be optimized to avoid unnecessary ops
1213 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1214 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1215 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1216 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1217 }
1218#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001219 }
1220 }
1221
Romain Guyb18d2d02011-02-10 15:52:54 -08001222#if RENDER_LAYERS_AS_REGIONS
1223 if (hasActiveLayer) {
1224 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1225 }
1226#endif
1227
Romain Guy5a7b4662011-01-20 19:09:30 -08001228 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1229 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001230 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001231}
1232
Romain Guy8ba548f2010-06-30 19:21:21 -07001233void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1234 float srcLeft, float srcTop, float srcRight, float srcBottom,
1235 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001236 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001237 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1238 return;
1239 }
1240
Romain Guy746b7402010-10-26 16:27:31 -07001241 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001242 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001243 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001244 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001245 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guy8ba548f2010-06-30 19:21:21 -07001246
Romain Guy8ba548f2010-06-30 19:21:21 -07001247 const float width = texture->width;
1248 const float height = texture->height;
1249
1250 const float u1 = srcLeft / width;
1251 const float v1 = srcTop / height;
1252 const float u2 = srcRight / width;
1253 const float v2 = srcBottom / height;
1254
Romain Guy03750a02010-10-18 14:06:08 -07001255 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001256 resetDrawTextureTexCoords(u1, v1, u2, v2);
1257
Romain Guy03750a02010-10-18 14:06:08 -07001258 int alpha;
1259 SkXfermode::Mode mode;
1260 getAlphaAndMode(paint, &alpha, &mode);
1261
Romain Guy6620c6d2010-12-06 18:07:02 -08001262 if (mSnapshot->transform->isPureTranslate()) {
1263 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1264 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1265
1266 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1267 texture->id, alpha / 255.0f, mode, texture->blend,
1268 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1269 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1270 } else {
1271 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1272 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1273 GL_TRIANGLE_STRIP, gMeshCount);
1274 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001275
1276 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1277}
1278
Romain Guy4aa90572010-09-26 18:40:37 -07001279void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001280 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001281 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001282 if (quickReject(left, top, right, bottom)) {
1283 return;
1284 }
1285
Romain Guy746b7402010-10-26 16:27:31 -07001286 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001287 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001288 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001289 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001290 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guyf7f93552010-07-08 19:17:03 -07001291
1292 int alpha;
1293 SkXfermode::Mode mode;
1294 getAlphaAndMode(paint, &alpha, &mode);
1295
Romain Guy2728f962010-10-08 18:36:15 -07001296 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001297 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001298
Romain Guya5ef39a2010-12-03 16:48:20 -08001299 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001300 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001301#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001302 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001303 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001304 const float offsetX = left + mSnapshot->transform->getTranslateX();
1305 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001306 const size_t count = mesh->quads.size();
1307 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001308 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001309 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001310 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1311 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1312 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001313 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001314 dirtyLayer(left + bounds.left, top + bounds.top,
1315 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001316 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001317 }
1318 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001319#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001320
Romain Guy6620c6d2010-12-06 18:07:02 -08001321 if (pureTranslate) {
1322 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1323 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1324
1325 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1326 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1327 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1328 true, !mesh->hasEmptyQuads);
1329 } else {
1330 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1331 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1332 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1333 true, !mesh->hasEmptyQuads);
1334 }
Romain Guy054dc182010-10-15 17:55:25 -07001335 }
Romain Guyf7f93552010-07-08 19:17:03 -07001336}
1337
Chet Haase5c13d892010-10-08 08:37:55 -07001338void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001339 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001340
Romain Guya957eea2010-12-08 18:34:42 -08001341 const bool isAA = paint->isAntiAlias();
1342 const float strokeWidth = paint->getStrokeWidth() * 0.5f;
1343 // A stroke width of 0 has a special meaningin Skia:
1344 // it draws an unscaled 1px wide line
1345 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
1346
Romain Guy759ea802010-09-16 20:49:46 -07001347 int alpha;
1348 SkXfermode::Mode mode;
1349 getAlphaAndMode(paint, &alpha, &mode);
1350
Romain Guya957eea2010-12-08 18:34:42 -08001351 int verticesCount = count >> 2;
Romain Guy7230a742011-01-10 22:26:16 -08001352 int generatedVerticesCount = 0;
Romain Guya957eea2010-12-08 18:34:42 -08001353 if (!isHairLine) {
1354 // TODO: AA needs more vertices
1355 verticesCount *= 6;
Romain Guyc95c8d62010-09-17 15:31:32 -07001356 } else {
Romain Guya957eea2010-12-08 18:34:42 -08001357 // TODO: AA will be different
1358 verticesCount *= 2;
Romain Guyc95c8d62010-09-17 15:31:32 -07001359 }
1360
Romain Guya957eea2010-12-08 18:34:42 -08001361 TextureVertex lines[verticesCount];
1362 TextureVertex* vertex = &lines[0];
Romain Guy759ea802010-09-16 20:49:46 -07001363
Romain Guy8d0d4782010-12-14 20:13:35 -08001364 setupDraw();
1365 setupDrawColor(paint->getColor(), alpha);
1366 setupDrawColorFilter();
1367 setupDrawShader();
1368 setupDrawBlending(mode);
1369 setupDrawProgram();
1370 setupDrawModelViewIdentity();
1371 setupDrawColorUniforms();
1372 setupDrawColorFilterUniforms();
1373 setupDrawShaderIdentityUniforms();
1374 setupDrawMesh(vertex);
Romain Guya957eea2010-12-08 18:34:42 -08001375
1376 if (!isHairLine) {
1377 // TODO: Handle the AA case
1378 for (int i = 0; i < count; i += 4) {
1379 // a = start point, b = end point
1380 vec2 a(points[i], points[i + 1]);
1381 vec2 b(points[i + 2], points[i + 3]);
1382
1383 // Bias to snap to the same pixels as Skia
1384 a += 0.375;
1385 b += 0.375;
1386
1387 // Find the normal to the line
1388 vec2 n = (b - a).copyNormalized() * strokeWidth;
1389 float x = n.x;
1390 n.x = -n.y;
1391 n.y = x;
1392
1393 // Four corners of the rectangle defining a thick line
1394 vec2 p1 = a - n;
1395 vec2 p2 = a + n;
1396 vec2 p3 = b + n;
1397 vec2 p4 = b - n;
1398
Romain Guy7230a742011-01-10 22:26:16 -08001399 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1400 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1401 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1402 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
Romain Guya957eea2010-12-08 18:34:42 -08001403
Romain Guy7230a742011-01-10 22:26:16 -08001404 if (!quickReject(left, top, right, bottom)) {
1405 // Draw the line as 2 triangles, could be optimized
1406 // by using only 4 vertices and the correct indices
1407 // Also we should probably used non textured vertices
1408 // when line AA is disabled to save on bandwidth
1409 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1410 TextureVertex::set(vertex++, p2.x, p2.y, 0.0f, 0.0f);
1411 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1412 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1413 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1414 TextureVertex::set(vertex++, p4.x, p4.y, 0.0f, 0.0f);
1415
1416 generatedVerticesCount += 6;
1417
1418 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1419 }
Romain Guya957eea2010-12-08 18:34:42 -08001420 }
1421
Romain Guy7230a742011-01-10 22:26:16 -08001422 if (generatedVerticesCount > 0) {
1423 // GL_LINE does not give the result we want to match Skia
1424 glDrawArrays(GL_TRIANGLES, 0, generatedVerticesCount);
1425 }
Romain Guya957eea2010-12-08 18:34:42 -08001426 } else {
1427 // TODO: Handle the AA case
1428 for (int i = 0; i < count; i += 4) {
Romain Guy67ac2172011-03-18 17:14:27 -07001429 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1430 TextureVertex::set(vertex++, points[i + 2], points[i + 3], 0.0f, 0.0f);
Romain Guy7230a742011-01-10 22:26:16 -08001431
Romain Guy67ac2172011-03-18 17:14:27 -07001432 generatedVerticesCount += 2;
Romain Guy7230a742011-01-10 22:26:16 -08001433
Romain Guy67ac2172011-03-18 17:14:27 -07001434 const float left = fmin(points[i], points[i + 2]);
1435 const float right = fmax(points[i], points[i + 2]);
1436 const float top = fmin(points[i + 1], points[i + 3]);
1437 const float bottom = fmax(points[i + 1], points[i + 3]);
Romain Guy7230a742011-01-10 22:26:16 -08001438
Romain Guy67ac2172011-03-18 17:14:27 -07001439 dirtyLayer(left, top,
1440 right == left ? left + 1 : right, bottom == top ? top + 1 : bottom,
1441 *mSnapshot->transform);
Romain Guya957eea2010-12-08 18:34:42 -08001442 }
Romain Guy8d0d4782010-12-14 20:13:35 -08001443
Romain Guy67ac2172011-03-18 17:14:27 -07001444 glLineWidth(1.0f);
1445 glDrawArrays(GL_LINES, 0, generatedVerticesCount);
Romain Guy29d89972010-09-22 16:10:57 -07001446 }
Romain Guy759ea802010-09-16 20:49:46 -07001447}
1448
Romain Guy85bf02f2010-06-22 13:11:24 -07001449void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001450 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001451 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001452
Romain Guyae88e5e2010-10-22 17:49:18 -07001453 Rect& clip(*mSnapshot->clipRect);
1454 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001455
Romain Guy3d58c032010-07-14 16:34:53 -07001456 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001457}
Romain Guy9d5316e2010-06-24 19:30:36 -07001458
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001459void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001460 if (!texture) return;
1461 const AutoTexture autoCleanup(texture);
1462
1463 const float x = left + texture->left - texture->offset;
1464 const float y = top + texture->top - texture->offset;
1465
1466 drawPathTexture(texture, x, y, paint);
1467}
1468
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001469void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1470 float rx, float ry, SkPaint* paint) {
1471 if (mSnapshot->isIgnored()) return;
1472
1473 glActiveTexture(gTextureUnits[0]);
1474 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1475 right - left, bottom - top, rx, ry, paint);
1476 drawShape(left, top, texture, paint);
1477}
1478
Romain Guy01d58e42011-01-19 21:54:02 -08001479void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1480 if (mSnapshot->isIgnored()) return;
1481
1482 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08001483 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001484 drawShape(x - radius, y - radius, texture, paint);
1485}
Romain Guy01d58e42011-01-19 21:54:02 -08001486
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001487void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
1488 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001489
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001490 glActiveTexture(gTextureUnits[0]);
1491 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
1492 drawShape(left, top, texture, paint);
1493}
1494
Romain Guy8b2f5262011-01-23 16:15:02 -08001495void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
1496 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
1497 if (mSnapshot->isIgnored()) return;
1498
1499 if (fabs(sweepAngle) >= 360.0f) {
1500 drawOval(left, top, right, bottom, paint);
1501 return;
1502 }
1503
1504 glActiveTexture(gTextureUnits[0]);
1505 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
1506 startAngle, sweepAngle, useCenter, paint);
1507 drawShape(left, top, texture, paint);
1508}
1509
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001510void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
1511 SkPaint* paint) {
1512 if (mSnapshot->isIgnored()) return;
1513
1514 glActiveTexture(gTextureUnits[0]);
1515 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
1516 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08001517}
1518
Chet Haase5c13d892010-10-08 08:37:55 -07001519void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001520 if (p->getStyle() != SkPaint::kFill_Style) {
1521 drawRectAsShape(left, top, right, bottom, p);
1522 return;
1523 }
1524
Romain Guy6926c722010-07-12 20:20:03 -07001525 if (quickReject(left, top, right, bottom)) {
1526 return;
1527 }
1528
Romain Guy026c5e162010-06-28 17:12:22 -07001529 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07001530 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001531 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1532 if (!isMode) {
1533 // Assume SRC_OVER
1534 mode = SkXfermode::kSrcOver_Mode;
1535 }
1536 } else {
1537 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07001538 }
1539
Romain Guy026c5e162010-06-28 17:12:22 -07001540 int color = p->getColor();
Romain Guy026c5e162010-06-28 17:12:22 -07001541 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -07001542}
Romain Guy9d5316e2010-06-24 19:30:36 -07001543
Romain Guye8e62a42010-07-23 18:55:21 -07001544void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1545 float x, float y, SkPaint* paint) {
Romain Guyb146b122010-12-15 17:06:45 -08001546 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07001547 return;
1548 }
Romain Guyaf636eb2010-12-09 17:47:21 -08001549 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07001550
Romain Guyf607bdc2010-09-10 19:20:06 -07001551 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -07001552
Romain Guya674ab72010-08-10 17:26:42 -07001553 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07001554 switch (paint->getTextAlign()) {
1555 case SkPaint::kCenter_Align:
1556 length = paint->measureText(text, bytesCount);
1557 x -= length / 2.0f;
1558 break;
1559 case SkPaint::kRight_Align:
1560 length = paint->measureText(text, bytesCount);
1561 x -= length;
1562 break;
1563 default:
1564 break;
1565 }
1566
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001567 const float oldX = x;
1568 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08001569 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
1570 if (pureTranslate) {
1571 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
1572 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
1573 }
1574
Romain Guyb45c0c92010-08-26 20:35:23 -07001575 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
1576 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07001577 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07001578
Romain Guy86568192010-12-14 15:55:39 -08001579 int alpha;
1580 SkXfermode::Mode mode;
1581 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07001582
Romain Guy1e45aae2010-08-13 19:39:53 -07001583 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07001584 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -07001585 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -07001586 count, mShadowRadius);
1587 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07001588
Romain Guy86568192010-12-14 15:55:39 -08001589 const float sx = x - shadow->left + mShadowDx;
1590 const float sy = y - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07001591
Romain Guy86568192010-12-14 15:55:39 -08001592 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
1593
1594 glActiveTexture(gTextureUnits[0]);
1595 setupDraw();
1596 setupDrawWithTexture(true);
1597 setupDrawAlpha8Color(mShadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
1598 setupDrawBlending(true, mode);
1599 setupDrawProgram();
1600 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height, pureTranslate);
1601 setupDrawTexture(shadow->id);
1602 setupDrawPureColorUniforms();
1603 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1604
Romain Guy0a417492010-08-16 20:26:20 -07001605 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy86568192010-12-14 15:55:39 -08001606 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07001607 }
1608
Romain Guyb146b122010-12-15 17:06:45 -08001609 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
1610 return;
1611 }
1612
Romain Guy6620c6d2010-12-06 18:07:02 -08001613 // Pick the appropriate texture filtering
1614 bool linearFilter = mSnapshot->transform->changesBounds();
1615 if (pureTranslate && !linearFilter) {
1616 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
1617 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001618
Romain Guy86568192010-12-14 15:55:39 -08001619 glActiveTexture(gTextureUnits[0]);
1620 setupDraw();
1621 setupDrawDirtyRegionsDisabled();
1622 setupDrawWithTexture(true);
1623 setupDrawAlpha8Color(paint->getColor(), alpha);
1624 setupDrawColorFilter();
1625 setupDrawShader();
1626 setupDrawBlending(true, mode);
1627 setupDrawProgram();
1628 setupDrawModelView(x, y, x, y, pureTranslate, true);
1629 setupDrawTexture(fontRenderer.getTexture(linearFilter));
1630 setupDrawPureColorUniforms();
1631 setupDrawColorFilterUniforms();
1632 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07001633
Romain Guy6620c6d2010-12-06 18:07:02 -08001634 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07001635 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
1636
1637#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08001638 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07001639#else
Romain Guyf219da52011-01-16 12:54:25 -08001640 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07001641#endif
Romain Guy03750a02010-10-18 14:06:08 -07001642 mCaches.unbindMeshBuffer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08001643
1644 // Tell font renderer the locations of position and texture coord
1645 // attributes so it can bind its data properly
1646 int positionSlot = mCaches.currentProgram->position;
1647 fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot);
Romain Guy6620c6d2010-12-06 18:07:02 -08001648 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08001649 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001650#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08001651 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001652 if (!pureTranslate) {
1653 mSnapshot->transform->mapRect(bounds);
1654 }
Romain Guyf219da52011-01-16 12:54:25 -08001655 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001656 }
1657#endif
1658 }
Romain Guy694b5192010-07-21 21:33:20 -07001659
1660 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001661 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07001662
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001663 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07001664}
1665
Romain Guy7fbcc042010-08-04 15:40:07 -07001666void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001667 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001668
Romain Guy01d58e42011-01-19 21:54:02 -08001669 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07001670
Romain Guyfb8b7632010-08-23 21:05:08 -07001671 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001672 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001673 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07001674
Romain Guy8b55f372010-08-18 17:10:07 -07001675 const float x = texture->left - texture->offset;
1676 const float y = texture->top - texture->offset;
1677
Romain Guy01d58e42011-01-19 21:54:02 -08001678 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07001679}
1680
Romain Guyada830f2011-01-13 12:13:20 -08001681void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
1682 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08001683 return;
1684 }
1685
1686 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08001687
1688 int alpha;
1689 SkXfermode::Mode mode;
1690 getAlphaAndMode(paint, &alpha, &mode);
1691
Romain Guyada830f2011-01-13 12:13:20 -08001692 layer->alpha = alpha;
1693 layer->mode = mode;
Romain Guy6c319ca2011-01-11 14:29:25 -08001694
Romain Guyf219da52011-01-16 12:54:25 -08001695#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08001696 if (!layer->region.isEmpty()) {
1697 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07001698 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08001699 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08001700 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08001701 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08001702
Romain Guyc88e3572011-01-22 00:32:12 -08001703 setupDraw();
1704 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08001705 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08001706 setupDrawColorFilter();
1707 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
1708 setupDrawProgram();
Romain Guy40667672011-03-18 14:34:03 -07001709 setupDrawModelViewTranslate(x, y,
1710 x + layer->layer.getWidth(), y + layer->layer.getHeight());
Romain Guyc88e3572011-01-22 00:32:12 -08001711 setupDrawPureColorUniforms();
1712 setupDrawColorFilterUniforms();
1713 setupDrawTexture(layer->texture);
Romain Guyc88e3572011-01-22 00:32:12 -08001714 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08001715
Romain Guyc88e3572011-01-22 00:32:12 -08001716 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
1717 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08001718
Romain Guyc88e3572011-01-22 00:32:12 -08001719 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08001720
1721#if DEBUG_LAYERS_AS_REGIONS
1722 drawRegionRects(layer->region);
1723#endif
Romain Guyc88e3572011-01-22 00:32:12 -08001724 }
Romain Guyf219da52011-01-16 12:54:25 -08001725 }
1726#else
Romain Guyada830f2011-01-13 12:13:20 -08001727 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1728 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08001729#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08001730}
1731
Romain Guy6926c722010-07-12 20:20:03 -07001732///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07001733// Shaders
1734///////////////////////////////////////////////////////////////////////////////
1735
1736void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07001737 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07001738}
1739
Romain Guy06f96e22010-07-30 19:18:16 -07001740void OpenGLRenderer::setupShader(SkiaShader* shader) {
1741 mShader = shader;
1742 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001743 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07001744 }
Romain Guy7fac2e12010-07-16 17:10:13 -07001745}
1746
Romain Guyd27977d2010-07-14 19:18:51 -07001747///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07001748// Color filters
1749///////////////////////////////////////////////////////////////////////////////
1750
1751void OpenGLRenderer::resetColorFilter() {
1752 mColorFilter = NULL;
1753}
1754
1755void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
1756 mColorFilter = filter;
1757}
1758
1759///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07001760// Drop shadow
1761///////////////////////////////////////////////////////////////////////////////
1762
1763void OpenGLRenderer::resetShadow() {
1764 mHasShadow = false;
1765}
1766
1767void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
1768 mHasShadow = true;
1769 mShadowRadius = radius;
1770 mShadowDx = dx;
1771 mShadowDy = dy;
1772 mShadowColor = color;
1773}
1774
1775///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07001776// Drawing implementation
1777///////////////////////////////////////////////////////////////////////////////
1778
Romain Guy01d58e42011-01-19 21:54:02 -08001779void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
1780 float x, float y, SkPaint* paint) {
1781 if (quickReject(x, y, x + texture->width, y + texture->height)) {
1782 return;
1783 }
1784
1785 int alpha;
1786 SkXfermode::Mode mode;
1787 getAlphaAndMode(paint, &alpha, &mode);
1788
1789 setupDraw();
1790 setupDrawWithTexture(true);
1791 setupDrawAlpha8Color(paint->getColor(), alpha);
1792 setupDrawColorFilter();
1793 setupDrawShader();
1794 setupDrawBlending(true, mode);
1795 setupDrawProgram();
1796 setupDrawModelView(x, y, x + texture->width, y + texture->height);
1797 setupDrawTexture(texture->id);
1798 setupDrawPureColorUniforms();
1799 setupDrawColorFilterUniforms();
1800 setupDrawShaderUniforms();
1801 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1802
1803 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1804
1805 finishDrawTexture();
1806}
1807
Romain Guyf607bdc2010-09-10 19:20:06 -07001808// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001809#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1810#define kStdUnderline_Offset (1.0f / 9.0f)
1811#define kStdUnderline_Thickness (1.0f / 18.0f)
1812
1813void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1814 float x, float y, SkPaint* paint) {
1815 // Handle underline and strike-through
1816 uint32_t flags = paint->getFlags();
1817 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1818 float underlineWidth = length;
1819 // If length is > 0.0f, we already measured the text for the text alignment
1820 if (length <= 0.0f) {
1821 underlineWidth = paint->measureText(text, bytesCount);
1822 }
1823
1824 float offsetX = 0;
1825 switch (paint->getTextAlign()) {
1826 case SkPaint::kCenter_Align:
1827 offsetX = underlineWidth * 0.5f;
1828 break;
1829 case SkPaint::kRight_Align:
1830 offsetX = underlineWidth;
1831 break;
1832 default:
1833 break;
1834 }
1835
1836 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001837 const float textSize = paint->getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08001838 // TODO: Support stroke width < 1.0f when we have AA lines
1839 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07001840
Romain Guye20ecbd2010-09-22 19:49:04 -07001841 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001842 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001843
Romain Guyf6834472011-01-23 13:32:12 -08001844 int linesCount = 0;
1845 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
1846 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
1847
1848 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07001849 float points[pointsCount];
1850 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001851
1852 if (flags & SkPaint::kUnderlineText_Flag) {
1853 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001854 points[currentPoint++] = left;
1855 points[currentPoint++] = top;
1856 points[currentPoint++] = left + underlineWidth;
1857 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001858 }
1859
1860 if (flags & SkPaint::kStrikeThruText_Flag) {
1861 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001862 points[currentPoint++] = left;
1863 points[currentPoint++] = top;
1864 points[currentPoint++] = left + underlineWidth;
1865 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001866 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001867
1868 SkPaint linesPaint(*paint);
1869 linesPaint.setStrokeWidth(strokeWidth);
1870
1871 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001872 }
1873 }
1874}
1875
Romain Guy026c5e162010-06-28 17:12:22 -07001876void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001877 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07001878 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001879 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001880 color |= 0x00ffffff;
1881 }
1882
Romain Guy70ca14e2010-12-13 18:24:33 -08001883 setupDraw();
1884 setupDrawColor(color);
1885 setupDrawShader();
1886 setupDrawColorFilter();
1887 setupDrawBlending(mode);
1888 setupDrawProgram();
1889 setupDrawModelView(left, top, right, bottom, ignoreTransform);
1890 setupDrawColorUniforms();
1891 setupDrawShaderUniforms(ignoreTransform);
1892 setupDrawColorFilterUniforms();
1893 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07001894
Romain Guyc95c8d62010-09-17 15:31:32 -07001895 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1896}
1897
Romain Guy82ba8142010-07-09 13:25:56 -07001898void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07001899 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001900 int alpha;
1901 SkXfermode::Mode mode;
1902 getAlphaAndMode(paint, &alpha, &mode);
1903
Romain Guy8164c2d2010-10-25 18:03:28 -07001904 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1905
Romain Guy6620c6d2010-12-06 18:07:02 -08001906 if (mSnapshot->transform->isPureTranslate()) {
1907 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1908 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1909
1910 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1911 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
1912 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
1913 } else {
1914 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
1915 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
1916 GL_TRIANGLE_STRIP, gMeshCount);
1917 }
Romain Guy85bf02f2010-06-22 13:11:24 -07001918}
1919
Romain Guybd6b79b2010-06-26 00:13:53 -07001920void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001921 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1922 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07001923 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001924}
1925
1926void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001927 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001928 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07001929 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001930
Romain Guy746b7402010-10-26 16:27:31 -07001931 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08001932 setupDrawWithTexture();
1933 setupDrawColor(alpha, alpha, alpha, alpha);
1934 setupDrawColorFilter();
1935 setupDrawBlending(blend, mode, swapSrcDst);
1936 setupDrawProgram();
1937 if (!dirty) {
1938 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07001939 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001940 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001941 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001942 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08001943 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001944 }
Romain Guy86568192010-12-14 15:55:39 -08001945 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08001946 setupDrawColorFilterUniforms();
1947 setupDrawTexture(texture);
1948 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07001949
Romain Guy6820ac82010-09-15 18:11:50 -07001950 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08001951
1952 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07001953}
1954
Romain Guya5aed0d2010-09-09 14:42:43 -07001955void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001956 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001957 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1958 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001959 if (mode < SkXfermode::kPlus_Mode) {
1960 if (!mCaches.blend) {
1961 glEnable(GL_BLEND);
1962 }
Romain Guy82ba8142010-07-09 13:25:56 -07001963
Romain Guyf607bdc2010-09-10 19:20:06 -07001964 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1965 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001966
Romain Guya5aed0d2010-09-09 14:42:43 -07001967 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1968 glBlendFunc(sourceMode, destMode);
1969 mCaches.lastSrcMode = sourceMode;
1970 mCaches.lastDstMode = destMode;
1971 }
1972 } else {
1973 // These blend modes are not supported by OpenGL directly and have
1974 // to be implemented using shaders. Since the shader will perform
1975 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07001976 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001977 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001978 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001979 }
1980
1981 if (mCaches.blend) {
1982 glDisable(GL_BLEND);
1983 }
1984 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001985 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001986 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001987 glDisable(GL_BLEND);
1988 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001989 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001990}
1991
Romain Guy889f8d12010-07-29 14:37:42 -07001992bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001993 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001994 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001995 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001996 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001997 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001998 }
Romain Guy6926c722010-07-12 20:20:03 -07001999 return true;
Romain Guy260e1022010-07-12 14:41:06 -07002000}
2001
Romain Guy026c5e162010-06-28 17:12:22 -07002002void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07002003 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07002004 TextureVertex::setUV(v++, u1, v1);
2005 TextureVertex::setUV(v++, u2, v1);
2006 TextureVertex::setUV(v++, u1, v2);
2007 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002008}
2009
Chet Haase5c13d892010-10-08 08:37:55 -07002010void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002011 if (paint) {
Romain Guy746b7402010-10-26 16:27:31 -07002012 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07002013 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
2014 if (!isMode) {
2015 // Assume SRC_OVER
2016 *mode = SkXfermode::kSrcOver_Mode;
2017 }
2018 } else {
2019 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07002020 }
2021
2022 // Skia draws using the color's alpha channel if < 255
2023 // Otherwise, it uses the paint's alpha
2024 int color = paint->getColor();
2025 *alpha = (color >> 24) & 0xFF;
2026 if (*alpha == 255) {
2027 *alpha = paint->getAlpha();
2028 }
2029 } else {
2030 *mode = SkXfermode::kSrcOver_Mode;
2031 *alpha = 255;
2032 }
Romain Guy026c5e162010-06-28 17:12:22 -07002033}
2034
Romain Guya5aed0d2010-09-09 14:42:43 -07002035SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Romain Guyb37cbec2011-02-24 17:21:29 -08002036 // In the future we should look at unifying the Porter-Duff modes and
2037 // SkXferModes so that we can use SkXfermode::IsMode(xfer, &mode).
Romain Guya5aed0d2010-09-09 14:42:43 -07002038 if (mode == NULL) {
2039 return SkXfermode::kSrcOver_Mode;
2040 }
2041 return mode->fMode;
2042}
2043
Romain Guy746b7402010-10-26 16:27:31 -07002044void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
Romain Guy8164c2d2010-10-25 18:03:28 -07002045 bool bound = false;
2046 if (wrapS != texture->wrapS) {
2047 glBindTexture(GL_TEXTURE_2D, texture->id);
2048 bound = true;
2049 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
2050 texture->wrapS = wrapS;
2051 }
2052 if (wrapT != texture->wrapT) {
Romain Guy3e3ba152010-10-25 18:33:16 -07002053 if (!bound) {
Romain Guy3e3ba152010-10-25 18:33:16 -07002054 glBindTexture(GL_TEXTURE_2D, texture->id);
2055 }
Romain Guy8164c2d2010-10-25 18:03:28 -07002056 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
2057 texture->wrapT = wrapT;
2058 }
Romain Guya1db5742010-07-20 13:09:13 -07002059}
2060
Romain Guy9d5316e2010-06-24 19:30:36 -07002061}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002062}; // namespace android