blob: 6c907044ed898acd4ad300f7a65ecf8001b28340 [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();
Romain Guya5aed0d2010-09-09 14:42:43 -0700243 if (!mExtensions.hasFramebufferFetch()) {
244 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
245 if (!isMode) {
246 // Assume SRC_OVER
247 mode = SkXfermode::kSrcOver_Mode;
248 }
249 } else {
250 mode = getXfermode(p->getXfermode());
Romain Guyd55a8612010-06-28 17:42:46 -0700251 }
252 } else {
253 mode = SkXfermode::kSrcOver_Mode;
254 }
255
Romain Guy8b55f372010-08-18 17:10:07 -0700256 if (alpha > 0 && !mSnapshot->invisible) {
257 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
258 } else {
259 mSnapshot->invisible = true;
260 }
Romain Guyd55a8612010-06-28 17:42:46 -0700261
262 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700263}
264
265int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
266 int alpha, int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700267 if (alpha == 0xff) {
268 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700269 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700270 SkPaint paint;
271 paint.setAlpha(alpha);
272 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700273 }
Romain Guyd55a8612010-06-28 17:42:46 -0700274}
Romain Guybd6b79b2010-06-26 00:13:53 -0700275
Romain Guyd55a8612010-06-28 17:42:46 -0700276bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
277 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guyd27977d2010-07-14 19:18:51 -0700278 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700279 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700280
Romain Guy8aef54f2010-09-01 15:13:49 -0700281 Rect bounds(left, top, right, bottom);
282 // TODO: Apply transformations and treat layers in screen coordinates
283 // mSnapshot->transform->mapRect(bounds);
284
Romain Guyf18fd992010-07-08 11:45:51 -0700285 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
Romain Guy8aef54f2010-09-01 15:13:49 -0700286 LayerSize size(bounds.getWidth(), bounds.getHeight());
Romain Guyf18fd992010-07-08 11:45:51 -0700287
Romain Guyfb8b7632010-08-23 21:05:08 -0700288 Layer* layer = mCaches.layerCache.get(size, previousFbo);
Romain Guydda570202010-07-06 11:39:32 -0700289 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700290 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700291 }
292
Romain Guyf18fd992010-07-08 11:45:51 -0700293 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
294
Romain Guyf86ef572010-07-01 11:05:42 -0700295 // Clear the FBO
296 glDisable(GL_SCISSOR_TEST);
297 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
298 glClear(GL_COLOR_BUFFER_BIT);
299 glEnable(GL_SCISSOR_TEST);
300
Romain Guydda570202010-07-06 11:39:32 -0700301 layer->mode = mode;
302 layer->alpha = alpha / 255.0f;
Romain Guy8aef54f2010-09-01 15:13:49 -0700303 layer->layer.set(bounds);
Romain Guydda570202010-07-06 11:39:32 -0700304
Romain Guy8fb95422010-08-17 18:38:51 -0700305 // Save the layer in the snapshot
306 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700307 snapshot->layer = layer;
308 snapshot->fbo = layer->fbo;
Romain Guy8aef54f2010-09-01 15:13:49 -0700309 // TODO: Temporary until real layer support is implemented
310 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
311 // TODO: Temporary until real layer support is implemented
312 snapshot->resetClip(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
313 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
314 snapshot->height = bounds.getHeight();
Romain Guy2542d192010-08-18 11:47:12 -0700315 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
316 snapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy1d83e192010-08-17 11:37:00 -0700317
Romain Guyf86ef572010-07-01 11:05:42 -0700318 setScissorFromClip();
319
Romain Guyf86ef572010-07-01 11:05:42 -0700320 // Change the ortho projection
Romain Guy8aef54f2010-09-01 15:13:49 -0700321 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
322 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
Romain Guyf86ef572010-07-01 11:05:42 -0700323
Romain Guyd55a8612010-06-28 17:42:46 -0700324 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700325}
326
Romain Guy1d83e192010-08-17 11:37:00 -0700327void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
328 if (!current->layer) {
329 LOGE("Attempting to compose a layer that does not exist");
330 return;
331 }
332
333 // Unbind current FBO and restore previous one
334 // Most of the time, previous->fbo will be 0 to bind the default buffer
335 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
336
337 // Restore the clip from the previous snapshot
Romain Guy8aef54f2010-09-01 15:13:49 -0700338 const Rect& clip = *previous->clipRect;
339 glScissor(clip.left, previous->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy1d83e192010-08-17 11:37:00 -0700340
341 Layer* layer = current->layer;
342 const Rect& rect = layer->layer;
343
Romain Guy8b55f372010-08-18 17:10:07 -0700344 // FBOs are already drawn with a top-left origin, don't flip the texture
345 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
346
Romain Guy1d83e192010-08-17 11:37:00 -0700347 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
348 layer->texture, layer->alpha, layer->mode, layer->blend);
349
Romain Guy8b55f372010-08-18 17:10:07 -0700350 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
351
Romain Guy1d83e192010-08-17 11:37:00 -0700352 LayerSize size(rect.getWidth(), rect.getHeight());
353 // Failing to add the layer to the cache should happen only if the
354 // layer is too large
Romain Guyfb8b7632010-08-23 21:05:08 -0700355 if (!mCaches.layerCache.put(size, layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700356 LAYER_LOGD("Deleting layer");
357
358 glDeleteFramebuffers(1, &layer->fbo);
359 glDeleteTextures(1, &layer->texture);
360
361 delete layer;
362 }
363}
364
Romain Guybd6b79b2010-06-26 00:13:53 -0700365///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700366// Transforms
367///////////////////////////////////////////////////////////////////////////////
368
369void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700370 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700371}
372
373void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700374 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700375}
376
377void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700378 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700379}
380
381void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700382 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700383}
384
385void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700386 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700387}
388
389void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700390 mat4 m(*matrix);
Romain Guy8aef54f2010-09-01 15:13:49 -0700391 mSnapshot->transform->multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700392}
393
394///////////////////////////////////////////////////////////////////////////////
395// Clipping
396///////////////////////////////////////////////////////////////////////////////
397
Romain Guybb9524b2010-06-22 18:56:38 -0700398void OpenGLRenderer::setScissorFromClip() {
Romain Guy8aef54f2010-09-01 15:13:49 -0700399 const Rect& clip = *mSnapshot->clipRect;
Romain Guyf86ef572010-07-01 11:05:42 -0700400 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700401}
402
403const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700404 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700405}
406
Romain Guyc7d53492010-06-25 13:41:57 -0700407bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guy8b55f372010-08-18 17:10:07 -0700408 if (mSnapshot->invisible) return true;
409
Romain Guy1d83e192010-08-17 11:37:00 -0700410 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700411 mSnapshot->transform->mapRect(r);
412 return !mSnapshot->clipRect->intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700413}
414
Romain Guy079ba2c2010-07-16 14:12:24 -0700415bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
416 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700417 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700418 setScissorFromClip();
419 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700420 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700421}
422
Romain Guyf6a11b82010-06-23 17:47:49 -0700423///////////////////////////////////////////////////////////////////////////////
424// Drawing
425///////////////////////////////////////////////////////////////////////////////
426
Romain Guyc1396e92010-06-30 17:56:19 -0700427void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700428 const float right = left + bitmap->width();
429 const float bottom = top + bitmap->height();
430
431 if (quickReject(left, top, right, bottom)) {
432 return;
433 }
434
Romain Guy61c8c9c2010-08-09 20:48:09 -0700435 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700436 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700437 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700438 const AutoTexture autoCleanup(texture);
439
Romain Guy6926c722010-07-12 20:20:03 -0700440 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700441}
442
Romain Guyf86ef572010-07-01 11:05:42 -0700443void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
444 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
445 const mat4 transform(*matrix);
446 transform.mapRect(r);
447
Romain Guy6926c722010-07-12 20:20:03 -0700448 if (quickReject(r.left, r.top, r.right, r.bottom)) {
449 return;
450 }
451
Romain Guy61c8c9c2010-08-09 20:48:09 -0700452 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700453 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700454 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700455 const AutoTexture autoCleanup(texture);
456
Romain Guy82ba8142010-07-09 13:25:56 -0700457 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700458}
459
Romain Guy8ba548f2010-06-30 19:21:21 -0700460void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
461 float srcLeft, float srcTop, float srcRight, float srcBottom,
462 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700463 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700464 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
465 return;
466 }
467
Romain Guy61c8c9c2010-08-09 20:48:09 -0700468 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700469 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700470 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700471 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700472
Romain Guy8ba548f2010-06-30 19:21:21 -0700473 const float width = texture->width;
474 const float height = texture->height;
475
476 const float u1 = srcLeft / width;
477 const float v1 = srcTop / height;
478 const float u2 = srcRight / width;
479 const float v2 = srcBottom / height;
480
481 resetDrawTextureTexCoords(u1, v1, u2, v2);
482
Romain Guy82ba8142010-07-09 13:25:56 -0700483 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700484
485 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
486}
487
Romain Guydeba7852010-07-07 17:54:48 -0700488void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
489 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700490 if (quickReject(left, top, right, bottom)) {
491 return;
492 }
493
Romain Guy61c8c9c2010-08-09 20:48:09 -0700494 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700495 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700496 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700497 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700498
499 int alpha;
500 SkXfermode::Mode mode;
501 getAlphaAndMode(paint, &alpha, &mode);
502
Romain Guyfb8b7632010-08-23 21:05:08 -0700503 Patch* mesh = mCaches.patchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700504 mesh->updateVertices(bitmap, left, top, right, bottom,
505 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700506
507 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
508 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700509 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
510 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy16202fc2010-07-09 16:13:28 -0700511 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700512}
513
Romain Guy85bf02f2010-06-22 13:11:24 -0700514void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy8b55f372010-08-18 17:10:07 -0700515 if (mSnapshot->invisible) return;
Romain Guy8aef54f2010-09-01 15:13:49 -0700516 const Rect& clip = *mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700517 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700518}
Romain Guy9d5316e2010-06-24 19:30:36 -0700519
Romain Guybd6b79b2010-06-26 00:13:53 -0700520void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700521 if (quickReject(left, top, right, bottom)) {
522 return;
523 }
524
Romain Guy026c5e162010-06-28 17:12:22 -0700525 SkXfermode::Mode mode;
Romain Guya5aed0d2010-09-09 14:42:43 -0700526 if (!mExtensions.hasFramebufferFetch()) {
527 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
528 if (!isMode) {
529 // Assume SRC_OVER
530 mode = SkXfermode::kSrcOver_Mode;
531 }
532 } else {
533 mode = getXfermode(p->getXfermode());
Romain Guy026c5e162010-06-28 17:12:22 -0700534 }
535
536 // Skia draws using the color's alpha channel if < 255
537 // Otherwise, it uses the paint's alpha
538 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700539 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700540 color |= p->getAlpha() << 24;
541 }
542
543 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700544}
Romain Guy9d5316e2010-06-24 19:30:36 -0700545
Romain Guye8e62a42010-07-23 18:55:21 -0700546void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
547 float x, float y, SkPaint* paint) {
Romain Guy8b55f372010-08-18 17:10:07 -0700548 if (mSnapshot->invisible || text == NULL || count == 0 ||
549 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700550 return;
551 }
552
Romain Guya80d32f2010-08-20 18:42:37 -0700553 float scaleX = paint->getTextScaleX();
554 bool applyScaleX = scaleX < 0.9999f || scaleX > 1.0001f;
555 if (applyScaleX) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700556 save(SkCanvas::kMatrix_SaveFlag);
Romain Guya80d32f2010-08-20 18:42:37 -0700557 translate(x - (x * scaleX), 0.0f);
558 scale(scaleX, 1.0f);
559 }
560
Romain Guya674ab72010-08-10 17:26:42 -0700561 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700562 switch (paint->getTextAlign()) {
563 case SkPaint::kCenter_Align:
564 length = paint->measureText(text, bytesCount);
565 x -= length / 2.0f;
566 break;
567 case SkPaint::kRight_Align:
568 length = paint->measureText(text, bytesCount);
569 x -= length;
570 break;
571 default:
572 break;
573 }
574
Romain Guy694b5192010-07-21 21:33:20 -0700575 int alpha;
576 SkXfermode::Mode mode;
577 getAlphaAndMode(paint, &alpha, &mode);
578
Romain Guy2542d192010-08-18 11:47:12 -0700579 uint32_t color = paint->getColor();
580 const GLfloat a = alpha / 255.0f;
581 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
582 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
583 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
584
Romain Guyb45c0c92010-08-26 20:35:23 -0700585 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
586 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -0700587 paint->getTextSize());
Romain Guy1e45aae2010-08-13 19:39:53 -0700588 if (mHasShadow) {
589 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -0700590 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -0700591 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700592 count, mShadowRadius);
593 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700594
Romain Guy2542d192010-08-18 11:47:12 -0700595 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700596
597 // Draw the mesh
598 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700599 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700600 }
601
Romain Guy06f96e22010-07-30 19:18:16 -0700602 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700603 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700604
Romain Guyb45c0c92010-08-26 20:35:23 -0700605 setupTextureAlpha8(fontRenderer.getTexture(), 0, 0, textureUnit, x, y, r, g, b, a,
Romain Guy0a417492010-08-16 20:26:20 -0700606 mode, false, true);
Romain Guy06f96e22010-07-30 19:18:16 -0700607
Romain Guy09147fb2010-07-22 13:08:20 -0700608 const Rect& clip = mSnapshot->getLocalClip();
Romain Guyb45c0c92010-08-26 20:35:23 -0700609 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700610
611 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700612 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700613
Romain Guy0a417492010-08-16 20:26:20 -0700614 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guya80d32f2010-08-20 18:42:37 -0700615
616 if (applyScaleX) {
617 restore();
618 }
Romain Guy694b5192010-07-21 21:33:20 -0700619}
620
Romain Guy7fbcc042010-08-04 15:40:07 -0700621void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guy8b55f372010-08-18 17:10:07 -0700622 if (mSnapshot->invisible) return;
623
Romain Guy7fbcc042010-08-04 15:40:07 -0700624 GLuint textureUnit = 0;
625 glActiveTexture(gTextureUnits[textureUnit]);
626
Romain Guyfb8b7632010-08-23 21:05:08 -0700627 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700628 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700629 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700630
Romain Guy8b55f372010-08-18 17:10:07 -0700631 const float x = texture->left - texture->offset;
632 const float y = texture->top - texture->offset;
633
634 if (quickReject(x, y, x + texture->width, y + texture->height)) {
635 return;
636 }
637
Romain Guy7fbcc042010-08-04 15:40:07 -0700638 int alpha;
639 SkXfermode::Mode mode;
640 getAlphaAndMode(paint, &alpha, &mode);
641
642 uint32_t color = paint->getColor();
643 const GLfloat a = alpha / 255.0f;
644 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
645 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
646 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
647
Romain Guy0a417492010-08-16 20:26:20 -0700648 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
649
650 // Draw the mesh
651 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700652 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700653}
654
Romain Guy6926c722010-07-12 20:20:03 -0700655///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700656// Shaders
657///////////////////////////////////////////////////////////////////////////////
658
659void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700660 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700661}
662
Romain Guy06f96e22010-07-30 19:18:16 -0700663void OpenGLRenderer::setupShader(SkiaShader* shader) {
664 mShader = shader;
665 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700666 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700667 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700668}
669
Romain Guyd27977d2010-07-14 19:18:51 -0700670///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700671// Color filters
672///////////////////////////////////////////////////////////////////////////////
673
674void OpenGLRenderer::resetColorFilter() {
675 mColorFilter = NULL;
676}
677
678void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
679 mColorFilter = filter;
680}
681
682///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700683// Drop shadow
684///////////////////////////////////////////////////////////////////////////////
685
686void OpenGLRenderer::resetShadow() {
687 mHasShadow = false;
688}
689
690void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
691 mHasShadow = true;
692 mShadowRadius = radius;
693 mShadowDx = dx;
694 mShadowDy = dy;
695 mShadowColor = color;
696}
697
698///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700699// Drawing implementation
700///////////////////////////////////////////////////////////////////////////////
701
Romain Guy0a417492010-08-16 20:26:20 -0700702void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700703 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700704 const float sx = x - texture->left + mShadowDx;
705 const float sy = y - texture->top + mShadowDy;
706
Romain Guy2542d192010-08-18 11:47:12 -0700707 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
708 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700709 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
710 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
711 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
712
713 GLuint textureUnit = 0;
714 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
715}
716
717void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
718 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
719 bool transforms, bool applyFilters) {
720 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
721 x, y, r, g, b, a, mode, transforms, applyFilters);
722}
723
724void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
725 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
726 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
727 // Describe the required shaders
728 ProgramDescription description;
729 description.hasTexture = true;
730 description.hasAlpha8Texture = true;
731
732 if (applyFilters) {
733 if (mShader) {
734 mShader->describe(description, mExtensions);
735 }
736 if (mColorFilter) {
737 mColorFilter->describe(description, mExtensions);
738 }
739 }
740
Romain Guya5aed0d2010-09-09 14:42:43 -0700741 // Setup the blending mode
742 chooseBlending(true, mode, description);
743
Romain Guy0a417492010-08-16 20:26:20 -0700744 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700745 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -0700746
Romain Guy0a417492010-08-16 20:26:20 -0700747 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
Romain Guyfb8b7632010-08-23 21:05:08 -0700748 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700749
Romain Guyfb8b7632010-08-23 21:05:08 -0700750 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -0700751 glEnableVertexAttribArray(texCoordsSlot);
752
753 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700754 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy0a417492010-08-16 20:26:20 -0700755 gMeshStride, &mMeshVertices[0].position[0]);
756 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
757 gMeshStride, &mMeshVertices[0].texture[0]);
758
759 // Setup uniforms
760 if (transforms) {
761 mModelView.loadTranslate(x, y, 0.0f);
762 mModelView.scale(width, height, 1.0f);
763 } else {
764 mModelView.loadIdentity();
765 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700766 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyfb8b7632010-08-23 21:05:08 -0700767 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -0700768
769 textureUnit++;
770 if (applyFilters) {
771 // Setup attributes and uniforms required by the shaders
772 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700773 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700774 }
775 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700776 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -0700777 }
778 }
779}
780
781#define kStdStrikeThru_Offset (-6.0f / 21.0f)
782#define kStdUnderline_Offset (1.0f / 9.0f)
783#define kStdUnderline_Thickness (1.0f / 18.0f)
784
785void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
786 float x, float y, SkPaint* paint) {
787 // Handle underline and strike-through
788 uint32_t flags = paint->getFlags();
789 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
790 float underlineWidth = length;
791 // If length is > 0.0f, we already measured the text for the text alignment
792 if (length <= 0.0f) {
793 underlineWidth = paint->measureText(text, bytesCount);
794 }
795
796 float offsetX = 0;
797 switch (paint->getTextAlign()) {
798 case SkPaint::kCenter_Align:
799 offsetX = underlineWidth * 0.5f;
800 break;
801 case SkPaint::kRight_Align:
802 offsetX = underlineWidth;
803 break;
804 default:
805 break;
806 }
807
808 if (underlineWidth > 0.0f) {
809 float textSize = paint->getTextSize();
810 float height = textSize * kStdUnderline_Thickness;
811
812 float left = x - offsetX;
813 float top = 0.0f;
814 float right = left + underlineWidth;
815 float bottom = 0.0f;
816
817 if (flags & SkPaint::kUnderlineText_Flag) {
818 top = y + textSize * kStdUnderline_Offset;
819 bottom = top + height;
820 drawRect(left, top, right, bottom, paint);
821 }
822
823 if (flags & SkPaint::kStrikeThruText_Flag) {
824 top = y + textSize * kStdStrikeThru_Offset;
825 bottom = top + height;
826 drawRect(left, top, right, bottom, paint);
827 }
828 }
829 }
830}
831
Romain Guy026c5e162010-06-28 17:12:22 -0700832void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700833 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -0700834 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -0700835 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -0700836 color |= 0x00ffffff;
837 }
838
839 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700840 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700841 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -0700842 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
843 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
844 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
845
Romain Guy06f96e22010-07-30 19:18:16 -0700846 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -0700847
Romain Guy06f96e22010-07-30 19:18:16 -0700848 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -0700849 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -0700850 if (mShader) {
851 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -0700852 }
Romain Guydb1938e2010-08-02 18:50:22 -0700853 if (mColorFilter) {
854 mColorFilter->describe(description, mExtensions);
855 }
Romain Guyd27977d2010-07-14 19:18:51 -0700856
Romain Guya5aed0d2010-09-09 14:42:43 -0700857 // Setup the blending mode
858 chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode, description);
859
Romain Guy06f96e22010-07-30 19:18:16 -0700860 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700861 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -0700862
863 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700864 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy06f96e22010-07-30 19:18:16 -0700865 gMeshStride, &mMeshVertices[0].position[0]);
866
867 // Setup uniforms
868 mModelView.loadTranslate(left, top, 0.0f);
869 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -0700870 if (!ignoreTransform) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700871 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700872 } else {
873 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -0700874 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700875 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700876 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700877
Romain Guy06f96e22010-07-30 19:18:16 -0700878 // Setup attributes and uniforms required by the shaders
879 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700880 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -0700881 }
Romain Guydb1938e2010-08-02 18:50:22 -0700882 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700883 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -0700884 }
Romain Guyc0ac1932010-07-19 18:43:02 -0700885
Romain Guy06f96e22010-07-30 19:18:16 -0700886 // Draw the mesh
Romain Guy889f8d12010-07-29 14:37:42 -0700887 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyd27977d2010-07-14 19:18:51 -0700888}
889
Romain Guy82ba8142010-07-09 13:25:56 -0700890void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700891 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700892 int alpha;
893 SkXfermode::Mode mode;
894 getAlphaAndMode(paint, &alpha, &mode);
895
Romain Guya9794742010-07-13 11:37:54 -0700896 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guyac670c02010-07-27 17:39:27 -0700897 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700898}
899
Romain Guybd6b79b2010-06-26 00:13:53 -0700900void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700901 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
902 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyac670c02010-07-27 17:39:27 -0700903 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guyf7f93552010-07-08 19:17:03 -0700904}
905
906void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700907 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700908 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guy889f8d12010-07-29 14:37:42 -0700909 ProgramDescription description;
910 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -0700911 if (mColorFilter) {
912 mColorFilter->describe(description, mExtensions);
913 }
Romain Guy889f8d12010-07-29 14:37:42 -0700914
Romain Guybd6b79b2010-06-26 00:13:53 -0700915 mModelView.loadTranslate(left, top, 0.0f);
916 mModelView.scale(right - left, bottom - top, 1.0f);
917
Romain Guya5aed0d2010-09-09 14:42:43 -0700918 chooseBlending(blend || alpha < 1.0f, mode, description);
919
Romain Guyfb8b7632010-08-23 21:05:08 -0700920 useProgram(mCaches.programCache.get(description));
Romain Guy8aef54f2010-09-01 15:13:49 -0700921 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700922
Romain Guy889f8d12010-07-29 14:37:42 -0700923 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -0700924 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700925 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -0700926
Romain Guya9794742010-07-13 11:37:54 -0700927 // Always premultiplied
Romain Guyfb8b7632010-08-23 21:05:08 -0700928 glUniform4f(mCaches.currentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700929
Romain Guy889f8d12010-07-29 14:37:42 -0700930 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -0700931 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -0700932 glEnableVertexAttribArray(texCoordsSlot);
Romain Guyfb8b7632010-08-23 21:05:08 -0700933 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700934 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -0700935 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700936
Romain Guydb1938e2010-08-02 18:50:22 -0700937 // Color filter
938 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700939 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -0700940 }
941
Romain Guyf7f93552010-07-08 19:17:03 -0700942 if (!indices) {
Romain Guyac670c02010-07-27 17:39:27 -0700943 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700944 } else {
945 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
946 }
Romain Guy889f8d12010-07-29 14:37:42 -0700947 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -0700948}
949
Romain Guya5aed0d2010-09-09 14:42:43 -0700950void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
951 ProgramDescription& description) {
Romain Guy82ba8142010-07-09 13:25:56 -0700952 blend = blend || mode != SkXfermode::kSrcOver_Mode;
953 if (blend) {
Romain Guya5aed0d2010-09-09 14:42:43 -0700954 if (mode < SkXfermode::kPlus_Mode) {
955 if (!mCaches.blend) {
956 glEnable(GL_BLEND);
957 }
Romain Guy82ba8142010-07-09 13:25:56 -0700958
Romain Guya5aed0d2010-09-09 14:42:43 -0700959 GLenum sourceMode = gBlends[mode].src;
960 GLenum destMode = gBlends[mode].dst;
Romain Guy82ba8142010-07-09 13:25:56 -0700961
Romain Guya5aed0d2010-09-09 14:42:43 -0700962 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
963 glBlendFunc(sourceMode, destMode);
964 mCaches.lastSrcMode = sourceMode;
965 mCaches.lastDstMode = destMode;
966 }
967 } else {
968 // These blend modes are not supported by OpenGL directly and have
969 // to be implemented using shaders. Since the shader will perform
970 // the blending, turn blending off here
971 if (mExtensions.hasFramebufferFetch()) {
972 description.framebufferMode = mode;
973 }
974
975 if (mCaches.blend) {
976 glDisable(GL_BLEND);
977 }
978 blend = false;
Romain Guy82ba8142010-07-09 13:25:56 -0700979 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700980 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -0700981 glDisable(GL_BLEND);
982 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700983 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700984}
985
Romain Guy889f8d12010-07-29 14:37:42 -0700986bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -0700987 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700988 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -0700989 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -0700990 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -0700991 return false;
Romain Guy260e1022010-07-12 14:41:06 -0700992 }
Romain Guy6926c722010-07-12 20:20:03 -0700993 return true;
Romain Guy260e1022010-07-12 14:41:06 -0700994}
995
Romain Guy026c5e162010-06-28 17:12:22 -0700996void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -0700997 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -0700998 TextureVertex::setUV(v++, u1, v1);
999 TextureVertex::setUV(v++, u2, v1);
1000 TextureVertex::setUV(v++, u1, v2);
1001 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07001002}
1003
1004void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
1005 if (paint) {
Romain Guya5aed0d2010-09-09 14:42:43 -07001006 if (!mExtensions.hasFramebufferFetch()) {
1007 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1008 if (!isMode) {
1009 // Assume SRC_OVER
1010 *mode = SkXfermode::kSrcOver_Mode;
1011 }
1012 } else {
1013 *mode = getXfermode(paint->getXfermode());
Romain Guy8ba548f2010-06-30 19:21:21 -07001014 }
1015
1016 // Skia draws using the color's alpha channel if < 255
1017 // Otherwise, it uses the paint's alpha
1018 int color = paint->getColor();
1019 *alpha = (color >> 24) & 0xFF;
1020 if (*alpha == 255) {
1021 *alpha = paint->getAlpha();
1022 }
1023 } else {
1024 *mode = SkXfermode::kSrcOver_Mode;
1025 *alpha = 255;
1026 }
Romain Guy026c5e162010-06-28 17:12:22 -07001027}
1028
Romain Guya5aed0d2010-09-09 14:42:43 -07001029SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1030 if (mode == NULL) {
1031 return SkXfermode::kSrcOver_Mode;
1032 }
1033 return mode->fMode;
1034}
1035
Romain Guy889f8d12010-07-29 14:37:42 -07001036void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
1037 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001038 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001039 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1040 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1041}
1042
Romain Guy9d5316e2010-06-24 19:30:36 -07001043}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001044}; // namespace android