blob: ad725844650972cd36f4d4b679bb061f7a53366a [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 Guye4d01122010-06-16 18:44:05 -070033
34namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070035namespace uirenderer {
36
37///////////////////////////////////////////////////////////////////////////////
38// Defines
39///////////////////////////////////////////////////////////////////////////////
40
Romain Guy759ea802010-09-16 20:49:46 -070041#define RAD_TO_DEG (180.0f / 3.14159265f)
42#define MIN_ANGLE 0.001f
43
Romain Guydbc26d22010-10-11 17:58:29 -070044// TODO: This should be set in properties
45#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH)
46
Romain Guy9d5316e2010-06-24 19:30:36 -070047///////////////////////////////////////////////////////////////////////////////
48// Globals
49///////////////////////////////////////////////////////////////////////////////
50
Romain Guy889f8d12010-07-29 14:37:42 -070051/**
52 * Structure mapping Skia xfermodes to OpenGL blending factors.
53 */
54struct Blender {
55 SkXfermode::Mode mode;
56 GLenum src;
57 GLenum dst;
58}; // struct Blender
59
Romain Guy026c5e162010-06-28 17:12:22 -070060// In this array, the index of each Blender equals the value of the first
61// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
62static const Blender gBlends[] = {
63 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
64 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
65 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
66 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
67 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
68 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
69 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
70 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
71 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
72 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
73 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
74 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
75};
Romain Guye4d01122010-06-16 18:44:05 -070076
Romain Guy87a76572010-09-13 18:11:21 -070077// This array contains the swapped version of each SkXfermode. For instance
78// this array's SrcOver blending mode is actually DstOver. You can refer to
79// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070080static const Blender gBlendsSwap[] = {
81 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
82 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
83 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
84 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
85 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
86 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
87 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
88 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
89 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
90 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
91 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
92 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
93};
94
Romain Guy889f8d12010-07-29 14:37:42 -070095static const GLenum gTextureUnits[] = {
Romain Guy06f96e22010-07-30 19:18:16 -070096 GL_TEXTURE0,
97 GL_TEXTURE1,
98 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -070099};
100
Romain Guyf6a11b82010-06-23 17:47:49 -0700101///////////////////////////////////////////////////////////////////////////////
102// Constructors/destructor
103///////////////////////////////////////////////////////////////////////////////
104
Romain Guyfb8b7632010-08-23 21:05:08 -0700105OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy06f96e22010-07-30 19:18:16 -0700106 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -0700107 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -0700108 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700109
Romain Guyac670c02010-07-27 17:39:27 -0700110 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
111
Romain Guyae5575b2010-07-29 18:48:04 -0700112 mFirstSnapshot = new Snapshot;
Romain Guye4d01122010-06-16 18:44:05 -0700113}
114
Romain Guy85bf02f2010-06-22 13:11:24 -0700115OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700116 // The context has already been destroyed at this point, do not call
117 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700118}
119
Romain Guyf6a11b82010-06-23 17:47:49 -0700120///////////////////////////////////////////////////////////////////////////////
121// Setup
122///////////////////////////////////////////////////////////////////////////////
123
Romain Guy85bf02f2010-06-22 13:11:24 -0700124void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700125 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700126 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700127
128 mWidth = width;
129 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700130
131 mFirstSnapshot->height = height;
132 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guy746b7402010-10-26 16:27:31 -0700133
134 mDirtyClip = false;
Romain Guye4d01122010-06-16 18:44:05 -0700135}
136
Romain Guy6b7bd242010-10-06 19:49:23 -0700137void OpenGLRenderer::prepare(bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800138 mCaches.clearGarbage();
139
Romain Guy8aef54f2010-09-01 15:13:49 -0700140 mSnapshot = new Snapshot(mFirstSnapshot,
141 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy8fb95422010-08-17 18:38:51 -0700142 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700143
Romain Guyfb8b7632010-08-23 21:05:08 -0700144 glViewport(0, 0, mWidth, mHeight);
145
Romain Guyd90f23e2010-09-09 11:47:54 -0700146 glDisable(GL_DITHER);
Romain Guybb9524b2010-06-22 18:56:38 -0700147
Romain Guy6b7bd242010-10-06 19:49:23 -0700148 if (!opaque) {
Romain Guy746b7402010-10-26 16:27:31 -0700149 glDisable(GL_SCISSOR_TEST);
Romain Guy6b7bd242010-10-06 19:49:23 -0700150 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
151 glClear(GL_COLOR_BUFFER_BIT);
152 }
Romain Guybb9524b2010-06-22 18:56:38 -0700153
Chet Haase0d200832010-11-05 15:36:16 -0700154 glEnable(GL_SCISSOR_TEST);
155 glScissor(0, 0, mWidth, mHeight);
Romain Guyb82da652010-07-30 11:36:12 -0700156 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700157}
158
Romain Guyb025b9c2010-09-16 14:16:48 -0700159void OpenGLRenderer::finish() {
160#if DEBUG_OPENGL
161 GLenum status = GL_NO_ERROR;
162 while ((status = glGetError()) != GL_NO_ERROR) {
163 LOGD("GL error from OpenGLRenderer: 0x%x", status);
164 }
165#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800166#if DEBUG_MEMORY_USAGE
167 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800168#else
169 if (mCaches.getDebugLevel() & kDebugMemory) {
170 mCaches.dumpMemoryUsage();
171 }
Romain Guyc15008e2010-11-10 11:59:15 -0800172#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700173}
174
Romain Guyda8532c2010-08-31 11:50:35 -0700175void OpenGLRenderer::acquireContext() {
176 if (mCaches.currentProgram) {
177 if (mCaches.currentProgram->isInUse()) {
178 mCaches.currentProgram->remove();
179 mCaches.currentProgram = NULL;
180 }
181 }
Romain Guy50c0f092010-10-19 11:42:22 -0700182 mCaches.unbindMeshBuffer();
Romain Guyda8532c2010-08-31 11:50:35 -0700183}
184
185void OpenGLRenderer::releaseContext() {
Romain Guyeb993562010-10-05 18:14:38 -0700186 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700187
188 glEnable(GL_SCISSOR_TEST);
Romain Guy746b7402010-10-26 16:27:31 -0700189 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700190
Romain Guyf607bdc2010-09-10 19:20:06 -0700191 glDisable(GL_DITHER);
192
193 glBindFramebuffer(GL_FRAMEBUFFER, 0);
Romain Guy03750a02010-10-18 14:06:08 -0700194 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700195
Romain Guy50c0f092010-10-19 11:42:22 -0700196 mCaches.blend = true;
197 glEnable(GL_BLEND);
198 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
199 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700200}
201
Romain Guyf6a11b82010-06-23 17:47:49 -0700202///////////////////////////////////////////////////////////////////////////////
203// State management
204///////////////////////////////////////////////////////////////////////////////
205
Romain Guybb9524b2010-06-22 18:56:38 -0700206int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700207 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700208}
209
210int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700211 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700212}
213
214void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700215 if (mSaveCount > 1) {
216 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700217 }
Romain Guybb9524b2010-06-22 18:56:38 -0700218}
219
220void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700221 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700222
Romain Guy8fb95422010-08-17 18:38:51 -0700223 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700224 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700225 }
Romain Guybb9524b2010-06-22 18:56:38 -0700226}
227
Romain Guy8aef54f2010-09-01 15:13:49 -0700228int OpenGLRenderer::saveSnapshot(int flags) {
229 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700230 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700231}
232
233bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700234 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700235 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700236 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700237
Romain Guybd6b79b2010-06-26 00:13:53 -0700238 sp<Snapshot> current = mSnapshot;
239 sp<Snapshot> previous = mSnapshot->previous;
240
Romain Guyeb993562010-10-05 18:14:38 -0700241 if (restoreOrtho) {
242 Rect& r = previous->viewport;
243 glViewport(r.left, r.top, r.right, r.bottom);
244 mOrthoMatrix.load(current->orthoMatrix);
245 }
246
Romain Guy8b55f372010-08-18 17:10:07 -0700247 mSaveCount--;
248 mSnapshot = previous;
249
Romain Guy2542d192010-08-18 11:47:12 -0700250 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700251 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700252 }
Romain Guy2542d192010-08-18 11:47:12 -0700253
Romain Guy5ec99242010-11-03 16:19:08 -0700254 if (restoreLayer) {
255 composeLayer(current, previous);
256 }
257
Romain Guy2542d192010-08-18 11:47:12 -0700258 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700259}
260
Romain Guyf6a11b82010-06-23 17:47:49 -0700261///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700262// Layers
263///////////////////////////////////////////////////////////////////////////////
264
265int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700266 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700267 const GLuint previousFbo = mSnapshot->fbo;
268 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700269
Romain Guye45362c2010-11-03 19:58:32 -0700270 if (!mSnapshot->invisible) {
271 int alpha = 255;
272 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700273
Romain Guye45362c2010-11-03 19:58:32 -0700274 if (p) {
275 alpha = p->getAlpha();
276 if (!mCaches.extensions.hasFramebufferFetch()) {
277 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
278 if (!isMode) {
279 // Assume SRC_OVER
280 mode = SkXfermode::kSrcOver_Mode;
281 }
282 } else {
283 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700284 }
285 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700286 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700287 }
Romain Guyd55a8612010-06-28 17:42:46 -0700288
Romain Guydbc26d22010-10-11 17:58:29 -0700289 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
290 }
Romain Guyd55a8612010-06-28 17:42:46 -0700291
292 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700293}
294
295int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
296 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700297 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700298 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700299 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700300 SkPaint paint;
301 paint.setAlpha(alpha);
302 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700303 }
Romain Guyd55a8612010-06-28 17:42:46 -0700304}
Romain Guybd6b79b2010-06-26 00:13:53 -0700305
Romain Guy1c740bc2010-09-13 18:00:09 -0700306/**
307 * Layers are viewed by Skia are slightly different than layers in image editing
308 * programs (for instance.) When a layer is created, previously created layers
309 * and the frame buffer still receive every drawing command. For instance, if a
310 * layer is created and a shape intersecting the bounds of the layers and the
311 * framebuffer is draw, the shape will be drawn on both (unless the layer was
312 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
313 *
314 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
315 * texture. Unfortunately, this is inefficient as it requires every primitive to
316 * be drawn n + 1 times, where n is the number of active layers. In practice this
317 * means, for every primitive:
318 * - Switch active frame buffer
319 * - Change viewport, clip and projection matrix
320 * - Issue the drawing
321 *
322 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700323 * To avoid this, layers are implemented in a different way here, at least in the
324 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
325 * is set. When this flag is set we can redirect all drawing operations into a
326 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700327 *
328 * This implementation relies on the frame buffer being at least RGBA 8888. When
329 * a layer is created, only a texture is created, not an FBO. The content of the
330 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700331 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700332 * buffer and drawing continues as normal. This technique therefore treats the
333 * frame buffer as a scratch buffer for the layers.
334 *
335 * To compose the layers back onto the frame buffer, each layer texture
336 * (containing the original frame buffer data) is drawn as a simple quad over
337 * the frame buffer. The trick is that the quad is set as the composition
338 * destination in the blending equation, and the frame buffer becomes the source
339 * of the composition.
340 *
341 * Drawing layers with an alpha value requires an extra step before composition.
342 * An empty quad is drawn over the layer's region in the frame buffer. This quad
343 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
344 * quad is used to multiply the colors in the frame buffer. This is achieved by
345 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
346 * GL_ZERO, GL_SRC_ALPHA.
347 *
348 * Because glCopyTexImage2D() can be slow, an alternative implementation might
349 * be use to draw a single clipped layer. The implementation described above
350 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700351 *
352 * (1) The frame buffer is actually not cleared right away. To allow the GPU
353 * to potentially optimize series of calls to glCopyTexImage2D, the frame
354 * buffer is left untouched until the first drawing operation. Only when
355 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700356 */
Romain Guyd55a8612010-06-28 17:42:46 -0700357bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700358 float right, float bottom, int alpha, SkXfermode::Mode mode,
359 int flags, GLuint previousFbo) {
360 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700361 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700362
Romain Guyeb993562010-10-05 18:14:38 -0700363 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
364
Romain Guyf607bdc2010-09-10 19:20:06 -0700365 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700366 Rect bounds(left, top, right, bottom);
Romain Guyae88e5e2010-10-22 17:49:18 -0700367 if (fboLayer) {
368 // Clear the previous layer regions before we change the viewport
369 clearLayerRegions();
370 } else {
Romain Guyeb993562010-10-05 18:14:38 -0700371 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700372
Romain Guyeb993562010-10-05 18:14:38 -0700373 // Layers only make sense if they are in the framebuffer's bounds
Romain Guy58ae6db2010-10-22 11:16:37 -0700374 bounds.intersect(*snapshot->clipRect);
Romain Guyae517592010-10-22 10:40:27 -0700375
Romain Guyae88e5e2010-10-22 17:49:18 -0700376 // We cannot work with sub-pixels in this case
377 bounds.snapToPixelBoundaries();
378
Romain Guyae517592010-10-22 10:40:27 -0700379 // When the layer is not an FBO, we may use glCopyTexImage so we
380 // need to make sure the layer does not extend outside the bounds
381 // of the framebuffer
382 bounds.intersect(snapshot->previous->viewport);
Romain Guyeb993562010-10-05 18:14:38 -0700383 }
Romain Guybf434112010-09-16 14:40:17 -0700384
Romain Guy746b7402010-10-26 16:27:31 -0700385 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
386 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guydbc26d22010-10-11 17:58:29 -0700387 snapshot->invisible = true;
388 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700389 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700390 }
391
392 // Bail out if we won't draw in this snapshot
393 if (snapshot->invisible) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700394 return false;
395 }
Romain Guyf18fd992010-07-08 11:45:51 -0700396
Romain Guy5b3b3522010-10-27 18:57:51 -0700397 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700398 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700399 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700400 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700401 }
402
Romain Guydda570202010-07-06 11:39:32 -0700403 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700404 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700405 layer->layer.set(bounds);
Romain Guy8550c4c2010-10-08 15:49:53 -0700406 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
407 bounds.getWidth() / float(layer->width), 0.0f);
Romain Guydda570202010-07-06 11:39:32 -0700408
Romain Guy8fb95422010-08-17 18:38:51 -0700409 // Save the layer in the snapshot
410 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700411 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700412
Romain Guyeb993562010-10-05 18:14:38 -0700413 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700414 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700415 } else {
416 // Copy the framebuffer into the layer
417 glBindTexture(GL_TEXTURE_2D, layer->texture);
418
Romain Guyae88e5e2010-10-22 17:49:18 -0700419 if (layer->empty) {
420 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left,
421 snapshot->height - bounds.bottom, layer->width, layer->height, 0);
422 layer->empty = false;
423 } else {
424 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
425 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
426 }
Romain Guyeb993562010-10-05 18:14:38 -0700427
428 // Enqueue the buffer coordinates to clear the corresponding region later
429 mLayers.push(new Rect(bounds));
430 }
Romain Guyf86ef572010-07-01 11:05:42 -0700431
Romain Guyd55a8612010-06-28 17:42:46 -0700432 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700433}
434
Romain Guy5b3b3522010-10-27 18:57:51 -0700435bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
436 GLuint previousFbo) {
437 layer->fbo = mCaches.fboCache.get();
438
439#if RENDER_LAYERS_AS_REGIONS
440 snapshot->region = &snapshot->layer->region;
441 snapshot->flags |= Snapshot::kFlagFboTarget;
442#endif
443
444 Rect clip(bounds);
445 snapshot->transform->mapRect(clip);
446 clip.intersect(*snapshot->clipRect);
447 clip.snapToPixelBoundaries();
448 clip.intersect(snapshot->previous->viewport);
449
450 mat4 inverse;
451 inverse.loadInverse(*mSnapshot->transform);
452
453 inverse.mapRect(clip);
454 clip.snapToPixelBoundaries();
455 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700456 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700457
458 snapshot->flags |= Snapshot::kFlagIsFboLayer;
459 snapshot->fbo = layer->fbo;
460 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
461 //snapshot->resetClip(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
462 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
463 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
464 snapshot->height = bounds.getHeight();
465 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
466 snapshot->orthoMatrix.load(mOrthoMatrix);
467
468 // Bind texture to FBO
469 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
470 glBindTexture(GL_TEXTURE_2D, layer->texture);
471
472 // Initialize the texture if needed
473 if (layer->empty) {
474 layer->empty = false;
475 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
476 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
477 }
478
479 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
480 layer->texture, 0);
481
482#if DEBUG_LAYERS_AS_REGIONS
483 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
484 if (status != GL_FRAMEBUFFER_COMPLETE) {
485 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
486
487 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
488 glDeleteTextures(1, &layer->texture);
489 mCaches.fboCache.put(layer->fbo);
490
491 delete layer;
492
493 return false;
494 }
495#endif
496
497 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
498 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
499 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
500 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
501 glClear(GL_COLOR_BUFFER_BIT);
502
503 dirtyClip();
504
505 // Change the ortho projection
506 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
507 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
508
509 return true;
510}
511
Romain Guy1c740bc2010-09-13 18:00:09 -0700512/**
513 * Read the documentation of createLayer() before doing anything in this method.
514 */
Romain Guy1d83e192010-08-17 11:37:00 -0700515void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
516 if (!current->layer) {
517 LOGE("Attempting to compose a layer that does not exist");
518 return;
519 }
520
Romain Guy5b3b3522010-10-27 18:57:51 -0700521 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700522
523 if (fboLayer) {
524 // Unbind current FBO and restore previous one
525 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
526 }
527
Romain Guy1d83e192010-08-17 11:37:00 -0700528 Layer* layer = current->layer;
529 const Rect& rect = layer->layer;
530
Romain Guyeb993562010-10-05 18:14:38 -0700531 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700532 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700533 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700534 // Required below, composeLayerRect() will divide by 255
535 layer->alpha = 255;
Romain Guyf607bdc2010-09-10 19:20:06 -0700536 }
537
Romain Guy03750a02010-10-18 14:06:08 -0700538 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700539
Romain Guy746b7402010-10-26 16:27:31 -0700540 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700541
Romain Guy5b3b3522010-10-27 18:57:51 -0700542 // When the layer is stored in an FBO, we can save a bit of fillrate by
543 // drawing only the dirty region
544 if (fboLayer) {
545 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
546 composeLayerRegion(layer, rect);
547 } else {
548 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
549 composeLayerRect(layer, rect, true);
550 }
Romain Guy8b55f372010-08-18 17:10:07 -0700551
Romain Guyeb993562010-10-05 18:14:38 -0700552 if (fboLayer) {
553 // Detach the texture from the FBO
554 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
555 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
556 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
557
558 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
559 mCaches.fboCache.put(current->fbo);
560 }
561
Romain Guy746b7402010-10-26 16:27:31 -0700562 dirtyClip();
563
Romain Guyeb993562010-10-05 18:14:38 -0700564 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700565 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700566 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700567 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700568 delete layer;
569 }
570}
571
Romain Guy5b3b3522010-10-27 18:57:51 -0700572void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
573 const Rect& texCoords = layer->texCoords;
574 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
575
576 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
577 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
578 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap);
579
580 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
581}
582
583void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
584#if RENDER_LAYERS_AS_REGIONS
585 if (layer->region.isRect()) {
586 composeLayerRect(layer, rect);
587 layer->region.clear();
588 return;
589 }
590
591 if (!layer->region.isEmpty()) {
592 size_t count;
593 const android::Rect* rects = layer->region.getArray(&count);
594
595 setupDraw();
596
597 ProgramDescription description;
598 description.hasTexture = true;
599
600 const float alpha = layer->alpha / 255.0f;
601 const bool setColor = description.setColor(alpha, alpha, alpha, alpha);
602 chooseBlending(layer->blend || layer->alpha < 255, layer->mode, description, false);
603
604 useProgram(mCaches.programCache.get(description));
605
606 // Texture
607 bindTexture(layer->texture);
608 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
609
610 // Always premultiplied
611 if (setColor) {
612 mCaches.currentProgram->setColor(alpha, alpha, alpha, alpha);
613 }
614
615 // Mesh
616 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
617 glEnableVertexAttribArray(texCoordsSlot);
618
619 mModelView.loadIdentity();
620 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
621
622 const float texX = 1.0f / float(layer->width);
623 const float texY = 1.0f / float(layer->height);
624
625 TextureVertex* mesh = mCaches.getRegionMesh();
626 GLsizei numQuads = 0;
627
628 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
629 gMeshStride, &mesh[0].position[0]);
630 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
631 gMeshStride, &mesh[0].texture[0]);
632
633 for (size_t i = 0; i < count; i++) {
634 const android::Rect* r = &rects[i];
635
636 const float u1 = r->left * texX;
637 const float v1 = (rect.getHeight() - r->top) * texY;
638 const float u2 = r->right * texX;
639 const float v2 = (rect.getHeight() - r->bottom) * texY;
640
641 // TODO: Reject quads outside of the clip
642 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
643 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
644 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
645 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
646
647 numQuads++;
648
649 if (numQuads >= REGION_MESH_QUAD_COUNT) {
650 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
651 numQuads = 0;
652 mesh = mCaches.getRegionMesh();
653 }
654 }
655
656 if (numQuads > 0) {
657 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
658 }
659
660 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
661 glDisableVertexAttribArray(texCoordsSlot);
662
663#if DEBUG_LAYERS_AS_REGIONS
664 uint32_t colors[] = {
665 0x7fff0000, 0x7f00ff00,
666 0x7f0000ff, 0x7fff00ff,
667 };
668
669 int offset = 0;
670 int32_t top = rects[0].top;
671 int i = 0;
672
673 for (size_t i = 0; i < count; i++) {
674 if (top != rects[i].top) {
675 offset ^= 0x2;
676 top = rects[i].top;
677 }
678
679 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
680 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
681 SkXfermode::kSrcOver_Mode);
682 }
683#endif
684
685 layer->region.clear();
686 }
687#else
688 composeLayerRect(layer, rect);
689#endif
690}
691
692void OpenGLRenderer::dirtyLayer(const float left, const float top,
693 const float right, const float bottom, const mat4 transform) {
694#if RENDER_LAYERS_AS_REGIONS
695 if ((mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region) {
696 Rect bounds(left, top, right, bottom);
697 transform.mapRect(bounds);
698 bounds.intersect(*mSnapshot->clipRect);
699 bounds.snapToPixelBoundaries();
700
701 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
702 if (!dirty.isEmpty()) {
703 mSnapshot->region->orSelf(dirty);
704 }
705 }
706#endif
707}
708
709void OpenGLRenderer::dirtyLayer(const float left, const float top,
710 const float right, const float bottom) {
711#if RENDER_LAYERS_AS_REGIONS
712 if ((mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region) {
713 Rect bounds(left, top, right, bottom);
714 bounds.intersect(*mSnapshot->clipRect);
715 bounds.snapToPixelBoundaries();
716
717 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
718 if (!dirty.isEmpty()) {
719 mSnapshot->region->orSelf(dirty);
720 }
721 }
722#endif
723}
724
Romain Guy746b7402010-10-26 16:27:31 -0700725void OpenGLRenderer::setupDraw() {
726 clearLayerRegions();
727 if (mDirtyClip) {
728 setScissorFromClip();
729 }
730}
731
Romain Guy86942302010-09-12 13:02:16 -0700732void OpenGLRenderer::clearLayerRegions() {
Romain Guydbc26d22010-10-11 17:58:29 -0700733 if (mLayers.size() == 0 || mSnapshot->invisible) return;
Romain Guy86942302010-09-12 13:02:16 -0700734
Romain Guy5b3b3522010-10-27 18:57:51 -0700735 Rect clipRect(*mSnapshot->clipRect);
736 clipRect.snapToPixelBoundaries();
737
Romain Guy86942302010-09-12 13:02:16 -0700738 for (uint32_t i = 0; i < mLayers.size(); i++) {
739 Rect* bounds = mLayers.itemAt(i);
Romain Guy5b3b3522010-10-27 18:57:51 -0700740 if (clipRect.intersects(*bounds)) {
741 // Clear the framebuffer where the layer will draw
742 glScissor(bounds->left, mSnapshot->height - bounds->bottom,
743 bounds->getWidth(), bounds->getHeight());
744 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
745 glClear(GL_COLOR_BUFFER_BIT);
Romain Guy86942302010-09-12 13:02:16 -0700746
Romain Guy5b3b3522010-10-27 18:57:51 -0700747 // Restore the clip
748 dirtyClip();
749 }
Romain Guy86942302010-09-12 13:02:16 -0700750
751 delete bounds;
752 }
Romain Guy86942302010-09-12 13:02:16 -0700753
Romain Guy5b3b3522010-10-27 18:57:51 -0700754 mLayers.clear();
Romain Guy86942302010-09-12 13:02:16 -0700755}
756
Romain Guybd6b79b2010-06-26 00:13:53 -0700757///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700758// Transforms
759///////////////////////////////////////////////////////////////////////////////
760
761void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700762 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700763}
764
765void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700766 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700767}
768
769void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700770 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700771}
772
773void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700774 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700775}
776
Romain Guy41030da2010-10-13 13:40:37 -0700777const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700778 if (mSnapshot->fbo != 0) {
779 return &mSnapshot->transform->data[0];
780 }
781 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700782}
783
Romain Guyf6a11b82010-06-23 17:47:49 -0700784void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700785 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700786}
787
788void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700789 SkMatrix transform;
790 mSnapshot->transform->copyTo(transform);
791 transform.preConcat(*matrix);
792 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700793}
794
795///////////////////////////////////////////////////////////////////////////////
796// Clipping
797///////////////////////////////////////////////////////////////////////////////
798
Romain Guybb9524b2010-06-22 18:56:38 -0700799void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700800 Rect clip(*mSnapshot->clipRect);
801 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700802 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700803 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700804}
805
806const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700807 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700808}
809
Romain Guyc7d53492010-06-25 13:41:57 -0700810bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guydbc26d22010-10-11 17:58:29 -0700811 if (mSnapshot->invisible) {
812 return true;
813 }
814
Romain Guy1d83e192010-08-17 11:37:00 -0700815 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700816 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700817 r.snapToPixelBoundaries();
818
819 Rect clipRect(*mSnapshot->clipRect);
820 clipRect.snapToPixelBoundaries();
821
822 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700823}
824
Romain Guy079ba2c2010-07-16 14:12:24 -0700825bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
826 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700827 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700828 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700829 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700830 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700831}
832
Romain Guyf6a11b82010-06-23 17:47:49 -0700833///////////////////////////////////////////////////////////////////////////////
834// Drawing
835///////////////////////////////////////////////////////////////////////////////
836
Romain Guy0fe478e2010-11-08 12:08:41 -0800837void OpenGLRenderer::drawDisplayList(DisplayList* displayList) {
838 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
839 // will be performed by the display list itself
840 if (displayList) {
841 displayList->replay(*this);
842 }
843}
844
Chet Haase5c13d892010-10-08 08:37:55 -0700845void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700846 const float right = left + bitmap->width();
847 const float bottom = top + bitmap->height();
848
849 if (quickReject(left, top, right, bottom)) {
850 return;
851 }
852
Romain Guy61c8c9c2010-08-09 20:48:09 -0700853 glActiveTexture(GL_TEXTURE0);
Romain Guy8164c2d2010-10-25 18:03:28 -0700854 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700855 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700856 const AutoTexture autoCleanup(texture);
857
Romain Guy6926c722010-07-12 20:20:03 -0700858 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700859}
860
Chet Haase5c13d892010-10-08 08:37:55 -0700861void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -0700862 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
863 const mat4 transform(*matrix);
864 transform.mapRect(r);
865
Romain Guy6926c722010-07-12 20:20:03 -0700866 if (quickReject(r.left, r.top, r.right, r.bottom)) {
867 return;
868 }
869
Romain Guy61c8c9c2010-08-09 20:48:09 -0700870 glActiveTexture(GL_TEXTURE0);
Romain Guy8164c2d2010-10-25 18:03:28 -0700871 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700872 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700873 const AutoTexture autoCleanup(texture);
874
Romain Guy5b3b3522010-10-27 18:57:51 -0700875 // This could be done in a cheaper way, all we need is pass the matrix
876 // to the vertex shader. The save/restore is a bit overkill.
877 save(SkCanvas::kMatrix_SaveFlag);
878 concatMatrix(matrix);
879 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
880 restore();
Romain Guyf86ef572010-07-01 11:05:42 -0700881}
882
Romain Guy8ba548f2010-06-30 19:21:21 -0700883void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
884 float srcLeft, float srcTop, float srcRight, float srcBottom,
885 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700886 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700887 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
888 return;
889 }
890
Romain Guy746b7402010-10-26 16:27:31 -0700891 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -0700892 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700893 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700894 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -0700895 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guy8ba548f2010-06-30 19:21:21 -0700896
Romain Guy8ba548f2010-06-30 19:21:21 -0700897 const float width = texture->width;
898 const float height = texture->height;
899
900 const float u1 = srcLeft / width;
901 const float v1 = srcTop / height;
902 const float u2 = srcRight / width;
903 const float v2 = srcBottom / height;
904
Romain Guy03750a02010-10-18 14:06:08 -0700905 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -0700906 resetDrawTextureTexCoords(u1, v1, u2, v2);
907
Romain Guy03750a02010-10-18 14:06:08 -0700908 int alpha;
909 SkXfermode::Mode mode;
910 getAlphaAndMode(paint, &alpha, &mode);
911
912 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
913 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
914 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy8ba548f2010-06-30 19:21:21 -0700915
916 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
917}
918
Romain Guy4aa90572010-09-26 18:40:37 -0700919void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -0700920 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -0700921 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700922 if (quickReject(left, top, right, bottom)) {
923 return;
924 }
925
Romain Guy746b7402010-10-26 16:27:31 -0700926 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -0700927 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700928 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700929 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -0700930 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guyf7f93552010-07-08 19:17:03 -0700931
932 int alpha;
933 SkXfermode::Mode mode;
934 getAlphaAndMode(paint, &alpha, &mode);
935
Romain Guy2728f962010-10-08 18:36:15 -0700936 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -0700937 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -0700938
Romain Guy054dc182010-10-15 17:55:25 -0700939 if (mesh) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700940 // Mark the current layer dirty where we are going to draw the patch
941 if ((mSnapshot->flags & Snapshot::kFlagFboTarget) &&
942 mSnapshot->region && mesh->hasEmptyQuads) {
943 const size_t count = mesh->quads.size();
944 for (size_t i = 0; i < count; i++) {
945 Rect bounds = mesh->quads.itemAt(i);
946 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom,
947 *mSnapshot->transform);
948 }
949 }
950
951 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
Romain Guy03750a02010-10-18 14:06:08 -0700952 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
Romain Guy5b3b3522010-10-27 18:57:51 -0700953 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
954 true, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -0700955 }
Romain Guyf7f93552010-07-08 19:17:03 -0700956}
957
Chet Haase5c13d892010-10-08 08:37:55 -0700958void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Romain Guy7f78b0c2010-11-04 14:36:26 -0700959 // TODO: Should do quickReject for each line
Romain Guydbc26d22010-10-11 17:58:29 -0700960 if (mSnapshot->invisible) return;
961
Romain Guy7f78b0c2010-11-04 14:36:26 -0700962 setupDraw();
963
Romain Guy759ea802010-09-16 20:49:46 -0700964 int alpha;
965 SkXfermode::Mode mode;
966 getAlphaAndMode(paint, &alpha, &mode);
967
968 uint32_t color = paint->getColor();
969 const GLfloat a = alpha / 255.0f;
970 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
971 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
972 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
973
Romain Guyc95c8d62010-09-17 15:31:32 -0700974 const bool isAA = paint->isAntiAlias();
975 if (isAA) {
976 GLuint textureUnit = 0;
Romain Guy746b7402010-10-26 16:27:31 -0700977 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy29d89972010-09-22 16:10:57 -0700978 setupTextureAlpha8(mCaches.line.getTexture(), 0, 0, textureUnit, 0.0f, 0.0f, r, g, b, a,
Romain Guy03750a02010-10-18 14:06:08 -0700979 mode, false, true, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
980 mCaches.line.getMeshBuffer());
Romain Guyc95c8d62010-09-17 15:31:32 -0700981 } else {
Romain Guy7f78b0c2010-11-04 14:36:26 -0700982 setupColorRect(0.0f, 0.0f, 1.0f, 1.0f, r, g, b, a, mode, false, true);
Romain Guyc95c8d62010-09-17 15:31:32 -0700983 }
984
985 const float strokeWidth = paint->getStrokeWidth();
Romain Guy29d89972010-09-22 16:10:57 -0700986 const GLsizei elementsCount = isAA ? mCaches.line.getElementsCount() : gMeshCount;
Romain Guyc95c8d62010-09-17 15:31:32 -0700987 const GLenum drawMode = isAA ? GL_TRIANGLES : GL_TRIANGLE_STRIP;
Romain Guy759ea802010-09-16 20:49:46 -0700988
989 for (int i = 0; i < count; i += 4) {
990 float tx = 0.0f;
991 float ty = 0.0f;
992
Romain Guyc95c8d62010-09-17 15:31:32 -0700993 if (isAA) {
Romain Guy29d89972010-09-22 16:10:57 -0700994 mCaches.line.update(points[i], points[i + 1], points[i + 2], points[i + 3],
Romain Guyc95c8d62010-09-17 15:31:32 -0700995 strokeWidth, tx, ty);
996 } else {
Romain Guyb5ab4172010-09-17 15:36:56 -0700997 ty = strokeWidth <= 1.0f ? 0.0f : -strokeWidth * 0.5f;
Romain Guyc95c8d62010-09-17 15:31:32 -0700998 }
Romain Guy759ea802010-09-16 20:49:46 -0700999
1000 const float dx = points[i + 2] - points[i];
1001 const float dy = points[i + 3] - points[i + 1];
1002 const float mag = sqrtf(dx * dx + dy * dy);
1003 const float angle = acos(dx / mag);
1004
1005 mModelView.loadTranslate(points[i], points[i + 1], 0.0f);
1006 if (angle > MIN_ANGLE || angle < -MIN_ANGLE) {
1007 mModelView.rotate(angle * RAD_TO_DEG, 0.0f, 0.0f, 1.0f);
1008 }
1009 mModelView.translate(tx, ty, 0.0f);
Romain Guyc95c8d62010-09-17 15:31:32 -07001010 if (!isAA) {
Romain Guy29d89972010-09-22 16:10:57 -07001011 float length = mCaches.line.getLength(points[i], points[i + 1],
1012 points[i + 2], points[i + 3]);
Romain Guyc95c8d62010-09-17 15:31:32 -07001013 mModelView.scale(length, strokeWidth, 1.0f);
1014 }
Romain Guy759ea802010-09-16 20:49:46 -07001015 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001016 // TODO: Add bounds to the layer's region
Romain Guy759ea802010-09-16 20:49:46 -07001017
1018 if (mShader) {
1019 mShader->updateTransforms(mCaches.currentProgram, mModelView, *mSnapshot);
1020 }
1021
Romain Guyc95c8d62010-09-17 15:31:32 -07001022 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy759ea802010-09-16 20:49:46 -07001023 }
1024
Romain Guy29d89972010-09-22 16:10:57 -07001025 if (isAA) {
1026 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
1027 }
Romain Guy759ea802010-09-16 20:49:46 -07001028}
1029
Romain Guy85bf02f2010-06-22 13:11:24 -07001030void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001031 // No need to check against the clip, we fill the clip region
1032 if (mSnapshot->invisible) return;
1033
Romain Guyae88e5e2010-10-22 17:49:18 -07001034 Rect& clip(*mSnapshot->clipRect);
1035 clip.snapToPixelBoundaries();
Romain Guy3d58c032010-07-14 16:34:53 -07001036 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001037}
Romain Guy9d5316e2010-06-24 19:30:36 -07001038
Chet Haase5c13d892010-10-08 08:37:55 -07001039void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -07001040 if (quickReject(left, top, right, bottom)) {
1041 return;
1042 }
1043
Romain Guy026c5e162010-06-28 17:12:22 -07001044 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07001045 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001046 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1047 if (!isMode) {
1048 // Assume SRC_OVER
1049 mode = SkXfermode::kSrcOver_Mode;
1050 }
1051 } else {
1052 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07001053 }
1054
1055 // Skia draws using the color's alpha channel if < 255
1056 // Otherwise, it uses the paint's alpha
1057 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07001058 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -07001059 color |= p->getAlpha() << 24;
1060 }
1061
1062 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -07001063}
Romain Guy9d5316e2010-06-24 19:30:36 -07001064
Romain Guye8e62a42010-07-23 18:55:21 -07001065void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1066 float x, float y, SkPaint* paint) {
Romain Guyf607bdc2010-09-10 19:20:06 -07001067 if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -07001068 return;
1069 }
Romain Guydbc26d22010-10-11 17:58:29 -07001070 if (mSnapshot->invisible) return;
Romain Guye2d345e2010-09-24 18:39:22 -07001071
Romain Guyf607bdc2010-09-10 19:20:06 -07001072 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -07001073
Romain Guya674ab72010-08-10 17:26:42 -07001074 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07001075 switch (paint->getTextAlign()) {
1076 case SkPaint::kCenter_Align:
1077 length = paint->measureText(text, bytesCount);
1078 x -= length / 2.0f;
1079 break;
1080 case SkPaint::kRight_Align:
1081 length = paint->measureText(text, bytesCount);
1082 x -= length;
1083 break;
1084 default:
1085 break;
1086 }
1087
Romain Guy694b5192010-07-21 21:33:20 -07001088 int alpha;
1089 SkXfermode::Mode mode;
1090 getAlphaAndMode(paint, &alpha, &mode);
1091
Romain Guy2542d192010-08-18 11:47:12 -07001092 uint32_t color = paint->getColor();
1093 const GLfloat a = alpha / 255.0f;
1094 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1095 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1096 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1097
Romain Guyb45c0c92010-08-26 20:35:23 -07001098 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
1099 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07001100 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07001101
Romain Guy746b7402010-10-26 16:27:31 -07001102 setupDraw();
Romain Guy9d13fe252010-10-15 16:06:03 -07001103
Romain Guy1e45aae2010-08-13 19:39:53 -07001104 if (mHasShadow) {
1105 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -07001106 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -07001107 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -07001108 count, mShadowRadius);
1109 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07001110
Romain Guy2542d192010-08-18 11:47:12 -07001111 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -07001112
1113 // Draw the mesh
1114 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -07001115 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -07001116 }
1117
Romain Guy06f96e22010-07-30 19:18:16 -07001118 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -07001119 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -07001120
Romain Guye8cb9c142010-10-04 14:14:11 -07001121 // Assume that the modelView matrix does not force scales, rotates, etc.
1122 const bool linearFilter = mSnapshot->transform->changesBounds();
Romain Guy5b3b3522010-10-27 18:57:51 -07001123
1124 // Dimensions are set to (0,0), the layer (if any) won't be dirtied
Romain Guye8cb9c142010-10-04 14:14:11 -07001125 setupTextureAlpha8(fontRenderer.getTexture(linearFilter), 0, 0, textureUnit,
Romain Guy03750a02010-10-18 14:06:08 -07001126 x, y, r, g, b, a, mode, false, true, NULL, NULL);
Romain Guy06f96e22010-07-30 19:18:16 -07001127
Romain Guy09147fb2010-07-22 13:08:20 -07001128 const Rect& clip = mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07001129 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
1130
1131#if RENDER_LAYERS_AS_REGIONS
1132 bool hasLayer = (mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region;
1133#else
1134 bool hasLayer = false;
1135#endif
Romain Guy9d13fe252010-10-15 16:06:03 -07001136
Romain Guy03750a02010-10-18 14:06:08 -07001137 mCaches.unbindMeshBuffer();
Romain Guy5b3b3522010-10-27 18:57:51 -07001138 if (fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y,
1139 hasLayer ? &bounds : NULL)) {
1140#if RENDER_LAYERS_AS_REGIONS
1141 if (hasLayer) {
1142 mSnapshot->transform->mapRect(bounds);
1143 bounds.intersect(*mSnapshot->clipRect);
1144 bounds.snapToPixelBoundaries();
1145
1146 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1147 mSnapshot->region->orSelf(dirty);
1148 }
1149#endif
1150 }
Romain Guy694b5192010-07-21 21:33:20 -07001151
1152 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001153 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07001154
Romain Guy0a417492010-08-16 20:26:20 -07001155 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guy694b5192010-07-21 21:33:20 -07001156}
1157
Romain Guy7fbcc042010-08-04 15:40:07 -07001158void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guydbc26d22010-10-11 17:58:29 -07001159 if (mSnapshot->invisible) return;
1160
Romain Guy7fbcc042010-08-04 15:40:07 -07001161 GLuint textureUnit = 0;
1162 glActiveTexture(gTextureUnits[textureUnit]);
1163
Romain Guyfb8b7632010-08-23 21:05:08 -07001164 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001165 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001166 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07001167
Romain Guy8b55f372010-08-18 17:10:07 -07001168 const float x = texture->left - texture->offset;
1169 const float y = texture->top - texture->offset;
1170
1171 if (quickReject(x, y, x + texture->width, y + texture->height)) {
1172 return;
1173 }
1174
Romain Guy7fbcc042010-08-04 15:40:07 -07001175 int alpha;
1176 SkXfermode::Mode mode;
1177 getAlphaAndMode(paint, &alpha, &mode);
1178
1179 uint32_t color = paint->getColor();
1180 const GLfloat a = alpha / 255.0f;
1181 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1182 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1183 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1184
Romain Guy0a417492010-08-16 20:26:20 -07001185 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
1186
Romain Guy746b7402010-10-26 16:27:31 -07001187 setupDraw();
Romain Guy86942302010-09-12 13:02:16 -07001188
Romain Guy0a417492010-08-16 20:26:20 -07001189 // Draw the mesh
1190 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -07001191 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -07001192}
1193
Romain Guy6926c722010-07-12 20:20:03 -07001194///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07001195// Shaders
1196///////////////////////////////////////////////////////////////////////////////
1197
1198void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07001199 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07001200}
1201
Romain Guy06f96e22010-07-30 19:18:16 -07001202void OpenGLRenderer::setupShader(SkiaShader* shader) {
1203 mShader = shader;
1204 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001205 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07001206 }
Romain Guy7fac2e12010-07-16 17:10:13 -07001207}
1208
Romain Guyd27977d2010-07-14 19:18:51 -07001209///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07001210// Color filters
1211///////////////////////////////////////////////////////////////////////////////
1212
1213void OpenGLRenderer::resetColorFilter() {
1214 mColorFilter = NULL;
1215}
1216
1217void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
1218 mColorFilter = filter;
1219}
1220
1221///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07001222// Drop shadow
1223///////////////////////////////////////////////////////////////////////////////
1224
1225void OpenGLRenderer::resetShadow() {
1226 mHasShadow = false;
1227}
1228
1229void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
1230 mHasShadow = true;
1231 mShadowRadius = radius;
1232 mShadowDx = dx;
1233 mShadowDy = dy;
1234 mShadowColor = color;
1235}
1236
1237///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07001238// Drawing implementation
1239///////////////////////////////////////////////////////////////////////////////
1240
Romain Guy0a417492010-08-16 20:26:20 -07001241void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -07001242 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -07001243 const float sx = x - texture->left + mShadowDx;
1244 const float sy = y - texture->top + mShadowDy;
1245
Romain Guy2542d192010-08-18 11:47:12 -07001246 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
1247 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -07001248 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
1249 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
1250 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
1251
1252 GLuint textureUnit = 0;
1253 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
1254}
1255
1256void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
1257 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
1258 bool transforms, bool applyFilters) {
1259 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
Romain Guy759ea802010-09-16 20:49:46 -07001260 x, y, r, g, b, a, mode, transforms, applyFilters,
Romain Guy03750a02010-10-18 14:06:08 -07001261 (GLvoid*) 0, (GLvoid*) gMeshTextureOffset);
Romain Guy0a417492010-08-16 20:26:20 -07001262}
1263
1264void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
1265 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
1266 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
Romain Guy03750a02010-10-18 14:06:08 -07001267 setupTextureAlpha8(texture, width, height, textureUnit, x, y, r, g, b, a, mode,
1268 transforms, applyFilters, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset);
Romain Guy759ea802010-09-16 20:49:46 -07001269}
1270
1271void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
1272 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
1273 SkXfermode::Mode mode, bool transforms, bool applyFilters,
Romain Guy03750a02010-10-18 14:06:08 -07001274 GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guy0a417492010-08-16 20:26:20 -07001275 // Describe the required shaders
1276 ProgramDescription description;
1277 description.hasTexture = true;
1278 description.hasAlpha8Texture = true;
Romain Guy707b2f72010-10-11 16:34:59 -07001279 const bool setColor = description.setAlpha8Color(r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -07001280
1281 if (applyFilters) {
1282 if (mShader) {
Romain Guy746b7402010-10-26 16:27:31 -07001283 mShader->describe(description, mCaches.extensions);
Romain Guy0a417492010-08-16 20:26:20 -07001284 }
1285 if (mColorFilter) {
Romain Guy746b7402010-10-26 16:27:31 -07001286 mColorFilter->describe(description, mCaches.extensions);
Romain Guy0a417492010-08-16 20:26:20 -07001287 }
1288 }
1289
Romain Guya5aed0d2010-09-09 14:42:43 -07001290 // Setup the blending mode
1291 chooseBlending(true, mode, description);
1292
Romain Guy0a417492010-08-16 20:26:20 -07001293 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001294 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -07001295
Romain Guy746b7402010-10-26 16:27:31 -07001296 bindTexture(texture);
Romain Guyfb8b7632010-08-23 21:05:08 -07001297 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -07001298
Romain Guyfb8b7632010-08-23 21:05:08 -07001299 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -07001300 glEnableVertexAttribArray(texCoordsSlot);
1301
Romain Guy03750a02010-10-18 14:06:08 -07001302 if (texCoords) {
1303 // Setup attributes
1304 if (!vertices) {
1305 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1306 } else {
1307 mCaches.unbindMeshBuffer();
1308 }
1309 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1310 gMeshStride, vertices);
1311 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1312 }
Romain Guy0a417492010-08-16 20:26:20 -07001313
1314 // Setup uniforms
1315 if (transforms) {
1316 mModelView.loadTranslate(x, y, 0.0f);
1317 mModelView.scale(width, height, 1.0f);
1318 } else {
1319 mModelView.loadIdentity();
1320 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001321
Romain Guy8aef54f2010-09-01 15:13:49 -07001322 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001323 if (width > 0 && height > 0) {
1324 dirtyLayer(x, y, x + width, y + height, *mSnapshot->transform);
1325 }
1326
Romain Guy707b2f72010-10-11 16:34:59 -07001327 if (setColor) {
1328 mCaches.currentProgram->setColor(r, g, b, a);
1329 }
Romain Guy0a417492010-08-16 20:26:20 -07001330
1331 textureUnit++;
1332 if (applyFilters) {
1333 // Setup attributes and uniforms required by the shaders
1334 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001335 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -07001336 }
1337 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001338 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -07001339 }
1340 }
1341}
1342
Romain Guyf607bdc2010-09-10 19:20:06 -07001343// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001344#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1345#define kStdUnderline_Offset (1.0f / 9.0f)
1346#define kStdUnderline_Thickness (1.0f / 18.0f)
1347
1348void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1349 float x, float y, SkPaint* paint) {
1350 // Handle underline and strike-through
1351 uint32_t flags = paint->getFlags();
1352 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1353 float underlineWidth = length;
1354 // If length is > 0.0f, we already measured the text for the text alignment
1355 if (length <= 0.0f) {
1356 underlineWidth = paint->measureText(text, bytesCount);
1357 }
1358
1359 float offsetX = 0;
1360 switch (paint->getTextAlign()) {
1361 case SkPaint::kCenter_Align:
1362 offsetX = underlineWidth * 0.5f;
1363 break;
1364 case SkPaint::kRight_Align:
1365 offsetX = underlineWidth;
1366 break;
1367 default:
1368 break;
1369 }
1370
1371 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001372 const float textSize = paint->getTextSize();
1373 const float strokeWidth = textSize * kStdUnderline_Thickness;
Romain Guy0a417492010-08-16 20:26:20 -07001374
Romain Guye20ecbd2010-09-22 19:49:04 -07001375 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001376 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001377
1378 const int pointsCount = 4 * (flags & SkPaint::kStrikeThruText_Flag ? 2 : 1);
1379 float points[pointsCount];
1380 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001381
1382 if (flags & SkPaint::kUnderlineText_Flag) {
1383 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001384 points[currentPoint++] = left;
1385 points[currentPoint++] = top;
1386 points[currentPoint++] = left + underlineWidth;
1387 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001388 }
1389
1390 if (flags & SkPaint::kStrikeThruText_Flag) {
1391 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001392 points[currentPoint++] = left;
1393 points[currentPoint++] = top;
1394 points[currentPoint++] = left + underlineWidth;
1395 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001396 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001397
1398 SkPaint linesPaint(*paint);
1399 linesPaint.setStrokeWidth(strokeWidth);
1400
1401 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001402 }
1403 }
1404}
1405
Romain Guy026c5e162010-06-28 17:12:22 -07001406void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001407 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guy746b7402010-10-26 16:27:31 -07001408 setupDraw();
Romain Guy86942302010-09-12 13:02:16 -07001409
Romain Guyd27977d2010-07-14 19:18:51 -07001410 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001411 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001412 color |= 0x00ffffff;
1413 }
1414
1415 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -07001416 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -07001417 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -07001418 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
1419 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
1420 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
1421
Romain Guyc95c8d62010-09-17 15:31:32 -07001422 setupColorRect(left, top, right, bottom, r, g, b, a, mode, ignoreTransform);
1423
1424 // Draw the mesh
1425 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1426}
1427
1428void OpenGLRenderer::setupColorRect(float left, float top, float right, float bottom,
Romain Guy7f78b0c2010-11-04 14:36:26 -07001429 float r, float g, float b, float a, SkXfermode::Mode mode,
1430 bool ignoreTransform, bool ignoreMatrix) {
Romain Guy06f96e22010-07-30 19:18:16 -07001431 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -07001432
Romain Guy06f96e22010-07-30 19:18:16 -07001433 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -07001434 ProgramDescription description;
Romain Guy707b2f72010-10-11 16:34:59 -07001435 const bool setColor = description.setColor(r, g, b, a);
1436
Romain Guy06f96e22010-07-30 19:18:16 -07001437 if (mShader) {
Romain Guy746b7402010-10-26 16:27:31 -07001438 mShader->describe(description, mCaches.extensions);
Romain Guy6926c722010-07-12 20:20:03 -07001439 }
Romain Guydb1938e2010-08-02 18:50:22 -07001440 if (mColorFilter) {
Romain Guy746b7402010-10-26 16:27:31 -07001441 mColorFilter->describe(description, mCaches.extensions);
Romain Guydb1938e2010-08-02 18:50:22 -07001442 }
Romain Guyd27977d2010-07-14 19:18:51 -07001443
Romain Guy1c740bc2010-09-13 18:00:09 -07001444 // Setup the blending mode
Romain Guyc95c8d62010-09-17 15:31:32 -07001445 chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description);
Romain Guya5aed0d2010-09-09 14:42:43 -07001446
Romain Guy06f96e22010-07-30 19:18:16 -07001447 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -07001448 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -07001449
1450 // Setup attributes
Romain Guy03750a02010-10-18 14:06:08 -07001451 mCaches.bindMeshBuffer();
Romain Guyfb8b7632010-08-23 21:05:08 -07001452 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy03750a02010-10-18 14:06:08 -07001453 gMeshStride, 0);
Romain Guy06f96e22010-07-30 19:18:16 -07001454
Romain Guy7f78b0c2010-11-04 14:36:26 -07001455 if (!ignoreMatrix) {
1456 // Setup uniforms
1457 mModelView.loadTranslate(left, top, 0.0f);
1458 mModelView.scale(right - left, bottom - top, 1.0f);
1459 if (!ignoreTransform) {
1460 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
1461 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1462 } else {
1463 mat4 identity;
1464 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
1465 dirtyLayer(left, top, right, bottom);
1466 }
Romain Guyd27977d2010-07-14 19:18:51 -07001467 }
Romain Guy01d06572010-11-11 12:06:27 -08001468 if (!mShader || (mShader && setColor)) {
1469 mCaches.currentProgram->setColor(r, g, b, a);
1470 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001471
Romain Guy06f96e22010-07-30 19:18:16 -07001472 // Setup attributes and uniforms required by the shaders
1473 if (mShader) {
Romain Guy7f78b0c2010-11-04 14:36:26 -07001474 if (ignoreMatrix) mModelView.loadIdentity();
Romain Guyfb8b7632010-08-23 21:05:08 -07001475 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -07001476 }
Romain Guydb1938e2010-08-02 18:50:22 -07001477 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001478 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001479 }
Romain Guyd27977d2010-07-14 19:18:51 -07001480}
1481
Romain Guy82ba8142010-07-09 13:25:56 -07001482void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07001483 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001484 int alpha;
1485 SkXfermode::Mode mode;
1486 getAlphaAndMode(paint, &alpha, &mode);
1487
Romain Guy8164c2d2010-10-25 18:03:28 -07001488 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1489
Romain Guy6820ac82010-09-15 18:11:50 -07001490 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy03750a02010-10-18 14:06:08 -07001491 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
Romain Guy6820ac82010-09-15 18:11:50 -07001492 GL_TRIANGLE_STRIP, gMeshCount);
Romain Guy85bf02f2010-06-22 13:11:24 -07001493}
1494
Romain Guybd6b79b2010-06-26 00:13:53 -07001495void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001496 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1497 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07001498 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001499}
1500
1501void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001502 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001503 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07001504 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy746b7402010-10-26 16:27:31 -07001505 setupDraw();
Romain Guy86942302010-09-12 13:02:16 -07001506
Romain Guy889f8d12010-07-29 14:37:42 -07001507 ProgramDescription description;
1508 description.hasTexture = true;
Romain Guy707b2f72010-10-11 16:34:59 -07001509 const bool setColor = description.setColor(alpha, alpha, alpha, alpha);
Romain Guydb1938e2010-08-02 18:50:22 -07001510 if (mColorFilter) {
Romain Guy746b7402010-10-26 16:27:31 -07001511 mColorFilter->describe(description, mCaches.extensions);
Romain Guydb1938e2010-08-02 18:50:22 -07001512 }
Romain Guy889f8d12010-07-29 14:37:42 -07001513
Romain Guybd6b79b2010-06-26 00:13:53 -07001514 mModelView.loadTranslate(left, top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -07001515 if (!ignoreScale) {
1516 mModelView.scale(right - left, bottom - top, 1.0f);
1517 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001518
Romain Guyf607bdc2010-09-10 19:20:06 -07001519 chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst);
Romain Guya5aed0d2010-09-09 14:42:43 -07001520
Romain Guyfb8b7632010-08-23 21:05:08 -07001521 useProgram(mCaches.programCache.get(description));
Romain Guyf607bdc2010-09-10 19:20:06 -07001522 if (!ignoreTransform) {
1523 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001524 if (dirty) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001525 } else {
Romain Guy5b3b3522010-10-27 18:57:51 -07001526 mat4 identity;
1527 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
1528 if (dirty) dirtyLayer(left, top, right, bottom);
Romain Guyf607bdc2010-09-10 19:20:06 -07001529 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001530
Romain Guy889f8d12010-07-29 14:37:42 -07001531 // Texture
Romain Guy8164c2d2010-10-25 18:03:28 -07001532 bindTexture(texture);
Romain Guyfb8b7632010-08-23 21:05:08 -07001533 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -07001534
Romain Guya9794742010-07-13 11:37:54 -07001535 // Always premultiplied
Romain Guy707b2f72010-10-11 16:34:59 -07001536 if (setColor) {
1537 mCaches.currentProgram->setColor(alpha, alpha, alpha, alpha);
1538 }
Romain Guybd6b79b2010-06-26 00:13:53 -07001539
Romain Guy889f8d12010-07-29 14:37:42 -07001540 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -07001541 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -07001542 glEnableVertexAttribArray(texCoordsSlot);
Romain Guy03750a02010-10-18 14:06:08 -07001543
1544 if (!vertices) {
1545 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1546 } else {
1547 mCaches.unbindMeshBuffer();
1548 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001549 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -07001550 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -07001551 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -07001552
Romain Guydb1938e2010-08-02 18:50:22 -07001553 // Color filter
1554 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001555 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -07001556 }
1557
Romain Guy6820ac82010-09-15 18:11:50 -07001558 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy889f8d12010-07-29 14:37:42 -07001559 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -07001560}
1561
Romain Guya5aed0d2010-09-09 14:42:43 -07001562void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001563 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001564 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1565 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001566 if (mode < SkXfermode::kPlus_Mode) {
1567 if (!mCaches.blend) {
1568 glEnable(GL_BLEND);
1569 }
Romain Guy82ba8142010-07-09 13:25:56 -07001570
Romain Guyf607bdc2010-09-10 19:20:06 -07001571 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1572 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001573
Romain Guya5aed0d2010-09-09 14:42:43 -07001574 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1575 glBlendFunc(sourceMode, destMode);
1576 mCaches.lastSrcMode = sourceMode;
1577 mCaches.lastDstMode = destMode;
1578 }
1579 } else {
1580 // These blend modes are not supported by OpenGL directly and have
1581 // to be implemented using shaders. Since the shader will perform
1582 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07001583 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001584 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001585 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001586 }
1587
1588 if (mCaches.blend) {
1589 glDisable(GL_BLEND);
1590 }
1591 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001592 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001593 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001594 glDisable(GL_BLEND);
1595 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001596 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001597}
1598
Romain Guy889f8d12010-07-29 14:37:42 -07001599bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001600 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001601 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001602 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001603 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001604 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001605 }
Romain Guy6926c722010-07-12 20:20:03 -07001606 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001607}
1608
Romain Guy026c5e162010-06-28 17:12:22 -07001609void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001610 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001611 TextureVertex::setUV(v++, u1, v1);
1612 TextureVertex::setUV(v++, u2, v1);
1613 TextureVertex::setUV(v++, u1, v2);
1614 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001615}
1616
Chet Haase5c13d892010-10-08 08:37:55 -07001617void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07001618 if (paint) {
Romain Guy746b7402010-10-26 16:27:31 -07001619 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001620 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1621 if (!isMode) {
1622 // Assume SRC_OVER
1623 *mode = SkXfermode::kSrcOver_Mode;
1624 }
1625 } else {
1626 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001627 }
1628
1629 // Skia draws using the color's alpha channel if < 255
1630 // Otherwise, it uses the paint's alpha
1631 int color = paint->getColor();
1632 *alpha = (color >> 24) & 0xFF;
1633 if (*alpha == 255) {
1634 *alpha = paint->getAlpha();
1635 }
1636 } else {
1637 *mode = SkXfermode::kSrcOver_Mode;
1638 *alpha = 255;
1639 }
Romain Guy026c5e162010-06-28 17:12:22 -07001640}
1641
Romain Guya5aed0d2010-09-09 14:42:43 -07001642SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1643 if (mode == NULL) {
1644 return SkXfermode::kSrcOver_Mode;
1645 }
1646 return mode->fMode;
1647}
1648
Romain Guy746b7402010-10-26 16:27:31 -07001649void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
Romain Guy8164c2d2010-10-25 18:03:28 -07001650 bool bound = false;
1651 if (wrapS != texture->wrapS) {
1652 glBindTexture(GL_TEXTURE_2D, texture->id);
1653 bound = true;
1654 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1655 texture->wrapS = wrapS;
1656 }
1657 if (wrapT != texture->wrapT) {
Romain Guy3e3ba152010-10-25 18:33:16 -07001658 if (!bound) {
Romain Guy3e3ba152010-10-25 18:33:16 -07001659 glBindTexture(GL_TEXTURE_2D, texture->id);
1660 }
Romain Guy8164c2d2010-10-25 18:03:28 -07001661 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1662 texture->wrapT = wrapT;
1663 }
Romain Guya1db5742010-07-20 13:09:13 -07001664}
1665
Romain Guy9d5316e2010-06-24 19:30:36 -07001666}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001667}; // namespace android