blob: f70bca74942ae14da751574604e41d04beadb461 [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>
27
Romain Guy85bf02f2010-06-22 13:11:24 -070028#include "OpenGLRenderer.h"
Romain Guye4d01122010-06-16 18:44:05 -070029
30namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070031namespace uirenderer {
32
33///////////////////////////////////////////////////////////////////////////////
34// Defines
35///////////////////////////////////////////////////////////////////////////////
36
Romain Guy06f96e22010-07-30 19:18:16 -070037#define REQUIRED_TEXTURE_UNITS_COUNT 3
38
Romain Guydda570202010-07-06 11:39:32 -070039// Generates simple and textured vertices
Romain Guybd6b79b2010-06-26 00:13:53 -070040#define FV(x, y, u, v) { { x, y }, { u, v } }
Romain Guy9d5316e2010-06-24 19:30:36 -070041
42///////////////////////////////////////////////////////////////////////////////
43// Globals
44///////////////////////////////////////////////////////////////////////////////
45
Romain Guy026c5e162010-06-28 17:12:22 -070046// This array is never used directly but used as a memcpy source in the
47// OpenGLRenderer constructor
Romain Guyac670c02010-07-27 17:39:27 -070048static const TextureVertex gMeshVertices[] = {
Romain Guyc1396e92010-06-30 17:56:19 -070049 FV(0.0f, 0.0f, 0.0f, 0.0f),
50 FV(1.0f, 0.0f, 1.0f, 0.0f),
51 FV(0.0f, 1.0f, 0.0f, 1.0f),
52 FV(1.0f, 1.0f, 1.0f, 1.0f)
Romain Guybd6b79b2010-06-26 00:13:53 -070053};
Romain Guyac670c02010-07-27 17:39:27 -070054static const GLsizei gMeshStride = sizeof(TextureVertex);
55static const GLsizei gMeshCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070056
Romain Guy889f8d12010-07-29 14:37:42 -070057/**
58 * Structure mapping Skia xfermodes to OpenGL blending factors.
59 */
60struct Blender {
61 SkXfermode::Mode mode;
62 GLenum src;
63 GLenum dst;
64}; // struct Blender
65
Romain Guy026c5e162010-06-28 17:12:22 -070066// In this array, the index of each Blender equals the value of the first
67// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
68static const Blender gBlends[] = {
69 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
70 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
71 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
72 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
73 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
74 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
75 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
76 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
77 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
78 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
79 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
80 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
81};
Romain Guye4d01122010-06-16 18:44:05 -070082
Romain Guy889f8d12010-07-29 14:37:42 -070083static const GLenum gTextureUnits[] = {
Romain Guy06f96e22010-07-30 19:18:16 -070084 GL_TEXTURE0,
85 GL_TEXTURE1,
86 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -070087};
88
Romain Guyf6a11b82010-06-23 17:47:49 -070089///////////////////////////////////////////////////////////////////////////////
90// Constructors/destructor
91///////////////////////////////////////////////////////////////////////////////
92
Romain Guyfb8b7632010-08-23 21:05:08 -070093OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy85bf02f2010-06-22 13:11:24 -070094 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -070095
Romain Guy06f96e22010-07-30 19:18:16 -070096 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -070097 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -070098 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -070099
Romain Guyac670c02010-07-27 17:39:27 -0700100 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
101
Romain Guyae5575b2010-07-29 18:48:04 -0700102 mFirstSnapshot = new Snapshot;
103
104 GLint maxTextureUnits;
105 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
106 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
Romain Guy889f8d12010-07-29 14:37:42 -0700107 LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
108 }
Romain Guye4d01122010-06-16 18:44:05 -0700109}
110
Romain Guy85bf02f2010-06-22 13:11:24 -0700111OpenGLRenderer::~OpenGLRenderer() {
112 LOGD("Destroy OpenGLRenderer");
Romain Guye4d01122010-06-16 18:44:05 -0700113}
114
Romain Guyf6a11b82010-06-23 17:47:49 -0700115///////////////////////////////////////////////////////////////////////////////
116// Setup
117///////////////////////////////////////////////////////////////////////////////
118
Romain Guy85bf02f2010-06-22 13:11:24 -0700119void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700120 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700121 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700122
123 mWidth = width;
124 mHeight = height;
Romain Guyae5575b2010-07-29 18:48:04 -0700125 mFirstSnapshot->height = height;
Romain Guy1d83e192010-08-17 11:37:00 -0700126 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700127}
128
Romain Guy85bf02f2010-06-22 13:11:24 -0700129void OpenGLRenderer::prepare() {
Romain Guy8aef54f2010-09-01 15:13:49 -0700130 mSnapshot = new Snapshot(mFirstSnapshot,
131 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy8fb95422010-08-17 18:38:51 -0700132 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700133
Romain Guyfb8b7632010-08-23 21:05:08 -0700134 glViewport(0, 0, mWidth, mHeight);
135
Romain Guyd90f23e2010-09-09 11:47:54 -0700136 glDisable(GL_DITHER);
Romain Guy08ae3172010-06-21 19:35:50 -0700137 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700138
Romain Guy08ae3172010-06-21 19:35:50 -0700139 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
140 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700141
Romain Guy08ae3172010-06-21 19:35:50 -0700142 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700143 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700144
Romain Guyb82da652010-07-30 11:36:12 -0700145 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700146}
147
Romain Guyda8532c2010-08-31 11:50:35 -0700148void OpenGLRenderer::acquireContext() {
149 if (mCaches.currentProgram) {
150 if (mCaches.currentProgram->isInUse()) {
151 mCaches.currentProgram->remove();
152 mCaches.currentProgram = NULL;
153 }
154 }
155}
156
157void OpenGLRenderer::releaseContext() {
158 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
159
160 glEnable(GL_SCISSOR_TEST);
161 setScissorFromClip();
162
163 if (mCaches.blend) {
164 glEnable(GL_BLEND);
165 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
166 } else {
167 glDisable(GL_BLEND);
168 }
169}
170
Romain Guyf6a11b82010-06-23 17:47:49 -0700171///////////////////////////////////////////////////////////////////////////////
172// State management
173///////////////////////////////////////////////////////////////////////////////
174
Romain Guybb9524b2010-06-22 18:56:38 -0700175int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700176 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700177}
178
179int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700180 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700181}
182
183void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700184 if (mSaveCount > 1) {
185 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700186 }
Romain Guybb9524b2010-06-22 18:56:38 -0700187}
188
189void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700190 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700191
Romain Guy8fb95422010-08-17 18:38:51 -0700192 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700193 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700194 }
Romain Guybb9524b2010-06-22 18:56:38 -0700195}
196
Romain Guy8aef54f2010-09-01 15:13:49 -0700197int OpenGLRenderer::saveSnapshot(int flags) {
198 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700199 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700200}
201
202bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700203 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700204 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700205 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700206
Romain Guybd6b79b2010-06-26 00:13:53 -0700207 sp<Snapshot> current = mSnapshot;
208 sp<Snapshot> previous = mSnapshot->previous;
209
Romain Guyf86ef572010-07-01 11:05:42 -0700210 if (restoreOrtho) {
Romain Guy1d83e192010-08-17 11:37:00 -0700211 Rect& r = previous->viewport;
212 glViewport(r.left, r.top, r.right, r.bottom);
Romain Guy260e1022010-07-12 14:41:06 -0700213 mOrthoMatrix.load(current->orthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700214 }
215
Romain Guy8b55f372010-08-18 17:10:07 -0700216 mSaveCount--;
217 mSnapshot = previous;
218
Romain Guybd6b79b2010-06-26 00:13:53 -0700219 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700220 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700221 }
222
Romain Guy2542d192010-08-18 11:47:12 -0700223 if (restoreClip) {
224 setScissorFromClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700225 }
Romain Guy2542d192010-08-18 11:47:12 -0700226
227 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700228}
229
Romain Guyf6a11b82010-06-23 17:47:49 -0700230///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700231// Layers
232///////////////////////////////////////////////////////////////////////////////
233
234int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
235 const SkPaint* p, int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700236 int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700237
238 int alpha = 255;
239 SkXfermode::Mode mode;
240
241 if (p) {
242 alpha = p->getAlpha();
243 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
244 if (!isMode) {
245 // Assume SRC_OVER
246 mode = SkXfermode::kSrcOver_Mode;
247 }
248 } else {
249 mode = SkXfermode::kSrcOver_Mode;
250 }
251
Romain Guy8b55f372010-08-18 17:10:07 -0700252 if (alpha > 0 && !mSnapshot->invisible) {
253 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
254 } else {
255 mSnapshot->invisible = true;
256 }
Romain Guyd55a8612010-06-28 17:42:46 -0700257
258 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700259}
260
261int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
262 int alpha, int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700263 if (alpha == 0xff) {
264 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700265 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700266 SkPaint paint;
267 paint.setAlpha(alpha);
268 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700269 }
Romain Guyd55a8612010-06-28 17:42:46 -0700270}
Romain Guybd6b79b2010-06-26 00:13:53 -0700271
Romain Guyd55a8612010-06-28 17:42:46 -0700272bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
273 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guyd27977d2010-07-14 19:18:51 -0700274 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700275 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700276
Romain Guy8aef54f2010-09-01 15:13:49 -0700277 Rect bounds(left, top, right, bottom);
278 // TODO: Apply transformations and treat layers in screen coordinates
279 // mSnapshot->transform->mapRect(bounds);
280
Romain Guyf18fd992010-07-08 11:45:51 -0700281 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
Romain Guy8aef54f2010-09-01 15:13:49 -0700282 LayerSize size(bounds.getWidth(), bounds.getHeight());
Romain Guyf18fd992010-07-08 11:45:51 -0700283
Romain Guyfb8b7632010-08-23 21:05:08 -0700284 Layer* layer = mCaches.layerCache.get(size, previousFbo);
Romain Guydda570202010-07-06 11:39:32 -0700285 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700286 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700287 }
288
Romain Guyf18fd992010-07-08 11:45:51 -0700289 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
290
Romain Guyf86ef572010-07-01 11:05:42 -0700291 // Clear the FBO
292 glDisable(GL_SCISSOR_TEST);
293 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
294 glClear(GL_COLOR_BUFFER_BIT);
295 glEnable(GL_SCISSOR_TEST);
296
Romain Guydda570202010-07-06 11:39:32 -0700297 layer->mode = mode;
298 layer->alpha = alpha / 255.0f;
Romain Guy8aef54f2010-09-01 15:13:49 -0700299 layer->layer.set(bounds);
Romain Guydda570202010-07-06 11:39:32 -0700300
Romain Guy8fb95422010-08-17 18:38:51 -0700301 // Save the layer in the snapshot
302 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700303 snapshot->layer = layer;
304 snapshot->fbo = layer->fbo;
Romain Guy8aef54f2010-09-01 15:13:49 -0700305 // TODO: Temporary until real layer support is implemented
306 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
307 // TODO: Temporary until real layer support is implemented
308 snapshot->resetClip(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
309 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
310 snapshot->height = bounds.getHeight();
Romain Guy2542d192010-08-18 11:47:12 -0700311 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
312 snapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy1d83e192010-08-17 11:37:00 -0700313
Romain Guyf86ef572010-07-01 11:05:42 -0700314 setScissorFromClip();
315
Romain Guyf86ef572010-07-01 11:05:42 -0700316 // Change the ortho projection
Romain Guy8aef54f2010-09-01 15:13:49 -0700317 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
318 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
Romain Guyf86ef572010-07-01 11:05:42 -0700319
Romain Guyd55a8612010-06-28 17:42:46 -0700320 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700321}
322
Romain Guy1d83e192010-08-17 11:37:00 -0700323void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
324 if (!current->layer) {
325 LOGE("Attempting to compose a layer that does not exist");
326 return;
327 }
328
329 // Unbind current FBO and restore previous one
330 // Most of the time, previous->fbo will be 0 to bind the default buffer
331 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
332
333 // Restore the clip from the previous snapshot
Romain Guy8aef54f2010-09-01 15:13:49 -0700334 const Rect& clip = *previous->clipRect;
335 glScissor(clip.left, previous->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy1d83e192010-08-17 11:37:00 -0700336
337 Layer* layer = current->layer;
338 const Rect& rect = layer->layer;
339
Romain Guy8b55f372010-08-18 17:10:07 -0700340 // FBOs are already drawn with a top-left origin, don't flip the texture
341 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
342
Romain Guy1d83e192010-08-17 11:37:00 -0700343 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
344 layer->texture, layer->alpha, layer->mode, layer->blend);
345
Romain Guy8b55f372010-08-18 17:10:07 -0700346 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
347
Romain Guy1d83e192010-08-17 11:37:00 -0700348 LayerSize size(rect.getWidth(), rect.getHeight());
349 // Failing to add the layer to the cache should happen only if the
350 // layer is too large
Romain Guyfb8b7632010-08-23 21:05:08 -0700351 if (!mCaches.layerCache.put(size, layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700352 LAYER_LOGD("Deleting layer");
353
354 glDeleteFramebuffers(1, &layer->fbo);
355 glDeleteTextures(1, &layer->texture);
356
357 delete layer;
358 }
359}
360
Romain Guybd6b79b2010-06-26 00:13:53 -0700361///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700362// Transforms
363///////////////////////////////////////////////////////////////////////////////
364
365void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700366 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700367}
368
369void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700370 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700371}
372
373void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700374 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700375}
376
377void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700378 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700379}
380
381void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700382 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700383}
384
385void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700386 mat4 m(*matrix);
Romain Guy8aef54f2010-09-01 15:13:49 -0700387 mSnapshot->transform->multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700388}
389
390///////////////////////////////////////////////////////////////////////////////
391// Clipping
392///////////////////////////////////////////////////////////////////////////////
393
Romain Guybb9524b2010-06-22 18:56:38 -0700394void OpenGLRenderer::setScissorFromClip() {
Romain Guy8aef54f2010-09-01 15:13:49 -0700395 const Rect& clip = *mSnapshot->clipRect;
Romain Guyf86ef572010-07-01 11:05:42 -0700396 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700397}
398
399const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700400 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700401}
402
Romain Guyc7d53492010-06-25 13:41:57 -0700403bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guy8b55f372010-08-18 17:10:07 -0700404 if (mSnapshot->invisible) return true;
405
Romain Guy1d83e192010-08-17 11:37:00 -0700406 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700407 mSnapshot->transform->mapRect(r);
408 return !mSnapshot->clipRect->intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700409}
410
Romain Guy079ba2c2010-07-16 14:12:24 -0700411bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
412 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700413 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700414 setScissorFromClip();
415 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700416 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700417}
418
Romain Guyf6a11b82010-06-23 17:47:49 -0700419///////////////////////////////////////////////////////////////////////////////
420// Drawing
421///////////////////////////////////////////////////////////////////////////////
422
Romain Guyc1396e92010-06-30 17:56:19 -0700423void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700424 const float right = left + bitmap->width();
425 const float bottom = top + bitmap->height();
426
427 if (quickReject(left, top, right, bottom)) {
428 return;
429 }
430
Romain Guy61c8c9c2010-08-09 20:48:09 -0700431 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700432 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700433 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700434 const AutoTexture autoCleanup(texture);
435
Romain Guy6926c722010-07-12 20:20:03 -0700436 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700437}
438
Romain Guyf86ef572010-07-01 11:05:42 -0700439void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
440 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
441 const mat4 transform(*matrix);
442 transform.mapRect(r);
443
Romain Guy6926c722010-07-12 20:20:03 -0700444 if (quickReject(r.left, r.top, r.right, r.bottom)) {
445 return;
446 }
447
Romain Guy61c8c9c2010-08-09 20:48:09 -0700448 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700449 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700450 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700451 const AutoTexture autoCleanup(texture);
452
Romain Guy82ba8142010-07-09 13:25:56 -0700453 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700454}
455
Romain Guy8ba548f2010-06-30 19:21:21 -0700456void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
457 float srcLeft, float srcTop, float srcRight, float srcBottom,
458 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700459 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700460 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
461 return;
462 }
463
Romain Guy61c8c9c2010-08-09 20:48:09 -0700464 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700465 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700466 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700467 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700468
Romain Guy8ba548f2010-06-30 19:21:21 -0700469 const float width = texture->width;
470 const float height = texture->height;
471
472 const float u1 = srcLeft / width;
473 const float v1 = srcTop / height;
474 const float u2 = srcRight / width;
475 const float v2 = srcBottom / height;
476
477 resetDrawTextureTexCoords(u1, v1, u2, v2);
478
Romain Guy82ba8142010-07-09 13:25:56 -0700479 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700480
481 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
482}
483
Romain Guydeba7852010-07-07 17:54:48 -0700484void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
485 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700486 if (quickReject(left, top, right, bottom)) {
487 return;
488 }
489
Romain Guy61c8c9c2010-08-09 20:48:09 -0700490 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700491 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700492 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700493 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700494
495 int alpha;
496 SkXfermode::Mode mode;
497 getAlphaAndMode(paint, &alpha, &mode);
498
Romain Guyfb8b7632010-08-23 21:05:08 -0700499 Patch* mesh = mCaches.patchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700500 mesh->updateVertices(bitmap, left, top, right, bottom,
501 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700502
503 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
504 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700505 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
506 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy16202fc2010-07-09 16:13:28 -0700507 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700508}
509
Romain Guy85bf02f2010-06-22 13:11:24 -0700510void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy8b55f372010-08-18 17:10:07 -0700511 if (mSnapshot->invisible) return;
Romain Guy8aef54f2010-09-01 15:13:49 -0700512 const Rect& clip = *mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700513 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700514}
Romain Guy9d5316e2010-06-24 19:30:36 -0700515
Romain Guybd6b79b2010-06-26 00:13:53 -0700516void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700517 if (quickReject(left, top, right, bottom)) {
518 return;
519 }
520
Romain Guy026c5e162010-06-28 17:12:22 -0700521 SkXfermode::Mode mode;
522
523 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
524 if (!isMode) {
525 // Assume SRC_OVER
526 mode = SkXfermode::kSrcOver_Mode;
527 }
528
529 // Skia draws using the color's alpha channel if < 255
530 // Otherwise, it uses the paint's alpha
531 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700532 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700533 color |= p->getAlpha() << 24;
534 }
535
536 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700537}
Romain Guy9d5316e2010-06-24 19:30:36 -0700538
Romain Guye8e62a42010-07-23 18:55:21 -0700539void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
540 float x, float y, SkPaint* paint) {
Romain Guy8b55f372010-08-18 17:10:07 -0700541 if (mSnapshot->invisible || text == NULL || count == 0 ||
542 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700543 return;
544 }
545
Romain Guya80d32f2010-08-20 18:42:37 -0700546 float scaleX = paint->getTextScaleX();
547 bool applyScaleX = scaleX < 0.9999f || scaleX > 1.0001f;
548 if (applyScaleX) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700549 save(SkCanvas::kMatrix_SaveFlag);
Romain Guya80d32f2010-08-20 18:42:37 -0700550 translate(x - (x * scaleX), 0.0f);
551 scale(scaleX, 1.0f);
552 }
553
Romain Guya674ab72010-08-10 17:26:42 -0700554 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700555 switch (paint->getTextAlign()) {
556 case SkPaint::kCenter_Align:
557 length = paint->measureText(text, bytesCount);
558 x -= length / 2.0f;
559 break;
560 case SkPaint::kRight_Align:
561 length = paint->measureText(text, bytesCount);
562 x -= length;
563 break;
564 default:
565 break;
566 }
567
Romain Guy694b5192010-07-21 21:33:20 -0700568 int alpha;
569 SkXfermode::Mode mode;
570 getAlphaAndMode(paint, &alpha, &mode);
571
Romain Guy2542d192010-08-18 11:47:12 -0700572 uint32_t color = paint->getColor();
573 const GLfloat a = alpha / 255.0f;
574 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
575 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
576 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
577
Romain Guyb45c0c92010-08-26 20:35:23 -0700578 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
579 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -0700580 paint->getTextSize());
Romain Guy1e45aae2010-08-13 19:39:53 -0700581 if (mHasShadow) {
582 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -0700583 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -0700584 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700585 count, mShadowRadius);
586 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700587
Romain Guy2542d192010-08-18 11:47:12 -0700588 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700589
590 // Draw the mesh
591 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700592 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700593 }
594
Romain Guy06f96e22010-07-30 19:18:16 -0700595 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700596 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700597
Romain Guyb45c0c92010-08-26 20:35:23 -0700598 setupTextureAlpha8(fontRenderer.getTexture(), 0, 0, textureUnit, x, y, r, g, b, a,
Romain Guy0a417492010-08-16 20:26:20 -0700599 mode, false, true);
Romain Guy06f96e22010-07-30 19:18:16 -0700600
Romain Guy09147fb2010-07-22 13:08:20 -0700601 const Rect& clip = mSnapshot->getLocalClip();
Romain Guyb45c0c92010-08-26 20:35:23 -0700602 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700603
604 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700605 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700606
Romain Guy0a417492010-08-16 20:26:20 -0700607 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guya80d32f2010-08-20 18:42:37 -0700608
609 if (applyScaleX) {
610 restore();
611 }
Romain Guy694b5192010-07-21 21:33:20 -0700612}
613
Romain Guy7fbcc042010-08-04 15:40:07 -0700614void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guy8b55f372010-08-18 17:10:07 -0700615 if (mSnapshot->invisible) return;
616
Romain Guy7fbcc042010-08-04 15:40:07 -0700617 GLuint textureUnit = 0;
618 glActiveTexture(gTextureUnits[textureUnit]);
619
Romain Guyfb8b7632010-08-23 21:05:08 -0700620 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700621 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700622 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700623
Romain Guy8b55f372010-08-18 17:10:07 -0700624 const float x = texture->left - texture->offset;
625 const float y = texture->top - texture->offset;
626
627 if (quickReject(x, y, x + texture->width, y + texture->height)) {
628 return;
629 }
630
Romain Guy7fbcc042010-08-04 15:40:07 -0700631 int alpha;
632 SkXfermode::Mode mode;
633 getAlphaAndMode(paint, &alpha, &mode);
634
635 uint32_t color = paint->getColor();
636 const GLfloat a = alpha / 255.0f;
637 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
638 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
639 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
640
Romain Guy0a417492010-08-16 20:26:20 -0700641 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
642
643 // Draw the mesh
644 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700645 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700646}
647
Romain Guy6926c722010-07-12 20:20:03 -0700648///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700649// Shaders
650///////////////////////////////////////////////////////////////////////////////
651
652void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700653 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700654}
655
Romain Guy06f96e22010-07-30 19:18:16 -0700656void OpenGLRenderer::setupShader(SkiaShader* shader) {
657 mShader = shader;
658 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700659 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700660 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700661}
662
Romain Guyd27977d2010-07-14 19:18:51 -0700663///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700664// Color filters
665///////////////////////////////////////////////////////////////////////////////
666
667void OpenGLRenderer::resetColorFilter() {
668 mColorFilter = NULL;
669}
670
671void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
672 mColorFilter = filter;
673}
674
675///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700676// Drop shadow
677///////////////////////////////////////////////////////////////////////////////
678
679void OpenGLRenderer::resetShadow() {
680 mHasShadow = false;
681}
682
683void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
684 mHasShadow = true;
685 mShadowRadius = radius;
686 mShadowDx = dx;
687 mShadowDy = dy;
688 mShadowColor = color;
689}
690
691///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700692// Drawing implementation
693///////////////////////////////////////////////////////////////////////////////
694
Romain Guy0a417492010-08-16 20:26:20 -0700695void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700696 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700697 const float sx = x - texture->left + mShadowDx;
698 const float sy = y - texture->top + mShadowDy;
699
Romain Guy2542d192010-08-18 11:47:12 -0700700 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
701 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700702 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
703 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
704 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
705
706 GLuint textureUnit = 0;
707 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
708}
709
710void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
711 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
712 bool transforms, bool applyFilters) {
713 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
714 x, y, r, g, b, a, mode, transforms, applyFilters);
715}
716
717void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
718 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
719 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
720 // Describe the required shaders
721 ProgramDescription description;
722 description.hasTexture = true;
723 description.hasAlpha8Texture = true;
724
725 if (applyFilters) {
726 if (mShader) {
727 mShader->describe(description, mExtensions);
728 }
729 if (mColorFilter) {
730 mColorFilter->describe(description, mExtensions);
731 }
732 }
733
734 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700735 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -0700736
737 // Setup the blending mode
738 chooseBlending(true, mode);
739 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
Romain Guyfb8b7632010-08-23 21:05:08 -0700740 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700741
Romain Guyfb8b7632010-08-23 21:05:08 -0700742 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -0700743 glEnableVertexAttribArray(texCoordsSlot);
744
745 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700746 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy0a417492010-08-16 20:26:20 -0700747 gMeshStride, &mMeshVertices[0].position[0]);
748 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
749 gMeshStride, &mMeshVertices[0].texture[0]);
750
751 // Setup uniforms
752 if (transforms) {
753 mModelView.loadTranslate(x, y, 0.0f);
754 mModelView.scale(width, height, 1.0f);
755 } else {
756 mModelView.loadIdentity();
757 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700758 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyfb8b7632010-08-23 21:05:08 -0700759 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -0700760
761 textureUnit++;
762 if (applyFilters) {
763 // Setup attributes and uniforms required by the shaders
764 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700765 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700766 }
767 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700768 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -0700769 }
770 }
771}
772
773#define kStdStrikeThru_Offset (-6.0f / 21.0f)
774#define kStdUnderline_Offset (1.0f / 9.0f)
775#define kStdUnderline_Thickness (1.0f / 18.0f)
776
777void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
778 float x, float y, SkPaint* paint) {
779 // Handle underline and strike-through
780 uint32_t flags = paint->getFlags();
781 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
782 float underlineWidth = length;
783 // If length is > 0.0f, we already measured the text for the text alignment
784 if (length <= 0.0f) {
785 underlineWidth = paint->measureText(text, bytesCount);
786 }
787
788 float offsetX = 0;
789 switch (paint->getTextAlign()) {
790 case SkPaint::kCenter_Align:
791 offsetX = underlineWidth * 0.5f;
792 break;
793 case SkPaint::kRight_Align:
794 offsetX = underlineWidth;
795 break;
796 default:
797 break;
798 }
799
800 if (underlineWidth > 0.0f) {
801 float textSize = paint->getTextSize();
802 float height = textSize * kStdUnderline_Thickness;
803
804 float left = x - offsetX;
805 float top = 0.0f;
806 float right = left + underlineWidth;
807 float bottom = 0.0f;
808
809 if (flags & SkPaint::kUnderlineText_Flag) {
810 top = y + textSize * kStdUnderline_Offset;
811 bottom = top + height;
812 drawRect(left, top, right, bottom, paint);
813 }
814
815 if (flags & SkPaint::kStrikeThruText_Flag) {
816 top = y + textSize * kStdStrikeThru_Offset;
817 bottom = top + height;
818 drawRect(left, top, right, bottom, paint);
819 }
820 }
821 }
822}
823
Romain Guy026c5e162010-06-28 17:12:22 -0700824void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700825 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -0700826 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -0700827 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -0700828 color |= 0x00ffffff;
829 }
830
831 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700832 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700833 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -0700834 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
835 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
836 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
837
Romain Guy06f96e22010-07-30 19:18:16 -0700838 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -0700839
Romain Guy06f96e22010-07-30 19:18:16 -0700840 // Setup the blending mode
841 chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode);
Romain Guy9d5316e2010-06-24 19:30:36 -0700842
Romain Guy06f96e22010-07-30 19:18:16 -0700843 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -0700844 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -0700845 if (mShader) {
846 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -0700847 }
Romain Guydb1938e2010-08-02 18:50:22 -0700848 if (mColorFilter) {
849 mColorFilter->describe(description, mExtensions);
850 }
Romain Guyd27977d2010-07-14 19:18:51 -0700851
Romain Guy06f96e22010-07-30 19:18:16 -0700852 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700853 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -0700854
855 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700856 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy06f96e22010-07-30 19:18:16 -0700857 gMeshStride, &mMeshVertices[0].position[0]);
858
859 // Setup uniforms
860 mModelView.loadTranslate(left, top, 0.0f);
861 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -0700862 if (!ignoreTransform) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700863 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700864 } else {
865 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -0700866 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700867 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700868 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700869
Romain Guy06f96e22010-07-30 19:18:16 -0700870 // Setup attributes and uniforms required by the shaders
871 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700872 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -0700873 }
Romain Guydb1938e2010-08-02 18:50:22 -0700874 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700875 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -0700876 }
Romain Guyc0ac1932010-07-19 18:43:02 -0700877
Romain Guy06f96e22010-07-30 19:18:16 -0700878 // Draw the mesh
Romain Guy889f8d12010-07-29 14:37:42 -0700879 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyd27977d2010-07-14 19:18:51 -0700880}
881
Romain Guy82ba8142010-07-09 13:25:56 -0700882void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700883 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700884 int alpha;
885 SkXfermode::Mode mode;
886 getAlphaAndMode(paint, &alpha, &mode);
887
Romain Guya9794742010-07-13 11:37:54 -0700888 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guyac670c02010-07-27 17:39:27 -0700889 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700890}
891
Romain Guybd6b79b2010-06-26 00:13:53 -0700892void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700893 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
894 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyac670c02010-07-27 17:39:27 -0700895 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guyf7f93552010-07-08 19:17:03 -0700896}
897
898void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700899 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700900 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guy889f8d12010-07-29 14:37:42 -0700901 ProgramDescription description;
902 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -0700903 if (mColorFilter) {
904 mColorFilter->describe(description, mExtensions);
905 }
Romain Guy889f8d12010-07-29 14:37:42 -0700906
Romain Guybd6b79b2010-06-26 00:13:53 -0700907 mModelView.loadTranslate(left, top, 0.0f);
908 mModelView.scale(right - left, bottom - top, 1.0f);
909
Romain Guyfb8b7632010-08-23 21:05:08 -0700910 useProgram(mCaches.programCache.get(description));
Romain Guy8aef54f2010-09-01 15:13:49 -0700911 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700912
Romain Guya9794742010-07-13 11:37:54 -0700913 chooseBlending(blend || alpha < 1.0f, mode);
Romain Guy889f8d12010-07-29 14:37:42 -0700914
915 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -0700916 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700917 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -0700918
Romain Guya9794742010-07-13 11:37:54 -0700919 // Always premultiplied
Romain Guyfb8b7632010-08-23 21:05:08 -0700920 glUniform4f(mCaches.currentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700921
Romain Guy889f8d12010-07-29 14:37:42 -0700922 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -0700923 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -0700924 glEnableVertexAttribArray(texCoordsSlot);
Romain Guyfb8b7632010-08-23 21:05:08 -0700925 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700926 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -0700927 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700928
Romain Guydb1938e2010-08-02 18:50:22 -0700929 // Color filter
930 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700931 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -0700932 }
933
Romain Guyf7f93552010-07-08 19:17:03 -0700934 if (!indices) {
Romain Guyac670c02010-07-27 17:39:27 -0700935 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700936 } else {
937 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
938 }
Romain Guy889f8d12010-07-29 14:37:42 -0700939 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -0700940}
941
942void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
Romain Guy82ba8142010-07-09 13:25:56 -0700943 blend = blend || mode != SkXfermode::kSrcOver_Mode;
944 if (blend) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700945 if (!mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -0700946 glEnable(GL_BLEND);
947 }
948
949 GLenum sourceMode = gBlends[mode].src;
950 GLenum destMode = gBlends[mode].dst;
951 if (!isPremultiplied && sourceMode == GL_ONE) {
952 sourceMode = GL_SRC_ALPHA;
953 }
954
Romain Guyfb8b7632010-08-23 21:05:08 -0700955 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
Romain Guy82ba8142010-07-09 13:25:56 -0700956 glBlendFunc(sourceMode, destMode);
Romain Guyfb8b7632010-08-23 21:05:08 -0700957 mCaches.lastSrcMode = sourceMode;
958 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -0700959 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700960 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -0700961 glDisable(GL_BLEND);
962 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700963 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700964}
965
Romain Guy889f8d12010-07-29 14:37:42 -0700966bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -0700967 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700968 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -0700969 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -0700970 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -0700971 return false;
Romain Guy260e1022010-07-12 14:41:06 -0700972 }
Romain Guy6926c722010-07-12 20:20:03 -0700973 return true;
Romain Guy260e1022010-07-12 14:41:06 -0700974}
975
Romain Guy026c5e162010-06-28 17:12:22 -0700976void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -0700977 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -0700978 TextureVertex::setUV(v++, u1, v1);
979 TextureVertex::setUV(v++, u2, v1);
980 TextureVertex::setUV(v++, u1, v2);
981 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -0700982}
983
984void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
985 if (paint) {
986 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
987 if (!isMode) {
988 // Assume SRC_OVER
989 *mode = SkXfermode::kSrcOver_Mode;
990 }
991
992 // Skia draws using the color's alpha channel if < 255
993 // Otherwise, it uses the paint's alpha
994 int color = paint->getColor();
995 *alpha = (color >> 24) & 0xFF;
996 if (*alpha == 255) {
997 *alpha = paint->getAlpha();
998 }
999 } else {
1000 *mode = SkXfermode::kSrcOver_Mode;
1001 *alpha = 255;
1002 }
Romain Guy026c5e162010-06-28 17:12:22 -07001003}
1004
Romain Guy889f8d12010-07-29 14:37:42 -07001005void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
1006 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001007 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001008 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1009 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1010}
1011
Romain Guy9d5316e2010-06-24 19:30:36 -07001012}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001013}; // namespace android