blob: 361815a07ab3a807bfad4cba035a58c04772d3b8 [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 Guy5b3b3522010-10-27 18:57:51 -070029#include <ui/Rect.h>
30
Romain Guy85bf02f2010-06-22 13:11:24 -070031#include "OpenGLRenderer.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080032#include "DisplayListRenderer.h"
Romain Guya957eea2010-12-08 18:34:42 -080033#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070034
35namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070036namespace uirenderer {
37
38///////////////////////////////////////////////////////////////////////////////
39// Defines
40///////////////////////////////////////////////////////////////////////////////
41
Romain Guy759ea802010-09-16 20:49:46 -070042#define RAD_TO_DEG (180.0f / 3.14159265f)
43#define MIN_ANGLE 0.001f
44
Romain Guydbc26d22010-10-11 17:58:29 -070045// TODO: This should be set in properties
46#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH)
47
Romain Guy9d5316e2010-06-24 19:30:36 -070048///////////////////////////////////////////////////////////////////////////////
49// Globals
50///////////////////////////////////////////////////////////////////////////////
51
Romain Guy889f8d12010-07-29 14:37:42 -070052/**
53 * Structure mapping Skia xfermodes to OpenGL blending factors.
54 */
55struct Blender {
56 SkXfermode::Mode mode;
57 GLenum src;
58 GLenum dst;
59}; // struct Blender
60
Romain Guy026c5e162010-06-28 17:12:22 -070061// In this array, the index of each Blender equals the value of the first
62// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
63static const Blender gBlends[] = {
Romain Guyb146b122010-12-15 17:06:45 -080064 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
65 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
66 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
67 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
68 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
69 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
70 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
71 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
72 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
73 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
74 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
75 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
Romain Guy026c5e162010-06-28 17:12:22 -070076};
Romain Guye4d01122010-06-16 18:44:05 -070077
Romain Guy87a76572010-09-13 18:11:21 -070078// This array contains the swapped version of each SkXfermode. For instance
79// this array's SrcOver blending mode is actually DstOver. You can refer to
80// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070081static const Blender gBlendsSwap[] = {
Romain Guyb146b122010-12-15 17:06:45 -080082 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
83 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
84 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
85 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
86 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
87 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
88 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
89 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
90 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
91 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
92 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
93 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
Romain Guyf607bdc2010-09-10 19:20:06 -070094};
95
Romain Guy889f8d12010-07-29 14:37:42 -070096static const GLenum gTextureUnits[] = {
Romain Guyb146b122010-12-15 17:06:45 -080097 GL_TEXTURE0,
98 GL_TEXTURE1,
99 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -0700100};
101
Romain Guyf6a11b82010-06-23 17:47:49 -0700102///////////////////////////////////////////////////////////////////////////////
103// Constructors/destructor
104///////////////////////////////////////////////////////////////////////////////
105
Romain Guyfb8b7632010-08-23 21:05:08 -0700106OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700107 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700108 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700109 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700110
Romain Guyac670c02010-07-27 17:39:27 -0700111 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
112
Romain Guyae5575b2010-07-29 18:48:04 -0700113 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700114}
115
Romain Guy85bf02f2010-06-22 13:11:24 -0700116OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700117 // The context has already been destroyed at this point, do not call
118 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700119}
120
Romain Guyf6a11b82010-06-23 17:47:49 -0700121///////////////////////////////////////////////////////////////////////////////
122// Setup
123///////////////////////////////////////////////////////////////////////////////
124
Romain Guy85bf02f2010-06-22 13:11:24 -0700125void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700126 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700127 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700128
129 mWidth = width;
130 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700131
132 mFirstSnapshot->height = height;
133 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700134
135 mDirtyClip = false;
Romain Guye4d01122010-06-16 18:44:05 -0700136}
137
Romain Guy6b7bd242010-10-06 19:49:23 -0700138void OpenGLRenderer::prepare(bool opaque) {
Romain Guy7d7b5492011-01-24 16:33:45 -0800139 prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
140}
141
142void OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800143 mCaches.clearGarbage();
144
Romain Guy8aef54f2010-09-01 15:13:49 -0700145 mSnapshot = new Snapshot(mFirstSnapshot,
146 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy8fb95422010-08-17 18:38:51 -0700147 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700148
Romain Guyfb8b7632010-08-23 21:05:08 -0700149 glViewport(0, 0, mWidth, mHeight);
150
Romain Guyd90f23e2010-09-09 11:47:54 -0700151 glDisable(GL_DITHER);
Romain Guybb9524b2010-06-22 18:56:38 -0700152
Romain Guy7d7b5492011-01-24 16:33:45 -0800153 glEnable(GL_SCISSOR_TEST);
154 glScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
155 mSnapshot->setClip(left, top, right, bottom);
156
Romain Guy6b7bd242010-10-06 19:49:23 -0700157 if (!opaque) {
158 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
159 glClear(GL_COLOR_BUFFER_BIT);
160 }
Romain Guybb9524b2010-06-22 18:56:38 -0700161}
162
Romain Guyb025b9c2010-09-16 14:16:48 -0700163void OpenGLRenderer::finish() {
164#if DEBUG_OPENGL
165 GLenum status = GL_NO_ERROR;
166 while ((status = glGetError()) != GL_NO_ERROR) {
167 LOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800168 switch (status) {
169 case GL_OUT_OF_MEMORY:
170 LOGE(" OpenGLRenderer is out of memory!");
171 break;
172 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700173 }
174#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800175#if DEBUG_MEMORY_USAGE
176 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800177#else
178 if (mCaches.getDebugLevel() & kDebugMemory) {
179 mCaches.dumpMemoryUsage();
180 }
Romain Guyc15008e2010-11-10 11:59:15 -0800181#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700182}
183
Romain Guy6c319ca2011-01-11 14:29:25 -0800184void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700185 if (mCaches.currentProgram) {
186 if (mCaches.currentProgram->isInUse()) {
187 mCaches.currentProgram->remove();
188 mCaches.currentProgram = NULL;
189 }
190 }
Romain Guy50c0f092010-10-19 11:42:22 -0700191 mCaches.unbindMeshBuffer();
Romain Guyda8532c2010-08-31 11:50:35 -0700192}
193
Romain Guy6c319ca2011-01-11 14:29:25 -0800194void OpenGLRenderer::resume() {
Romain Guyeb993562010-10-05 18:14:38 -0700195 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700196
197 glEnable(GL_SCISSOR_TEST);
Romain Guy746b7402010-10-26 16:27:31 -0700198 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700199
Romain Guyf607bdc2010-09-10 19:20:06 -0700200 glDisable(GL_DITHER);
201
Romain Guy42f3a4b2011-01-19 13:42:26 -0800202 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy03750a02010-10-18 14:06:08 -0700203 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700204
Romain Guy50c0f092010-10-19 11:42:22 -0700205 mCaches.blend = true;
206 glEnable(GL_BLEND);
207 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
208 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700209}
210
Chet Haasedaf98e92011-01-10 14:10:36 -0800211bool OpenGLRenderer::callDrawGLFunction(Functor *functor) {
212 interrupt();
Romain Guyf90f8172011-01-25 22:53:24 -0800213 if (mDirtyClip) {
214 setScissorFromClip();
215 }
Romain Guyd643bb52011-03-01 14:55:21 -0800216
217#if RENDER_LAYERS_AS_REGIONS
218 // Since we don't know what the functor will draw, let's dirty
219 // tne entire clip region
220 if (hasLayer()) {
221 Rect clip(*mSnapshot->clipRect);
222 clip.snapToPixelBoundaries();
223 dirtyLayerUnchecked(clip, getRegion());
224 }
225#endif
226
Chet Haasedaf98e92011-01-10 14:10:36 -0800227 status_t result = (*functor)();
228 resume();
229 return (result == 0) ? false : true;
230}
231
Romain Guyf6a11b82010-06-23 17:47:49 -0700232///////////////////////////////////////////////////////////////////////////////
233// State management
234///////////////////////////////////////////////////////////////////////////////
235
Romain Guybb9524b2010-06-22 18:56:38 -0700236int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700237 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700238}
239
240int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700241 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700242}
243
244void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700245 if (mSaveCount > 1) {
246 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700247 }
Romain Guybb9524b2010-06-22 18:56:38 -0700248}
249
250void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700251 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700252
Romain Guy8fb95422010-08-17 18:38:51 -0700253 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700254 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700255 }
Romain Guybb9524b2010-06-22 18:56:38 -0700256}
257
Romain Guy8aef54f2010-09-01 15:13:49 -0700258int OpenGLRenderer::saveSnapshot(int flags) {
259 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700260 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700261}
262
263bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700264 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700265 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700266 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700267
Romain Guybd6b79b2010-06-26 00:13:53 -0700268 sp<Snapshot> current = mSnapshot;
269 sp<Snapshot> previous = mSnapshot->previous;
270
Romain Guyeb993562010-10-05 18:14:38 -0700271 if (restoreOrtho) {
272 Rect& r = previous->viewport;
273 glViewport(r.left, r.top, r.right, r.bottom);
274 mOrthoMatrix.load(current->orthoMatrix);
275 }
276
Romain Guy8b55f372010-08-18 17:10:07 -0700277 mSaveCount--;
278 mSnapshot = previous;
279
Romain Guy2542d192010-08-18 11:47:12 -0700280 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700281 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700282 }
Romain Guy2542d192010-08-18 11:47:12 -0700283
Romain Guy5ec99242010-11-03 16:19:08 -0700284 if (restoreLayer) {
285 composeLayer(current, previous);
286 }
287
Romain Guy2542d192010-08-18 11:47:12 -0700288 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700289}
290
Romain Guyf6a11b82010-06-23 17:47:49 -0700291///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700292// Layers
293///////////////////////////////////////////////////////////////////////////////
294
295int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700296 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700297 const GLuint previousFbo = mSnapshot->fbo;
298 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700299
Romain Guyaf636eb2010-12-09 17:47:21 -0800300 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700301 int alpha = 255;
302 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700303
Romain Guye45362c2010-11-03 19:58:32 -0700304 if (p) {
305 alpha = p->getAlpha();
306 if (!mCaches.extensions.hasFramebufferFetch()) {
307 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
308 if (!isMode) {
309 // Assume SRC_OVER
310 mode = SkXfermode::kSrcOver_Mode;
311 }
312 } else {
313 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700314 }
315 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700316 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700317 }
Romain Guyd55a8612010-06-28 17:42:46 -0700318
Romain Guydbc26d22010-10-11 17:58:29 -0700319 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
320 }
Romain Guyd55a8612010-06-28 17:42:46 -0700321
322 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700323}
324
325int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
326 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700327 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700328 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700329 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700330 SkPaint paint;
331 paint.setAlpha(alpha);
332 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700333 }
Romain Guyd55a8612010-06-28 17:42:46 -0700334}
Romain Guybd6b79b2010-06-26 00:13:53 -0700335
Romain Guy1c740bc2010-09-13 18:00:09 -0700336/**
337 * Layers are viewed by Skia are slightly different than layers in image editing
338 * programs (for instance.) When a layer is created, previously created layers
339 * and the frame buffer still receive every drawing command. For instance, if a
340 * layer is created and a shape intersecting the bounds of the layers and the
341 * framebuffer is draw, the shape will be drawn on both (unless the layer was
342 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
343 *
344 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
345 * texture. Unfortunately, this is inefficient as it requires every primitive to
346 * be drawn n + 1 times, where n is the number of active layers. In practice this
347 * means, for every primitive:
348 * - Switch active frame buffer
349 * - Change viewport, clip and projection matrix
350 * - Issue the drawing
351 *
352 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700353 * To avoid this, layers are implemented in a different way here, at least in the
354 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
355 * is set. When this flag is set we can redirect all drawing operations into a
356 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700357 *
358 * This implementation relies on the frame buffer being at least RGBA 8888. When
359 * a layer is created, only a texture is created, not an FBO. The content of the
360 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700361 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700362 * buffer and drawing continues as normal. This technique therefore treats the
363 * frame buffer as a scratch buffer for the layers.
364 *
365 * To compose the layers back onto the frame buffer, each layer texture
366 * (containing the original frame buffer data) is drawn as a simple quad over
367 * the frame buffer. The trick is that the quad is set as the composition
368 * destination in the blending equation, and the frame buffer becomes the source
369 * of the composition.
370 *
371 * Drawing layers with an alpha value requires an extra step before composition.
372 * An empty quad is drawn over the layer's region in the frame buffer. This quad
373 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
374 * quad is used to multiply the colors in the frame buffer. This is achieved by
375 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
376 * GL_ZERO, GL_SRC_ALPHA.
377 *
378 * Because glCopyTexImage2D() can be slow, an alternative implementation might
379 * be use to draw a single clipped layer. The implementation described above
380 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700381 *
382 * (1) The frame buffer is actually not cleared right away. To allow the GPU
383 * to potentially optimize series of calls to glCopyTexImage2D, the frame
384 * buffer is left untouched until the first drawing operation. Only when
385 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700386 */
Romain Guyd55a8612010-06-28 17:42:46 -0700387bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700388 float right, float bottom, int alpha, SkXfermode::Mode mode,
389 int flags, GLuint previousFbo) {
390 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700391 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700392
Romain Guyeb993562010-10-05 18:14:38 -0700393 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
394
Romain Guyf607bdc2010-09-10 19:20:06 -0700395 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700396 Rect bounds(left, top, right, bottom);
Romain Guyae88e5e2010-10-22 17:49:18 -0700397 if (fboLayer) {
398 // Clear the previous layer regions before we change the viewport
399 clearLayerRegions();
400 } else {
Romain Guyeb993562010-10-05 18:14:38 -0700401 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700402
Romain Guyeb993562010-10-05 18:14:38 -0700403 // Layers only make sense if they are in the framebuffer's bounds
Romain Guy58ae6db2010-10-22 11:16:37 -0700404 bounds.intersect(*snapshot->clipRect);
Romain Guyae517592010-10-22 10:40:27 -0700405
Romain Guyae88e5e2010-10-22 17:49:18 -0700406 // We cannot work with sub-pixels in this case
407 bounds.snapToPixelBoundaries();
408
Romain Guyae517592010-10-22 10:40:27 -0700409 // When the layer is not an FBO, we may use glCopyTexImage so we
410 // need to make sure the layer does not extend outside the bounds
411 // of the framebuffer
412 bounds.intersect(snapshot->previous->viewport);
Romain Guyeb993562010-10-05 18:14:38 -0700413 }
Romain Guybf434112010-09-16 14:40:17 -0700414
Romain Guy746b7402010-10-26 16:27:31 -0700415 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
416 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800417 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700418 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700419 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700420 }
421
422 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800423 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700424 return false;
425 }
Romain Guyf18fd992010-07-08 11:45:51 -0700426
Romain Guy5b3b3522010-10-27 18:57:51 -0700427 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700428 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700429 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700430 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700431 }
432
Romain Guydda57022010-07-06 11:39:32 -0700433 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700434 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700435 layer->layer.set(bounds);
Romain Guy8550c4c2010-10-08 15:49:53 -0700436 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
437 bounds.getWidth() / float(layer->width), 0.0f);
Romain Guy171c5922011-01-06 10:04:23 -0800438 layer->colorFilter = mColorFilter;
Romain Guydda57022010-07-06 11:39:32 -0700439
Romain Guy8fb95422010-08-17 18:38:51 -0700440 // Save the layer in the snapshot
441 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700442 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700443
Romain Guyeb993562010-10-05 18:14:38 -0700444 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700445 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700446 } else {
447 // Copy the framebuffer into the layer
448 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy514fb182011-01-19 14:38:29 -0800449 if (!bounds.isEmpty()) {
450 if (layer->empty) {
451 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left,
452 snapshot->height - bounds.bottom, layer->width, layer->height, 0);
453 layer->empty = false;
454 } else {
455 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
456 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
457 }
458 // Enqueue the buffer coordinates to clear the corresponding region later
459 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700460 }
Romain Guyeb993562010-10-05 18:14:38 -0700461 }
Romain Guyf86ef572010-07-01 11:05:42 -0700462
Romain Guyd55a8612010-06-28 17:42:46 -0700463 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700464}
465
Romain Guy5b3b3522010-10-27 18:57:51 -0700466bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
467 GLuint previousFbo) {
468 layer->fbo = mCaches.fboCache.get();
469
470#if RENDER_LAYERS_AS_REGIONS
471 snapshot->region = &snapshot->layer->region;
472 snapshot->flags |= Snapshot::kFlagFboTarget;
473#endif
474
475 Rect clip(bounds);
476 snapshot->transform->mapRect(clip);
477 clip.intersect(*snapshot->clipRect);
478 clip.snapToPixelBoundaries();
479 clip.intersect(snapshot->previous->viewport);
480
481 mat4 inverse;
482 inverse.loadInverse(*mSnapshot->transform);
483
484 inverse.mapRect(clip);
485 clip.snapToPixelBoundaries();
486 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700487 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700488
489 snapshot->flags |= Snapshot::kFlagIsFboLayer;
490 snapshot->fbo = layer->fbo;
491 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700492 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
493 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
494 snapshot->height = bounds.getHeight();
495 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
496 snapshot->orthoMatrix.load(mOrthoMatrix);
497
498 // Bind texture to FBO
499 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
500 glBindTexture(GL_TEXTURE_2D, layer->texture);
501
502 // Initialize the texture if needed
503 if (layer->empty) {
504 layer->empty = false;
505 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
506 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
507 }
508
509 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
510 layer->texture, 0);
511
512#if DEBUG_LAYERS_AS_REGIONS
513 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
514 if (status != GL_FRAMEBUFFER_COMPLETE) {
515 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
516
517 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
518 glDeleteTextures(1, &layer->texture);
519 mCaches.fboCache.put(layer->fbo);
520
521 delete layer;
522
523 return false;
524 }
525#endif
526
527 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
528 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
529 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
530 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
531 glClear(GL_COLOR_BUFFER_BIT);
532
533 dirtyClip();
534
535 // Change the ortho projection
536 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
537 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
538
539 return true;
540}
541
Romain Guy1c740bc2010-09-13 18:00:09 -0700542/**
543 * Read the documentation of createLayer() before doing anything in this method.
544 */
Romain Guy1d83e192010-08-17 11:37:00 -0700545void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
546 if (!current->layer) {
547 LOGE("Attempting to compose a layer that does not exist");
548 return;
549 }
550
Romain Guy5b3b3522010-10-27 18:57:51 -0700551 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700552
553 if (fboLayer) {
554 // Unbind current FBO and restore previous one
555 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
556 }
557
Romain Guy1d83e192010-08-17 11:37:00 -0700558 Layer* layer = current->layer;
559 const Rect& rect = layer->layer;
560
Romain Guyeb993562010-10-05 18:14:38 -0700561 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700562 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700563 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700564 // Required below, composeLayerRect() will divide by 255
565 layer->alpha = 255;
Romain Guyf607bdc2010-09-10 19:20:06 -0700566 }
567
Romain Guy03750a02010-10-18 14:06:08 -0700568 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700569
Romain Guy746b7402010-10-26 16:27:31 -0700570 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700571
Romain Guy5b3b3522010-10-27 18:57:51 -0700572 // When the layer is stored in an FBO, we can save a bit of fillrate by
573 // drawing only the dirty region
574 if (fboLayer) {
575 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy171c5922011-01-06 10:04:23 -0800576 if (layer->colorFilter) {
577 setupColorFilter(layer->colorFilter);
578 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700579 composeLayerRegion(layer, rect);
Romain Guy171c5922011-01-06 10:04:23 -0800580 if (layer->colorFilter) {
581 resetColorFilter();
582 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700583 } else {
Romain Guy514fb182011-01-19 14:38:29 -0800584 if (!rect.isEmpty()) {
585 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
586 composeLayerRect(layer, rect, true);
587 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700588 }
Romain Guy8b55f372010-08-18 17:10:07 -0700589
Romain Guyeb993562010-10-05 18:14:38 -0700590 if (fboLayer) {
591 // Detach the texture from the FBO
592 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
593 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
594 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
595
596 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
597 mCaches.fboCache.put(current->fbo);
598 }
599
Romain Guy746b7402010-10-26 16:27:31 -0700600 dirtyClip();
601
Romain Guyeb993562010-10-05 18:14:38 -0700602 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700603 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700604 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700605 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700606 delete layer;
607 }
608}
609
Romain Guy5b3b3522010-10-27 18:57:51 -0700610void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
611 const Rect& texCoords = layer->texCoords;
612 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
613
614 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
615 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
616 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap);
617
618 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
619}
620
621void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
622#if RENDER_LAYERS_AS_REGIONS
623 if (layer->region.isRect()) {
624 composeLayerRect(layer, rect);
625 layer->region.clear();
626 return;
627 }
628
629 if (!layer->region.isEmpty()) {
630 size_t count;
631 const android::Rect* rects = layer->region.getArray(&count);
632
Romain Guy5b3b3522010-10-27 18:57:51 -0700633 const float alpha = layer->alpha / 255.0f;
Romain Guy5b3b3522010-10-27 18:57:51 -0700634 const float texX = 1.0f / float(layer->width);
635 const float texY = 1.0f / float(layer->height);
Romain Guyf219da52011-01-16 12:54:25 -0800636 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700637
638 TextureVertex* mesh = mCaches.getRegionMesh();
639 GLsizei numQuads = 0;
640
Romain Guy7230a742011-01-10 22:26:16 -0800641 setupDraw();
642 setupDrawWithTexture();
643 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800644 setupDrawColorFilter();
Romain Guy7230a742011-01-10 22:26:16 -0800645 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
646 setupDrawProgram();
647 setupDrawDirtyRegionsDisabled();
648 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800649 setupDrawColorFilterUniforms();
Romain Guy7230a742011-01-10 22:26:16 -0800650 setupDrawTexture(layer->texture);
Romain Guyc038ea32011-01-12 15:08:47 -0800651 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
Romain Guy7230a742011-01-10 22:26:16 -0800652 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700653
654 for (size_t i = 0; i < count; i++) {
655 const android::Rect* r = &rects[i];
656
657 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800658 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700659 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800660 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700661
662 // TODO: Reject quads outside of the clip
663 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
664 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
665 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
666 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
667
668 numQuads++;
669
670 if (numQuads >= REGION_MESH_QUAD_COUNT) {
671 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
672 numQuads = 0;
673 mesh = mCaches.getRegionMesh();
674 }
675 }
676
677 if (numQuads > 0) {
678 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
679 }
680
681 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800682 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700683
684#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -0800685 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -0700686#endif
687
688 layer->region.clear();
689 }
690#else
691 composeLayerRect(layer, rect);
692#endif
693}
694
Romain Guy3a3133d2011-02-01 22:59:58 -0800695void OpenGLRenderer::drawRegionRects(const Region& region) {
696#if DEBUG_LAYERS_AS_REGIONS
697 size_t count;
698 const android::Rect* rects = region.getArray(&count);
699
700 uint32_t colors[] = {
701 0x7fff0000, 0x7f00ff00,
702 0x7f0000ff, 0x7fff00ff,
703 };
704
705 int offset = 0;
706 int32_t top = rects[0].top;
707
708 for (size_t i = 0; i < count; i++) {
709 if (top != rects[i].top) {
710 offset ^= 0x2;
711 top = rects[i].top;
712 }
713
714 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
715 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
716 SkXfermode::kSrcOver_Mode);
717 }
718#endif
719}
720
Romain Guy5b3b3522010-10-27 18:57:51 -0700721void OpenGLRenderer::dirtyLayer(const float left, const float top,
722 const float right, const float bottom, const mat4 transform) {
723#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800724 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700725 Rect bounds(left, top, right, bottom);
726 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800727 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700728 }
729#endif
730}
731
732void OpenGLRenderer::dirtyLayer(const float left, const float top,
733 const float right, const float bottom) {
734#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800735 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700736 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800737 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800738 }
739#endif
740}
741
742void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
743#if RENDER_LAYERS_AS_REGIONS
744 if (bounds.intersect(*mSnapshot->clipRect)) {
745 bounds.snapToPixelBoundaries();
746 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
747 if (!dirty.isEmpty()) {
748 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700749 }
750 }
751#endif
752}
753
Romain Guy86942302010-09-12 13:02:16 -0700754void OpenGLRenderer::clearLayerRegions() {
Romain Guyaf636eb2010-12-09 17:47:21 -0800755 if (mLayers.size() == 0 || mSnapshot->isIgnored()) return;
Romain Guy86942302010-09-12 13:02:16 -0700756
Romain Guy5b3b3522010-10-27 18:57:51 -0700757 Rect clipRect(*mSnapshot->clipRect);
758 clipRect.snapToPixelBoundaries();
759
Romain Guy86942302010-09-12 13:02:16 -0700760 for (uint32_t i = 0; i < mLayers.size(); i++) {
761 Rect* bounds = mLayers.itemAt(i);
Romain Guy5b3b3522010-10-27 18:57:51 -0700762 if (clipRect.intersects(*bounds)) {
763 // Clear the framebuffer where the layer will draw
764 glScissor(bounds->left, mSnapshot->height - bounds->bottom,
765 bounds->getWidth(), bounds->getHeight());
766 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
767 glClear(GL_COLOR_BUFFER_BIT);
Romain Guy86942302010-09-12 13:02:16 -0700768
Romain Guy5b3b3522010-10-27 18:57:51 -0700769 // Restore the clip
770 dirtyClip();
771 }
Romain Guy86942302010-09-12 13:02:16 -0700772
773 delete bounds;
774 }
Romain Guy86942302010-09-12 13:02:16 -0700775
Romain Guy5b3b3522010-10-27 18:57:51 -0700776 mLayers.clear();
Romain Guy86942302010-09-12 13:02:16 -0700777}
778
Romain Guybd6b79b2010-06-26 00:13:53 -0700779///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700780// Transforms
781///////////////////////////////////////////////////////////////////////////////
782
783void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700784 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700785}
786
787void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700788 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700789}
790
791void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700792 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700793}
794
Romain Guy807daf72011-01-18 11:19:19 -0800795void OpenGLRenderer::skew(float sx, float sy) {
796 mSnapshot->transform->skew(sx, sy);
797}
798
Romain Guyf6a11b82010-06-23 17:47:49 -0700799void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700800 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700801}
802
Romain Guy41030da2010-10-13 13:40:37 -0700803const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700804 if (mSnapshot->fbo != 0) {
805 return &mSnapshot->transform->data[0];
806 }
807 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700808}
809
Romain Guyf6a11b82010-06-23 17:47:49 -0700810void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700811 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700812}
813
814void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700815 SkMatrix transform;
816 mSnapshot->transform->copyTo(transform);
817 transform.preConcat(*matrix);
818 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700819}
820
821///////////////////////////////////////////////////////////////////////////////
822// Clipping
823///////////////////////////////////////////////////////////////////////////////
824
Romain Guybb9524b2010-06-22 18:56:38 -0700825void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700826 Rect clip(*mSnapshot->clipRect);
827 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700828 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700829 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700830}
831
832const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700833 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700834}
835
Romain Guyc7d53492010-06-25 13:41:57 -0700836bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800837 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700838 return true;
839 }
840
Romain Guy1d83e192010-08-17 11:37:00 -0700841 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700842 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700843 r.snapToPixelBoundaries();
844
845 Rect clipRect(*mSnapshot->clipRect);
846 clipRect.snapToPixelBoundaries();
847
848 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700849}
850
Romain Guy079ba2c2010-07-16 14:12:24 -0700851bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
852 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700853 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700854 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700855 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700856 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700857}
858
Romain Guyf6a11b82010-06-23 17:47:49 -0700859///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -0800860// Drawing commands
861///////////////////////////////////////////////////////////////////////////////
862
863void OpenGLRenderer::setupDraw() {
864 clearLayerRegions();
865 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
Chet Haasedaf98e92011-01-10 14:10:36 -08001058bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t level) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001059 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1060 // will be performed by the display list itself
1061 if (displayList) {
Chet Haasedaf98e92011-01-10 14:10:36 -08001062 return displayList->replay(*this, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001063 }
Chet Haasedaf98e92011-01-10 14:10:36 -08001064 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001065}
1066
Chet Haase5c13d892010-10-08 08:37:55 -07001067void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001068 const float right = left + bitmap->width();
1069 const float bottom = top + bitmap->height();
1070
1071 if (quickReject(left, top, right, bottom)) {
1072 return;
1073 }
1074
Romain Guy86568192010-12-14 15:55:39 -08001075 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001076 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001077 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001078 const AutoTexture autoCleanup(texture);
1079
Romain Guy6926c722010-07-12 20:20:03 -07001080 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -07001081}
1082
Chet Haase5c13d892010-10-08 08:37:55 -07001083void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001084 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1085 const mat4 transform(*matrix);
1086 transform.mapRect(r);
1087
Romain Guy6926c722010-07-12 20:20:03 -07001088 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1089 return;
1090 }
1091
Romain Guy86568192010-12-14 15:55:39 -08001092 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001093 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001094 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001095 const AutoTexture autoCleanup(texture);
1096
Romain Guy5b3b3522010-10-27 18:57:51 -07001097 // This could be done in a cheaper way, all we need is pass the matrix
1098 // to the vertex shader. The save/restore is a bit overkill.
1099 save(SkCanvas::kMatrix_SaveFlag);
1100 concatMatrix(matrix);
1101 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1102 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001103}
1104
Romain Guy5a7b4662011-01-20 19:09:30 -08001105void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1106 float* vertices, int* colors, SkPaint* paint) {
1107 // TODO: Do a quickReject
1108 if (!vertices || mSnapshot->isIgnored()) {
1109 return;
1110 }
1111
1112 glActiveTexture(gTextureUnits[0]);
1113 Texture* texture = mCaches.textureCache.get(bitmap);
1114 if (!texture) return;
1115 const AutoTexture autoCleanup(texture);
1116 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1117
1118 int alpha;
1119 SkXfermode::Mode mode;
1120 getAlphaAndMode(paint, &alpha, &mode);
1121
Romain Guy5a7b4662011-01-20 19:09:30 -08001122 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001123
Romain Guyb18d2d02011-02-10 15:52:54 -08001124 float left = FLT_MAX;
1125 float top = FLT_MAX;
1126 float right = FLT_MIN;
1127 float bottom = FLT_MIN;
1128
1129#if RENDER_LAYERS_AS_REGIONS
1130 bool hasActiveLayer = hasLayer();
1131#else
1132 bool hasActiveLayer = false;
1133#endif
1134
Romain Guya566b7c2011-01-23 16:36:11 -08001135 // TODO: Support the colors array
1136 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001137 TextureVertex* vertex = mesh;
1138 for (int32_t y = 0; y < meshHeight; y++) {
1139 for (int32_t x = 0; x < meshWidth; x++) {
1140 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1141
1142 float u1 = float(x) / meshWidth;
1143 float u2 = float(x + 1) / meshWidth;
1144 float v1 = float(y) / meshHeight;
1145 float v2 = float(y + 1) / meshHeight;
1146
1147 int ax = i + (meshWidth + 1) * 2;
1148 int ay = ax + 1;
1149 int bx = i;
1150 int by = bx + 1;
1151 int cx = i + 2;
1152 int cy = cx + 1;
1153 int dx = i + (meshWidth + 1) * 2 + 2;
1154 int dy = dx + 1;
1155
1156 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1157 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1158 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1159
1160 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1161 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1162 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
Romain Guyb18d2d02011-02-10 15:52:54 -08001163
1164#if RENDER_LAYERS_AS_REGIONS
1165 if (hasActiveLayer) {
1166 // TODO: This could be optimized to avoid unnecessary ops
1167 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1168 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1169 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1170 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1171 }
1172#endif
Romain Guy5a7b4662011-01-20 19:09:30 -08001173 }
1174 }
1175
Romain Guyb18d2d02011-02-10 15:52:54 -08001176#if RENDER_LAYERS_AS_REGIONS
1177 if (hasActiveLayer) {
1178 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1179 }
1180#endif
1181
Romain Guy5a7b4662011-01-20 19:09:30 -08001182 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1183 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
Romain Guyb18d2d02011-02-10 15:52:54 -08001184 GL_TRIANGLES, count, false, false, 0, false, false);
Romain Guy5a7b4662011-01-20 19:09:30 -08001185}
1186
Romain Guy8ba548f2010-06-30 19:21:21 -07001187void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1188 float srcLeft, float srcTop, float srcRight, float srcBottom,
1189 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001190 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001191 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1192 return;
1193 }
1194
Romain Guy746b7402010-10-26 16:27:31 -07001195 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001196 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001197 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001198 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001199 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guy8ba548f2010-06-30 19:21:21 -07001200
Romain Guy8ba548f2010-06-30 19:21:21 -07001201 const float width = texture->width;
1202 const float height = texture->height;
1203
1204 const float u1 = srcLeft / width;
1205 const float v1 = srcTop / height;
1206 const float u2 = srcRight / width;
1207 const float v2 = srcBottom / height;
1208
Romain Guy03750a02010-10-18 14:06:08 -07001209 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001210 resetDrawTextureTexCoords(u1, v1, u2, v2);
1211
Romain Guy03750a02010-10-18 14:06:08 -07001212 int alpha;
1213 SkXfermode::Mode mode;
1214 getAlphaAndMode(paint, &alpha, &mode);
1215
Romain Guy6620c6d2010-12-06 18:07:02 -08001216 if (mSnapshot->transform->isPureTranslate()) {
1217 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1218 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1219
1220 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1221 texture->id, alpha / 255.0f, mode, texture->blend,
1222 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1223 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1224 } else {
1225 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1226 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1227 GL_TRIANGLE_STRIP, gMeshCount);
1228 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001229
1230 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1231}
1232
Romain Guy4aa90572010-09-26 18:40:37 -07001233void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001234 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001235 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001236 if (quickReject(left, top, right, bottom)) {
1237 return;
1238 }
1239
Romain Guy746b7402010-10-26 16:27:31 -07001240 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001241 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001242 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001243 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001244 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guyf7f93552010-07-08 19:17:03 -07001245
1246 int alpha;
1247 SkXfermode::Mode mode;
1248 getAlphaAndMode(paint, &alpha, &mode);
1249
Romain Guy2728f962010-10-08 18:36:15 -07001250 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001251 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001252
Romain Guya5ef39a2010-12-03 16:48:20 -08001253 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001254 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001255#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001256 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001257 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001258 const float offsetX = left + mSnapshot->transform->getTranslateX();
1259 const float offsetY = top + mSnapshot->transform->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07001260 const size_t count = mesh->quads.size();
1261 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001262 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001263 if (pureTranslate) {
Romain Guyc78b5d52011-02-04 14:00:42 -08001264 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1265 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1266 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08001267 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08001268 dirtyLayer(left + bounds.left, top + bounds.top,
1269 left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
Romain Guy6620c6d2010-12-06 18:07:02 -08001270 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001271 }
1272 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001273#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001274
Romain Guy6620c6d2010-12-06 18:07:02 -08001275 if (pureTranslate) {
1276 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1277 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1278
1279 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1280 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1281 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1282 true, !mesh->hasEmptyQuads);
1283 } else {
1284 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1285 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1286 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1287 true, !mesh->hasEmptyQuads);
1288 }
Romain Guy054dc182010-10-15 17:55:25 -07001289 }
Romain Guyf7f93552010-07-08 19:17:03 -07001290}
1291
Chet Haase5c13d892010-10-08 08:37:55 -07001292void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001293 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001294
Romain Guya957eea2010-12-08 18:34:42 -08001295 const bool isAA = paint->isAntiAlias();
1296 const float strokeWidth = paint->getStrokeWidth() * 0.5f;
1297 // A stroke width of 0 has a special meaningin Skia:
1298 // it draws an unscaled 1px wide line
1299 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
1300
Romain Guy759ea802010-09-16 20:49:46 -07001301 int alpha;
1302 SkXfermode::Mode mode;
1303 getAlphaAndMode(paint, &alpha, &mode);
1304
Romain Guya957eea2010-12-08 18:34:42 -08001305 int verticesCount = count >> 2;
Romain Guy7230a742011-01-10 22:26:16 -08001306 int generatedVerticesCount = 0;
Romain Guya957eea2010-12-08 18:34:42 -08001307 if (!isHairLine) {
1308 // TODO: AA needs more vertices
1309 verticesCount *= 6;
Romain Guyc95c8d62010-09-17 15:31:32 -07001310 } else {
Romain Guya957eea2010-12-08 18:34:42 -08001311 // TODO: AA will be different
1312 verticesCount *= 2;
Romain Guyc95c8d62010-09-17 15:31:32 -07001313 }
1314
Romain Guya957eea2010-12-08 18:34:42 -08001315 TextureVertex lines[verticesCount];
1316 TextureVertex* vertex = &lines[0];
Romain Guy759ea802010-09-16 20:49:46 -07001317
Romain Guy8d0d4782010-12-14 20:13:35 -08001318 setupDraw();
1319 setupDrawColor(paint->getColor(), alpha);
1320 setupDrawColorFilter();
1321 setupDrawShader();
1322 setupDrawBlending(mode);
1323 setupDrawProgram();
1324 setupDrawModelViewIdentity();
1325 setupDrawColorUniforms();
1326 setupDrawColorFilterUniforms();
1327 setupDrawShaderIdentityUniforms();
1328 setupDrawMesh(vertex);
Romain Guya957eea2010-12-08 18:34:42 -08001329
1330 if (!isHairLine) {
1331 // TODO: Handle the AA case
1332 for (int i = 0; i < count; i += 4) {
1333 // a = start point, b = end point
1334 vec2 a(points[i], points[i + 1]);
1335 vec2 b(points[i + 2], points[i + 3]);
1336
1337 // Bias to snap to the same pixels as Skia
1338 a += 0.375;
1339 b += 0.375;
1340
1341 // Find the normal to the line
1342 vec2 n = (b - a).copyNormalized() * strokeWidth;
1343 float x = n.x;
1344 n.x = -n.y;
1345 n.y = x;
1346
1347 // Four corners of the rectangle defining a thick line
1348 vec2 p1 = a - n;
1349 vec2 p2 = a + n;
1350 vec2 p3 = b + n;
1351 vec2 p4 = b - n;
1352
Romain Guy7230a742011-01-10 22:26:16 -08001353 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1354 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1355 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1356 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
Romain Guya957eea2010-12-08 18:34:42 -08001357
Romain Guy7230a742011-01-10 22:26:16 -08001358 if (!quickReject(left, top, right, bottom)) {
1359 // Draw the line as 2 triangles, could be optimized
1360 // by using only 4 vertices and the correct indices
1361 // Also we should probably used non textured vertices
1362 // when line AA is disabled to save on bandwidth
1363 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1364 TextureVertex::set(vertex++, p2.x, p2.y, 0.0f, 0.0f);
1365 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1366 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1367 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1368 TextureVertex::set(vertex++, p4.x, p4.y, 0.0f, 0.0f);
1369
1370 generatedVerticesCount += 6;
1371
1372 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1373 }
Romain Guya957eea2010-12-08 18:34:42 -08001374 }
1375
Romain Guy7230a742011-01-10 22:26:16 -08001376 if (generatedVerticesCount > 0) {
1377 // GL_LINE does not give the result we want to match Skia
1378 glDrawArrays(GL_TRIANGLES, 0, generatedVerticesCount);
1379 }
Romain Guya957eea2010-12-08 18:34:42 -08001380 } else {
1381 // TODO: Handle the AA case
1382 for (int i = 0; i < count; i += 4) {
Romain Guy7230a742011-01-10 22:26:16 -08001383 const float left = fmin(points[i], points[i + 1]);
1384 const float right = fmax(points[i], points[i + 1]);
1385 const float top = fmin(points[i + 2], points[i + 3]);
1386 const float bottom = fmax(points[i + 2], points[i + 3]);
1387
1388 if (!quickReject(left, top, right, bottom)) {
1389 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1390 TextureVertex::set(vertex++, points[i + 2], points[i + 3], 0.0f, 0.0f);
1391
1392 generatedVerticesCount += 2;
1393
1394 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1395 }
Romain Guya957eea2010-12-08 18:34:42 -08001396 }
Romain Guy8d0d4782010-12-14 20:13:35 -08001397
Romain Guy7230a742011-01-10 22:26:16 -08001398 if (generatedVerticesCount > 0) {
1399 glLineWidth(1.0f);
1400 glDrawArrays(GL_LINES, 0, generatedVerticesCount);
1401 }
Romain Guy29d89972010-09-22 16:10:57 -07001402 }
Romain Guy759ea802010-09-16 20:49:46 -07001403}
1404
Romain Guy85bf02f2010-06-22 13:11:24 -07001405void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001406 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001407 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001408
Romain Guyae88e5e2010-10-22 17:49:18 -07001409 Rect& clip(*mSnapshot->clipRect);
1410 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001411
Romain Guy3d58c032010-07-14 16:34:53 -07001412 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001413}
Romain Guy9d5316e2010-06-24 19:30:36 -07001414
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001415void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001416 if (!texture) return;
1417 const AutoTexture autoCleanup(texture);
1418
1419 const float x = left + texture->left - texture->offset;
1420 const float y = top + texture->top - texture->offset;
1421
1422 drawPathTexture(texture, x, y, paint);
1423}
1424
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001425void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1426 float rx, float ry, SkPaint* paint) {
1427 if (mSnapshot->isIgnored()) return;
1428
1429 glActiveTexture(gTextureUnits[0]);
1430 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1431 right - left, bottom - top, rx, ry, paint);
1432 drawShape(left, top, texture, paint);
1433}
1434
Romain Guy01d58e42011-01-19 21:54:02 -08001435void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1436 if (mSnapshot->isIgnored()) return;
1437
1438 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08001439 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001440 drawShape(x - radius, y - radius, texture, paint);
1441}
Romain Guy01d58e42011-01-19 21:54:02 -08001442
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001443void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
1444 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001445
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001446 glActiveTexture(gTextureUnits[0]);
1447 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
1448 drawShape(left, top, texture, paint);
1449}
1450
Romain Guy8b2f5262011-01-23 16:15:02 -08001451void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
1452 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
1453 if (mSnapshot->isIgnored()) return;
1454
1455 if (fabs(sweepAngle) >= 360.0f) {
1456 drawOval(left, top, right, bottom, paint);
1457 return;
1458 }
1459
1460 glActiveTexture(gTextureUnits[0]);
1461 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
1462 startAngle, sweepAngle, useCenter, paint);
1463 drawShape(left, top, texture, paint);
1464}
1465
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001466void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
1467 SkPaint* paint) {
1468 if (mSnapshot->isIgnored()) return;
1469
1470 glActiveTexture(gTextureUnits[0]);
1471 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
1472 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08001473}
1474
Chet Haase5c13d892010-10-08 08:37:55 -07001475void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001476 if (p->getStyle() != SkPaint::kFill_Style) {
1477 drawRectAsShape(left, top, right, bottom, p);
1478 return;
1479 }
1480
Romain Guy6926c722010-07-12 20:20:03 -07001481 if (quickReject(left, top, right, bottom)) {
1482 return;
1483 }
1484
Romain Guy026c5e162010-06-28 17:12:22 -07001485 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07001486 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001487 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1488 if (!isMode) {
1489 // Assume SRC_OVER
1490 mode = SkXfermode::kSrcOver_Mode;
1491 }
1492 } else {
1493 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07001494 }
1495
Romain Guy026c5e162010-06-28 17:12:22 -07001496 int color = p->getColor();
Romain Guy026c5e162010-06-28 17:12:22 -07001497 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -07001498}
Romain Guy9d5316e2010-06-24 19:30:36 -07001499
Romain Guye8e62a42010-07-23 18:55:21 -07001500void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1501 float x, float y, SkPaint* paint) {
Romain Guyb146b122010-12-15 17:06:45 -08001502 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07001503 return;
1504 }
Romain Guyaf636eb2010-12-09 17:47:21 -08001505 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07001506
Romain Guyf607bdc2010-09-10 19:20:06 -07001507 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -07001508
Romain Guya674ab72010-08-10 17:26:42 -07001509 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07001510 switch (paint->getTextAlign()) {
1511 case SkPaint::kCenter_Align:
1512 length = paint->measureText(text, bytesCount);
1513 x -= length / 2.0f;
1514 break;
1515 case SkPaint::kRight_Align:
1516 length = paint->measureText(text, bytesCount);
1517 x -= length;
1518 break;
1519 default:
1520 break;
1521 }
1522
Romain Guy6620c6d2010-12-06 18:07:02 -08001523 // TODO: Handle paint->getTextScaleX()
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001524 const float oldX = x;
1525 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08001526 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
1527 if (pureTranslate) {
1528 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
1529 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
1530 }
1531
Romain Guyb45c0c92010-08-26 20:35:23 -07001532 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
1533 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07001534 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07001535
Romain Guy86568192010-12-14 15:55:39 -08001536 int alpha;
1537 SkXfermode::Mode mode;
1538 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07001539
Romain Guy1e45aae2010-08-13 19:39:53 -07001540 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07001541 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -07001542 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -07001543 count, mShadowRadius);
1544 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07001545
Romain Guy86568192010-12-14 15:55:39 -08001546 const float sx = x - shadow->left + mShadowDx;
1547 const float sy = y - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07001548
Romain Guy86568192010-12-14 15:55:39 -08001549 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
1550
1551 glActiveTexture(gTextureUnits[0]);
1552 setupDraw();
1553 setupDrawWithTexture(true);
1554 setupDrawAlpha8Color(mShadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
1555 setupDrawBlending(true, mode);
1556 setupDrawProgram();
1557 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height, pureTranslate);
1558 setupDrawTexture(shadow->id);
1559 setupDrawPureColorUniforms();
1560 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1561
Romain Guy0a417492010-08-16 20:26:20 -07001562 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy86568192010-12-14 15:55:39 -08001563 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07001564 }
1565
Romain Guyb146b122010-12-15 17:06:45 -08001566 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
1567 return;
1568 }
1569
Romain Guy6620c6d2010-12-06 18:07:02 -08001570 // Pick the appropriate texture filtering
1571 bool linearFilter = mSnapshot->transform->changesBounds();
1572 if (pureTranslate && !linearFilter) {
1573 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
1574 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001575
Romain Guy86568192010-12-14 15:55:39 -08001576 glActiveTexture(gTextureUnits[0]);
1577 setupDraw();
1578 setupDrawDirtyRegionsDisabled();
1579 setupDrawWithTexture(true);
1580 setupDrawAlpha8Color(paint->getColor(), alpha);
1581 setupDrawColorFilter();
1582 setupDrawShader();
1583 setupDrawBlending(true, mode);
1584 setupDrawProgram();
1585 setupDrawModelView(x, y, x, y, pureTranslate, true);
1586 setupDrawTexture(fontRenderer.getTexture(linearFilter));
1587 setupDrawPureColorUniforms();
1588 setupDrawColorFilterUniforms();
1589 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07001590
Romain Guy6620c6d2010-12-06 18:07:02 -08001591 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07001592 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
1593
1594#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08001595 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07001596#else
Romain Guyf219da52011-01-16 12:54:25 -08001597 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07001598#endif
Romain Guy03750a02010-10-18 14:06:08 -07001599 mCaches.unbindMeshBuffer();
Alex Sakhartchouk894df172011-02-17 16:45:37 -08001600
1601 // Tell font renderer the locations of position and texture coord
1602 // attributes so it can bind its data properly
1603 int positionSlot = mCaches.currentProgram->position;
1604 fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot);
Romain Guy6620c6d2010-12-06 18:07:02 -08001605 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08001606 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001607#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08001608 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001609 if (!pureTranslate) {
1610 mSnapshot->transform->mapRect(bounds);
1611 }
Romain Guyf219da52011-01-16 12:54:25 -08001612 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001613 }
1614#endif
1615 }
Romain Guy694b5192010-07-21 21:33:20 -07001616
1617 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001618 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07001619
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001620 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07001621}
1622
Romain Guy7fbcc042010-08-04 15:40:07 -07001623void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001624 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001625
Romain Guy01d58e42011-01-19 21:54:02 -08001626 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07001627
Romain Guyfb8b7632010-08-23 21:05:08 -07001628 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -07001629 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001630 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07001631
Romain Guy8b55f372010-08-18 17:10:07 -07001632 const float x = texture->left - texture->offset;
1633 const float y = texture->top - texture->offset;
1634
Romain Guy01d58e42011-01-19 21:54:02 -08001635 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07001636}
1637
Romain Guyada830f2011-01-13 12:13:20 -08001638void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
1639 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08001640 return;
1641 }
1642
1643 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08001644
1645 int alpha;
1646 SkXfermode::Mode mode;
1647 getAlphaAndMode(paint, &alpha, &mode);
1648
Romain Guyada830f2011-01-13 12:13:20 -08001649 layer->alpha = alpha;
1650 layer->mode = mode;
Romain Guy6c319ca2011-01-11 14:29:25 -08001651
Romain Guyf219da52011-01-16 12:54:25 -08001652#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08001653 if (!layer->region.isEmpty()) {
1654 if (layer->region.isRect()) {
1655 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1656 composeLayerRect(layer, r);
1657 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08001658 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08001659 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08001660
Romain Guyc88e3572011-01-22 00:32:12 -08001661 setupDraw();
1662 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08001663 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08001664 setupDrawColorFilter();
1665 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
1666 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08001667 setupDrawPureColorUniforms();
1668 setupDrawColorFilterUniforms();
1669 setupDrawTexture(layer->texture);
Romain Guy4f09f542011-01-26 22:41:43 -08001670 // TODO: The current layer, if any, will be dirtied with the bounding box
1671 // of the layer we are drawing. Since the layer we are drawing has
1672 // a mesh, we know the dirty region, we should use it instead
Romain Guyc88e3572011-01-22 00:32:12 -08001673 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1674 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08001675
Romain Guyc88e3572011-01-22 00:32:12 -08001676 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
1677 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08001678
Romain Guyc88e3572011-01-22 00:32:12 -08001679 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08001680
1681#if DEBUG_LAYERS_AS_REGIONS
1682 drawRegionRects(layer->region);
1683#endif
Romain Guyc88e3572011-01-22 00:32:12 -08001684 }
Romain Guyf219da52011-01-16 12:54:25 -08001685 }
1686#else
Romain Guyada830f2011-01-13 12:13:20 -08001687 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1688 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08001689#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08001690}
1691
Romain Guy6926c722010-07-12 20:20:03 -07001692///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07001693// Shaders
1694///////////////////////////////////////////////////////////////////////////////
1695
1696void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07001697 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07001698}
1699
Romain Guy06f96e22010-07-30 19:18:16 -07001700void OpenGLRenderer::setupShader(SkiaShader* shader) {
1701 mShader = shader;
1702 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001703 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07001704 }
Romain Guy7fac2e12010-07-16 17:10:13 -07001705}
1706
Romain Guyd27977d2010-07-14 19:18:51 -07001707///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07001708// Color filters
1709///////////////////////////////////////////////////////////////////////////////
1710
1711void OpenGLRenderer::resetColorFilter() {
1712 mColorFilter = NULL;
1713}
1714
1715void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
1716 mColorFilter = filter;
1717}
1718
1719///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07001720// Drop shadow
1721///////////////////////////////////////////////////////////////////////////////
1722
1723void OpenGLRenderer::resetShadow() {
1724 mHasShadow = false;
1725}
1726
1727void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
1728 mHasShadow = true;
1729 mShadowRadius = radius;
1730 mShadowDx = dx;
1731 mShadowDy = dy;
1732 mShadowColor = color;
1733}
1734
1735///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07001736// Drawing implementation
1737///////////////////////////////////////////////////////////////////////////////
1738
Romain Guy01d58e42011-01-19 21:54:02 -08001739void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
1740 float x, float y, SkPaint* paint) {
1741 if (quickReject(x, y, x + texture->width, y + texture->height)) {
1742 return;
1743 }
1744
1745 int alpha;
1746 SkXfermode::Mode mode;
1747 getAlphaAndMode(paint, &alpha, &mode);
1748
1749 setupDraw();
1750 setupDrawWithTexture(true);
1751 setupDrawAlpha8Color(paint->getColor(), alpha);
1752 setupDrawColorFilter();
1753 setupDrawShader();
1754 setupDrawBlending(true, mode);
1755 setupDrawProgram();
1756 setupDrawModelView(x, y, x + texture->width, y + texture->height);
1757 setupDrawTexture(texture->id);
1758 setupDrawPureColorUniforms();
1759 setupDrawColorFilterUniforms();
1760 setupDrawShaderUniforms();
1761 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1762
1763 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1764
1765 finishDrawTexture();
1766}
1767
Romain Guyf607bdc2010-09-10 19:20:06 -07001768// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001769#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1770#define kStdUnderline_Offset (1.0f / 9.0f)
1771#define kStdUnderline_Thickness (1.0f / 18.0f)
1772
1773void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1774 float x, float y, SkPaint* paint) {
1775 // Handle underline and strike-through
1776 uint32_t flags = paint->getFlags();
1777 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1778 float underlineWidth = length;
1779 // If length is > 0.0f, we already measured the text for the text alignment
1780 if (length <= 0.0f) {
1781 underlineWidth = paint->measureText(text, bytesCount);
1782 }
1783
1784 float offsetX = 0;
1785 switch (paint->getTextAlign()) {
1786 case SkPaint::kCenter_Align:
1787 offsetX = underlineWidth * 0.5f;
1788 break;
1789 case SkPaint::kRight_Align:
1790 offsetX = underlineWidth;
1791 break;
1792 default:
1793 break;
1794 }
1795
1796 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001797 const float textSize = paint->getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08001798 // TODO: Support stroke width < 1.0f when we have AA lines
1799 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07001800
Romain Guye20ecbd2010-09-22 19:49:04 -07001801 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001802 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001803
Romain Guyf6834472011-01-23 13:32:12 -08001804 int linesCount = 0;
1805 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
1806 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
1807
1808 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07001809 float points[pointsCount];
1810 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001811
1812 if (flags & SkPaint::kUnderlineText_Flag) {
1813 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001814 points[currentPoint++] = left;
1815 points[currentPoint++] = top;
1816 points[currentPoint++] = left + underlineWidth;
1817 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001818 }
1819
1820 if (flags & SkPaint::kStrikeThruText_Flag) {
1821 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001822 points[currentPoint++] = left;
1823 points[currentPoint++] = top;
1824 points[currentPoint++] = left + underlineWidth;
1825 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001826 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001827
1828 SkPaint linesPaint(*paint);
1829 linesPaint.setStrokeWidth(strokeWidth);
1830
1831 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001832 }
1833 }
1834}
1835
Romain Guy026c5e162010-06-28 17:12:22 -07001836void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001837 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07001838 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001839 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001840 color |= 0x00ffffff;
1841 }
1842
Romain Guy70ca14e2010-12-13 18:24:33 -08001843 setupDraw();
1844 setupDrawColor(color);
1845 setupDrawShader();
1846 setupDrawColorFilter();
1847 setupDrawBlending(mode);
1848 setupDrawProgram();
1849 setupDrawModelView(left, top, right, bottom, ignoreTransform);
1850 setupDrawColorUniforms();
1851 setupDrawShaderUniforms(ignoreTransform);
1852 setupDrawColorFilterUniforms();
1853 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07001854
Romain Guyc95c8d62010-09-17 15:31:32 -07001855 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1856}
1857
Romain Guy82ba8142010-07-09 13:25:56 -07001858void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07001859 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001860 int alpha;
1861 SkXfermode::Mode mode;
1862 getAlphaAndMode(paint, &alpha, &mode);
1863
Romain Guy8164c2d2010-10-25 18:03:28 -07001864 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1865
Romain Guy6620c6d2010-12-06 18:07:02 -08001866 if (mSnapshot->transform->isPureTranslate()) {
1867 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1868 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1869
1870 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1871 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
1872 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
1873 } else {
1874 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
1875 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
1876 GL_TRIANGLE_STRIP, gMeshCount);
1877 }
Romain Guy85bf02f2010-06-22 13:11:24 -07001878}
1879
Romain Guybd6b79b2010-06-26 00:13:53 -07001880void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001881 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1882 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07001883 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001884}
1885
1886void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001887 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001888 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07001889 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001890
Romain Guy746b7402010-10-26 16:27:31 -07001891 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08001892 setupDrawWithTexture();
1893 setupDrawColor(alpha, alpha, alpha, alpha);
1894 setupDrawColorFilter();
1895 setupDrawBlending(blend, mode, swapSrcDst);
1896 setupDrawProgram();
1897 if (!dirty) {
1898 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07001899 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001900 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001901 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001902 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08001903 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001904 }
Romain Guy86568192010-12-14 15:55:39 -08001905 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08001906 setupDrawColorFilterUniforms();
1907 setupDrawTexture(texture);
1908 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07001909
Romain Guy6820ac82010-09-15 18:11:50 -07001910 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08001911
1912 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07001913}
1914
Romain Guya5aed0d2010-09-09 14:42:43 -07001915void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001916 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001917 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1918 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001919 if (mode < SkXfermode::kPlus_Mode) {
1920 if (!mCaches.blend) {
1921 glEnable(GL_BLEND);
1922 }
Romain Guy82ba8142010-07-09 13:25:56 -07001923
Romain Guyf607bdc2010-09-10 19:20:06 -07001924 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1925 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001926
Romain Guya5aed0d2010-09-09 14:42:43 -07001927 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1928 glBlendFunc(sourceMode, destMode);
1929 mCaches.lastSrcMode = sourceMode;
1930 mCaches.lastDstMode = destMode;
1931 }
1932 } else {
1933 // These blend modes are not supported by OpenGL directly and have
1934 // to be implemented using shaders. Since the shader will perform
1935 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07001936 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001937 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001938 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001939 }
1940
1941 if (mCaches.blend) {
1942 glDisable(GL_BLEND);
1943 }
1944 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001945 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001946 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001947 glDisable(GL_BLEND);
1948 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001949 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001950}
1951
Romain Guy889f8d12010-07-29 14:37:42 -07001952bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001953 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001954 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001955 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001956 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001957 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001958 }
Romain Guy6926c722010-07-12 20:20:03 -07001959 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001960}
1961
Romain Guy026c5e162010-06-28 17:12:22 -07001962void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001963 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001964 TextureVertex::setUV(v++, u1, v1);
1965 TextureVertex::setUV(v++, u2, v1);
1966 TextureVertex::setUV(v++, u1, v2);
1967 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001968}
1969
Chet Haase5c13d892010-10-08 08:37:55 -07001970void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07001971 if (paint) {
Romain Guy746b7402010-10-26 16:27:31 -07001972 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001973 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1974 if (!isMode) {
1975 // Assume SRC_OVER
1976 *mode = SkXfermode::kSrcOver_Mode;
1977 }
1978 } else {
1979 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001980 }
1981
1982 // Skia draws using the color's alpha channel if < 255
1983 // Otherwise, it uses the paint's alpha
1984 int color = paint->getColor();
1985 *alpha = (color >> 24) & 0xFF;
1986 if (*alpha == 255) {
1987 *alpha = paint->getAlpha();
1988 }
1989 } else {
1990 *mode = SkXfermode::kSrcOver_Mode;
1991 *alpha = 255;
1992 }
Romain Guy026c5e162010-06-28 17:12:22 -07001993}
1994
Romain Guya5aed0d2010-09-09 14:42:43 -07001995SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
Romain Guyb37cbec2011-02-24 17:21:29 -08001996 // In the future we should look at unifying the Porter-Duff modes and
1997 // SkXferModes so that we can use SkXfermode::IsMode(xfer, &mode).
Romain Guya5aed0d2010-09-09 14:42:43 -07001998 if (mode == NULL) {
1999 return SkXfermode::kSrcOver_Mode;
2000 }
2001 return mode->fMode;
2002}
2003
Romain Guy746b7402010-10-26 16:27:31 -07002004void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
Romain Guy8164c2d2010-10-25 18:03:28 -07002005 bool bound = false;
2006 if (wrapS != texture->wrapS) {
2007 glBindTexture(GL_TEXTURE_2D, texture->id);
2008 bound = true;
2009 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
2010 texture->wrapS = wrapS;
2011 }
2012 if (wrapT != texture->wrapT) {
Romain Guy3e3ba152010-10-25 18:33:16 -07002013 if (!bound) {
Romain Guy3e3ba152010-10-25 18:33:16 -07002014 glBindTexture(GL_TEXTURE_2D, texture->id);
2015 }
Romain Guy8164c2d2010-10-25 18:03:28 -07002016 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
2017 texture->wrapT = wrapT;
2018 }
Romain Guya1db5742010-07-20 13:09:13 -07002019}
2020
Romain Guy9d5316e2010-06-24 19:30:36 -07002021}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07002022}; // namespace android