blob: 91ac16689ccfacfe0f087196f4a7c701eee2a845 [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 Guyfe48f652010-11-11 15:36:56 -0800139 mCaches.clearGarbage();
140
Romain Guy8aef54f2010-09-01 15:13:49 -0700141 mSnapshot = new Snapshot(mFirstSnapshot,
142 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy8fb95422010-08-17 18:38:51 -0700143 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700144
Romain Guyfb8b7632010-08-23 21:05:08 -0700145 glViewport(0, 0, mWidth, mHeight);
146
Romain Guyd90f23e2010-09-09 11:47:54 -0700147 glDisable(GL_DITHER);
Romain Guybb9524b2010-06-22 18:56:38 -0700148
Romain Guy6b7bd242010-10-06 19:49:23 -0700149 if (!opaque) {
Romain Guy746b7402010-10-26 16:27:31 -0700150 glDisable(GL_SCISSOR_TEST);
Romain Guy6b7bd242010-10-06 19:49:23 -0700151 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
152 glClear(GL_COLOR_BUFFER_BIT);
153 }
Romain Guybb9524b2010-06-22 18:56:38 -0700154
Chet Haase0d200832010-11-05 15:36:16 -0700155 glEnable(GL_SCISSOR_TEST);
156 glScissor(0, 0, mWidth, mHeight);
Romain Guyb82da652010-07-30 11:36:12 -0700157 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700158}
159
Romain Guyb025b9c2010-09-16 14:16:48 -0700160void OpenGLRenderer::finish() {
161#if DEBUG_OPENGL
162 GLenum status = GL_NO_ERROR;
163 while ((status = glGetError()) != GL_NO_ERROR) {
164 LOGD("GL error from OpenGLRenderer: 0x%x", status);
Romain Guya07105b2011-01-10 21:14:18 -0800165 switch (status) {
166 case GL_OUT_OF_MEMORY:
167 LOGE(" OpenGLRenderer is out of memory!");
168 break;
169 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700170 }
171#endif
Romain Guyc15008e2010-11-10 11:59:15 -0800172#if DEBUG_MEMORY_USAGE
173 mCaches.dumpMemoryUsage();
Romain Guye190aa62010-11-10 19:01:29 -0800174#else
175 if (mCaches.getDebugLevel() & kDebugMemory) {
176 mCaches.dumpMemoryUsage();
177 }
Romain Guyc15008e2010-11-10 11:59:15 -0800178#endif
Romain Guyb025b9c2010-09-16 14:16:48 -0700179}
180
Romain Guy6c319ca2011-01-11 14:29:25 -0800181void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700182 if (mCaches.currentProgram) {
183 if (mCaches.currentProgram->isInUse()) {
184 mCaches.currentProgram->remove();
185 mCaches.currentProgram = NULL;
186 }
187 }
Romain Guy50c0f092010-10-19 11:42:22 -0700188 mCaches.unbindMeshBuffer();
Romain Guyda8532c2010-08-31 11:50:35 -0700189}
190
Romain Guy6c319ca2011-01-11 14:29:25 -0800191void OpenGLRenderer::acquireContext() {
192 interrupt();
193}
194
195void OpenGLRenderer::resume() {
Romain Guyeb993562010-10-05 18:14:38 -0700196 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700197
198 glEnable(GL_SCISSOR_TEST);
Romain Guy746b7402010-10-26 16:27:31 -0700199 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700200
Romain Guyf607bdc2010-09-10 19:20:06 -0700201 glDisable(GL_DITHER);
202
Romain Guy42f3a4b2011-01-19 13:42:26 -0800203 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy03750a02010-10-18 14:06:08 -0700204 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700205
Romain Guy50c0f092010-10-19 11:42:22 -0700206 mCaches.blend = true;
207 glEnable(GL_BLEND);
208 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
209 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700210}
211
Romain Guy6c319ca2011-01-11 14:29:25 -0800212void OpenGLRenderer::releaseContext() {
213 resume();
214}
215
Romain Guyf6a11b82010-06-23 17:47:49 -0700216///////////////////////////////////////////////////////////////////////////////
217// State management
218///////////////////////////////////////////////////////////////////////////////
219
Romain Guybb9524b2010-06-22 18:56:38 -0700220int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700221 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700222}
223
224int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700225 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700226}
227
228void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700229 if (mSaveCount > 1) {
230 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700231 }
Romain Guybb9524b2010-06-22 18:56:38 -0700232}
233
234void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700235 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700236
Romain Guy8fb95422010-08-17 18:38:51 -0700237 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700238 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700239 }
Romain Guybb9524b2010-06-22 18:56:38 -0700240}
241
Romain Guy8aef54f2010-09-01 15:13:49 -0700242int OpenGLRenderer::saveSnapshot(int flags) {
243 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700244 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700245}
246
247bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700248 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700249 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700250 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700251
Romain Guybd6b79b2010-06-26 00:13:53 -0700252 sp<Snapshot> current = mSnapshot;
253 sp<Snapshot> previous = mSnapshot->previous;
254
Romain Guyeb993562010-10-05 18:14:38 -0700255 if (restoreOrtho) {
256 Rect& r = previous->viewport;
257 glViewport(r.left, r.top, r.right, r.bottom);
258 mOrthoMatrix.load(current->orthoMatrix);
259 }
260
Romain Guy8b55f372010-08-18 17:10:07 -0700261 mSaveCount--;
262 mSnapshot = previous;
263
Romain Guy2542d192010-08-18 11:47:12 -0700264 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700265 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700266 }
Romain Guy2542d192010-08-18 11:47:12 -0700267
Romain Guy5ec99242010-11-03 16:19:08 -0700268 if (restoreLayer) {
269 composeLayer(current, previous);
270 }
271
Romain Guy2542d192010-08-18 11:47:12 -0700272 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700273}
274
Romain Guyf6a11b82010-06-23 17:47:49 -0700275///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700276// Layers
277///////////////////////////////////////////////////////////////////////////////
278
279int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700280 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700281 const GLuint previousFbo = mSnapshot->fbo;
282 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700283
Romain Guyaf636eb2010-12-09 17:47:21 -0800284 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700285 int alpha = 255;
286 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700287
Romain Guye45362c2010-11-03 19:58:32 -0700288 if (p) {
289 alpha = p->getAlpha();
290 if (!mCaches.extensions.hasFramebufferFetch()) {
291 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
292 if (!isMode) {
293 // Assume SRC_OVER
294 mode = SkXfermode::kSrcOver_Mode;
295 }
296 } else {
297 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700298 }
299 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700300 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700301 }
Romain Guyd55a8612010-06-28 17:42:46 -0700302
Romain Guydbc26d22010-10-11 17:58:29 -0700303 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
304 }
Romain Guyd55a8612010-06-28 17:42:46 -0700305
306 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700307}
308
309int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
310 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700311 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700312 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700313 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700314 SkPaint paint;
315 paint.setAlpha(alpha);
316 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700317 }
Romain Guyd55a8612010-06-28 17:42:46 -0700318}
Romain Guybd6b79b2010-06-26 00:13:53 -0700319
Romain Guy1c740bc2010-09-13 18:00:09 -0700320/**
321 * Layers are viewed by Skia are slightly different than layers in image editing
322 * programs (for instance.) When a layer is created, previously created layers
323 * and the frame buffer still receive every drawing command. For instance, if a
324 * layer is created and a shape intersecting the bounds of the layers and the
325 * framebuffer is draw, the shape will be drawn on both (unless the layer was
326 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
327 *
328 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
329 * texture. Unfortunately, this is inefficient as it requires every primitive to
330 * be drawn n + 1 times, where n is the number of active layers. In practice this
331 * means, for every primitive:
332 * - Switch active frame buffer
333 * - Change viewport, clip and projection matrix
334 * - Issue the drawing
335 *
336 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700337 * To avoid this, layers are implemented in a different way here, at least in the
338 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
339 * is set. When this flag is set we can redirect all drawing operations into a
340 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700341 *
342 * This implementation relies on the frame buffer being at least RGBA 8888. When
343 * a layer is created, only a texture is created, not an FBO. The content of the
344 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700345 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700346 * buffer and drawing continues as normal. This technique therefore treats the
347 * frame buffer as a scratch buffer for the layers.
348 *
349 * To compose the layers back onto the frame buffer, each layer texture
350 * (containing the original frame buffer data) is drawn as a simple quad over
351 * the frame buffer. The trick is that the quad is set as the composition
352 * destination in the blending equation, and the frame buffer becomes the source
353 * of the composition.
354 *
355 * Drawing layers with an alpha value requires an extra step before composition.
356 * An empty quad is drawn over the layer's region in the frame buffer. This quad
357 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
358 * quad is used to multiply the colors in the frame buffer. This is achieved by
359 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
360 * GL_ZERO, GL_SRC_ALPHA.
361 *
362 * Because glCopyTexImage2D() can be slow, an alternative implementation might
363 * be use to draw a single clipped layer. The implementation described above
364 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700365 *
366 * (1) The frame buffer is actually not cleared right away. To allow the GPU
367 * to potentially optimize series of calls to glCopyTexImage2D, the frame
368 * buffer is left untouched until the first drawing operation. Only when
369 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700370 */
Romain Guyd55a8612010-06-28 17:42:46 -0700371bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700372 float right, float bottom, int alpha, SkXfermode::Mode mode,
373 int flags, GLuint previousFbo) {
374 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700375 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700376
Romain Guyeb993562010-10-05 18:14:38 -0700377 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
378
Romain Guyf607bdc2010-09-10 19:20:06 -0700379 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700380 Rect bounds(left, top, right, bottom);
Romain Guyae88e5e2010-10-22 17:49:18 -0700381 if (fboLayer) {
382 // Clear the previous layer regions before we change the viewport
383 clearLayerRegions();
384 } else {
Romain Guyeb993562010-10-05 18:14:38 -0700385 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700386
Romain Guyeb993562010-10-05 18:14:38 -0700387 // Layers only make sense if they are in the framebuffer's bounds
Romain Guy58ae6db2010-10-22 11:16:37 -0700388 bounds.intersect(*snapshot->clipRect);
Romain Guyae517592010-10-22 10:40:27 -0700389
Romain Guyae88e5e2010-10-22 17:49:18 -0700390 // We cannot work with sub-pixels in this case
391 bounds.snapToPixelBoundaries();
392
Romain Guyae517592010-10-22 10:40:27 -0700393 // When the layer is not an FBO, we may use glCopyTexImage so we
394 // need to make sure the layer does not extend outside the bounds
395 // of the framebuffer
396 bounds.intersect(snapshot->previous->viewport);
Romain Guyeb993562010-10-05 18:14:38 -0700397 }
Romain Guybf434112010-09-16 14:40:17 -0700398
Romain Guy746b7402010-10-26 16:27:31 -0700399 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
400 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800401 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700402 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700403 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700404 }
405
406 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800407 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700408 return false;
409 }
Romain Guyf18fd992010-07-08 11:45:51 -0700410
Romain Guy5b3b3522010-10-27 18:57:51 -0700411 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700412 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700413 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700414 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700415 }
416
Romain Guydda570202010-07-06 11:39:32 -0700417 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700418 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700419 layer->layer.set(bounds);
Romain Guy8550c4c2010-10-08 15:49:53 -0700420 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
421 bounds.getWidth() / float(layer->width), 0.0f);
Romain Guy171c5922011-01-06 10:04:23 -0800422 layer->colorFilter = mColorFilter;
Romain Guydda570202010-07-06 11:39:32 -0700423
Romain Guy8fb95422010-08-17 18:38:51 -0700424 // Save the layer in the snapshot
425 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700426 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700427
Romain Guyeb993562010-10-05 18:14:38 -0700428 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700429 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700430 } else {
431 // Copy the framebuffer into the layer
432 glBindTexture(GL_TEXTURE_2D, layer->texture);
433
Romain Guyae88e5e2010-10-22 17:49:18 -0700434 if (layer->empty) {
435 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left,
436 snapshot->height - bounds.bottom, layer->width, layer->height, 0);
437 layer->empty = false;
438 } else {
439 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
440 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
441 }
Romain Guyeb993562010-10-05 18:14:38 -0700442
443 // Enqueue the buffer coordinates to clear the corresponding region later
444 mLayers.push(new Rect(bounds));
445 }
Romain Guyf86ef572010-07-01 11:05:42 -0700446
Romain Guyd55a8612010-06-28 17:42:46 -0700447 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700448}
449
Romain Guy5b3b3522010-10-27 18:57:51 -0700450bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
451 GLuint previousFbo) {
452 layer->fbo = mCaches.fboCache.get();
453
454#if RENDER_LAYERS_AS_REGIONS
455 snapshot->region = &snapshot->layer->region;
456 snapshot->flags |= Snapshot::kFlagFboTarget;
457#endif
458
459 Rect clip(bounds);
460 snapshot->transform->mapRect(clip);
461 clip.intersect(*snapshot->clipRect);
462 clip.snapToPixelBoundaries();
463 clip.intersect(snapshot->previous->viewport);
464
465 mat4 inverse;
466 inverse.loadInverse(*mSnapshot->transform);
467
468 inverse.mapRect(clip);
469 clip.snapToPixelBoundaries();
470 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700471 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700472
473 snapshot->flags |= Snapshot::kFlagIsFboLayer;
474 snapshot->fbo = layer->fbo;
475 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700476 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
477 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
478 snapshot->height = bounds.getHeight();
479 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
480 snapshot->orthoMatrix.load(mOrthoMatrix);
481
482 // Bind texture to FBO
483 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
484 glBindTexture(GL_TEXTURE_2D, layer->texture);
485
486 // Initialize the texture if needed
487 if (layer->empty) {
488 layer->empty = false;
489 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
490 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
491 }
492
493 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
494 layer->texture, 0);
495
496#if DEBUG_LAYERS_AS_REGIONS
497 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
498 if (status != GL_FRAMEBUFFER_COMPLETE) {
499 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
500
501 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
502 glDeleteTextures(1, &layer->texture);
503 mCaches.fboCache.put(layer->fbo);
504
505 delete layer;
506
507 return false;
508 }
509#endif
510
511 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
512 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
513 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
514 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
515 glClear(GL_COLOR_BUFFER_BIT);
516
517 dirtyClip();
518
519 // Change the ortho projection
520 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
521 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
522
523 return true;
524}
525
Romain Guy1c740bc2010-09-13 18:00:09 -0700526/**
527 * Read the documentation of createLayer() before doing anything in this method.
528 */
Romain Guy1d83e192010-08-17 11:37:00 -0700529void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
530 if (!current->layer) {
531 LOGE("Attempting to compose a layer that does not exist");
532 return;
533 }
534
Romain Guy5b3b3522010-10-27 18:57:51 -0700535 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700536
537 if (fboLayer) {
538 // Unbind current FBO and restore previous one
539 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
540 }
541
Romain Guy1d83e192010-08-17 11:37:00 -0700542 Layer* layer = current->layer;
543 const Rect& rect = layer->layer;
544
Romain Guyeb993562010-10-05 18:14:38 -0700545 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700546 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700547 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700548 // Required below, composeLayerRect() will divide by 255
549 layer->alpha = 255;
Romain Guyf607bdc2010-09-10 19:20:06 -0700550 }
551
Romain Guy03750a02010-10-18 14:06:08 -0700552 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700553
Romain Guy746b7402010-10-26 16:27:31 -0700554 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700555
Romain Guy5b3b3522010-10-27 18:57:51 -0700556 // When the layer is stored in an FBO, we can save a bit of fillrate by
557 // drawing only the dirty region
558 if (fboLayer) {
559 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy171c5922011-01-06 10:04:23 -0800560 if (layer->colorFilter) {
561 setupColorFilter(layer->colorFilter);
562 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700563 composeLayerRegion(layer, rect);
Romain Guy171c5922011-01-06 10:04:23 -0800564 if (layer->colorFilter) {
565 resetColorFilter();
566 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700567 } else {
568 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
569 composeLayerRect(layer, rect, true);
570 }
Romain Guy8b55f372010-08-18 17:10:07 -0700571
Romain Guyeb993562010-10-05 18:14:38 -0700572 if (fboLayer) {
573 // Detach the texture from the FBO
574 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
575 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
576 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
577
578 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
579 mCaches.fboCache.put(current->fbo);
580 }
581
Romain Guy746b7402010-10-26 16:27:31 -0700582 dirtyClip();
583
Romain Guyeb993562010-10-05 18:14:38 -0700584 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700585 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700586 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700587 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700588 delete layer;
589 }
590}
591
Romain Guy5b3b3522010-10-27 18:57:51 -0700592void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
593 const Rect& texCoords = layer->texCoords;
594 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
595
596 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
597 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
598 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap);
599
600 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
601}
602
603void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
604#if RENDER_LAYERS_AS_REGIONS
605 if (layer->region.isRect()) {
606 composeLayerRect(layer, rect);
607 layer->region.clear();
608 return;
609 }
610
611 if (!layer->region.isEmpty()) {
612 size_t count;
613 const android::Rect* rects = layer->region.getArray(&count);
614
Romain Guy5b3b3522010-10-27 18:57:51 -0700615 const float alpha = layer->alpha / 255.0f;
Romain Guy5b3b3522010-10-27 18:57:51 -0700616 const float texX = 1.0f / float(layer->width);
617 const float texY = 1.0f / float(layer->height);
Romain Guyf219da52011-01-16 12:54:25 -0800618 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700619
620 TextureVertex* mesh = mCaches.getRegionMesh();
621 GLsizei numQuads = 0;
622
Romain Guy7230a742011-01-10 22:26:16 -0800623 setupDraw();
624 setupDrawWithTexture();
625 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -0800626 setupDrawColorFilter();
Romain Guy7230a742011-01-10 22:26:16 -0800627 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
628 setupDrawProgram();
629 setupDrawDirtyRegionsDisabled();
630 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -0800631 setupDrawColorFilterUniforms();
Romain Guy7230a742011-01-10 22:26:16 -0800632 setupDrawTexture(layer->texture);
Romain Guyc038ea32011-01-12 15:08:47 -0800633 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
Romain Guy7230a742011-01-10 22:26:16 -0800634 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700635
636 for (size_t i = 0; i < count; i++) {
637 const android::Rect* r = &rects[i];
638
639 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800640 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700641 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -0800642 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -0700643
644 // TODO: Reject quads outside of the clip
645 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
646 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
647 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
648 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
649
650 numQuads++;
651
652 if (numQuads >= REGION_MESH_QUAD_COUNT) {
653 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
654 numQuads = 0;
655 mesh = mCaches.getRegionMesh();
656 }
657 }
658
659 if (numQuads > 0) {
660 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
661 }
662
663 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800664 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700665
666#if DEBUG_LAYERS_AS_REGIONS
667 uint32_t colors[] = {
668 0x7fff0000, 0x7f00ff00,
669 0x7f0000ff, 0x7fff00ff,
670 };
671
672 int offset = 0;
673 int32_t top = rects[0].top;
674 int i = 0;
675
676 for (size_t i = 0; i < count; i++) {
677 if (top != rects[i].top) {
678 offset ^= 0x2;
679 top = rects[i].top;
680 }
681
682 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
683 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
684 SkXfermode::kSrcOver_Mode);
685 }
686#endif
687
688 layer->region.clear();
689 }
690#else
691 composeLayerRect(layer, rect);
692#endif
693}
694
695void OpenGLRenderer::dirtyLayer(const float left, const float top,
696 const float right, const float bottom, const mat4 transform) {
697#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800698 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700699 Rect bounds(left, top, right, bottom);
700 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -0800701 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -0700702 }
703#endif
704}
705
706void OpenGLRenderer::dirtyLayer(const float left, const float top,
707 const float right, const float bottom) {
708#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -0800709 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700710 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -0800711 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -0800712 }
713#endif
714}
715
716void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
717#if RENDER_LAYERS_AS_REGIONS
718 if (bounds.intersect(*mSnapshot->clipRect)) {
719 bounds.snapToPixelBoundaries();
720 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
721 if (!dirty.isEmpty()) {
722 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -0700723 }
724 }
725#endif
726}
727
Romain Guy86942302010-09-12 13:02:16 -0700728void OpenGLRenderer::clearLayerRegions() {
Romain Guyaf636eb2010-12-09 17:47:21 -0800729 if (mLayers.size() == 0 || mSnapshot->isIgnored()) return;
Romain Guy86942302010-09-12 13:02:16 -0700730
Romain Guy5b3b3522010-10-27 18:57:51 -0700731 Rect clipRect(*mSnapshot->clipRect);
732 clipRect.snapToPixelBoundaries();
733
Romain Guy86942302010-09-12 13:02:16 -0700734 for (uint32_t i = 0; i < mLayers.size(); i++) {
735 Rect* bounds = mLayers.itemAt(i);
Romain Guy5b3b3522010-10-27 18:57:51 -0700736 if (clipRect.intersects(*bounds)) {
737 // Clear the framebuffer where the layer will draw
738 glScissor(bounds->left, mSnapshot->height - bounds->bottom,
739 bounds->getWidth(), bounds->getHeight());
740 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
741 glClear(GL_COLOR_BUFFER_BIT);
Romain Guy86942302010-09-12 13:02:16 -0700742
Romain Guy5b3b3522010-10-27 18:57:51 -0700743 // Restore the clip
744 dirtyClip();
745 }
Romain Guy86942302010-09-12 13:02:16 -0700746
747 delete bounds;
748 }
Romain Guy86942302010-09-12 13:02:16 -0700749
Romain Guy5b3b3522010-10-27 18:57:51 -0700750 mLayers.clear();
Romain Guy86942302010-09-12 13:02:16 -0700751}
752
Romain Guybd6b79b2010-06-26 00:13:53 -0700753///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700754// Transforms
755///////////////////////////////////////////////////////////////////////////////
756
757void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700758 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700759}
760
761void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700762 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700763}
764
765void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700766 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700767}
768
Romain Guy807daf72011-01-18 11:19:19 -0800769void OpenGLRenderer::skew(float sx, float sy) {
770 mSnapshot->transform->skew(sx, sy);
771}
772
Romain Guyf6a11b82010-06-23 17:47:49 -0700773void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700774 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700775}
776
Romain Guy41030da2010-10-13 13:40:37 -0700777const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700778 if (mSnapshot->fbo != 0) {
779 return &mSnapshot->transform->data[0];
780 }
781 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700782}
783
Romain Guyf6a11b82010-06-23 17:47:49 -0700784void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700785 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700786}
787
788void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700789 SkMatrix transform;
790 mSnapshot->transform->copyTo(transform);
791 transform.preConcat(*matrix);
792 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700793}
794
795///////////////////////////////////////////////////////////////////////////////
796// Clipping
797///////////////////////////////////////////////////////////////////////////////
798
Romain Guybb9524b2010-06-22 18:56:38 -0700799void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700800 Rect clip(*mSnapshot->clipRect);
801 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700802 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700803 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700804}
805
806const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700807 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700808}
809
Romain Guyc7d53492010-06-25 13:41:57 -0700810bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800811 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700812 return true;
813 }
814
Romain Guy1d83e192010-08-17 11:37:00 -0700815 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700816 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700817 r.snapToPixelBoundaries();
818
819 Rect clipRect(*mSnapshot->clipRect);
820 clipRect.snapToPixelBoundaries();
821
822 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700823}
824
Romain Guy079ba2c2010-07-16 14:12:24 -0700825bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
826 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700827 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700828 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700829 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700830 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700831}
832
Romain Guyf6a11b82010-06-23 17:47:49 -0700833///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -0800834// Drawing commands
835///////////////////////////////////////////////////////////////////////////////
836
837void OpenGLRenderer::setupDraw() {
838 clearLayerRegions();
839 if (mDirtyClip) {
840 setScissorFromClip();
841 }
842 mDescription.reset();
843 mSetShaderColor = false;
844 mColorSet = false;
845 mColorA = mColorR = mColorG = mColorB = 0.0f;
846 mTextureUnit = 0;
847 mTrackDirtyRegions = true;
Romain Guy8d0d4782010-12-14 20:13:35 -0800848 mTexCoordsSlot = -1;
Romain Guy70ca14e2010-12-13 18:24:33 -0800849}
850
851void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
852 mDescription.hasTexture = true;
853 mDescription.hasAlpha8Texture = isAlpha8;
854}
855
856void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -0800857 setupDrawColor(color, (color >> 24) & 0xFF);
858}
859
860void OpenGLRenderer::setupDrawColor(int color, int alpha) {
861 mColorA = alpha / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -0800862 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -0800863 mColorR = a * ((color >> 16) & 0xFF);
864 mColorG = a * ((color >> 8) & 0xFF);
865 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -0800866 mColorSet = true;
867 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
868}
869
Romain Guy86568192010-12-14 15:55:39 -0800870void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
871 mColorA = alpha / 255.0f;
872 const float a = mColorA / 255.0f;
873 mColorR = a * ((color >> 16) & 0xFF);
874 mColorG = a * ((color >> 8) & 0xFF);
875 mColorB = a * ((color ) & 0xFF);
876 mColorSet = true;
877 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
878}
879
Romain Guy70ca14e2010-12-13 18:24:33 -0800880void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
881 mColorA = a;
882 mColorR = r;
883 mColorG = g;
884 mColorB = b;
885 mColorSet = true;
886 mSetShaderColor = mDescription.setColor(r, g, b, a);
887}
888
Romain Guy86568192010-12-14 15:55:39 -0800889void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
890 mColorA = a;
891 mColorR = r;
892 mColorG = g;
893 mColorB = b;
894 mColorSet = true;
895 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
896}
897
Romain Guy70ca14e2010-12-13 18:24:33 -0800898void OpenGLRenderer::setupDrawShader() {
899 if (mShader) {
900 mShader->describe(mDescription, mCaches.extensions);
901 }
902}
903
904void OpenGLRenderer::setupDrawColorFilter() {
905 if (mColorFilter) {
906 mColorFilter->describe(mDescription, mCaches.extensions);
907 }
908}
909
910void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
911 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
912 mDescription, swapSrcDst);
913}
914
915void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
916 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
917 mDescription, swapSrcDst);
918}
919
920void OpenGLRenderer::setupDrawProgram() {
921 useProgram(mCaches.programCache.get(mDescription));
922}
923
924void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
925 mTrackDirtyRegions = false;
926}
927
928void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
929 bool ignoreTransform) {
930 mModelView.loadTranslate(left, top, 0.0f);
931 if (!ignoreTransform) {
932 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
933 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
934 } else {
935 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
936 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
937 }
938}
939
Romain Guy8d0d4782010-12-14 20:13:35 -0800940void OpenGLRenderer::setupDrawModelViewIdentity() {
941 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform);
942}
943
Romain Guy70ca14e2010-12-13 18:24:33 -0800944void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
945 bool ignoreTransform, bool ignoreModelView) {
946 if (!ignoreModelView) {
947 mModelView.loadTranslate(left, top, 0.0f);
948 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -0800949 } else {
950 mModelView.loadIdentity();
951 }
Romain Guy86568192010-12-14 15:55:39 -0800952 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
953 if (!ignoreTransform) {
954 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
955 if (mTrackDirtyRegions && dirty) {
956 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
957 }
958 } else {
959 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
960 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
961 }
Romain Guy70ca14e2010-12-13 18:24:33 -0800962}
963
964void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -0800965 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -0800966 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
967 }
968}
969
Romain Guy86568192010-12-14 15:55:39 -0800970void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -0800971 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -0800972 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -0800973 }
974}
975
Romain Guy70ca14e2010-12-13 18:24:33 -0800976void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
977 if (mShader) {
978 if (ignoreTransform) {
979 mModelView.loadInverse(*mSnapshot->transform);
980 }
981 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
982 }
983}
984
Romain Guy8d0d4782010-12-14 20:13:35 -0800985void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
986 if (mShader) {
987 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
988 }
989}
990
Romain Guy70ca14e2010-12-13 18:24:33 -0800991void OpenGLRenderer::setupDrawColorFilterUniforms() {
992 if (mColorFilter) {
993 mColorFilter->setupProgram(mCaches.currentProgram);
994 }
995}
996
997void OpenGLRenderer::setupDrawSimpleMesh() {
998 mCaches.bindMeshBuffer();
999 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1000 gMeshStride, 0);
1001}
1002
1003void OpenGLRenderer::setupDrawTexture(GLuint texture) {
1004 bindTexture(texture);
1005 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
1006
1007 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
1008 glEnableVertexAttribArray(mTexCoordsSlot);
1009}
1010
1011void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1012 if (!vertices) {
1013 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1014 } else {
1015 mCaches.unbindMeshBuffer();
1016 }
1017 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1018 gMeshStride, vertices);
Romain Guy8d0d4782010-12-14 20:13:35 -08001019 if (mTexCoordsSlot > 0) {
1020 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1021 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001022}
1023
1024void OpenGLRenderer::finishDrawTexture() {
1025 glDisableVertexAttribArray(mTexCoordsSlot);
1026}
1027
1028///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001029// Drawing
1030///////////////////////////////////////////////////////////////////////////////
1031
Romain Guyffac7fc2011-01-13 17:21:49 -08001032void OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t level) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001033 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1034 // will be performed by the display list itself
1035 if (displayList) {
Romain Guyffac7fc2011-01-13 17:21:49 -08001036 displayList->replay(*this, level);
Romain Guy0fe478e2010-11-08 12:08:41 -08001037 }
1038}
1039
Chet Haase5c13d892010-10-08 08:37:55 -07001040void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001041 const float right = left + bitmap->width();
1042 const float bottom = top + bitmap->height();
1043
1044 if (quickReject(left, top, right, bottom)) {
1045 return;
1046 }
1047
Romain Guy86568192010-12-14 15:55:39 -08001048 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001049 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001050 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001051 const AutoTexture autoCleanup(texture);
1052
Romain Guy6926c722010-07-12 20:20:03 -07001053 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -07001054}
1055
Chet Haase5c13d892010-10-08 08:37:55 -07001056void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001057 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1058 const mat4 transform(*matrix);
1059 transform.mapRect(r);
1060
Romain Guy6926c722010-07-12 20:20:03 -07001061 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1062 return;
1063 }
1064
Romain Guy86568192010-12-14 15:55:39 -08001065 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001066 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001067 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001068 const AutoTexture autoCleanup(texture);
1069
Romain Guy5b3b3522010-10-27 18:57:51 -07001070 // This could be done in a cheaper way, all we need is pass the matrix
1071 // to the vertex shader. The save/restore is a bit overkill.
1072 save(SkCanvas::kMatrix_SaveFlag);
1073 concatMatrix(matrix);
1074 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1075 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001076}
1077
Romain Guy8ba548f2010-06-30 19:21:21 -07001078void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1079 float srcLeft, float srcTop, float srcRight, float srcBottom,
1080 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001081 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001082 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1083 return;
1084 }
1085
Romain Guy746b7402010-10-26 16:27:31 -07001086 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001087 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001088 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001089 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001090 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guy8ba548f2010-06-30 19:21:21 -07001091
Romain Guy8ba548f2010-06-30 19:21:21 -07001092 const float width = texture->width;
1093 const float height = texture->height;
1094
1095 const float u1 = srcLeft / width;
1096 const float v1 = srcTop / height;
1097 const float u2 = srcRight / width;
1098 const float v2 = srcBottom / height;
1099
Romain Guy03750a02010-10-18 14:06:08 -07001100 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001101 resetDrawTextureTexCoords(u1, v1, u2, v2);
1102
Romain Guy03750a02010-10-18 14:06:08 -07001103 int alpha;
1104 SkXfermode::Mode mode;
1105 getAlphaAndMode(paint, &alpha, &mode);
1106
Romain Guy6620c6d2010-12-06 18:07:02 -08001107 if (mSnapshot->transform->isPureTranslate()) {
1108 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1109 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1110
1111 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1112 texture->id, alpha / 255.0f, mode, texture->blend,
1113 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1114 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1115 } else {
1116 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1117 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1118 GL_TRIANGLE_STRIP, gMeshCount);
1119 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001120
1121 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1122}
1123
Romain Guy4aa90572010-09-26 18:40:37 -07001124void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001125 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001126 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001127 if (quickReject(left, top, right, bottom)) {
1128 return;
1129 }
1130
Romain Guy746b7402010-10-26 16:27:31 -07001131 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001132 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001133 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001134 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001135 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guyf7f93552010-07-08 19:17:03 -07001136
1137 int alpha;
1138 SkXfermode::Mode mode;
1139 getAlphaAndMode(paint, &alpha, &mode);
1140
Romain Guy2728f962010-10-08 18:36:15 -07001141 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001142 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001143
Romain Guya5ef39a2010-12-03 16:48:20 -08001144 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001145 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001146#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001147 // Mark the current layer dirty where we are going to draw the patch
1148 if ((mSnapshot->flags & Snapshot::kFlagFboTarget) &&
1149 mSnapshot->region && mesh->hasEmptyQuads) {
1150 const size_t count = mesh->quads.size();
1151 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001152 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001153 if (pureTranslate) {
1154 const float x = (int) floorf(bounds.left + 0.5f);
1155 const float y = (int) floorf(bounds.top + 0.5f);
Romain Guy8ab40792010-12-07 13:30:10 -08001156 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Romain Guy6620c6d2010-12-06 18:07:02 -08001157 *mSnapshot->transform);
1158 } else {
1159 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom,
1160 *mSnapshot->transform);
1161 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001162 }
1163 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001164#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001165
Romain Guy6620c6d2010-12-06 18:07:02 -08001166 if (pureTranslate) {
1167 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1168 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1169
1170 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1171 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1172 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1173 true, !mesh->hasEmptyQuads);
1174 } else {
1175 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1176 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1177 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1178 true, !mesh->hasEmptyQuads);
1179 }
Romain Guy054dc182010-10-15 17:55:25 -07001180 }
Romain Guyf7f93552010-07-08 19:17:03 -07001181}
1182
Chet Haase5c13d892010-10-08 08:37:55 -07001183void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001184 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001185
Romain Guya957eea2010-12-08 18:34:42 -08001186 const bool isAA = paint->isAntiAlias();
1187 const float strokeWidth = paint->getStrokeWidth() * 0.5f;
1188 // A stroke width of 0 has a special meaningin Skia:
1189 // it draws an unscaled 1px wide line
1190 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
1191
Romain Guy759ea802010-09-16 20:49:46 -07001192 int alpha;
1193 SkXfermode::Mode mode;
1194 getAlphaAndMode(paint, &alpha, &mode);
1195
Romain Guya957eea2010-12-08 18:34:42 -08001196 int verticesCount = count >> 2;
Romain Guy7230a742011-01-10 22:26:16 -08001197 int generatedVerticesCount = 0;
Romain Guya957eea2010-12-08 18:34:42 -08001198 if (!isHairLine) {
1199 // TODO: AA needs more vertices
1200 verticesCount *= 6;
Romain Guyc95c8d62010-09-17 15:31:32 -07001201 } else {
Romain Guya957eea2010-12-08 18:34:42 -08001202 // TODO: AA will be different
1203 verticesCount *= 2;
Romain Guyc95c8d62010-09-17 15:31:32 -07001204 }
1205
Romain Guya957eea2010-12-08 18:34:42 -08001206 TextureVertex lines[verticesCount];
1207 TextureVertex* vertex = &lines[0];
Romain Guy759ea802010-09-16 20:49:46 -07001208
Romain Guy8d0d4782010-12-14 20:13:35 -08001209 setupDraw();
1210 setupDrawColor(paint->getColor(), alpha);
1211 setupDrawColorFilter();
1212 setupDrawShader();
1213 setupDrawBlending(mode);
1214 setupDrawProgram();
1215 setupDrawModelViewIdentity();
1216 setupDrawColorUniforms();
1217 setupDrawColorFilterUniforms();
1218 setupDrawShaderIdentityUniforms();
1219 setupDrawMesh(vertex);
Romain Guya957eea2010-12-08 18:34:42 -08001220
1221 if (!isHairLine) {
1222 // TODO: Handle the AA case
1223 for (int i = 0; i < count; i += 4) {
1224 // a = start point, b = end point
1225 vec2 a(points[i], points[i + 1]);
1226 vec2 b(points[i + 2], points[i + 3]);
1227
1228 // Bias to snap to the same pixels as Skia
1229 a += 0.375;
1230 b += 0.375;
1231
1232 // Find the normal to the line
1233 vec2 n = (b - a).copyNormalized() * strokeWidth;
1234 float x = n.x;
1235 n.x = -n.y;
1236 n.y = x;
1237
1238 // Four corners of the rectangle defining a thick line
1239 vec2 p1 = a - n;
1240 vec2 p2 = a + n;
1241 vec2 p3 = b + n;
1242 vec2 p4 = b - n;
1243
Romain Guy7230a742011-01-10 22:26:16 -08001244 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1245 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1246 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1247 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
Romain Guya957eea2010-12-08 18:34:42 -08001248
Romain Guy7230a742011-01-10 22:26:16 -08001249 if (!quickReject(left, top, right, bottom)) {
1250 // Draw the line as 2 triangles, could be optimized
1251 // by using only 4 vertices and the correct indices
1252 // Also we should probably used non textured vertices
1253 // when line AA is disabled to save on bandwidth
1254 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1255 TextureVertex::set(vertex++, p2.x, p2.y, 0.0f, 0.0f);
1256 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1257 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1258 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1259 TextureVertex::set(vertex++, p4.x, p4.y, 0.0f, 0.0f);
1260
1261 generatedVerticesCount += 6;
1262
1263 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1264 }
Romain Guya957eea2010-12-08 18:34:42 -08001265 }
1266
Romain Guy7230a742011-01-10 22:26:16 -08001267 if (generatedVerticesCount > 0) {
1268 // GL_LINE does not give the result we want to match Skia
1269 glDrawArrays(GL_TRIANGLES, 0, generatedVerticesCount);
1270 }
Romain Guya957eea2010-12-08 18:34:42 -08001271 } else {
1272 // TODO: Handle the AA case
1273 for (int i = 0; i < count; i += 4) {
Romain Guy7230a742011-01-10 22:26:16 -08001274 const float left = fmin(points[i], points[i + 1]);
1275 const float right = fmax(points[i], points[i + 1]);
1276 const float top = fmin(points[i + 2], points[i + 3]);
1277 const float bottom = fmax(points[i + 2], points[i + 3]);
1278
1279 if (!quickReject(left, top, right, bottom)) {
1280 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1281 TextureVertex::set(vertex++, points[i + 2], points[i + 3], 0.0f, 0.0f);
1282
1283 generatedVerticesCount += 2;
1284
1285 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1286 }
Romain Guya957eea2010-12-08 18:34:42 -08001287 }
Romain Guy8d0d4782010-12-14 20:13:35 -08001288
Romain Guy7230a742011-01-10 22:26:16 -08001289 if (generatedVerticesCount > 0) {
1290 glLineWidth(1.0f);
1291 glDrawArrays(GL_LINES, 0, generatedVerticesCount);
1292 }
Romain Guy29d89972010-09-22 16:10:57 -07001293 }
Romain Guy759ea802010-09-16 20:49:46 -07001294}
1295
Romain Guy85bf02f2010-06-22 13:11:24 -07001296void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001297 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001298 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001299
Romain Guyae88e5e2010-10-22 17:49:18 -07001300 Rect& clip(*mSnapshot->clipRect);
1301 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001302
Romain Guy3d58c032010-07-14 16:34:53 -07001303 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001304}
Romain Guy9d5316e2010-06-24 19:30:36 -07001305
Romain Guy01d58e42011-01-19 21:54:02 -08001306void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1307 float rx, float ry, SkPaint* paint) {
1308 if (mSnapshot->isIgnored()) return;
1309
1310 glActiveTexture(gTextureUnits[0]);
1311
1312 const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1313 right - left, bottom - top, rx, ry, paint);
1314 if (!texture) return;
1315 const AutoTexture autoCleanup(texture);
1316
1317 const float x = left + texture->left - texture->offset;
1318 const float y = top + texture->top - texture->offset;
1319
1320 drawPathTexture(texture, x, y, paint);
1321}
1322
1323void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1324 if (mSnapshot->isIgnored()) return;
1325
1326 glActiveTexture(gTextureUnits[0]);
1327
1328 const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
1329 if (!texture) return;
1330 const AutoTexture autoCleanup(texture);
1331
1332 const float left = (x - radius) + texture->left - texture->offset;
1333 const float top = (y - radius) + texture->top - texture->offset;
1334
1335 drawPathTexture(texture, left, top, paint);
1336}
1337
Chet Haase5c13d892010-10-08 08:37:55 -07001338void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -07001339 if (quickReject(left, top, right, bottom)) {
1340 return;
1341 }
1342
Romain Guy026c5e162010-06-28 17:12:22 -07001343 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07001344 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001345 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1346 if (!isMode) {
1347 // Assume SRC_OVER
1348 mode = SkXfermode::kSrcOver_Mode;
1349 }
1350 } else {
1351 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07001352 }
1353
1354 // Skia draws using the color's alpha channel if < 255
1355 // Otherwise, it uses the paint's alpha
1356 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07001357 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -07001358 color |= p->getAlpha() << 24;
1359 }
1360
1361 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -07001362}
Romain Guy9d5316e2010-06-24 19:30:36 -07001363
Romain Guye8e62a42010-07-23 18:55:21 -07001364void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1365 float x, float y, SkPaint* paint) {
Romain Guyb146b122010-12-15 17:06:45 -08001366 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07001367 return;
1368 }
Romain Guyaf636eb2010-12-09 17:47:21 -08001369 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07001370
Romain Guyf607bdc2010-09-10 19:20:06 -07001371 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -07001372
Romain Guya674ab72010-08-10 17:26:42 -07001373 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07001374 switch (paint->getTextAlign()) {
1375 case SkPaint::kCenter_Align:
1376 length = paint->measureText(text, bytesCount);
1377 x -= length / 2.0f;
1378 break;
1379 case SkPaint::kRight_Align:
1380 length = paint->measureText(text, bytesCount);
1381 x -= length;
1382 break;
1383 default:
1384 break;
1385 }
1386
Romain Guy6620c6d2010-12-06 18:07:02 -08001387 // TODO: Handle paint->getTextScaleX()
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001388 const float oldX = x;
1389 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08001390 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
1391 if (pureTranslate) {
1392 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
1393 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
1394 }
1395
Romain Guyb45c0c92010-08-26 20:35:23 -07001396 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
1397 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07001398 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07001399
Romain Guy86568192010-12-14 15:55:39 -08001400 int alpha;
1401 SkXfermode::Mode mode;
1402 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07001403
Romain Guy1e45aae2010-08-13 19:39:53 -07001404 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07001405 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -07001406 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -07001407 count, mShadowRadius);
1408 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07001409
Romain Guy86568192010-12-14 15:55:39 -08001410 const float sx = x - shadow->left + mShadowDx;
1411 const float sy = y - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07001412
Romain Guy86568192010-12-14 15:55:39 -08001413 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
1414
1415 glActiveTexture(gTextureUnits[0]);
1416 setupDraw();
1417 setupDrawWithTexture(true);
1418 setupDrawAlpha8Color(mShadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
1419 setupDrawBlending(true, mode);
1420 setupDrawProgram();
1421 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height, pureTranslate);
1422 setupDrawTexture(shadow->id);
1423 setupDrawPureColorUniforms();
1424 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1425
Romain Guy0a417492010-08-16 20:26:20 -07001426 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy86568192010-12-14 15:55:39 -08001427 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07001428 }
1429
Romain Guyb146b122010-12-15 17:06:45 -08001430 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
1431 return;
1432 }
1433
Romain Guy6620c6d2010-12-06 18:07:02 -08001434 // Pick the appropriate texture filtering
1435 bool linearFilter = mSnapshot->transform->changesBounds();
1436 if (pureTranslate && !linearFilter) {
1437 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
1438 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001439
Romain Guy86568192010-12-14 15:55:39 -08001440 glActiveTexture(gTextureUnits[0]);
1441 setupDraw();
1442 setupDrawDirtyRegionsDisabled();
1443 setupDrawWithTexture(true);
1444 setupDrawAlpha8Color(paint->getColor(), alpha);
1445 setupDrawColorFilter();
1446 setupDrawShader();
1447 setupDrawBlending(true, mode);
1448 setupDrawProgram();
1449 setupDrawModelView(x, y, x, y, pureTranslate, true);
1450 setupDrawTexture(fontRenderer.getTexture(linearFilter));
1451 setupDrawPureColorUniforms();
1452 setupDrawColorFilterUniforms();
1453 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07001454
Romain Guy6620c6d2010-12-06 18:07:02 -08001455 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07001456 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
1457
1458#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08001459 bool hasActiveLayer = hasLayer();
Romain Guy5b3b3522010-10-27 18:57:51 -07001460#else
Romain Guyf219da52011-01-16 12:54:25 -08001461 bool hasActiveLayer = false;
Romain Guy5b3b3522010-10-27 18:57:51 -07001462#endif
Romain Guy9d13fe252010-10-15 16:06:03 -07001463
Romain Guy03750a02010-10-18 14:06:08 -07001464 mCaches.unbindMeshBuffer();
Romain Guy6620c6d2010-12-06 18:07:02 -08001465 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guyf219da52011-01-16 12:54:25 -08001466 hasActiveLayer ? &bounds : NULL)) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001467#if RENDER_LAYERS_AS_REGIONS
Romain Guyf219da52011-01-16 12:54:25 -08001468 if (hasActiveLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001469 if (!pureTranslate) {
1470 mSnapshot->transform->mapRect(bounds);
1471 }
Romain Guyf219da52011-01-16 12:54:25 -08001472 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001473 }
1474#endif
1475 }
Romain Guy694b5192010-07-21 21:33:20 -07001476
1477 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001478 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07001479
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001480 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07001481}
1482
Romain Guy7fbcc042010-08-04 15:40:07 -07001483void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001484 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001485
Romain Guy01d58e42011-01-19 21:54:02 -08001486 glActiveTexture(gTextureUnits[0]);
Romain Guy7fbcc042010-08-04 15:40:07 -07001487
Romain Guyfb8b7632010-08-23 21:05:08 -07001488 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001489 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001490 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07001491
Romain Guy8b55f372010-08-18 17:10:07 -07001492 const float x = texture->left - texture->offset;
1493 const float y = texture->top - texture->offset;
1494
Romain Guy01d58e42011-01-19 21:54:02 -08001495 drawPathTexture(texture, x, y, paint);
Romain Guy7fbcc042010-08-04 15:40:07 -07001496}
1497
Romain Guyada830f2011-01-13 12:13:20 -08001498void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
1499 if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
Romain Guy6c319ca2011-01-11 14:29:25 -08001500 return;
1501 }
1502
1503 glActiveTexture(gTextureUnits[0]);
Romain Guy6c319ca2011-01-11 14:29:25 -08001504
1505 int alpha;
1506 SkXfermode::Mode mode;
1507 getAlphaAndMode(paint, &alpha, &mode);
1508
Romain Guyada830f2011-01-13 12:13:20 -08001509 layer->alpha = alpha;
1510 layer->mode = mode;
Romain Guy6c319ca2011-01-11 14:29:25 -08001511
Romain Guyf219da52011-01-16 12:54:25 -08001512
1513#if RENDER_LAYERS_AS_REGIONS
1514 if (layer->region.isRect()) {
1515 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1516 composeLayerRect(layer, r);
1517 } else if (!layer->region.isEmpty() && layer->mesh) {
1518 const Rect& rect = layer->layer;
1519
1520 setupDraw();
1521 setupDrawWithTexture();
1522 setupDrawColor(alpha, alpha, alpha, alpha);
1523 setupDrawColorFilter();
1524 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
1525 setupDrawProgram();
1526 setupDrawDirtyRegionsDisabled();
1527 setupDrawPureColorUniforms();
1528 setupDrawColorFilterUniforms();
1529 setupDrawTexture(layer->texture);
1530 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1531 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
1532
1533 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
1534 GL_UNSIGNED_SHORT, layer->meshIndices);
1535
1536 finishDrawTexture();
1537 }
1538#else
Romain Guyada830f2011-01-13 12:13:20 -08001539 const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1540 composeLayerRect(layer, r);
Romain Guyf219da52011-01-16 12:54:25 -08001541#endif
Romain Guy6c319ca2011-01-11 14:29:25 -08001542}
1543
Romain Guy6926c722010-07-12 20:20:03 -07001544///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07001545// Shaders
1546///////////////////////////////////////////////////////////////////////////////
1547
1548void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07001549 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07001550}
1551
Romain Guy06f96e22010-07-30 19:18:16 -07001552void OpenGLRenderer::setupShader(SkiaShader* shader) {
1553 mShader = shader;
1554 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001555 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07001556 }
Romain Guy7fac2e12010-07-16 17:10:13 -07001557}
1558
Romain Guyd27977d2010-07-14 19:18:51 -07001559///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07001560// Color filters
1561///////////////////////////////////////////////////////////////////////////////
1562
1563void OpenGLRenderer::resetColorFilter() {
1564 mColorFilter = NULL;
1565}
1566
1567void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
1568 mColorFilter = filter;
1569}
1570
1571///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07001572// Drop shadow
1573///////////////////////////////////////////////////////////////////////////////
1574
1575void OpenGLRenderer::resetShadow() {
1576 mHasShadow = false;
1577}
1578
1579void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
1580 mHasShadow = true;
1581 mShadowRadius = radius;
1582 mShadowDx = dx;
1583 mShadowDy = dy;
1584 mShadowColor = color;
1585}
1586
1587///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07001588// Drawing implementation
1589///////////////////////////////////////////////////////////////////////////////
1590
Romain Guy01d58e42011-01-19 21:54:02 -08001591void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
1592 float x, float y, SkPaint* paint) {
1593 if (quickReject(x, y, x + texture->width, y + texture->height)) {
1594 return;
1595 }
1596
1597 int alpha;
1598 SkXfermode::Mode mode;
1599 getAlphaAndMode(paint, &alpha, &mode);
1600
1601 setupDraw();
1602 setupDrawWithTexture(true);
1603 setupDrawAlpha8Color(paint->getColor(), alpha);
1604 setupDrawColorFilter();
1605 setupDrawShader();
1606 setupDrawBlending(true, mode);
1607 setupDrawProgram();
1608 setupDrawModelView(x, y, x + texture->width, y + texture->height);
1609 setupDrawTexture(texture->id);
1610 setupDrawPureColorUniforms();
1611 setupDrawColorFilterUniforms();
1612 setupDrawShaderUniforms();
1613 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1614
1615 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1616
1617 finishDrawTexture();
1618}
1619
Romain Guyf607bdc2010-09-10 19:20:06 -07001620// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001621#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1622#define kStdUnderline_Offset (1.0f / 9.0f)
1623#define kStdUnderline_Thickness (1.0f / 18.0f)
1624
1625void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1626 float x, float y, SkPaint* paint) {
1627 // Handle underline and strike-through
1628 uint32_t flags = paint->getFlags();
1629 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1630 float underlineWidth = length;
1631 // If length is > 0.0f, we already measured the text for the text alignment
1632 if (length <= 0.0f) {
1633 underlineWidth = paint->measureText(text, bytesCount);
1634 }
1635
1636 float offsetX = 0;
1637 switch (paint->getTextAlign()) {
1638 case SkPaint::kCenter_Align:
1639 offsetX = underlineWidth * 0.5f;
1640 break;
1641 case SkPaint::kRight_Align:
1642 offsetX = underlineWidth;
1643 break;
1644 default:
1645 break;
1646 }
1647
1648 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001649 const float textSize = paint->getTextSize();
1650 const float strokeWidth = textSize * kStdUnderline_Thickness;
Romain Guy0a417492010-08-16 20:26:20 -07001651
Romain Guye20ecbd2010-09-22 19:49:04 -07001652 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001653 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001654
1655 const int pointsCount = 4 * (flags & SkPaint::kStrikeThruText_Flag ? 2 : 1);
1656 float points[pointsCount];
1657 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001658
1659 if (flags & SkPaint::kUnderlineText_Flag) {
1660 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001661 points[currentPoint++] = left;
1662 points[currentPoint++] = top;
1663 points[currentPoint++] = left + underlineWidth;
1664 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001665 }
1666
1667 if (flags & SkPaint::kStrikeThruText_Flag) {
1668 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001669 points[currentPoint++] = left;
1670 points[currentPoint++] = top;
1671 points[currentPoint++] = left + underlineWidth;
1672 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001673 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001674
1675 SkPaint linesPaint(*paint);
1676 linesPaint.setStrokeWidth(strokeWidth);
1677
1678 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001679 }
1680 }
1681}
1682
Romain Guy026c5e162010-06-28 17:12:22 -07001683void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001684 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07001685 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001686 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001687 color |= 0x00ffffff;
1688 }
1689
Romain Guy70ca14e2010-12-13 18:24:33 -08001690 setupDraw();
1691 setupDrawColor(color);
1692 setupDrawShader();
1693 setupDrawColorFilter();
1694 setupDrawBlending(mode);
1695 setupDrawProgram();
1696 setupDrawModelView(left, top, right, bottom, ignoreTransform);
1697 setupDrawColorUniforms();
1698 setupDrawShaderUniforms(ignoreTransform);
1699 setupDrawColorFilterUniforms();
1700 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07001701
Romain Guyc95c8d62010-09-17 15:31:32 -07001702 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1703}
1704
Romain Guy82ba8142010-07-09 13:25:56 -07001705void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07001706 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001707 int alpha;
1708 SkXfermode::Mode mode;
1709 getAlphaAndMode(paint, &alpha, &mode);
1710
Romain Guy8164c2d2010-10-25 18:03:28 -07001711 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1712
Romain Guy6620c6d2010-12-06 18:07:02 -08001713 if (mSnapshot->transform->isPureTranslate()) {
1714 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1715 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1716
1717 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1718 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
1719 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
1720 } else {
1721 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
1722 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
1723 GL_TRIANGLE_STRIP, gMeshCount);
1724 }
Romain Guy85bf02f2010-06-22 13:11:24 -07001725}
1726
Romain Guybd6b79b2010-06-26 00:13:53 -07001727void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001728 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1729 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07001730 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001731}
1732
1733void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001734 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001735 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07001736 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001737
Romain Guy746b7402010-10-26 16:27:31 -07001738 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08001739 setupDrawWithTexture();
1740 setupDrawColor(alpha, alpha, alpha, alpha);
1741 setupDrawColorFilter();
1742 setupDrawBlending(blend, mode, swapSrcDst);
1743 setupDrawProgram();
1744 if (!dirty) {
1745 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07001746 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001747 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001748 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001749 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08001750 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001751 }
Romain Guy86568192010-12-14 15:55:39 -08001752 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08001753 setupDrawColorFilterUniforms();
1754 setupDrawTexture(texture);
1755 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07001756
Romain Guy6820ac82010-09-15 18:11:50 -07001757 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08001758
1759 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07001760}
1761
Romain Guya5aed0d2010-09-09 14:42:43 -07001762void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001763 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001764 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1765 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001766 if (mode < SkXfermode::kPlus_Mode) {
1767 if (!mCaches.blend) {
1768 glEnable(GL_BLEND);
1769 }
Romain Guy82ba8142010-07-09 13:25:56 -07001770
Romain Guyf607bdc2010-09-10 19:20:06 -07001771 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1772 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001773
Romain Guya5aed0d2010-09-09 14:42:43 -07001774 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1775 glBlendFunc(sourceMode, destMode);
1776 mCaches.lastSrcMode = sourceMode;
1777 mCaches.lastDstMode = destMode;
1778 }
1779 } else {
1780 // These blend modes are not supported by OpenGL directly and have
1781 // to be implemented using shaders. Since the shader will perform
1782 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07001783 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001784 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001785 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001786 }
1787
1788 if (mCaches.blend) {
1789 glDisable(GL_BLEND);
1790 }
1791 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001792 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001793 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001794 glDisable(GL_BLEND);
1795 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001796 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001797}
1798
Romain Guy889f8d12010-07-29 14:37:42 -07001799bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001800 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001801 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001802 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001803 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001804 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001805 }
Romain Guy6926c722010-07-12 20:20:03 -07001806 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001807}
1808
Romain Guy026c5e162010-06-28 17:12:22 -07001809void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001810 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001811 TextureVertex::setUV(v++, u1, v1);
1812 TextureVertex::setUV(v++, u2, v1);
1813 TextureVertex::setUV(v++, u1, v2);
1814 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001815}
1816
Chet Haase5c13d892010-10-08 08:37:55 -07001817void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07001818 if (paint) {
Romain Guy746b7402010-10-26 16:27:31 -07001819 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001820 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1821 if (!isMode) {
1822 // Assume SRC_OVER
1823 *mode = SkXfermode::kSrcOver_Mode;
1824 }
1825 } else {
1826 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001827 }
1828
1829 // Skia draws using the color's alpha channel if < 255
1830 // Otherwise, it uses the paint's alpha
1831 int color = paint->getColor();
1832 *alpha = (color >> 24) & 0xFF;
1833 if (*alpha == 255) {
1834 *alpha = paint->getAlpha();
1835 }
1836 } else {
1837 *mode = SkXfermode::kSrcOver_Mode;
1838 *alpha = 255;
1839 }
Romain Guy026c5e162010-06-28 17:12:22 -07001840}
1841
Romain Guya5aed0d2010-09-09 14:42:43 -07001842SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1843 if (mode == NULL) {
1844 return SkXfermode::kSrcOver_Mode;
1845 }
1846 return mode->fMode;
1847}
1848
Romain Guy746b7402010-10-26 16:27:31 -07001849void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
Romain Guy8164c2d2010-10-25 18:03:28 -07001850 bool bound = false;
1851 if (wrapS != texture->wrapS) {
1852 glBindTexture(GL_TEXTURE_2D, texture->id);
1853 bound = true;
1854 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1855 texture->wrapS = wrapS;
1856 }
1857 if (wrapT != texture->wrapT) {
Romain Guy3e3ba152010-10-25 18:33:16 -07001858 if (!bound) {
Romain Guy3e3ba152010-10-25 18:33:16 -07001859 glBindTexture(GL_TEXTURE_2D, texture->id);
1860 }
Romain Guy8164c2d2010-10-25 18:03:28 -07001861 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1862 texture->wrapT = wrapT;
1863 }
Romain Guya1db5742010-07-20 13:09:13 -07001864}
1865
Romain Guy9d5316e2010-06-24 19:30:36 -07001866}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001867}; // namespace android