blob: 99bb6f01031afcd3537645cf9ea4f953d3b6bf38 [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 Guyda8532c2010-08-31 11:50:35 -0700181void OpenGLRenderer::acquireContext() {
182 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
191void OpenGLRenderer::releaseContext() {
Romain Guyeb993562010-10-05 18:14:38 -0700192 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
Romain Guyda8532c2010-08-31 11:50:35 -0700193
194 glEnable(GL_SCISSOR_TEST);
Romain Guy746b7402010-10-26 16:27:31 -0700195 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700196
Romain Guyf607bdc2010-09-10 19:20:06 -0700197 glDisable(GL_DITHER);
198
199 glBindFramebuffer(GL_FRAMEBUFFER, 0);
Romain Guy03750a02010-10-18 14:06:08 -0700200 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700201
Romain Guy50c0f092010-10-19 11:42:22 -0700202 mCaches.blend = true;
203 glEnable(GL_BLEND);
204 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
205 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700206}
207
Romain Guyf6a11b82010-06-23 17:47:49 -0700208///////////////////////////////////////////////////////////////////////////////
209// State management
210///////////////////////////////////////////////////////////////////////////////
211
Romain Guybb9524b2010-06-22 18:56:38 -0700212int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700213 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700214}
215
216int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700217 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700218}
219
220void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700221 if (mSaveCount > 1) {
222 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700223 }
Romain Guybb9524b2010-06-22 18:56:38 -0700224}
225
226void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700227 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700228
Romain Guy8fb95422010-08-17 18:38:51 -0700229 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700230 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700231 }
Romain Guybb9524b2010-06-22 18:56:38 -0700232}
233
Romain Guy8aef54f2010-09-01 15:13:49 -0700234int OpenGLRenderer::saveSnapshot(int flags) {
235 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700236 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700237}
238
239bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700240 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700241 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700242 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700243
Romain Guybd6b79b2010-06-26 00:13:53 -0700244 sp<Snapshot> current = mSnapshot;
245 sp<Snapshot> previous = mSnapshot->previous;
246
Romain Guyeb993562010-10-05 18:14:38 -0700247 if (restoreOrtho) {
248 Rect& r = previous->viewport;
249 glViewport(r.left, r.top, r.right, r.bottom);
250 mOrthoMatrix.load(current->orthoMatrix);
251 }
252
Romain Guy8b55f372010-08-18 17:10:07 -0700253 mSaveCount--;
254 mSnapshot = previous;
255
Romain Guy2542d192010-08-18 11:47:12 -0700256 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700257 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700258 }
Romain Guy2542d192010-08-18 11:47:12 -0700259
Romain Guy5ec99242010-11-03 16:19:08 -0700260 if (restoreLayer) {
261 composeLayer(current, previous);
262 }
263
Romain Guy2542d192010-08-18 11:47:12 -0700264 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700265}
266
Romain Guyf6a11b82010-06-23 17:47:49 -0700267///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700268// Layers
269///////////////////////////////////////////////////////////////////////////////
270
271int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chet Haase5c13d892010-10-08 08:37:55 -0700272 SkPaint* p, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700273 const GLuint previousFbo = mSnapshot->fbo;
274 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700275
Romain Guyaf636eb2010-12-09 17:47:21 -0800276 if (!mSnapshot->isIgnored()) {
Romain Guye45362c2010-11-03 19:58:32 -0700277 int alpha = 255;
278 SkXfermode::Mode mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700279
Romain Guye45362c2010-11-03 19:58:32 -0700280 if (p) {
281 alpha = p->getAlpha();
282 if (!mCaches.extensions.hasFramebufferFetch()) {
283 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
284 if (!isMode) {
285 // Assume SRC_OVER
286 mode = SkXfermode::kSrcOver_Mode;
287 }
288 } else {
289 mode = getXfermode(p->getXfermode());
Romain Guya5aed0d2010-09-09 14:42:43 -0700290 }
291 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700292 mode = SkXfermode::kSrcOver_Mode;
Romain Guyd55a8612010-06-28 17:42:46 -0700293 }
Romain Guyd55a8612010-06-28 17:42:46 -0700294
Romain Guydbc26d22010-10-11 17:58:29 -0700295 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
296 }
Romain Guyd55a8612010-06-28 17:42:46 -0700297
298 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700299}
300
301int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
302 int alpha, int flags) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700303 if (alpha >= 255 - ALPHA_THRESHOLD) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700304 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700305 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700306 SkPaint paint;
307 paint.setAlpha(alpha);
308 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700309 }
Romain Guyd55a8612010-06-28 17:42:46 -0700310}
Romain Guybd6b79b2010-06-26 00:13:53 -0700311
Romain Guy1c740bc2010-09-13 18:00:09 -0700312/**
313 * Layers are viewed by Skia are slightly different than layers in image editing
314 * programs (for instance.) When a layer is created, previously created layers
315 * and the frame buffer still receive every drawing command. For instance, if a
316 * layer is created and a shape intersecting the bounds of the layers and the
317 * framebuffer is draw, the shape will be drawn on both (unless the layer was
318 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
319 *
320 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
321 * texture. Unfortunately, this is inefficient as it requires every primitive to
322 * be drawn n + 1 times, where n is the number of active layers. In practice this
323 * means, for every primitive:
324 * - Switch active frame buffer
325 * - Change viewport, clip and projection matrix
326 * - Issue the drawing
327 *
328 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700329 * To avoid this, layers are implemented in a different way here, at least in the
330 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
331 * is set. When this flag is set we can redirect all drawing operations into a
332 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700333 *
334 * This implementation relies on the frame buffer being at least RGBA 8888. When
335 * a layer is created, only a texture is created, not an FBO. The content of the
336 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700337 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700338 * buffer and drawing continues as normal. This technique therefore treats the
339 * frame buffer as a scratch buffer for the layers.
340 *
341 * To compose the layers back onto the frame buffer, each layer texture
342 * (containing the original frame buffer data) is drawn as a simple quad over
343 * the frame buffer. The trick is that the quad is set as the composition
344 * destination in the blending equation, and the frame buffer becomes the source
345 * of the composition.
346 *
347 * Drawing layers with an alpha value requires an extra step before composition.
348 * An empty quad is drawn over the layer's region in the frame buffer. This quad
349 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
350 * quad is used to multiply the colors in the frame buffer. This is achieved by
351 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
352 * GL_ZERO, GL_SRC_ALPHA.
353 *
354 * Because glCopyTexImage2D() can be slow, an alternative implementation might
355 * be use to draw a single clipped layer. The implementation described above
356 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700357 *
358 * (1) The frame buffer is actually not cleared right away. To allow the GPU
359 * to potentially optimize series of calls to glCopyTexImage2D, the frame
360 * buffer is left untouched until the first drawing operation. Only when
361 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700362 */
Romain Guyd55a8612010-06-28 17:42:46 -0700363bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
Romain Guyeb993562010-10-05 18:14:38 -0700364 float right, float bottom, int alpha, SkXfermode::Mode mode,
365 int flags, GLuint previousFbo) {
366 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700367 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700368
Romain Guyeb993562010-10-05 18:14:38 -0700369 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
370
Romain Guyf607bdc2010-09-10 19:20:06 -0700371 // Window coordinates of the layer
Romain Guy8aef54f2010-09-01 15:13:49 -0700372 Rect bounds(left, top, right, bottom);
Romain Guyae88e5e2010-10-22 17:49:18 -0700373 if (fboLayer) {
374 // Clear the previous layer regions before we change the viewport
375 clearLayerRegions();
376 } else {
Romain Guyeb993562010-10-05 18:14:38 -0700377 mSnapshot->transform->mapRect(bounds);
Romain Guyae517592010-10-22 10:40:27 -0700378
Romain Guyeb993562010-10-05 18:14:38 -0700379 // Layers only make sense if they are in the framebuffer's bounds
Romain Guy58ae6db2010-10-22 11:16:37 -0700380 bounds.intersect(*snapshot->clipRect);
Romain Guyae517592010-10-22 10:40:27 -0700381
Romain Guyae88e5e2010-10-22 17:49:18 -0700382 // We cannot work with sub-pixels in this case
383 bounds.snapToPixelBoundaries();
384
Romain Guyae517592010-10-22 10:40:27 -0700385 // When the layer is not an FBO, we may use glCopyTexImage so we
386 // need to make sure the layer does not extend outside the bounds
387 // of the framebuffer
388 bounds.intersect(snapshot->previous->viewport);
Romain Guyeb993562010-10-05 18:14:38 -0700389 }
Romain Guybf434112010-09-16 14:40:17 -0700390
Romain Guy746b7402010-10-26 16:27:31 -0700391 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
392 bounds.getHeight() > mCaches.maxTextureSize) {
Romain Guy32963c32010-12-10 14:43:41 -0800393 snapshot->empty = fboLayer;
Romain Guydbc26d22010-10-11 17:58:29 -0700394 } else {
Romain Guye45362c2010-11-03 19:58:32 -0700395 snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
Romain Guydbc26d22010-10-11 17:58:29 -0700396 }
397
398 // Bail out if we won't draw in this snapshot
Romain Guyaf636eb2010-12-09 17:47:21 -0800399 if (snapshot->invisible || snapshot->empty) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700400 return false;
401 }
Romain Guyf18fd992010-07-08 11:45:51 -0700402
Romain Guy5b3b3522010-10-27 18:57:51 -0700403 glActiveTexture(gTextureUnits[0]);
Romain Guy8550c4c2010-10-08 15:49:53 -0700404 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700405 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700406 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700407 }
408
Romain Guydda570202010-07-06 11:39:32 -0700409 layer->mode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -0700410 layer->alpha = alpha;
Romain Guy8aef54f2010-09-01 15:13:49 -0700411 layer->layer.set(bounds);
Romain Guy8550c4c2010-10-08 15:49:53 -0700412 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
413 bounds.getWidth() / float(layer->width), 0.0f);
Romain Guy171c5922011-01-06 10:04:23 -0800414 layer->colorFilter = mColorFilter;
Romain Guydda570202010-07-06 11:39:32 -0700415
Romain Guy8fb95422010-08-17 18:38:51 -0700416 // Save the layer in the snapshot
417 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700418 snapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700419
Romain Guyeb993562010-10-05 18:14:38 -0700420 if (fboLayer) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700421 return createFboLayer(layer, bounds, snapshot, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700422 } else {
423 // Copy the framebuffer into the layer
424 glBindTexture(GL_TEXTURE_2D, layer->texture);
425
Romain Guyae88e5e2010-10-22 17:49:18 -0700426 if (layer->empty) {
427 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left,
428 snapshot->height - bounds.bottom, layer->width, layer->height, 0);
429 layer->empty = false;
430 } else {
431 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
432 snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
433 }
Romain Guyeb993562010-10-05 18:14:38 -0700434
435 // Enqueue the buffer coordinates to clear the corresponding region later
436 mLayers.push(new Rect(bounds));
437 }
Romain Guyf86ef572010-07-01 11:05:42 -0700438
Romain Guyd55a8612010-06-28 17:42:46 -0700439 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700440}
441
Romain Guy5b3b3522010-10-27 18:57:51 -0700442bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
443 GLuint previousFbo) {
444 layer->fbo = mCaches.fboCache.get();
445
446#if RENDER_LAYERS_AS_REGIONS
447 snapshot->region = &snapshot->layer->region;
448 snapshot->flags |= Snapshot::kFlagFboTarget;
449#endif
450
451 Rect clip(bounds);
452 snapshot->transform->mapRect(clip);
453 clip.intersect(*snapshot->clipRect);
454 clip.snapToPixelBoundaries();
455 clip.intersect(snapshot->previous->viewport);
456
457 mat4 inverse;
458 inverse.loadInverse(*mSnapshot->transform);
459
460 inverse.mapRect(clip);
461 clip.snapToPixelBoundaries();
462 clip.intersect(bounds);
Romain Guy5ec99242010-11-03 16:19:08 -0700463 clip.translate(-bounds.left, -bounds.top);
Romain Guy5b3b3522010-10-27 18:57:51 -0700464
465 snapshot->flags |= Snapshot::kFlagIsFboLayer;
466 snapshot->fbo = layer->fbo;
467 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700468 snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
469 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
470 snapshot->height = bounds.getHeight();
471 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
472 snapshot->orthoMatrix.load(mOrthoMatrix);
473
474 // Bind texture to FBO
475 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
476 glBindTexture(GL_TEXTURE_2D, layer->texture);
477
478 // Initialize the texture if needed
479 if (layer->empty) {
480 layer->empty = false;
481 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
482 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
483 }
484
485 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
486 layer->texture, 0);
487
488#if DEBUG_LAYERS_AS_REGIONS
489 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
490 if (status != GL_FRAMEBUFFER_COMPLETE) {
491 LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
492
493 glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
494 glDeleteTextures(1, &layer->texture);
495 mCaches.fboCache.put(layer->fbo);
496
497 delete layer;
498
499 return false;
500 }
501#endif
502
503 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
504 glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
505 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
506 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
507 glClear(GL_COLOR_BUFFER_BIT);
508
509 dirtyClip();
510
511 // Change the ortho projection
512 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
513 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
514
515 return true;
516}
517
Romain Guy1c740bc2010-09-13 18:00:09 -0700518/**
519 * Read the documentation of createLayer() before doing anything in this method.
520 */
Romain Guy1d83e192010-08-17 11:37:00 -0700521void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
522 if (!current->layer) {
523 LOGE("Attempting to compose a layer that does not exist");
524 return;
525 }
526
Romain Guy5b3b3522010-10-27 18:57:51 -0700527 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700528
529 if (fboLayer) {
530 // Unbind current FBO and restore previous one
531 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
532 }
533
Romain Guy1d83e192010-08-17 11:37:00 -0700534 Layer* layer = current->layer;
535 const Rect& rect = layer->layer;
536
Romain Guyeb993562010-10-05 18:14:38 -0700537 if (!fboLayer && layer->alpha < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700538 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700539 layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700540 // Required below, composeLayerRect() will divide by 255
541 layer->alpha = 255;
Romain Guyf607bdc2010-09-10 19:20:06 -0700542 }
543
Romain Guy03750a02010-10-18 14:06:08 -0700544 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700545
Romain Guy746b7402010-10-26 16:27:31 -0700546 glActiveTexture(gTextureUnits[0]);
Romain Guy1d83e192010-08-17 11:37:00 -0700547
Romain Guy5b3b3522010-10-27 18:57:51 -0700548 // When the layer is stored in an FBO, we can save a bit of fillrate by
549 // drawing only the dirty region
550 if (fboLayer) {
551 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy171c5922011-01-06 10:04:23 -0800552 if (layer->colorFilter) {
553 setupColorFilter(layer->colorFilter);
554 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700555 composeLayerRegion(layer, rect);
Romain Guy171c5922011-01-06 10:04:23 -0800556 if (layer->colorFilter) {
557 resetColorFilter();
558 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700559 } else {
560 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
561 composeLayerRect(layer, rect, true);
562 }
Romain Guy8b55f372010-08-18 17:10:07 -0700563
Romain Guyeb993562010-10-05 18:14:38 -0700564 if (fboLayer) {
565 // Detach the texture from the FBO
566 glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
567 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
568 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
569
570 // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
571 mCaches.fboCache.put(current->fbo);
572 }
573
Romain Guy746b7402010-10-26 16:27:31 -0700574 dirtyClip();
575
Romain Guyeb993562010-10-05 18:14:38 -0700576 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -0700577 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700578 LAYER_LOGD("Deleting layer");
Romain Guy1d83e192010-08-17 11:37:00 -0700579 glDeleteTextures(1, &layer->texture);
Romain Guy1d83e192010-08-17 11:37:00 -0700580 delete layer;
581 }
582}
583
Romain Guy5b3b3522010-10-27 18:57:51 -0700584void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
585 const Rect& texCoords = layer->texCoords;
586 resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
587
588 drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
589 layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
590 &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap);
591
592 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
593}
594
595void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
596#if RENDER_LAYERS_AS_REGIONS
597 if (layer->region.isRect()) {
598 composeLayerRect(layer, rect);
599 layer->region.clear();
600 return;
601 }
602
603 if (!layer->region.isEmpty()) {
604 size_t count;
605 const android::Rect* rects = layer->region.getArray(&count);
606
Romain Guy5b3b3522010-10-27 18:57:51 -0700607 const float alpha = layer->alpha / 255.0f;
Romain Guy5b3b3522010-10-27 18:57:51 -0700608 const float texX = 1.0f / float(layer->width);
609 const float texY = 1.0f / float(layer->height);
610
611 TextureVertex* mesh = mCaches.getRegionMesh();
612 GLsizei numQuads = 0;
613
Romain Guy7230a742011-01-10 22:26:16 -0800614 setupDraw();
615 setupDrawWithTexture();
616 setupDrawColor(alpha, alpha, alpha, alpha);
617 setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
618 setupDrawProgram();
619 setupDrawDirtyRegionsDisabled();
620 setupDrawPureColorUniforms();
621 setupDrawTexture(layer->texture);
622 setupDrawModelViewIdentity();
623 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700624
625 for (size_t i = 0; i < count; i++) {
626 const android::Rect* r = &rects[i];
627
628 const float u1 = r->left * texX;
629 const float v1 = (rect.getHeight() - r->top) * texY;
630 const float u2 = r->right * texX;
631 const float v2 = (rect.getHeight() - r->bottom) * texY;
632
633 // TODO: Reject quads outside of the clip
634 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
635 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
636 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
637 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
638
639 numQuads++;
640
641 if (numQuads >= REGION_MESH_QUAD_COUNT) {
642 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
643 numQuads = 0;
644 mesh = mCaches.getRegionMesh();
645 }
646 }
647
648 if (numQuads > 0) {
649 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
650 }
651
652 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guy7230a742011-01-10 22:26:16 -0800653 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700654
655#if DEBUG_LAYERS_AS_REGIONS
656 uint32_t colors[] = {
657 0x7fff0000, 0x7f00ff00,
658 0x7f0000ff, 0x7fff00ff,
659 };
660
661 int offset = 0;
662 int32_t top = rects[0].top;
663 int i = 0;
664
665 for (size_t i = 0; i < count; i++) {
666 if (top != rects[i].top) {
667 offset ^= 0x2;
668 top = rects[i].top;
669 }
670
671 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
672 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
673 SkXfermode::kSrcOver_Mode);
674 }
675#endif
676
677 layer->region.clear();
678 }
679#else
680 composeLayerRect(layer, rect);
681#endif
682}
683
684void OpenGLRenderer::dirtyLayer(const float left, const float top,
685 const float right, const float bottom, const mat4 transform) {
686#if RENDER_LAYERS_AS_REGIONS
687 if ((mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region) {
688 Rect bounds(left, top, right, bottom);
689 transform.mapRect(bounds);
690 bounds.intersect(*mSnapshot->clipRect);
691 bounds.snapToPixelBoundaries();
692
693 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
694 if (!dirty.isEmpty()) {
695 mSnapshot->region->orSelf(dirty);
696 }
697 }
698#endif
699}
700
701void OpenGLRenderer::dirtyLayer(const float left, const float top,
702 const float right, const float bottom) {
703#if RENDER_LAYERS_AS_REGIONS
704 if ((mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region) {
705 Rect bounds(left, top, right, bottom);
706 bounds.intersect(*mSnapshot->clipRect);
707 bounds.snapToPixelBoundaries();
708
709 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
710 if (!dirty.isEmpty()) {
711 mSnapshot->region->orSelf(dirty);
712 }
713 }
714#endif
715}
716
Romain Guy86942302010-09-12 13:02:16 -0700717void OpenGLRenderer::clearLayerRegions() {
Romain Guyaf636eb2010-12-09 17:47:21 -0800718 if (mLayers.size() == 0 || mSnapshot->isIgnored()) return;
Romain Guy86942302010-09-12 13:02:16 -0700719
Romain Guy5b3b3522010-10-27 18:57:51 -0700720 Rect clipRect(*mSnapshot->clipRect);
721 clipRect.snapToPixelBoundaries();
722
Romain Guy86942302010-09-12 13:02:16 -0700723 for (uint32_t i = 0; i < mLayers.size(); i++) {
724 Rect* bounds = mLayers.itemAt(i);
Romain Guy5b3b3522010-10-27 18:57:51 -0700725 if (clipRect.intersects(*bounds)) {
726 // Clear the framebuffer where the layer will draw
727 glScissor(bounds->left, mSnapshot->height - bounds->bottom,
728 bounds->getWidth(), bounds->getHeight());
729 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
730 glClear(GL_COLOR_BUFFER_BIT);
Romain Guy86942302010-09-12 13:02:16 -0700731
Romain Guy5b3b3522010-10-27 18:57:51 -0700732 // Restore the clip
733 dirtyClip();
734 }
Romain Guy86942302010-09-12 13:02:16 -0700735
736 delete bounds;
737 }
Romain Guy86942302010-09-12 13:02:16 -0700738
Romain Guy5b3b3522010-10-27 18:57:51 -0700739 mLayers.clear();
Romain Guy86942302010-09-12 13:02:16 -0700740}
741
Romain Guybd6b79b2010-06-26 00:13:53 -0700742///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700743// Transforms
744///////////////////////////////////////////////////////////////////////////////
745
746void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700747 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700748}
749
750void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700751 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700752}
753
754void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700755 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700756}
757
758void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700759 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700760}
761
Romain Guy41030da2010-10-13 13:40:37 -0700762const float* OpenGLRenderer::getMatrix() const {
Romain Guy99bcdc52010-10-13 15:17:00 -0700763 if (mSnapshot->fbo != 0) {
764 return &mSnapshot->transform->data[0];
765 }
766 return &mIdentity.data[0];
Romain Guy41030da2010-10-13 13:40:37 -0700767}
768
Romain Guyf6a11b82010-06-23 17:47:49 -0700769void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700770 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700771}
772
773void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -0700774 SkMatrix transform;
775 mSnapshot->transform->copyTo(transform);
776 transform.preConcat(*matrix);
777 mSnapshot->transform->load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -0700778}
779
780///////////////////////////////////////////////////////////////////////////////
781// Clipping
782///////////////////////////////////////////////////////////////////////////////
783
Romain Guybb9524b2010-06-22 18:56:38 -0700784void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -0700785 Rect clip(*mSnapshot->clipRect);
786 clip.snapToPixelBoundaries();
Romain Guyeb993562010-10-05 18:14:38 -0700787 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy746b7402010-10-26 16:27:31 -0700788 mDirtyClip = false;
Romain Guy9d5316e2010-06-24 19:30:36 -0700789}
790
791const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700792 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700793}
794
Romain Guyc7d53492010-06-25 13:41:57 -0700795bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guyaf636eb2010-12-09 17:47:21 -0800796 if (mSnapshot->isIgnored()) {
Romain Guydbc26d22010-10-11 17:58:29 -0700797 return true;
798 }
799
Romain Guy1d83e192010-08-17 11:37:00 -0700800 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700801 mSnapshot->transform->mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -0700802 r.snapToPixelBoundaries();
803
804 Rect clipRect(*mSnapshot->clipRect);
805 clipRect.snapToPixelBoundaries();
806
807 return !clipRect.intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700808}
809
Romain Guy079ba2c2010-07-16 14:12:24 -0700810bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
811 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700812 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -0700813 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700814 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700815 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700816}
817
Romain Guyf6a11b82010-06-23 17:47:49 -0700818///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -0800819// Drawing commands
820///////////////////////////////////////////////////////////////////////////////
821
822void OpenGLRenderer::setupDraw() {
823 clearLayerRegions();
824 if (mDirtyClip) {
825 setScissorFromClip();
826 }
827 mDescription.reset();
828 mSetShaderColor = false;
829 mColorSet = false;
830 mColorA = mColorR = mColorG = mColorB = 0.0f;
831 mTextureUnit = 0;
832 mTrackDirtyRegions = true;
Romain Guy8d0d4782010-12-14 20:13:35 -0800833 mTexCoordsSlot = -1;
Romain Guy70ca14e2010-12-13 18:24:33 -0800834}
835
836void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
837 mDescription.hasTexture = true;
838 mDescription.hasAlpha8Texture = isAlpha8;
839}
840
841void OpenGLRenderer::setupDrawColor(int color) {
Romain Guy8d0d4782010-12-14 20:13:35 -0800842 setupDrawColor(color, (color >> 24) & 0xFF);
843}
844
845void OpenGLRenderer::setupDrawColor(int color, int alpha) {
846 mColorA = alpha / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -0800847 const float a = mColorA / 255.0f;
Romain Guyfa7952d2010-12-14 13:45:54 -0800848 mColorR = a * ((color >> 16) & 0xFF);
849 mColorG = a * ((color >> 8) & 0xFF);
850 mColorB = a * ((color ) & 0xFF);
Romain Guy70ca14e2010-12-13 18:24:33 -0800851 mColorSet = true;
852 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
853}
854
Romain Guy86568192010-12-14 15:55:39 -0800855void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
856 mColorA = alpha / 255.0f;
857 const float a = mColorA / 255.0f;
858 mColorR = a * ((color >> 16) & 0xFF);
859 mColorG = a * ((color >> 8) & 0xFF);
860 mColorB = a * ((color ) & 0xFF);
861 mColorSet = true;
862 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
863}
864
Romain Guy70ca14e2010-12-13 18:24:33 -0800865void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
866 mColorA = a;
867 mColorR = r;
868 mColorG = g;
869 mColorB = b;
870 mColorSet = true;
871 mSetShaderColor = mDescription.setColor(r, g, b, a);
872}
873
Romain Guy86568192010-12-14 15:55:39 -0800874void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
875 mColorA = a;
876 mColorR = r;
877 mColorG = g;
878 mColorB = b;
879 mColorSet = true;
880 mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
881}
882
Romain Guy70ca14e2010-12-13 18:24:33 -0800883void OpenGLRenderer::setupDrawShader() {
884 if (mShader) {
885 mShader->describe(mDescription, mCaches.extensions);
886 }
887}
888
889void OpenGLRenderer::setupDrawColorFilter() {
890 if (mColorFilter) {
891 mColorFilter->describe(mDescription, mCaches.extensions);
892 }
893}
894
895void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
896 chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
897 mDescription, swapSrcDst);
898}
899
900void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
901 chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
902 mDescription, swapSrcDst);
903}
904
905void OpenGLRenderer::setupDrawProgram() {
906 useProgram(mCaches.programCache.get(mDescription));
907}
908
909void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
910 mTrackDirtyRegions = false;
911}
912
913void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
914 bool ignoreTransform) {
915 mModelView.loadTranslate(left, top, 0.0f);
916 if (!ignoreTransform) {
917 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
918 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
919 } else {
920 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
921 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
922 }
923}
924
Romain Guy8d0d4782010-12-14 20:13:35 -0800925void OpenGLRenderer::setupDrawModelViewIdentity() {
926 mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform);
927}
928
Romain Guy70ca14e2010-12-13 18:24:33 -0800929void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
930 bool ignoreTransform, bool ignoreModelView) {
931 if (!ignoreModelView) {
932 mModelView.loadTranslate(left, top, 0.0f);
933 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -0800934 } else {
935 mModelView.loadIdentity();
936 }
Romain Guy86568192010-12-14 15:55:39 -0800937 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
938 if (!ignoreTransform) {
939 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
940 if (mTrackDirtyRegions && dirty) {
941 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
942 }
943 } else {
944 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
945 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
946 }
Romain Guy70ca14e2010-12-13 18:24:33 -0800947}
948
949void OpenGLRenderer::setupDrawColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -0800950 if (mColorSet || (mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -0800951 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
952 }
953}
954
Romain Guy86568192010-12-14 15:55:39 -0800955void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -0800956 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -0800957 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -0800958 }
959}
960
Romain Guy70ca14e2010-12-13 18:24:33 -0800961void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
962 if (mShader) {
963 if (ignoreTransform) {
964 mModelView.loadInverse(*mSnapshot->transform);
965 }
966 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
967 }
968}
969
Romain Guy8d0d4782010-12-14 20:13:35 -0800970void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
971 if (mShader) {
972 mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
973 }
974}
975
Romain Guy70ca14e2010-12-13 18:24:33 -0800976void OpenGLRenderer::setupDrawColorFilterUniforms() {
977 if (mColorFilter) {
978 mColorFilter->setupProgram(mCaches.currentProgram);
979 }
980}
981
982void OpenGLRenderer::setupDrawSimpleMesh() {
983 mCaches.bindMeshBuffer();
984 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
985 gMeshStride, 0);
986}
987
988void OpenGLRenderer::setupDrawTexture(GLuint texture) {
989 bindTexture(texture);
990 glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
991
992 mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
993 glEnableVertexAttribArray(mTexCoordsSlot);
994}
995
996void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
997 if (!vertices) {
998 mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
999 } else {
1000 mCaches.unbindMeshBuffer();
1001 }
1002 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
1003 gMeshStride, vertices);
Romain Guy8d0d4782010-12-14 20:13:35 -08001004 if (mTexCoordsSlot > 0) {
1005 glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
1006 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001007}
1008
1009void OpenGLRenderer::finishDrawTexture() {
1010 glDisableVertexAttribArray(mTexCoordsSlot);
1011}
1012
1013///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001014// Drawing
1015///////////////////////////////////////////////////////////////////////////////
1016
Romain Guy0fe478e2010-11-08 12:08:41 -08001017void OpenGLRenderer::drawDisplayList(DisplayList* displayList) {
1018 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1019 // will be performed by the display list itself
1020 if (displayList) {
1021 displayList->replay(*this);
1022 }
1023}
1024
Chet Haase5c13d892010-10-08 08:37:55 -07001025void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001026 const float right = left + bitmap->width();
1027 const float bottom = top + bitmap->height();
1028
1029 if (quickReject(left, top, right, bottom)) {
1030 return;
1031 }
1032
Romain Guy86568192010-12-14 15:55:39 -08001033 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001034 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001035 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001036 const AutoTexture autoCleanup(texture);
1037
Romain Guy6926c722010-07-12 20:20:03 -07001038 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -07001039}
1040
Chet Haase5c13d892010-10-08 08:37:55 -07001041void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001042 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1043 const mat4 transform(*matrix);
1044 transform.mapRect(r);
1045
Romain Guy6926c722010-07-12 20:20:03 -07001046 if (quickReject(r.left, r.top, r.right, r.bottom)) {
1047 return;
1048 }
1049
Romain Guy86568192010-12-14 15:55:39 -08001050 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001051 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001052 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001053 const AutoTexture autoCleanup(texture);
1054
Romain Guy5b3b3522010-10-27 18:57:51 -07001055 // This could be done in a cheaper way, all we need is pass the matrix
1056 // to the vertex shader. The save/restore is a bit overkill.
1057 save(SkCanvas::kMatrix_SaveFlag);
1058 concatMatrix(matrix);
1059 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
1060 restore();
Romain Guyf86ef572010-07-01 11:05:42 -07001061}
1062
Romain Guy8ba548f2010-06-30 19:21:21 -07001063void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
1064 float srcLeft, float srcTop, float srcRight, float srcBottom,
1065 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07001066 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001067 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
1068 return;
1069 }
1070
Romain Guy746b7402010-10-26 16:27:31 -07001071 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001072 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001073 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001074 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001075 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guy8ba548f2010-06-30 19:21:21 -07001076
Romain Guy8ba548f2010-06-30 19:21:21 -07001077 const float width = texture->width;
1078 const float height = texture->height;
1079
1080 const float u1 = srcLeft / width;
1081 const float v1 = srcTop / height;
1082 const float u2 = srcRight / width;
1083 const float v2 = srcBottom / height;
1084
Romain Guy03750a02010-10-18 14:06:08 -07001085 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07001086 resetDrawTextureTexCoords(u1, v1, u2, v2);
1087
Romain Guy03750a02010-10-18 14:06:08 -07001088 int alpha;
1089 SkXfermode::Mode mode;
1090 getAlphaAndMode(paint, &alpha, &mode);
1091
Romain Guy6620c6d2010-12-06 18:07:02 -08001092 if (mSnapshot->transform->isPureTranslate()) {
1093 const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
1094 const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
1095
1096 drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
1097 texture->id, alpha / 255.0f, mode, texture->blend,
1098 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1099 GL_TRIANGLE_STRIP, gMeshCount, false, true);
1100 } else {
1101 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
1102 mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1103 GL_TRIANGLE_STRIP, gMeshCount);
1104 }
Romain Guy8ba548f2010-06-30 19:21:21 -07001105
1106 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1107}
1108
Romain Guy4aa90572010-09-26 18:40:37 -07001109void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07001110 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07001111 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001112 if (quickReject(left, top, right, bottom)) {
1113 return;
1114 }
1115
Romain Guy746b7402010-10-26 16:27:31 -07001116 glActiveTexture(gTextureUnits[0]);
Romain Guy8164c2d2010-10-25 18:03:28 -07001117 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001118 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001119 const AutoTexture autoCleanup(texture);
Romain Guy8164c2d2010-10-25 18:03:28 -07001120 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
Romain Guyf7f93552010-07-08 19:17:03 -07001121
1122 int alpha;
1123 SkXfermode::Mode mode;
1124 getAlphaAndMode(paint, &alpha, &mode);
1125
Romain Guy2728f962010-10-08 18:36:15 -07001126 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07001127 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07001128
Romain Guya5ef39a2010-12-03 16:48:20 -08001129 if (mesh && mesh->verticesCount > 0) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001130 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
Romain Guya5ef39a2010-12-03 16:48:20 -08001131#if RENDER_LAYERS_AS_REGIONS
Romain Guy5b3b3522010-10-27 18:57:51 -07001132 // Mark the current layer dirty where we are going to draw the patch
1133 if ((mSnapshot->flags & Snapshot::kFlagFboTarget) &&
1134 mSnapshot->region && mesh->hasEmptyQuads) {
1135 const size_t count = mesh->quads.size();
1136 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08001137 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy6620c6d2010-12-06 18:07:02 -08001138 if (pureTranslate) {
1139 const float x = (int) floorf(bounds.left + 0.5f);
1140 const float y = (int) floorf(bounds.top + 0.5f);
Romain Guy8ab40792010-12-07 13:30:10 -08001141 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Romain Guy6620c6d2010-12-06 18:07:02 -08001142 *mSnapshot->transform);
1143 } else {
1144 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom,
1145 *mSnapshot->transform);
1146 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001147 }
1148 }
Romain Guya5ef39a2010-12-03 16:48:20 -08001149#endif
Romain Guy5b3b3522010-10-27 18:57:51 -07001150
Romain Guy6620c6d2010-12-06 18:07:02 -08001151 if (pureTranslate) {
1152 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1153 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1154
1155 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
1156 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1157 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
1158 true, !mesh->hasEmptyQuads);
1159 } else {
1160 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
1161 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
1162 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
1163 true, !mesh->hasEmptyQuads);
1164 }
Romain Guy054dc182010-10-15 17:55:25 -07001165 }
Romain Guyf7f93552010-07-08 19:17:03 -07001166}
1167
Chet Haase5c13d892010-10-08 08:37:55 -07001168void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001169 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001170
Romain Guya957eea2010-12-08 18:34:42 -08001171 const bool isAA = paint->isAntiAlias();
1172 const float strokeWidth = paint->getStrokeWidth() * 0.5f;
1173 // A stroke width of 0 has a special meaningin Skia:
1174 // it draws an unscaled 1px wide line
1175 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
1176
Romain Guy759ea802010-09-16 20:49:46 -07001177 int alpha;
1178 SkXfermode::Mode mode;
1179 getAlphaAndMode(paint, &alpha, &mode);
1180
Romain Guya957eea2010-12-08 18:34:42 -08001181 int verticesCount = count >> 2;
Romain Guy7230a742011-01-10 22:26:16 -08001182 int generatedVerticesCount = 0;
Romain Guya957eea2010-12-08 18:34:42 -08001183 if (!isHairLine) {
1184 // TODO: AA needs more vertices
1185 verticesCount *= 6;
Romain Guyc95c8d62010-09-17 15:31:32 -07001186 } else {
Romain Guya957eea2010-12-08 18:34:42 -08001187 // TODO: AA will be different
1188 verticesCount *= 2;
Romain Guyc95c8d62010-09-17 15:31:32 -07001189 }
1190
Romain Guya957eea2010-12-08 18:34:42 -08001191 TextureVertex lines[verticesCount];
1192 TextureVertex* vertex = &lines[0];
Romain Guy759ea802010-09-16 20:49:46 -07001193
Romain Guy8d0d4782010-12-14 20:13:35 -08001194 setupDraw();
1195 setupDrawColor(paint->getColor(), alpha);
1196 setupDrawColorFilter();
1197 setupDrawShader();
1198 setupDrawBlending(mode);
1199 setupDrawProgram();
1200 setupDrawModelViewIdentity();
1201 setupDrawColorUniforms();
1202 setupDrawColorFilterUniforms();
1203 setupDrawShaderIdentityUniforms();
1204 setupDrawMesh(vertex);
Romain Guya957eea2010-12-08 18:34:42 -08001205
1206 if (!isHairLine) {
1207 // TODO: Handle the AA case
1208 for (int i = 0; i < count; i += 4) {
1209 // a = start point, b = end point
1210 vec2 a(points[i], points[i + 1]);
1211 vec2 b(points[i + 2], points[i + 3]);
1212
1213 // Bias to snap to the same pixels as Skia
1214 a += 0.375;
1215 b += 0.375;
1216
1217 // Find the normal to the line
1218 vec2 n = (b - a).copyNormalized() * strokeWidth;
1219 float x = n.x;
1220 n.x = -n.y;
1221 n.y = x;
1222
1223 // Four corners of the rectangle defining a thick line
1224 vec2 p1 = a - n;
1225 vec2 p2 = a + n;
1226 vec2 p3 = b + n;
1227 vec2 p4 = b - n;
1228
Romain Guy7230a742011-01-10 22:26:16 -08001229 const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
1230 const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
1231 const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
1232 const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
Romain Guya957eea2010-12-08 18:34:42 -08001233
Romain Guy7230a742011-01-10 22:26:16 -08001234 if (!quickReject(left, top, right, bottom)) {
1235 // Draw the line as 2 triangles, could be optimized
1236 // by using only 4 vertices and the correct indices
1237 // Also we should probably used non textured vertices
1238 // when line AA is disabled to save on bandwidth
1239 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1240 TextureVertex::set(vertex++, p2.x, p2.y, 0.0f, 0.0f);
1241 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1242 TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
1243 TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
1244 TextureVertex::set(vertex++, p4.x, p4.y, 0.0f, 0.0f);
1245
1246 generatedVerticesCount += 6;
1247
1248 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1249 }
Romain Guya957eea2010-12-08 18:34:42 -08001250 }
1251
Romain Guy7230a742011-01-10 22:26:16 -08001252 if (generatedVerticesCount > 0) {
1253 // GL_LINE does not give the result we want to match Skia
1254 glDrawArrays(GL_TRIANGLES, 0, generatedVerticesCount);
1255 }
Romain Guya957eea2010-12-08 18:34:42 -08001256 } else {
1257 // TODO: Handle the AA case
1258 for (int i = 0; i < count; i += 4) {
Romain Guy7230a742011-01-10 22:26:16 -08001259 const float left = fmin(points[i], points[i + 1]);
1260 const float right = fmax(points[i], points[i + 1]);
1261 const float top = fmin(points[i + 2], points[i + 3]);
1262 const float bottom = fmax(points[i + 2], points[i + 3]);
1263
1264 if (!quickReject(left, top, right, bottom)) {
1265 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
1266 TextureVertex::set(vertex++, points[i + 2], points[i + 3], 0.0f, 0.0f);
1267
1268 generatedVerticesCount += 2;
1269
1270 dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1271 }
Romain Guya957eea2010-12-08 18:34:42 -08001272 }
Romain Guy8d0d4782010-12-14 20:13:35 -08001273
Romain Guy7230a742011-01-10 22:26:16 -08001274 if (generatedVerticesCount > 0) {
1275 glLineWidth(1.0f);
1276 glDrawArrays(GL_LINES, 0, generatedVerticesCount);
1277 }
Romain Guy29d89972010-09-22 16:10:57 -07001278 }
Romain Guy759ea802010-09-16 20:49:46 -07001279}
1280
Romain Guy85bf02f2010-06-22 13:11:24 -07001281void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07001282 // No need to check against the clip, we fill the clip region
Romain Guyaf636eb2010-12-09 17:47:21 -08001283 if (mSnapshot->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07001284
Romain Guyae88e5e2010-10-22 17:49:18 -07001285 Rect& clip(*mSnapshot->clipRect);
1286 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08001287
Romain Guy3d58c032010-07-14 16:34:53 -07001288 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -07001289}
Romain Guy9d5316e2010-06-24 19:30:36 -07001290
Chet Haase5c13d892010-10-08 08:37:55 -07001291void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -07001292 if (quickReject(left, top, right, bottom)) {
1293 return;
1294 }
1295
Romain Guy026c5e162010-06-28 17:12:22 -07001296 SkXfermode::Mode mode;
Romain Guy746b7402010-10-26 16:27:31 -07001297 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001298 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1299 if (!isMode) {
1300 // Assume SRC_OVER
1301 mode = SkXfermode::kSrcOver_Mode;
1302 }
1303 } else {
1304 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -07001305 }
1306
1307 // Skia draws using the color's alpha channel if < 255
1308 // Otherwise, it uses the paint's alpha
1309 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07001310 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -07001311 color |= p->getAlpha() << 24;
1312 }
1313
1314 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -07001315}
Romain Guy9d5316e2010-06-24 19:30:36 -07001316
Romain Guye8e62a42010-07-23 18:55:21 -07001317void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1318 float x, float y, SkPaint* paint) {
Romain Guyb146b122010-12-15 17:06:45 -08001319 if (text == NULL || count == 0) {
Romain Guye8e62a42010-07-23 18:55:21 -07001320 return;
1321 }
Romain Guyaf636eb2010-12-09 17:47:21 -08001322 if (mSnapshot->isIgnored()) return;
Romain Guye2d345e2010-09-24 18:39:22 -07001323
Romain Guyf607bdc2010-09-10 19:20:06 -07001324 paint->setAntiAlias(true);
Romain Guye8e62a42010-07-23 18:55:21 -07001325
Romain Guya674ab72010-08-10 17:26:42 -07001326 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -07001327 switch (paint->getTextAlign()) {
1328 case SkPaint::kCenter_Align:
1329 length = paint->measureText(text, bytesCount);
1330 x -= length / 2.0f;
1331 break;
1332 case SkPaint::kRight_Align:
1333 length = paint->measureText(text, bytesCount);
1334 x -= length;
1335 break;
1336 default:
1337 break;
1338 }
1339
Romain Guy6620c6d2010-12-06 18:07:02 -08001340 // TODO: Handle paint->getTextScaleX()
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001341 const float oldX = x;
1342 const float oldY = y;
Romain Guy6620c6d2010-12-06 18:07:02 -08001343 const bool pureTranslate = mSnapshot->transform->isPureTranslate();
1344 if (pureTranslate) {
1345 x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
1346 y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
1347 }
1348
Romain Guyb45c0c92010-08-26 20:35:23 -07001349 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
1350 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -07001351 paint->getTextSize());
Romain Guye2d345e2010-09-24 18:39:22 -07001352
Romain Guy86568192010-12-14 15:55:39 -08001353 int alpha;
1354 SkXfermode::Mode mode;
1355 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07001356
Romain Guy1e45aae2010-08-13 19:39:53 -07001357 if (mHasShadow) {
Romain Guyb45c0c92010-08-26 20:35:23 -07001358 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -07001359 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -07001360 count, mShadowRadius);
1361 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -07001362
Romain Guy86568192010-12-14 15:55:39 -08001363 const float sx = x - shadow->left + mShadowDx;
1364 const float sy = y - shadow->top + mShadowDy;
Romain Guy0a417492010-08-16 20:26:20 -07001365
Romain Guy86568192010-12-14 15:55:39 -08001366 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
1367
1368 glActiveTexture(gTextureUnits[0]);
1369 setupDraw();
1370 setupDrawWithTexture(true);
1371 setupDrawAlpha8Color(mShadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
1372 setupDrawBlending(true, mode);
1373 setupDrawProgram();
1374 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height, pureTranslate);
1375 setupDrawTexture(shadow->id);
1376 setupDrawPureColorUniforms();
1377 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
1378
Romain Guy0a417492010-08-16 20:26:20 -07001379 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy86568192010-12-14 15:55:39 -08001380 finishDrawTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -07001381 }
1382
Romain Guyb146b122010-12-15 17:06:45 -08001383 if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
1384 return;
1385 }
1386
Romain Guy6620c6d2010-12-06 18:07:02 -08001387 // Pick the appropriate texture filtering
1388 bool linearFilter = mSnapshot->transform->changesBounds();
1389 if (pureTranslate && !linearFilter) {
1390 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
1391 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001392
Romain Guy86568192010-12-14 15:55:39 -08001393 glActiveTexture(gTextureUnits[0]);
1394 setupDraw();
1395 setupDrawDirtyRegionsDisabled();
1396 setupDrawWithTexture(true);
1397 setupDrawAlpha8Color(paint->getColor(), alpha);
1398 setupDrawColorFilter();
1399 setupDrawShader();
1400 setupDrawBlending(true, mode);
1401 setupDrawProgram();
1402 setupDrawModelView(x, y, x, y, pureTranslate, true);
1403 setupDrawTexture(fontRenderer.getTexture(linearFilter));
1404 setupDrawPureColorUniforms();
1405 setupDrawColorFilterUniforms();
1406 setupDrawShaderUniforms(pureTranslate);
Romain Guy06f96e22010-07-30 19:18:16 -07001407
Romain Guy6620c6d2010-12-06 18:07:02 -08001408 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
Romain Guy5b3b3522010-10-27 18:57:51 -07001409 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
1410
1411#if RENDER_LAYERS_AS_REGIONS
1412 bool hasLayer = (mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region;
1413#else
1414 bool hasLayer = false;
1415#endif
Romain Guy9d13fe252010-10-15 16:06:03 -07001416
Romain Guy03750a02010-10-18 14:06:08 -07001417 mCaches.unbindMeshBuffer();
Romain Guy6620c6d2010-12-06 18:07:02 -08001418 if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy5b3b3522010-10-27 18:57:51 -07001419 hasLayer ? &bounds : NULL)) {
1420#if RENDER_LAYERS_AS_REGIONS
1421 if (hasLayer) {
Romain Guy6620c6d2010-12-06 18:07:02 -08001422 if (!pureTranslate) {
1423 mSnapshot->transform->mapRect(bounds);
1424 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001425 bounds.intersect(*mSnapshot->clipRect);
1426 bounds.snapToPixelBoundaries();
1427
1428 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1429 mSnapshot->region->orSelf(dirty);
1430 }
1431#endif
1432 }
Romain Guy694b5192010-07-21 21:33:20 -07001433
1434 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -07001435 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -07001436
Romain Guy3a3fa1b2010-12-06 18:47:50 -08001437 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Romain Guy694b5192010-07-21 21:33:20 -07001438}
1439
Romain Guy7fbcc042010-08-04 15:40:07 -07001440void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guyaf636eb2010-12-09 17:47:21 -08001441 if (mSnapshot->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07001442
Romain Guy7fbcc042010-08-04 15:40:07 -07001443 GLuint textureUnit = 0;
1444 glActiveTexture(gTextureUnits[textureUnit]);
1445
Romain Guyfb8b7632010-08-23 21:05:08 -07001446 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -07001447 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001448 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07001449
Romain Guy8b55f372010-08-18 17:10:07 -07001450 const float x = texture->left - texture->offset;
1451 const float y = texture->top - texture->offset;
1452
1453 if (quickReject(x, y, x + texture->width, y + texture->height)) {
1454 return;
1455 }
1456
Romain Guy7fbcc042010-08-04 15:40:07 -07001457 int alpha;
1458 SkXfermode::Mode mode;
1459 getAlphaAndMode(paint, &alpha, &mode);
1460
Romain Guy746b7402010-10-26 16:27:31 -07001461 setupDraw();
Romain Guy86568192010-12-14 15:55:39 -08001462 setupDrawWithTexture(true);
1463 setupDrawAlpha8Color(paint->getColor(), alpha);
1464 setupDrawColorFilter();
1465 setupDrawShader();
1466 setupDrawBlending(true, mode);
1467 setupDrawProgram();
1468 setupDrawModelView(x, y, x + texture->width, y + texture->height);
1469 setupDrawTexture(texture->id);
1470 setupDrawPureColorUniforms();
1471 setupDrawColorFilterUniforms();
1472 setupDrawShaderUniforms();
1473 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
Romain Guy86942302010-09-12 13:02:16 -07001474
Romain Guy0a417492010-08-16 20:26:20 -07001475 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy86568192010-12-14 15:55:39 -08001476
1477 finishDrawTexture();
Romain Guy7fbcc042010-08-04 15:40:07 -07001478}
1479
Romain Guy6926c722010-07-12 20:20:03 -07001480///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07001481// Shaders
1482///////////////////////////////////////////////////////////////////////////////
1483
1484void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -07001485 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07001486}
1487
Romain Guy06f96e22010-07-30 19:18:16 -07001488void OpenGLRenderer::setupShader(SkiaShader* shader) {
1489 mShader = shader;
1490 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001491 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07001492 }
Romain Guy7fac2e12010-07-16 17:10:13 -07001493}
1494
Romain Guyd27977d2010-07-14 19:18:51 -07001495///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07001496// Color filters
1497///////////////////////////////////////////////////////////////////////////////
1498
1499void OpenGLRenderer::resetColorFilter() {
1500 mColorFilter = NULL;
1501}
1502
1503void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
1504 mColorFilter = filter;
1505}
1506
1507///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07001508// Drop shadow
1509///////////////////////////////////////////////////////////////////////////////
1510
1511void OpenGLRenderer::resetShadow() {
1512 mHasShadow = false;
1513}
1514
1515void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
1516 mHasShadow = true;
1517 mShadowRadius = radius;
1518 mShadowDx = dx;
1519 mShadowDy = dy;
1520 mShadowColor = color;
1521}
1522
1523///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07001524// Drawing implementation
1525///////////////////////////////////////////////////////////////////////////////
1526
Romain Guyf607bdc2010-09-10 19:20:06 -07001527// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07001528#define kStdStrikeThru_Offset (-6.0f / 21.0f)
1529#define kStdUnderline_Offset (1.0f / 9.0f)
1530#define kStdUnderline_Thickness (1.0f / 18.0f)
1531
1532void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
1533 float x, float y, SkPaint* paint) {
1534 // Handle underline and strike-through
1535 uint32_t flags = paint->getFlags();
1536 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
1537 float underlineWidth = length;
1538 // If length is > 0.0f, we already measured the text for the text alignment
1539 if (length <= 0.0f) {
1540 underlineWidth = paint->measureText(text, bytesCount);
1541 }
1542
1543 float offsetX = 0;
1544 switch (paint->getTextAlign()) {
1545 case SkPaint::kCenter_Align:
1546 offsetX = underlineWidth * 0.5f;
1547 break;
1548 case SkPaint::kRight_Align:
1549 offsetX = underlineWidth;
1550 break;
1551 default:
1552 break;
1553 }
1554
1555 if (underlineWidth > 0.0f) {
Romain Guye20ecbd2010-09-22 19:49:04 -07001556 const float textSize = paint->getTextSize();
1557 const float strokeWidth = textSize * kStdUnderline_Thickness;
Romain Guy0a417492010-08-16 20:26:20 -07001558
Romain Guye20ecbd2010-09-22 19:49:04 -07001559 const float left = x - offsetX;
Romain Guy0a417492010-08-16 20:26:20 -07001560 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07001561
1562 const int pointsCount = 4 * (flags & SkPaint::kStrikeThruText_Flag ? 2 : 1);
1563 float points[pointsCount];
1564 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07001565
1566 if (flags & SkPaint::kUnderlineText_Flag) {
1567 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001568 points[currentPoint++] = left;
1569 points[currentPoint++] = top;
1570 points[currentPoint++] = left + underlineWidth;
1571 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001572 }
1573
1574 if (flags & SkPaint::kStrikeThruText_Flag) {
1575 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07001576 points[currentPoint++] = left;
1577 points[currentPoint++] = top;
1578 points[currentPoint++] = left + underlineWidth;
1579 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07001580 }
Romain Guye20ecbd2010-09-22 19:49:04 -07001581
1582 SkPaint linesPaint(*paint);
1583 linesPaint.setStrokeWidth(strokeWidth);
1584
1585 drawLines(&points[0], pointsCount, &linesPaint);
Romain Guy0a417492010-08-16 20:26:20 -07001586 }
1587 }
1588}
1589
Romain Guy026c5e162010-06-28 17:12:22 -07001590void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07001591 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07001592 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -07001593 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07001594 color |= 0x00ffffff;
1595 }
1596
Romain Guy70ca14e2010-12-13 18:24:33 -08001597 setupDraw();
1598 setupDrawColor(color);
1599 setupDrawShader();
1600 setupDrawColorFilter();
1601 setupDrawBlending(mode);
1602 setupDrawProgram();
1603 setupDrawModelView(left, top, right, bottom, ignoreTransform);
1604 setupDrawColorUniforms();
1605 setupDrawShaderUniforms(ignoreTransform);
1606 setupDrawColorFilterUniforms();
1607 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07001608
Romain Guyc95c8d62010-09-17 15:31:32 -07001609 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1610}
1611
Romain Guy82ba8142010-07-09 13:25:56 -07001612void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07001613 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07001614 int alpha;
1615 SkXfermode::Mode mode;
1616 getAlphaAndMode(paint, &alpha, &mode);
1617
Romain Guy8164c2d2010-10-25 18:03:28 -07001618 setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1619
Romain Guy6620c6d2010-12-06 18:07:02 -08001620 if (mSnapshot->transform->isPureTranslate()) {
1621 const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
1622 const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
1623
1624 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1625 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
1626 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
1627 } else {
1628 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
1629 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
1630 GL_TRIANGLE_STRIP, gMeshCount);
1631 }
Romain Guy85bf02f2010-06-22 13:11:24 -07001632}
1633
Romain Guybd6b79b2010-06-26 00:13:53 -07001634void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001635 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1636 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07001637 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07001638}
1639
1640void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07001641 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07001642 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07001643 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001644
Romain Guy746b7402010-10-26 16:27:31 -07001645 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08001646 setupDrawWithTexture();
1647 setupDrawColor(alpha, alpha, alpha, alpha);
1648 setupDrawColorFilter();
1649 setupDrawBlending(blend, mode, swapSrcDst);
1650 setupDrawProgram();
1651 if (!dirty) {
1652 setupDrawDirtyRegionsDisabled();
Romain Guydb1938e2010-08-02 18:50:22 -07001653 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001654 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001655 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001656 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08001657 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07001658 }
Romain Guy86568192010-12-14 15:55:39 -08001659 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08001660 setupDrawColorFilterUniforms();
1661 setupDrawTexture(texture);
1662 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07001663
Romain Guy6820ac82010-09-15 18:11:50 -07001664 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08001665
1666 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07001667}
1668
Romain Guya5aed0d2010-09-09 14:42:43 -07001669void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07001670 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07001671 blend = blend || mode != SkXfermode::kSrcOver_Mode;
1672 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001673 if (mode < SkXfermode::kPlus_Mode) {
1674 if (!mCaches.blend) {
1675 glEnable(GL_BLEND);
1676 }
Romain Guy82ba8142010-07-09 13:25:56 -07001677
Romain Guyf607bdc2010-09-10 19:20:06 -07001678 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1679 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -07001680
Romain Guya5aed0d2010-09-09 14:42:43 -07001681 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1682 glBlendFunc(sourceMode, destMode);
1683 mCaches.lastSrcMode = sourceMode;
1684 mCaches.lastDstMode = destMode;
1685 }
1686 } else {
1687 // These blend modes are not supported by OpenGL directly and have
1688 // to be implemented using shaders. Since the shader will perform
1689 // the blending, turn blending off here
Romain Guy746b7402010-10-26 16:27:31 -07001690 if (mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001691 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07001692 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07001693 }
1694
1695 if (mCaches.blend) {
1696 glDisable(GL_BLEND);
1697 }
1698 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -07001699 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001700 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07001701 glDisable(GL_BLEND);
1702 }
Romain Guyfb8b7632010-08-23 21:05:08 -07001703 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07001704}
1705
Romain Guy889f8d12010-07-29 14:37:42 -07001706bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07001707 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07001708 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07001709 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07001710 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07001711 return false;
Romain Guy260e1022010-07-12 14:41:06 -07001712 }
Romain Guy6926c722010-07-12 20:20:03 -07001713 return true;
Romain Guy260e1022010-07-12 14:41:06 -07001714}
1715
Romain Guy026c5e162010-06-28 17:12:22 -07001716void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07001717 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07001718 TextureVertex::setUV(v++, u1, v1);
1719 TextureVertex::setUV(v++, u2, v1);
1720 TextureVertex::setUV(v++, u1, v2);
1721 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001722}
1723
Chet Haase5c13d892010-10-08 08:37:55 -07001724void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Romain Guy8ba548f2010-06-30 19:21:21 -07001725 if (paint) {
Romain Guy746b7402010-10-26 16:27:31 -07001726 if (!mCaches.extensions.hasFramebufferFetch()) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001727 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1728 if (!isMode) {
1729 // Assume SRC_OVER
1730 *mode = SkXfermode::kSrcOver_Mode;
1731 }
1732 } else {
1733 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001734 }
1735
1736 // Skia draws using the color's alpha channel if < 255
1737 // Otherwise, it uses the paint's alpha
1738 int color = paint->getColor();
1739 *alpha = (color >> 24) & 0xFF;
1740 if (*alpha == 255) {
1741 *alpha = paint->getAlpha();
1742 }
1743 } else {
1744 *mode = SkXfermode::kSrcOver_Mode;
1745 *alpha = 255;
1746 }
Romain Guy026c5e162010-06-28 17:12:22 -07001747}
1748
Romain Guya5aed0d2010-09-09 14:42:43 -07001749SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1750 if (mode == NULL) {
1751 return SkXfermode::kSrcOver_Mode;
1752 }
1753 return mode->fMode;
1754}
1755
Romain Guy746b7402010-10-26 16:27:31 -07001756void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
Romain Guy8164c2d2010-10-25 18:03:28 -07001757 bool bound = false;
1758 if (wrapS != texture->wrapS) {
1759 glBindTexture(GL_TEXTURE_2D, texture->id);
1760 bound = true;
1761 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1762 texture->wrapS = wrapS;
1763 }
1764 if (wrapT != texture->wrapT) {
Romain Guy3e3ba152010-10-25 18:33:16 -07001765 if (!bound) {
Romain Guy3e3ba152010-10-25 18:33:16 -07001766 glBindTexture(GL_TEXTURE_2D, texture->id);
1767 }
Romain Guy8164c2d2010-10-25 18:03:28 -07001768 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1769 texture->wrapT = wrapT;
1770 }
Romain Guya1db5742010-07-20 13:09:13 -07001771}
1772
Romain Guy9d5316e2010-06-24 19:30:36 -07001773}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001774}; // namespace android