blob: 4813e931eff751097a37d5571d164d63cc1452df [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::acquireContext() {
195 interrupt();
196}
197
198void OpenGLRenderer::resume() {
Romain Guyeb993562010-10-05 18:14:38 -0700199 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700200
201 glEnable(GL_SCISSOR_TEST);
Romain Guy746b7402010-10-26 16:27:31 -0700202 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700203
Romain Guyf607bdc2010-09-10 19:20:06 -0700204 glDisable(GL_DITHER);
205
Romain Guy42f3a4b2011-01-19 13:42:26 -0800206 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy03750a02010-10-18 14:06:08 -0700207 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700208
Romain Guy50c0f092010-10-19 11:42:22 -0700209 mCaches.blend = true;
210 glEnable(GL_BLEND);
211 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
212 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700213}
214
Romain Guy6c319ca2011-01-11 14:29:25 -0800215void OpenGLRenderer::releaseContext() {
216 resume();
217}
218
Chet Haasedaf98e92011-01-10 14:10:36 -0800219bool OpenGLRenderer::callDrawGLFunction(Functor *functor) {
220 interrupt();
221 status_t result = (*functor)();
222 resume();
223 return (result == 0) ? false : true;
224}
225
Romain Guyf6a11b82010-06-23 17:47:49 -0700226///////////////////////////////////////////////////////////////////////////////
227// State management
228///////////////////////////////////////////////////////////////////////////////
229
Romain Guybb9524b2010-06-22 18:56:38 -0700230int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700231 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700232}
233
234int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700235 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700236}
237
238void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700239 if (mSaveCount > 1) {
240 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700241 }
Romain Guybb9524b2010-06-22 18:56:38 -0700242}
243
244void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700245 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700246
Romain Guy8fb95422010-08-17 18:38:51 -0700247 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700248 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700249 }
Romain Guybb9524b2010-06-22 18:56:38 -0700250}
251
Romain Guy8aef54f2010-09-01 15:13:49 -0700252int OpenGLRenderer::saveSnapshot(int flags) {
253 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700254 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700255}
256
257bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700258 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700259 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700260 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700261
Romain Guybd6b79b2010-06-26 00:13:53 -0700262 sp<Snapshot> current = mSnapshot;
263 sp<Snapshot> previous = mSnapshot->previous;
264
Romain Guyeb993562010-10-05 18:14:38 -0700265 if (restoreOrtho) {
266 Rect& r = previous->viewport;
267 glViewport(r.left, r.top, r.right, r.bottom);
268 mOrthoMatrix.load(current->orthoMatrix);
269 }
270
Romain Guy8b55f372010-08-18 17:10:07 -0700271 mSaveCount--;
272 mSnapshot = previous;
273
Romain Guy2542d192010-08-18 11:47:12 -0700274 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700275 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700276 }
Romain Guy2542d192010-08-18 11:47:12 -0700277
Romain Guy5ec99242010-11-03 16:19:08 -0700278 if (restoreLayer) {
279 composeLayer(current, previous);
280 }
281
Romain Guy2542d192010-08-18 11:47:12 -0700282 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700283}
284
Romain Guyf6a11b82010-06-23 17:47:49 -0700285///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700286// Layers
287///////////////////////////////////////////////////////////////////////////////
288
289int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700290 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700291 const GLuint previousFbo = mSnapshot->fbo;
292 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700293
Romain Guyaf636eb2010-12-09 17:47:21 -0800294 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700295 int alpha = 255;
296 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700297
Romain Guye45362c2010-11-03 19:58:32 -0700298 if (p) {
299 alpha = p->getAlpha();
300 if (!mCaches.extensions.hasFramebufferFetch()) {
301 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
302 if (!isMode) {
303 // Assume SRC_OVER
304 mode = SkXfermode::kSrcOver_Mode;
305 }
306 } else {
307 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700308 }
309 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700310 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700311 }
Romain Guyd55a8612010-06-28 17:42:46 -0700312
Romain Guydbc26d22010-10-11 17:58:29 -0700313 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
314 }
Romain Guyd55a8612010-06-28 17:42:46 -0700315
316 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700317}
318
319int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
320 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700321 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700322 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700323 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700324 SkPaint paint;
325 paint.setAlpha(alpha);
326 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700327 }
Romain Guyd55a8612010-06-28 17:42:46 -0700328}
Romain Guybd6b79b2010-06-26 00:13:53 -0700329
Romain Guy1c740bc2010-09-13 18:00:09 -0700330/**
331 * Layers are viewed by Skia are slightly different than layers in image editing
332 * programs (for instance.) When a layer is created, previously created layers
333 * and the frame buffer still receive every drawing command. For instance, if a
334 * layer is created and a shape intersecting the bounds of the layers and the
335 * framebuffer is draw, the shape will be drawn on both (unless the layer was
336 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
337 *
338 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
339 * texture. Unfortunately, this is inefficient as it requires every primitive to
340 * be drawn n + 1 times, where n is the number of active layers. In practice this
341 * means, for every primitive:
342 * - Switch active frame buffer
343 * - Change viewport, clip and projection matrix
344 * - Issue the drawing
345 *
346 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700347 * To avoid this, layers are implemented in a different way here, at least in the
348 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
349 * is set. When this flag is set we can redirect all drawing operations into a
350 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700351 *
352 * This implementation relies on the frame buffer being at least RGBA 8888. When
353 * a layer is created, only a texture is created, not an FBO. The content of the
354 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700355 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700356 * buffer and drawing continues as normal. This technique therefore treats the
357 * frame buffer as a scratch buffer for the layers.
358 *
359 * To compose the layers back onto the frame buffer, each layer texture
360 * (containing the original frame buffer data) is drawn as a simple quad over
361 * the frame buffer. The trick is that the quad is set as the composition
362 * destination in the blending equation, and the frame buffer becomes the source
363 * of the composition.
364 *
365 * Drawing layers with an alpha value requires an extra step before composition.
366 * An empty quad is drawn over the layer's region in the frame buffer. This quad
367 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
368 * quad is used to multiply the colors in the frame buffer. This is achieved by
369 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
370 * GL_ZERO, GL_SRC_ALPHA.
371 *
372 * Because glCopyTexImage2D() can be slow, an alternative implementation might
373 * be use to draw a single clipped layer. The implementation described above
374 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700375 *
376 * (1) The frame buffer is actually not cleared right away. To allow the GPU
377 * to potentially optimize series of calls to glCopyTexImage2D, the frame
378 * buffer is left untouched until the first drawing operation. Only when
379 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700380 */
Romain Guyd55a8612010-06-28 17:42:46 -0700381bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700382 float right, float bottom, int alpha, SkXfermode::Mode mode,
383 int flags, GLuint previousFbo) {
384 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700385 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700386
Romain Guyeb993562010-10-05 18:14:38 -0700387 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
388
Romain Guyf607bdc2010-09-10 19:20:06 -0700389 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700390 Rect bounds(left, top, right, bottom);
Romain Guyae88e5e2010-10-22 17:49:18 -0700391 if (fboLayer) {
392 // Clear the previous layer regions before we change the viewport
393 clearLayerRegions();
394 } else {
Romain Guyeb993562010-10-05 18:14:38 -0700395 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700396
Romain Guyeb993562010-10-05 18:14:38 -0700397 // Layers only make sense if they are in the framebuffer's bounds
Romain Guy58ae6db2010-10-22 11:16:37 -0700398 bounds.intersect(*snapshot->clipRect);
Romain Guyae517592010-10-22 10:40:27 -0700399
Romain Guyae88e5e2010-10-22 17:49:18 -0700400 // We cannot work with sub-pixels in this case
401 bounds.snapToPixelBoundaries();
402
Romain Guyae517592010-10-22 10:40:27 -0700403 // When the layer is not an FBO, we may use glCopyTexImage so we
404 // need to make sure the layer does not extend outside the bounds
405 // of the framebuffer
406 bounds.intersect(snapshot->previous->viewport);
Romain Guyeb993562010-10-05 18:14:38 -0700407 }
Romain Guybf434112010-09-16 14:40:17 -0700408
Romain Guy746b7402010-10-26 16:27:31 -0700409 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
410 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800411 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700412 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700413 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700414 }
415
416 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800417 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700418 return false;
419 }
Romain Guyf18fd992010-07-08 11:45:51 -0700420
Romain Guy5b3b3522010-10-27 18:57:51 -0700421 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700422 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700423 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700424 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700425 }
426
Romain Guydda570202010-07-06 11:39:32 -0700427 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700428 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700429 layer->layer.set(bounds);
Romain Guy8550c4c2010-10-08 15:49:53 -0700430 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
431 bounds.getWidth() / float(layer->width), 0.0f);
Romain Guy171c5922011-01-06 10:04:23 -0800432 layer->colorFilter = mColorFilter;
Romain Guydda570202010-07-06 11:39:32 -0700433
Romain Guy8fb95422010-08-17 18:38:51 -0700434 // Save the layer in the snapshot
435 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700436 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700437
Romain Guyeb993562010-10-05 18:14:38 -0700438 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700439 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700440 } else {
441 // Copy the framebuffer into the layer
442 glBindTexture(GL_TEXTURE_2D, layer->texture);
Romain Guy514fb182011-01-19 14:38:29 -0800443 if (!bounds.isEmpty()) {
444 if (layer->empty) {
445 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left,
446 snapshot->height - bounds.bottom, layer->width, layer->height, 0);
447 layer->empty = false;
448 } else {
449 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
450 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
451 }
452 // Enqueue the buffer coordinates to clear the corresponding region later
453 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700454 }
Romain Guyeb993562010-10-05 18:14:38 -0700455 }
Romain Guyf86ef572010-07-01 11:05:42 -0700456
Romain Guyd55a8612010-06-28 17:42:46 -0700457 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700458}
459
Romain Guy5b3b3522010-10-27 18:57:51 -0700460bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
461 GLuint previousFbo) {
462 layer->fbo = mCaches.fboCache.get();
463
464#if RENDER_LAYERS_AS_REGIONS
465 snapshot->region = &snapshot->layer->region;
466 snapshot->flags |= Snapshot::kFlagFboTarget;
467#endif
468
469 Rect clip(bounds);
470 snapshot->transform->mapRect(clip);
471 clip.intersect(*snapshot->clipRect);
472 clip.snapToPixelBoundaries();
473 clip.intersect(snapshot->previous->viewport);
474
475 mat4 inverse;
476 inverse.loadInverse(*mSnapshot->transform);
477
478 inverse.mapRect(clip);
479 clip.snapToPixelBoundaries();
480 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700481 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700482
483 snapshot->flags |= Snapshot::kFlagIsFboLayer;
484 snapshot->fbo = layer->fbo;
485 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700486 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
487 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
488 snapshot->height = bounds.getHeight();
489 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
490 snapshot->orthoMatrix.load(mOrthoMatrix);
491
492 // Bind texture to FBO
493 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
494 glBindTexture(GL_TEXTURE_2D, layer->texture);
495
496 // Initialize the texture if needed
497 if (layer->empty) {
498 layer->empty = false;
499 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
500 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
501 }
502
503 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
504 layer->texture, 0);
505
506#if DEBUG_LAYERS_AS_REGIONS
507 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
508 if (status != GL_FRAMEBUFFER_COMPLETE) {
509 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
510
511 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
512 glDeleteTextures(1, &layer->texture);
513 mCaches.fboCache.put(layer->fbo);
514
515 delete layer;
516
517 return false;
518 }
519#endif
520
521 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
522 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
523 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
524 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
525 glClear(GL_COLOR_BUFFER_BIT);
526
527 dirtyClip();
528
529 // Change the ortho projection
530 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
531 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
532
533 return true;
534}
535
Romain Guy1c740bc2010-09-13 18:00:09 -0700536/**
537 * Read the documentation of createLayer() before doing anything in this method.
538 */
Romain Guy1d83e192010-08-17 11:37:00 -0700539void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
540 if (!current->layer) {
541 LOGE("Attempting to compose a layer that does not exist");
542 return;
543 }
544
Romain Guy5b3b3522010-10-27 18:57:51 -0700545 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700546
547 if (fboLayer) {
548 // Unbind current FBO and restore previous one
549 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
550 }
551
Romain Guy1d83e192010-08-17 11:37:00 -0700552 Layer* layer = current->layer;
553 const Rect& rect = layer->layer;
554
Romain Guyeb993562010-10-05 18:14:38 -0700555 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700556 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700557 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700558 // Required below, composeLayerRect() will divide by 255
559 layer->alpha = 255;
Romain Guyf607bdc2010-09-10 19:20:06 -0700560 }
561
Romain Guy03750a02010-10-18 14:06:08 -0700562 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700563
Romain Guy746b7402010-10-26 16:27:31 -0700564 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700565
Romain Guy5b3b3522010-10-27 18:57:51 -0700566 // When the layer is stored in an FBO, we can save a bit of fillrate by
567 // drawing only the dirty region
568 if (fboLayer) {
569 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy171c5922011-01-06 10:04:23 -0800570 if (layer->colorFilter) {
571 setupColorFilter(layer->colorFilter);
572 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700573 composeLayerRegion(layer, rect);
Romain Guy171c5922011-01-06 10:04:23 -0800574 if (layer->colorFilter) {
575 resetColorFilter();
576 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700577 } else {
Romain Guy514fb182011-01-19 14:38:29 -0800578 if (!rect.isEmpty()) {
579 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
580 composeLayerRect(layer, rect, true);
581 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700582 }
Romain Guy8b55f372010-08-18 17:10:07 -0700583
Romain Guyeb993562010-10-05 18:14:38 -0700584 if (fboLayer) {
585 // Detach the texture from the FBO
586 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
587 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
588 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
589
590 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
591 mCaches.fboCache.put(current->fbo);
592 }
593
Romain Guy746b7402010-10-26 16:27:31 -0700594 dirtyClip();
595
Romain Guyeb993562010-10-05 18:14:38 -0700596 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700597 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700598 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700599 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700600 delete layer;
601 }
602}
603
Romain Guy5b3b3522010-10-27 18:57:51 -0700604void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
605 const Rect& texCoords = layer->texCoords;
606 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
607
608 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
609 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
610 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap);
611
612 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
613}
614
615void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
616#if RENDER_LAYERS_AS_REGIONS
617 if (layer->region.isRect()) {
618 composeLayerRect(layer, rect);
619 layer->region.clear();
620 return;
621 }
622
623 if (!layer->region.isEmpty()) {
624 size_t count;
625 const android::Rect* rects = layer->region.getArray(&count);
626
Romain Guy5b3b3522010-10-27 18:57:51 -0700627 const float alpha = layer->alpha / 255.0f;
Romain Guy5b3b3522010-10-27 18:57:51 -0700628 const float texX = 1.0f / float(layer->width);
629 const float texY = 1.0f / float(layer->height);
Romain Guyf219da52011-01-16 12:54:25 -0800630 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700631
632 TextureVertex* mesh = mCaches.getRegionMesh();
633 GLsizei numQuads = 0;
634
Romain Guy7230a742011-01-10 22:26:16 -0800635 setupDraw();
636 setupDrawWithTexture();
637 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800638 setupDrawColorFilter();
Romain Guy7230a742011-01-10 22:26:16 -0800639 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
640 setupDrawProgram();
641 setupDrawDirtyRegionsDisabled();
642 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800643 setupDrawColorFilterUniforms();
Romain Guy7230a742011-01-10 22:26:16 -0800644 setupDrawTexture(layer->texture);
Romain Guyc038ea32011-01-12 15:08:47 -0800645 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
Romain Guy7230a742011-01-10 22:26:16 -0800646 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700647
648 for (size_t i = 0; i < count; i++) {
649 const android::Rect* r = &rects[i];
650
651 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800652 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700653 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800654 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700655
656 // TODO: Reject quads outside of the clip
657 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
658 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
659 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
660 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
661
662 numQuads++;
663
664 if (numQuads >= REGION_MESH_QUAD_COUNT) {
665 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
666 numQuads = 0;
667 mesh = mCaches.getRegionMesh();
668 }
669 }
670
671 if (numQuads > 0) {
672 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
673 }
674
675 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800676 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700677
678#if DEBUG_LAYERS_AS_REGIONS
679 uint32_t colors[] = {
680 0x7fff0000, 0x7f00ff00,
681 0x7f0000ff, 0x7fff00ff,
682 };
683
684 int offset = 0;
685 int32_t top = rects[0].top;
686 int i = 0;
687
688 for (size_t i = 0; i < count; i++) {
689 if (top != rects[i].top) {
690 offset ^= 0x2;
691 top = rects[i].top;
692 }
693
694 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
695 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
696 SkXfermode::kSrcOver_Mode);
697 }
698#endif
699
700 layer->region.clear();
701 }
702#else
703 composeLayerRect(layer, rect);
704#endif
705}
706
707void OpenGLRenderer::dirtyLayer(const float left, const float top,
708 const float right, const float bottom, const mat4 transform) {
709#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800710 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700711 Rect bounds(left, top, right, bottom);
712 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800713 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700714 }
715#endif
716}
717
718void OpenGLRenderer::dirtyLayer(const float left, const float top,
719 const float right, const float bottom) {
720#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800721 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700722 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800723 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800724 }
725#endif
726}
727
728void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
729#if RENDER_LAYERS_AS_REGIONS
730 if (bounds.intersect(*mSnapshot->clipRect)) {
731 bounds.snapToPixelBoundaries();
732 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
733 if (!dirty.isEmpty()) {
734 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700735 }
736 }
737#endif
738}
739
Romain Guy86942302010-09-12 13:02:16 -0700740void OpenGLRenderer::clearLayerRegions() {
Romain Guyaf636eb2010-12-09 17:47:21 -0800741 if (mLayers.size() == 0 || mSnapshot->isIgnored()) return;
Romain Guy86942302010-09-12 13:02:16 -0700742
Romain Guy5b3b3522010-10-27 18:57:51 -0700743 Rect clipRect(*mSnapshot->clipRect);
744 clipRect.snapToPixelBoundaries();
745
Romain Guy86942302010-09-12 13:02:16 -0700746 for (uint32_t i = 0; i < mLayers.size(); i++) {
747 Rect* bounds = mLayers.itemAt(i);
Romain Guy5b3b3522010-10-27 18:57:51 -0700748 if (clipRect.intersects(*bounds)) {
749 // Clear the framebuffer where the layer will draw
750 glScissor(bounds->left, mSnapshot->height - bounds->bottom,
751 bounds->getWidth(), bounds->getHeight());
752 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
753 glClear(GL_COLOR_BUFFER_BIT);
Romain Guy86942302010-09-12 13:02:16 -0700754
Romain Guy5b3b3522010-10-27 18:57:51 -0700755 // Restore the clip
756 dirtyClip();
757 }
Romain Guy86942302010-09-12 13:02:16 -0700758
759 delete bounds;
760 }
Romain Guy86942302010-09-12 13:02:16 -0700761
Romain Guy5b3b3522010-10-27 18:57:51 -0700762 mLayers.clear();
Romain Guy86942302010-09-12 13:02:16 -0700763}
764
Romain Guybd6b79b2010-06-26 00:13:53 -0700765///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700766// Transforms
767///////////////////////////////////////////////////////////////////////////////
768
769void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700770 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700771}
772
773void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700774 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700775}
776
777void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700778 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700779}
780
Romain Guy807daf72011-01-18 11:19:19 -0800781void OpenGLRenderer::skew(float sx, float sy) {
782 mSnapshot->transform->skew(sx, sy);
783}
784
Romain Guyf6a11b82010-06-23 17:47:49 -0700785void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700786 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700787}
788
Romain Guy41030da2010-10-13 13:40:37 -0700789const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700790 if (mSnapshot->fbo != 0) {
791 return &mSnapshot->transform->data[0];
792 }
793 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700794}
795
Romain Guyf6a11b82010-06-23 17:47:49 -0700796void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700797 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700798}
799
800void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700801 SkMatrix transform;
802 mSnapshot->transform->copyTo(transform);
803 transform.preConcat(*matrix);
804 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700805}
806
807///////////////////////////////////////////////////////////////////////////////
808// Clipping
809///////////////////////////////////////////////////////////////////////////////
810
Romain Guybb9524b2010-06-22 18:56:38 -0700811void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700812 Rect clip(*mSnapshot->clipRect);
813 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700814 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700815 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700816}
817
818const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700819 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700820}
821
Romain Guyc7d53492010-06-25 13:41:57 -0700822bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800823 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700824 return true;
825 }
826
Romain Guy1d83e192010-08-17 11:37:00 -0700827 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700828 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700829 r.snapToPixelBoundaries();
830
831 Rect clipRect(*mSnapshot->clipRect);
832 clipRect.snapToPixelBoundaries();
833
834 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700835}
836
Romain Guy079ba2c2010-07-16 14:12:24 -0700837bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
838 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700839 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700840 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700841 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700842 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700843}
844
Romain Guyf6a11b82010-06-23 17:47:49 -0700845///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -0800846// Drawing commands
847///////////////////////////////////////////////////////////////////////////////
848
849void OpenGLRenderer::setupDraw() {
850 clearLayerRegions();
851 if (mDirtyClip) {
852 setScissorFromClip();
853 }
854 mDescription.reset();
855 mSetShaderColor = false;
856 mColorSet = false;
857 mColorA = mColorR = mColorG = mColorB = 0.0f;
858 mTextureUnit = 0;
859 mTrackDirtyRegions = true;
Romain Guy8d0d4782010-12-14 20:13:35 -0800860 mTexCoordsSlot = -1;
Romain Guy70ca14e2010-12-13 18:24:33 -0800861}
862
863void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
864 mDescription.hasTexture = true;
865 mDescription.hasAlpha8Texture = isAlpha8;
866}
867
868void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -0800869 setupDrawColor(color, (color >> 24) & 0xFF);
870}
871
872void OpenGLRenderer::setupDrawColor(int color, int alpha) {
873 mColorA = alpha / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -0800874 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -0800875 mColorR = a * ((color >> 16) & 0xFF);
876 mColorG = a * ((color >> 8) & 0xFF);
877 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -0800878 mColorSet = true;
879 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
880}
881
Romain Guy86568192010-12-14 15:55:39 -0800882void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
883 mColorA = alpha / 255.0f;
884 const float a = mColorA / 255.0f;
885 mColorR = a * ((color >> 16) & 0xFF);
886 mColorG = a * ((color >> 8) & 0xFF);
887 mColorB = a * ((color ) & 0xFF);
888 mColorSet = true;
889 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
890}
891
Romain Guy70ca14e2010-12-13 18:24:33 -0800892void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
893 mColorA = a;
894 mColorR = r;
895 mColorG = g;
896 mColorB = b;
897 mColorSet = true;
898 mSetShaderColor = mDescription.setColor(r, g, b, a);
899}
900
Romain Guy86568192010-12-14 15:55:39 -0800901void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
902 mColorA = a;
903 mColorR = r;
904 mColorG = g;
905 mColorB = b;
906 mColorSet = true;
907 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
908}
909
Romain Guy70ca14e2010-12-13 18:24:33 -0800910void OpenGLRenderer::setupDrawShader() {
911 if (mShader) {
912 mShader->describe(mDescription, mCaches.extensions);
913 }
914}
915
916void OpenGLRenderer::setupDrawColorFilter() {
917 if (mColorFilter) {
918 mColorFilter->describe(mDescription, mCaches.extensions);
919 }
920}
921
922void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
923 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
924 mDescription, swapSrcDst);
925}
926
927void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
928 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
929 mDescription, swapSrcDst);
930}
931
932void OpenGLRenderer::setupDrawProgram() {
933 useProgram(mCaches.programCache.get(mDescription));
934}
935
936void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
937 mTrackDirtyRegions = false;
938}
939
940void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
941 bool ignoreTransform) {
942 mModelView.loadTranslate(left, top, 0.0f);
943 if (!ignoreTransform) {
944 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
945 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
946 } else {
947 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
948 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
949 }
950}
951
Romain Guy8d0d4782010-12-14 20:13:35 -0800952void OpenGLRenderer::setupDrawModelViewIdentity() {
953 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform);
954}
955
Romain Guy70ca14e2010-12-13 18:24:33 -0800956void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
957 bool ignoreTransform, bool ignoreModelView) {
958 if (!ignoreModelView) {
959 mModelView.loadTranslate(left, top, 0.0f);
960 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -0800961 } else {
962 mModelView.loadIdentity();
963 }
Romain Guy86568192010-12-14 15:55:39 -0800964 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
965 if (!ignoreTransform) {
966 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
967 if (mTrackDirtyRegions && dirty) {
968 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
969 }
970 } else {
971 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
972 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
973 }
Romain Guy70ca14e2010-12-13 18:24:33 -0800974}
975
976void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -0800977 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -0800978 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
979 }
980}
981
Romain Guy86568192010-12-14 15:55:39 -0800982void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -0800983 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -0800984 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -0800985 }
986}
987
Romain Guy70ca14e2010-12-13 18:24:33 -0800988void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
989 if (mShader) {
990 if (ignoreTransform) {
991 mModelView.loadInverse(*mSnapshot->transform);
992 }
993 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
994 }
995}
996
Romain Guy8d0d4782010-12-14 20:13:35 -0800997void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
998 if (mShader) {
999 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
1000 }
1001}
1002
Romain Guy70ca14e2010-12-13 18:24:33 -08001003void OpenGLRenderer::setupDrawColorFilterUniforms() {
1004 if (mColorFilter) {
1005 mColorFilter->setupProgram(mCaches.currentProgram);
1006 }
1007}
1008
1009void OpenGLRenderer::setupDrawSimpleMesh() {
1010 mCaches.bindMeshBuffer();
1011 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1012 gMeshStride, 0);
1013}
1014
1015void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1016 bindTexture(texture);
1017 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1018
1019 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1020 glEnableVertexAttribArray(mTexCoordsSlot);
1021}
1022
1023void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1024 if (!vertices) {
1025 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1026 } else {
1027 mCaches.unbindMeshBuffer();
1028 }
1029 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1030 gMeshStride, vertices);
Romain Guy8d0d4782010-12-14 20:13:35 -08001031 if (mTexCoordsSlot > 0) {
1032 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1033 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001034}
1035
1036void OpenGLRenderer::finishDrawTexture() {
1037 glDisableVertexAttribArray(mTexCoordsSlot);
1038}
1039
1040///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001041// Drawing
1042///////////////////////////////////////////////////////////////////////////////
1043
Chet Haasedaf98e92011-01-10 14:10:36 -08001044bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t level) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001045 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1046 // will be performed by the display list itself
1047 if (displayList) {
Chet Haasedaf98e92011-01-10 14:10:36 -08001048 return displayList->replay(*this, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001049 }
Chet Haasedaf98e92011-01-10 14:10:36 -08001050 return false;
Romain Guy0fe478e2010-11-08 12:08:41 -08001051}
1052
Chet Haase5c13d892010-10-08 08:37:55 -07001053void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001054 const float right = left + bitmap->width();
1055 const float bottom = top + bitmap->height();
1056
1057 if (quickReject(left, top, right, bottom)) {
1058 return;
1059 }
1060
Romain Guy86568192010-12-14 15:55:39 -08001061 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001062 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001063 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001064 const AutoTexture autoCleanup(texture);
1065
Romain Guy6926c722010-07-12 20:20:03 -07001066 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -07001067}
1068
Chet Haase5c13d892010-10-08 08:37:55 -07001069void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001070 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1071 const mat4 transform(*matrix);
1072 transform.mapRect(r);
1073
Romain Guy6926c722010-07-12 20:20:03 -07001074 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1075 return;
1076 }
1077
Romain Guy86568192010-12-14 15:55:39 -08001078 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001079 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001080 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001081 const AutoTexture autoCleanup(texture);
1082
Romain Guy5b3b3522010-10-27 18:57:51 -07001083 // This could be done in a cheaper way, all we need is pass the matrix
1084 // to the vertex shader. The save/restore is a bit overkill.
1085 save(SkCanvas::kMatrix_SaveFlag);
1086 concatMatrix(matrix);
1087 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1088 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001089}
1090
Romain Guy5a7b4662011-01-20 19:09:30 -08001091void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
1092 float* vertices, int* colors, SkPaint* paint) {
1093 // TODO: Do a quickReject
1094 if (!vertices || mSnapshot->isIgnored()) {
1095 return;
1096 }
1097
1098 glActiveTexture(gTextureUnits[0]);
1099 Texture* texture = mCaches.textureCache.get(bitmap);
1100 if (!texture) return;
1101 const AutoTexture autoCleanup(texture);
1102 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1103
1104 int alpha;
1105 SkXfermode::Mode mode;
1106 getAlphaAndMode(paint, &alpha, &mode);
1107
Romain Guy5a7b4662011-01-20 19:09:30 -08001108 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guy5a7b4662011-01-20 19:09:30 -08001109
Romain Guya566b7c2011-01-23 16:36:11 -08001110 // TODO: Support the colors array
1111 TextureVertex mesh[count];
Romain Guy5a7b4662011-01-20 19:09:30 -08001112 TextureVertex* vertex = mesh;
1113 for (int32_t y = 0; y < meshHeight; y++) {
1114 for (int32_t x = 0; x < meshWidth; x++) {
1115 uint32_t i = (y * (meshWidth + 1) + x) * 2;
1116
1117 float u1 = float(x) / meshWidth;
1118 float u2 = float(x + 1) / meshWidth;
1119 float v1 = float(y) / meshHeight;
1120 float v2 = float(y + 1) / meshHeight;
1121
1122 int ax = i + (meshWidth + 1) * 2;
1123 int ay = ax + 1;
1124 int bx = i;
1125 int by = bx + 1;
1126 int cx = i + 2;
1127 int cy = cx + 1;
1128 int dx = i + (meshWidth + 1) * 2 + 2;
1129 int dy = dx + 1;
1130
1131 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1132 TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
1133 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1134
1135 TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
1136 TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
1137 TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
1138 }
1139 }
1140
1141 drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
1142 mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
1143 GL_TRIANGLES, count);
1144}
1145
Romain Guy8ba548f2010-06-30 19:21:21 -07001146void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1147 float srcLeft, float srcTop, float srcRight, float srcBottom,
1148 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001149 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001150 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1151 return;
1152 }
1153
Romain Guy746b7402010-10-26 16:27:31 -07001154 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001155 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001156 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001157 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001158 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guy8ba548f2010-06-30 19:21:21 -07001159
Romain Guy8ba548f2010-06-30 19:21:21 -07001160 const float width = texture->width;
1161 const float height = texture->height;
1162
1163 const float u1 = srcLeft / width;
1164 const float v1 = srcTop / height;
1165 const float u2 = srcRight / width;
1166 const float v2 = srcBottom / height;
1167
Romain Guy03750a02010-10-18 14:06:08 -07001168 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001169 resetDrawTextureTexCoords(u1, v1, u2, v2);
1170
Romain Guy03750a02010-10-18 14:06:08 -07001171 int alpha;
1172 SkXfermode::Mode mode;
1173 getAlphaAndMode(paint, &alpha, &mode);
1174
Romain Guy6620c6d2010-12-06 18:07:02 -08001175 if (mSnapshot->transform->isPureTranslate()) {
1176 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1177 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1178
1179 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1180 texture->id, alpha / 255.0f, mode, texture->blend,
1181 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1182 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1183 } else {
1184 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1185 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1186 GL_TRIANGLE_STRIP, gMeshCount);
1187 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001188
1189 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1190}
1191
Romain Guy4aa90572010-09-26 18:40:37 -07001192void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001193 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001194 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001195 if (quickReject(left, top, right, bottom)) {
1196 return;
1197 }
1198
Romain Guy746b7402010-10-26 16:27:31 -07001199 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001200 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001201 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001202 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001203 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guyf7f93552010-07-08 19:17:03 -07001204
1205 int alpha;
1206 SkXfermode::Mode mode;
1207 getAlphaAndMode(paint, &alpha, &mode);
1208
Romain Guy2728f962010-10-08 18:36:15 -07001209 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001210 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001211
Romain Guya5ef39a2010-12-03 16:48:20 -08001212 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001213 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001214#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001215 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08001216 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001217 const size_t count = mesh->quads.size();
1218 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001219 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001220 if (pureTranslate) {
1221 const float x = (int) floorf(bounds.left + 0.5f);
1222 const float y = (int) floorf(bounds.top + 0.5f);
Romain Guy8ab40792010-12-07 13:30:10 -08001223 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Romain Guy6620c6d2010-12-06 18:07:02 -08001224 *mSnapshot->transform);
1225 } else {
1226 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom,
1227 *mSnapshot->transform);
1228 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001229 }
1230 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001231#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001232
Romain Guy6620c6d2010-12-06 18:07:02 -08001233 if (pureTranslate) {
1234 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1235 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1236
1237 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1238 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1239 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1240 true, !mesh->hasEmptyQuads);
1241 } else {
1242 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1243 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1244 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1245 true, !mesh->hasEmptyQuads);
1246 }
Romain Guy054dc182010-10-15 17:55:25 -07001247 }
Romain Guyf7f93552010-07-08 19:17:03 -07001248}
1249
Chet Haase5c13d892010-10-08 08:37:55 -07001250void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001251 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001252
Romain Guya957eea2010-12-08 18:34:42 -08001253 const bool isAA = paint->isAntiAlias();
1254 const float strokeWidth = paint->getStrokeWidth() * 0.5f;
1255 // A stroke width of 0 has a special meaningin Skia:
1256 // it draws an unscaled 1px wide line
1257 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
1258
Romain Guy759ea802010-09-16 20:49:46 -07001259 int alpha;
1260 SkXfermode::Mode mode;
1261 getAlphaAndMode(paint, &alpha, &mode);
1262
Romain Guya957eea2010-12-08 18:34:42 -08001263 int verticesCount = count >> 2;
Romain Guy7230a742011-01-10 22:26:16 -08001264 int generatedVerticesCount = 0;
Romain Guya957eea2010-12-08 18:34:42 -08001265 if (!isHairLine) {
1266 // TODO: AA needs more vertices
1267 verticesCount *= 6;
Romain Guyc95c8d62010-09-17 15:31:32 -07001268 } else {
Romain Guya957eea2010-12-08 18:34:42 -08001269 // TODO: AA will be different
1270 verticesCount *= 2;
Romain Guyc95c8d62010-09-17 15:31:32 -07001271 }
1272
Romain Guya957eea2010-12-08 18:34:42 -08001273 TextureVertex lines[verticesCount];
1274 TextureVertex* vertex = &lines[0];
Romain Guy759ea802010-09-16 20:49:46 -07001275
Romain Guy8d0d4782010-12-14 20:13:35 -08001276 setupDraw();
1277 setupDrawColor(paint->getColor(), alpha);
1278 setupDrawColorFilter();
1279 setupDrawShader();
1280 setupDrawBlending(mode);
1281 setupDrawProgram();
1282 setupDrawModelViewIdentity();
1283 setupDrawColorUniforms();
1284 setupDrawColorFilterUniforms();
1285 setupDrawShaderIdentityUniforms();
1286 setupDrawMesh(vertex);
Romain Guya957eea2010-12-08 18:34:42 -08001287
1288 if (!isHairLine) {
1289 // TODO: Handle the AA case
1290 for (int i = 0; i < count; i += 4) {
1291 // a = start point, b = end point
1292 vec2 a(points[i], points[i + 1]);
1293 vec2 b(points[i + 2], points[i + 3]);
1294
1295 // Bias to snap to the same pixels as Skia
1296 a += 0.375;
1297 b += 0.375;
1298
1299 // Find the normal to the line
1300 vec2 n = (b - a).copyNormalized() * strokeWidth;
1301 float x = n.x;
1302 n.x = -n.y;
1303 n.y = x;
1304
1305 // Four corners of the rectangle defining a thick line
1306 vec2 p1 = a - n;
1307 vec2 p2 = a + n;
1308 vec2 p3 = b + n;
1309 vec2 p4 = b - n;
1310
Romain Guy7230a742011-01-10 22:26:16 -08001311 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1312 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1313 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1314 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
Romain Guya957eea2010-12-08 18:34:42 -08001315
Romain Guy7230a742011-01-10 22:26:16 -08001316 if (!quickReject(left, top, right, bottom)) {
1317 // Draw the line as 2 triangles, could be optimized
1318 // by using only 4 vertices and the correct indices
1319 // Also we should probably used non textured vertices
1320 // when line AA is disabled to save on bandwidth
1321 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1322 TextureVertex::set(vertex++, p2.x, p2.y, 0.0f, 0.0f);
1323 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1324 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1325 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1326 TextureVertex::set(vertex++, p4.x, p4.y, 0.0f, 0.0f);
1327
1328 generatedVerticesCount += 6;
1329
1330 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1331 }
Romain Guya957eea2010-12-08 18:34:42 -08001332 }
1333
Romain Guy7230a742011-01-10 22:26:16 -08001334 if (generatedVerticesCount > 0) {
1335 // GL_LINE does not give the result we want to match Skia
1336 glDrawArrays(GL_TRIANGLES, 0, generatedVerticesCount);
1337 }
Romain Guya957eea2010-12-08 18:34:42 -08001338 } else {
1339 // TODO: Handle the AA case
1340 for (int i = 0; i < count; i += 4) {
Romain Guy7230a742011-01-10 22:26:16 -08001341 const float left = fmin(points[i], points[i + 1]);
1342 const float right = fmax(points[i], points[i + 1]);
1343 const float top = fmin(points[i + 2], points[i + 3]);
1344 const float bottom = fmax(points[i + 2], points[i + 3]);
1345
1346 if (!quickReject(left, top, right, bottom)) {
1347 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1348 TextureVertex::set(vertex++, points[i + 2], points[i + 3], 0.0f, 0.0f);
1349
1350 generatedVerticesCount += 2;
1351
1352 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1353 }
Romain Guya957eea2010-12-08 18:34:42 -08001354 }
Romain Guy8d0d4782010-12-14 20:13:35 -08001355
Romain Guy7230a742011-01-10 22:26:16 -08001356 if (generatedVerticesCount > 0) {
1357 glLineWidth(1.0f);
1358 glDrawArrays(GL_LINES, 0, generatedVerticesCount);
1359 }
Romain Guy29d89972010-09-22 16:10:57 -07001360 }
Romain Guy759ea802010-09-16 20:49:46 -07001361}
1362
Romain Guy85bf02f2010-06-22 13:11:24 -07001363void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001364 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001365 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001366
Romain Guyae88e5e2010-10-22 17:49:18 -07001367 Rect& clip(*mSnapshot->clipRect);
1368 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001369
Romain Guy3d58c032010-07-14 16:34:53 -07001370 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001371}
Romain Guy9d5316e2010-06-24 19:30:36 -07001372
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001373void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
Romain Guy01d58e42011-01-19 21:54:02 -08001374 if (!texture) return;
1375 const AutoTexture autoCleanup(texture);
1376
1377 const float x = left + texture->left - texture->offset;
1378 const float y = top + texture->top - texture->offset;
1379
1380 drawPathTexture(texture, x, y, paint);
1381}
1382
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001383void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1384 float rx, float ry, SkPaint* paint) {
1385 if (mSnapshot->isIgnored()) return;
1386
1387 glActiveTexture(gTextureUnits[0]);
1388 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1389 right - left, bottom - top, rx, ry, paint);
1390 drawShape(left, top, texture, paint);
1391}
1392
Romain Guy01d58e42011-01-19 21:54:02 -08001393void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1394 if (mSnapshot->isIgnored()) return;
1395
1396 glActiveTexture(gTextureUnits[0]);
Romain Guy01d58e42011-01-19 21:54:02 -08001397 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001398 drawShape(x - radius, y - radius, texture, paint);
1399}
Romain Guy01d58e42011-01-19 21:54:02 -08001400
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001401void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
1402 if (mSnapshot->isIgnored()) return;
Romain Guy01d58e42011-01-19 21:54:02 -08001403
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001404 glActiveTexture(gTextureUnits[0]);
1405 const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
1406 drawShape(left, top, texture, paint);
1407}
1408
Romain Guy8b2f5262011-01-23 16:15:02 -08001409void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
1410 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
1411 if (mSnapshot->isIgnored()) return;
1412
1413 if (fabs(sweepAngle) >= 360.0f) {
1414 drawOval(left, top, right, bottom, paint);
1415 return;
1416 }
1417
1418 glActiveTexture(gTextureUnits[0]);
1419 const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
1420 startAngle, sweepAngle, useCenter, paint);
1421 drawShape(left, top, texture, paint);
1422}
1423
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001424void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
1425 SkPaint* paint) {
1426 if (mSnapshot->isIgnored()) return;
1427
1428 glActiveTexture(gTextureUnits[0]);
1429 const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
1430 drawShape(left, top, texture, paint);
Romain Guy01d58e42011-01-19 21:54:02 -08001431}
1432
Chet Haase5c13d892010-10-08 08:37:55 -07001433void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guyc1cd9ba32011-01-23 14:18:41 -08001434 if (p->getStyle() != SkPaint::kFill_Style) {
1435 drawRectAsShape(left, top, right, bottom, p);
1436 return;
1437 }
1438
Romain Guy6926c722010-07-12 20:20:03 -07001439 if (quickReject(left, top, right, bottom)) {
1440 return;
1441 }
1442
Romain Guy026c5e162010-06-28 17:12:22 -07001443 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07001444 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001445 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1446 if (!isMode) {
1447 // Assume SRC_OVER
1448 mode = SkXfermode::kSrcOver_Mode;
1449 }
1450 } else {
1451 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07001452 }
1453
1454 // Skia draws using the color's alpha channel if < 255
1455 // Otherwise, it uses the paint's alpha
1456 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07001457 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -07001458 color |= p->getAlpha() << 24;
1459 }
1460
1461 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -07001462}
Romain Guy9d5316e2010-06-24 19:30:36 -07001463
Romain Guye8e62a42010-07-23 18:55:21 -07001464void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1465 float x, float y, SkPaint* paint) {
Romain Guyb146b122010-12-15 17:06:45 -08001466 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07001467 return;
1468 }
Romain Guyaf636eb2010-12-09 17:47:21 -08001469 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07001470
Romain Guyf607bdc2010-09-10 19:20:06 -07001471 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -07001472
Romain Guya674ab72010-08-10 17:26:42 -07001473 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07001474 switch (paint->getTextAlign()) {
1475 case SkPaint::kCenter_Align:
1476 length = paint->measureText(text, bytesCount);
1477 x -= length / 2.0f;
1478 break;
1479 case SkPaint::kRight_Align:
1480 length = paint->measureText(text, bytesCount);
1481 x -= length;
1482 break;
1483 default:
1484 break;
1485 }
1486
Romain Guy6620c6d2010-12-06 18:07:02 -08001487 // TODO: Handle paint->getTextScaleX()
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001488 const float oldX = x;
1489 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08001490 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
1491 if (pureTranslate) {
1492 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
1493 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
1494 }
1495
Romain Guyb45c0c92010-08-26 20:35:23 -07001496 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
1497 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07001498 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07001499
Romain Guy86568192010-12-14 15:55:39 -08001500 int alpha;
1501 SkXfermode::Mode mode;
1502 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07001503
Romain Guy1e45aae2010-08-13 19:39:53 -07001504 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07001505 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -07001506 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -07001507 count, mShadowRadius);
1508 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07001509
Romain Guy86568192010-12-14 15:55:39 -08001510 const float sx = x - shadow->left + mShadowDx;
1511 const float sy = y - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07001512
Romain Guy86568192010-12-14 15:55:39 -08001513 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
1514
1515 glActiveTexture(gTextureUnits[0]);
1516 setupDraw();
1517 setupDrawWithTexture(true);
1518 setupDrawAlpha8Color(mShadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
1519 setupDrawBlending(true, mode);
1520 setupDrawProgram();
1521 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height, pureTranslate);
1522 setupDrawTexture(shadow->id);
1523 setupDrawPureColorUniforms();
1524 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1525
Romain Guy0a417492010-08-16 20:26:20 -07001526 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy86568192010-12-14 15:55:39 -08001527 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07001528 }
1529
Romain Guyb146b122010-12-15 17:06:45 -08001530 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
1531 return;
1532 }
1533
Romain Guy6620c6d2010-12-06 18:07:02 -08001534 // Pick the appropriate texture filtering
1535 bool linearFilter = mSnapshot->transform->changesBounds();
1536 if (pureTranslate && !linearFilter) {
1537 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
1538 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001539
Romain Guy86568192010-12-14 15:55:39 -08001540 glActiveTexture(gTextureUnits[0]);
1541 setupDraw();
1542 setupDrawDirtyRegionsDisabled();
1543 setupDrawWithTexture(true);
1544 setupDrawAlpha8Color(paint->getColor(), alpha);
1545 setupDrawColorFilter();
1546 setupDrawShader();
1547 setupDrawBlending(true, mode);
1548 setupDrawProgram();
1549 setupDrawModelView(x, y, x, y, pureTranslate, true);
1550 setupDrawTexture(fontRenderer.getTexture(linearFilter));
1551 setupDrawPureColorUniforms();
1552 setupDrawColorFilterUniforms();
1553 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07001554
Romain Guy6620c6d2010-12-06 18:07:02 -08001555 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07001556 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
1557
1558#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08001559 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07001560#else
Romain Guyf219da52011-01-16 12:54:25 -08001561 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07001562#endif
Romain Guy9d13fe252010-10-15 16:06:03 -07001563
Romain Guy03750a02010-10-18 14:06:08 -07001564 mCaches.unbindMeshBuffer();
Romain Guy6620c6d2010-12-06 18:07:02 -08001565 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08001566 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001567#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08001568 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001569 if (!pureTranslate) {
1570 mSnapshot->transform->mapRect(bounds);
1571 }
Romain Guyf219da52011-01-16 12:54:25 -08001572 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001573 }
1574#endif
1575 }
Romain Guy694b5192010-07-21 21:33:20 -07001576
1577 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001578 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07001579
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001580 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07001581}
1582
Romain Guy7fbcc042010-08-04 15:40:07 -07001583void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001584 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001585
Romain Guy01d58e42011-01-19 21:54:02 -08001586 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07001587
Romain Guyfb8b7632010-08-23 21:05:08 -07001588 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001589 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001590 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07001591
Romain Guy8b55f372010-08-18 17:10:07 -07001592 const float x = texture->left - texture->offset;
1593 const float y = texture->top - texture->offset;
1594
Romain Guy01d58e42011-01-19 21:54:02 -08001595 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07001596}
1597
Romain Guyada830f2011-01-13 12:13:20 -08001598void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
1599 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08001600 return;
1601 }
1602
1603 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08001604
1605 int alpha;
1606 SkXfermode::Mode mode;
1607 getAlphaAndMode(paint, &alpha, &mode);
1608
Romain Guyada830f2011-01-13 12:13:20 -08001609 layer->alpha = alpha;
1610 layer->mode = mode;
Romain Guy6c319ca2011-01-11 14:29:25 -08001611
Romain Guy81683962011-01-24 20:40:18 -08001612 LOGD("Drawing layer with alpha = %d", alpha);
Romain Guyf219da52011-01-16 12:54:25 -08001613
1614#if RENDER_LAYERS_AS_REGIONS
Romain Guyc88e3572011-01-22 00:32:12 -08001615 if (!layer->region.isEmpty()) {
1616 if (layer->region.isRect()) {
1617 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1618 composeLayerRect(layer, r);
1619 } else if (layer->mesh) {
Romain Guy81683962011-01-24 20:40:18 -08001620 const float a = alpha / 255.0f;
Romain Guyc88e3572011-01-22 00:32:12 -08001621 const Rect& rect = layer->layer;
Romain Guyf219da52011-01-16 12:54:25 -08001622
Romain Guyc88e3572011-01-22 00:32:12 -08001623 setupDraw();
1624 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08001625 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08001626 setupDrawColorFilter();
1627 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
1628 setupDrawProgram();
1629 setupDrawDirtyRegionsDisabled();
1630 setupDrawPureColorUniforms();
1631 setupDrawColorFilterUniforms();
1632 setupDrawTexture(layer->texture);
1633 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1634 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08001635
Romain Guyc88e3572011-01-22 00:32:12 -08001636 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
1637 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08001638
Romain Guyc88e3572011-01-22 00:32:12 -08001639 finishDrawTexture();
1640 }
Romain Guyf219da52011-01-16 12:54:25 -08001641 }
1642#else
Romain Guyada830f2011-01-13 12:13:20 -08001643 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1644 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08001645#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08001646}
1647
Romain Guy6926c722010-07-12 20:20:03 -07001648///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07001649// Shaders
1650///////////////////////////////////////////////////////////////////////////////
1651
1652void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07001653 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07001654}
1655
Romain Guy06f96e22010-07-30 19:18:16 -07001656void OpenGLRenderer::setupShader(SkiaShader* shader) {
1657 mShader = shader;
1658 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001659 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07001660 }
Romain Guy7fac2e12010-07-16 17:10:13 -07001661}
1662
Romain Guyd27977d2010-07-14 19:18:51 -07001663///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07001664// Color filters
1665///////////////////////////////////////////////////////////////////////////////
1666
1667void OpenGLRenderer::resetColorFilter() {
1668 mColorFilter = NULL;
1669}
1670
1671void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
1672 mColorFilter = filter;
1673}
1674
1675///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07001676// Drop shadow
1677///////////////////////////////////////////////////////////////////////////////
1678
1679void OpenGLRenderer::resetShadow() {
1680 mHasShadow = false;
1681}
1682
1683void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
1684 mHasShadow = true;
1685 mShadowRadius = radius;
1686 mShadowDx = dx;
1687 mShadowDy = dy;
1688 mShadowColor = color;
1689}
1690
1691///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07001692// Drawing implementation
1693///////////////////////////////////////////////////////////////////////////////
1694
Romain Guy01d58e42011-01-19 21:54:02 -08001695void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
1696 float x, float y, SkPaint* paint) {
1697 if (quickReject(x, y, x + texture->width, y + texture->height)) {
1698 return;
1699 }
1700
1701 int alpha;
1702 SkXfermode::Mode mode;
1703 getAlphaAndMode(paint, &alpha, &mode);
1704
1705 setupDraw();
1706 setupDrawWithTexture(true);
1707 setupDrawAlpha8Color(paint->getColor(), alpha);
1708 setupDrawColorFilter();
1709 setupDrawShader();
1710 setupDrawBlending(true, mode);
1711 setupDrawProgram();
1712 setupDrawModelView(x, y, x + texture->width, y + texture->height);
1713 setupDrawTexture(texture->id);
1714 setupDrawPureColorUniforms();
1715 setupDrawColorFilterUniforms();
1716 setupDrawShaderUniforms();
1717 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1718
1719 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1720
1721 finishDrawTexture();
1722}
1723
Romain Guyf607bdc2010-09-10 19:20:06 -07001724// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001725#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1726#define kStdUnderline_Offset (1.0f / 9.0f)
1727#define kStdUnderline_Thickness (1.0f / 18.0f)
1728
1729void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1730 float x, float y, SkPaint* paint) {
1731 // Handle underline and strike-through
1732 uint32_t flags = paint->getFlags();
1733 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1734 float underlineWidth = length;
1735 // If length is > 0.0f, we already measured the text for the text alignment
1736 if (length <= 0.0f) {
1737 underlineWidth = paint->measureText(text, bytesCount);
1738 }
1739
1740 float offsetX = 0;
1741 switch (paint->getTextAlign()) {
1742 case SkPaint::kCenter_Align:
1743 offsetX = underlineWidth * 0.5f;
1744 break;
1745 case SkPaint::kRight_Align:
1746 offsetX = underlineWidth;
1747 break;
1748 default:
1749 break;
1750 }
1751
1752 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001753 const float textSize = paint->getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08001754 // TODO: Support stroke width < 1.0f when we have AA lines
1755 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07001756
Romain Guye20ecbd2010-09-22 19:49:04 -07001757 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001758 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001759
Romain Guyf6834472011-01-23 13:32:12 -08001760 int linesCount = 0;
1761 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
1762 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
1763
1764 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07001765 float points[pointsCount];
1766 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001767
1768 if (flags & SkPaint::kUnderlineText_Flag) {
1769 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001770 points[currentPoint++] = left;
1771 points[currentPoint++] = top;
1772 points[currentPoint++] = left + underlineWidth;
1773 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001774 }
1775
1776 if (flags & SkPaint::kStrikeThruText_Flag) {
1777 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001778 points[currentPoint++] = left;
1779 points[currentPoint++] = top;
1780 points[currentPoint++] = left + underlineWidth;
1781 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001782 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001783
1784 SkPaint linesPaint(*paint);
1785 linesPaint.setStrokeWidth(strokeWidth);
1786
1787 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001788 }
1789 }
1790}
1791
Romain Guy026c5e162010-06-28 17:12:22 -07001792void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001793 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07001794 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001795 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001796 color |= 0x00ffffff;
1797 }
1798
Romain Guy70ca14e2010-12-13 18:24:33 -08001799 setupDraw();
1800 setupDrawColor(color);
1801 setupDrawShader();
1802 setupDrawColorFilter();
1803 setupDrawBlending(mode);
1804 setupDrawProgram();
1805 setupDrawModelView(left, top, right, bottom, ignoreTransform);
1806 setupDrawColorUniforms();
1807 setupDrawShaderUniforms(ignoreTransform);
1808 setupDrawColorFilterUniforms();
1809 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07001810
Romain Guyc95c8d62010-09-17 15:31:32 -07001811 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1812}
1813
Romain Guy82ba8142010-07-09 13:25:56 -07001814void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07001815 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001816 int alpha;
1817 SkXfermode::Mode mode;
1818 getAlphaAndMode(paint, &alpha, &mode);
1819
Romain Guy8164c2d2010-10-25 18:03:28 -07001820 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1821
Romain Guy6620c6d2010-12-06 18:07:02 -08001822 if (mSnapshot->transform->isPureTranslate()) {
1823 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1824 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1825
1826 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1827 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
1828 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
1829 } else {
1830 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
1831 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
1832 GL_TRIANGLE_STRIP, gMeshCount);
1833 }
Romain Guy85bf02f2010-06-22 13:11:24 -07001834}
1835
Romain Guybd6b79b2010-06-26 00:13:53 -07001836void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001837 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1838 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07001839 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001840}
1841
1842void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001843 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001844 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07001845 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001846
Romain Guy746b7402010-10-26 16:27:31 -07001847 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08001848 setupDrawWithTexture();
1849 setupDrawColor(alpha, alpha, alpha, alpha);
1850 setupDrawColorFilter();
1851 setupDrawBlending(blend, mode, swapSrcDst);
1852 setupDrawProgram();
1853 if (!dirty) {
1854 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07001855 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001856 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001857 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001858 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08001859 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001860 }
Romain Guy86568192010-12-14 15:55:39 -08001861 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08001862 setupDrawColorFilterUniforms();
1863 setupDrawTexture(texture);
1864 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07001865
Romain Guy6820ac82010-09-15 18:11:50 -07001866 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08001867
1868 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07001869}
1870
Romain Guya5aed0d2010-09-09 14:42:43 -07001871void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001872 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001873 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1874 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001875 if (mode < SkXfermode::kPlus_Mode) {
1876 if (!mCaches.blend) {
1877 glEnable(GL_BLEND);
1878 }
Romain Guy82ba8142010-07-09 13:25:56 -07001879
Romain Guyf607bdc2010-09-10 19:20:06 -07001880 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1881 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001882
Romain Guya5aed0d2010-09-09 14:42:43 -07001883 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1884 glBlendFunc(sourceMode, destMode);
1885 mCaches.lastSrcMode = sourceMode;
1886 mCaches.lastDstMode = destMode;
1887 }
1888 } else {
1889 // These blend modes are not supported by OpenGL directly and have
1890 // to be implemented using shaders. Since the shader will perform
1891 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07001892 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001893 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001894 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001895 }
1896
1897 if (mCaches.blend) {
1898 glDisable(GL_BLEND);
1899 }
1900 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001901 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001902 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001903 glDisable(GL_BLEND);
1904 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001905 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001906}
1907
Romain Guy889f8d12010-07-29 14:37:42 -07001908bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001909 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001910 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001911 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001912 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001913 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001914 }
Romain Guy6926c722010-07-12 20:20:03 -07001915 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001916}
1917
Romain Guy026c5e162010-06-28 17:12:22 -07001918void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001919 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001920 TextureVertex::setUV(v++, u1, v1);
1921 TextureVertex::setUV(v++, u2, v1);
1922 TextureVertex::setUV(v++, u1, v2);
1923 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001924}
1925
Chet Haase5c13d892010-10-08 08:37:55 -07001926void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07001927 if (paint) {
Romain Guy746b7402010-10-26 16:27:31 -07001928 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001929 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1930 if (!isMode) {
1931 // Assume SRC_OVER
1932 *mode = SkXfermode::kSrcOver_Mode;
1933 }
1934 } else {
1935 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001936 }
1937
1938 // Skia draws using the color's alpha channel if < 255
1939 // Otherwise, it uses the paint's alpha
1940 int color = paint->getColor();
1941 *alpha = (color >> 24) & 0xFF;
1942 if (*alpha == 255) {
1943 *alpha = paint->getAlpha();
1944 }
1945 } else {
1946 *mode = SkXfermode::kSrcOver_Mode;
1947 *alpha = 255;
1948 }
Romain Guy026c5e162010-06-28 17:12:22 -07001949}
1950
Romain Guya5aed0d2010-09-09 14:42:43 -07001951SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1952 if (mode == NULL) {
1953 return SkXfermode::kSrcOver_Mode;
1954 }
1955 return mode->fMode;
1956}
1957
Romain Guy746b7402010-10-26 16:27:31 -07001958void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
Romain Guy8164c2d2010-10-25 18:03:28 -07001959 bool bound = false;
1960 if (wrapS != texture->wrapS) {
1961 glBindTexture(GL_TEXTURE_2D, texture->id);
1962 bound = true;
1963 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1964 texture->wrapS = wrapS;
1965 }
1966 if (wrapT != texture->wrapT) {
Romain Guy3e3ba152010-10-25 18:33:16 -07001967 if (!bound) {
Romain Guy3e3ba152010-10-25 18:33:16 -07001968 glBindTexture(GL_TEXTURE_2D, texture->id);
1969 }
Romain Guy8164c2d2010-10-25 18:03:28 -07001970 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1971 texture->wrapT = wrapT;
1972 }
Romain Guya1db5742010-07-20 13:09:13 -07001973}
1974
Romain Guy9d5316e2010-06-24 19:30:36 -07001975}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001976}; // namespace android