blob: 02f5dc53c7b7715519cbfa1723bda014ed5762c0 [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 Guydda57022010-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 Guyb82da652010-07-30 11:36:12 -0700130 mSnapshot = new Snapshot(mFirstSnapshot);
Romain Guy8fb95422010-08-17 18:38:51 -0700131 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700132
Romain Guyfb8b7632010-08-23 21:05:08 -0700133 glViewport(0, 0, mWidth, mHeight);
134
Romain Guy08ae3172010-06-21 19:35:50 -0700135 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700136
Romain Guy08ae3172010-06-21 19:35:50 -0700137 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
138 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700139
Romain Guy08ae3172010-06-21 19:35:50 -0700140 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700141 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700142
Romain Guyb82da652010-07-30 11:36:12 -0700143 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700144}
145
Romain Guyda8532c2010-08-31 11:50:35 -0700146void OpenGLRenderer::acquireContext() {
147 if (mCaches.currentProgram) {
148 if (mCaches.currentProgram->isInUse()) {
149 mCaches.currentProgram->remove();
150 mCaches.currentProgram = NULL;
151 }
152 }
153}
154
155void OpenGLRenderer::releaseContext() {
156 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
157
158 glEnable(GL_SCISSOR_TEST);
159 setScissorFromClip();
160
161 if (mCaches.blend) {
162 glEnable(GL_BLEND);
163 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
164 } else {
165 glDisable(GL_BLEND);
166 }
167}
168
Romain Guyf6a11b82010-06-23 17:47:49 -0700169///////////////////////////////////////////////////////////////////////////////
170// State management
171///////////////////////////////////////////////////////////////////////////////
172
Romain Guybb9524b2010-06-22 18:56:38 -0700173int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700174 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700175}
176
177int OpenGLRenderer::save(int flags) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700178 return saveSnapshot();
Romain Guybb9524b2010-06-22 18:56:38 -0700179}
180
181void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700182 if (mSaveCount > 1) {
183 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700184 }
Romain Guybb9524b2010-06-22 18:56:38 -0700185}
186
187void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700188 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700189
Romain Guy8fb95422010-08-17 18:38:51 -0700190 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700191 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700192 }
Romain Guybb9524b2010-06-22 18:56:38 -0700193}
194
195int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700196 mSnapshot = new Snapshot(mSnapshot);
Romain Guy8fb95422010-08-17 18:38:51 -0700197 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700198}
199
200bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700201 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700202 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700203 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700204
Romain Guybd6b79b2010-06-26 00:13:53 -0700205 sp<Snapshot> current = mSnapshot;
206 sp<Snapshot> previous = mSnapshot->previous;
207
Romain Guyf86ef572010-07-01 11:05:42 -0700208 if (restoreOrtho) {
Romain Guy1d83e192010-08-17 11:37:00 -0700209 Rect& r = previous->viewport;
210 glViewport(r.left, r.top, r.right, r.bottom);
Romain Guy260e1022010-07-12 14:41:06 -0700211 mOrthoMatrix.load(current->orthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700212 }
213
Romain Guy8b55f372010-08-18 17:10:07 -0700214 mSaveCount--;
215 mSnapshot = previous;
216
Romain Guybd6b79b2010-06-26 00:13:53 -0700217 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700218 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700219 }
220
Romain Guy2542d192010-08-18 11:47:12 -0700221 if (restoreClip) {
222 setScissorFromClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700223 }
Romain Guy2542d192010-08-18 11:47:12 -0700224
225 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700226}
227
Romain Guyf6a11b82010-06-23 17:47:49 -0700228///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700229// Layers
230///////////////////////////////////////////////////////////////////////////////
231
232int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
233 const SkPaint* p, int flags) {
Romain Guyd55a8612010-06-28 17:42:46 -0700234 int count = saveSnapshot();
235
236 int alpha = 255;
237 SkXfermode::Mode mode;
238
239 if (p) {
240 alpha = p->getAlpha();
241 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
242 if (!isMode) {
243 // Assume SRC_OVER
244 mode = SkXfermode::kSrcOver_Mode;
245 }
246 } else {
247 mode = SkXfermode::kSrcOver_Mode;
248 }
249
Romain Guy8b55f372010-08-18 17:10:07 -0700250 if (alpha > 0 && !mSnapshot->invisible) {
251 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
252 } else {
253 mSnapshot->invisible = true;
254 }
Romain Guyd55a8612010-06-28 17:42:46 -0700255
256 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700257}
258
259int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
260 int alpha, int flags) {
261 int count = saveSnapshot();
Romain Guy8b55f372010-08-18 17:10:07 -0700262 if (alpha > 0 && !mSnapshot->invisible) {
263 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
264 } else {
265 mSnapshot->invisible = true;
266 }
Romain Guyd55a8612010-06-28 17:42:46 -0700267 return count;
268}
Romain Guybd6b79b2010-06-26 00:13:53 -0700269
Romain Guyd55a8612010-06-28 17:42:46 -0700270bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
271 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guyd27977d2010-07-14 19:18:51 -0700272 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700273 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700274
Romain Guyf18fd992010-07-08 11:45:51 -0700275 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
276 LayerSize size(right - left, bottom - top);
277
Romain Guyfb8b7632010-08-23 21:05:08 -0700278 Layer* layer = mCaches.layerCache.get(size, previousFbo);
Romain Guydda57022010-07-06 11:39:32 -0700279 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700280 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700281 }
282
Romain Guyf18fd992010-07-08 11:45:51 -0700283 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
284
Romain Guyf86ef572010-07-01 11:05:42 -0700285 // Clear the FBO
286 glDisable(GL_SCISSOR_TEST);
287 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
288 glClear(GL_COLOR_BUFFER_BIT);
289 glEnable(GL_SCISSOR_TEST);
290
Romain Guydda57022010-07-06 11:39:32 -0700291 layer->mode = mode;
292 layer->alpha = alpha / 255.0f;
293 layer->layer.set(left, top, right, bottom);
294
Romain Guy8fb95422010-08-17 18:38:51 -0700295 // Save the layer in the snapshot
296 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700297 snapshot->layer = layer;
298 snapshot->fbo = layer->fbo;
Romain Guy2542d192010-08-18 11:47:12 -0700299 snapshot->transform.loadTranslate(-left, -top, 0.0f);
300 snapshot->setClip(0.0f, 0.0f, right - left, bottom - top);
301 snapshot->viewport.set(0.0f, 0.0f, right - left, bottom - top);
302 snapshot->height = bottom - top;
303 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
304 snapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy1d83e192010-08-17 11:37:00 -0700305
Romain Guyf86ef572010-07-01 11:05:42 -0700306 setScissorFromClip();
307
Romain Guyf86ef572010-07-01 11:05:42 -0700308 // Change the ortho projection
Romain Guy1d83e192010-08-17 11:37:00 -0700309 glViewport(0, 0, right - left, bottom - top);
Romain Guy8b55f372010-08-18 17:10:07 -0700310 mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, -1.0f, 1.0f);
Romain Guyf86ef572010-07-01 11:05:42 -0700311
Romain Guyd55a8612010-06-28 17:42:46 -0700312 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700313}
314
Romain Guy1d83e192010-08-17 11:37:00 -0700315void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
316 if (!current->layer) {
317 LOGE("Attempting to compose a layer that does not exist");
318 return;
319 }
320
321 // Unbind current FBO and restore previous one
322 // Most of the time, previous->fbo will be 0 to bind the default buffer
323 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
324
325 // Restore the clip from the previous snapshot
326 const Rect& clip = previous->clipRect;
327 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
328
329 Layer* layer = current->layer;
330 const Rect& rect = layer->layer;
331
Romain Guy8b55f372010-08-18 17:10:07 -0700332 // FBOs are already drawn with a top-left origin, don't flip the texture
333 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
334
Romain Guy1d83e192010-08-17 11:37:00 -0700335 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
336 layer->texture, layer->alpha, layer->mode, layer->blend);
337
Romain Guy8b55f372010-08-18 17:10:07 -0700338 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
339
Romain Guy1d83e192010-08-17 11:37:00 -0700340 LayerSize size(rect.getWidth(), rect.getHeight());
341 // Failing to add the layer to the cache should happen only if the
342 // layer is too large
Romain Guyfb8b7632010-08-23 21:05:08 -0700343 if (!mCaches.layerCache.put(size, layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700344 LAYER_LOGD("Deleting layer");
345
346 glDeleteFramebuffers(1, &layer->fbo);
347 glDeleteTextures(1, &layer->texture);
348
349 delete layer;
350 }
351}
352
Romain Guybd6b79b2010-06-26 00:13:53 -0700353///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700354// Transforms
355///////////////////////////////////////////////////////////////////////////////
356
357void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700358 mSnapshot->transform.translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700359}
360
361void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700362 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700363}
364
365void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700366 mSnapshot->transform.scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700367}
368
369void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700370 mSnapshot->transform.load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700371}
372
373void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700374 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700375}
376
377void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700378 mat4 m(*matrix);
379 mSnapshot->transform.multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700380}
381
382///////////////////////////////////////////////////////////////////////////////
383// Clipping
384///////////////////////////////////////////////////////////////////////////////
385
Romain Guybb9524b2010-06-22 18:56:38 -0700386void OpenGLRenderer::setScissorFromClip() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700387 const Rect& clip = mSnapshot->clipRect;
Romain Guyf86ef572010-07-01 11:05:42 -0700388 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700389}
390
391const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700392 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700393}
394
Romain Guyc7d53492010-06-25 13:41:57 -0700395bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guy8b55f372010-08-18 17:10:07 -0700396 if (mSnapshot->invisible) return true;
397
Romain Guy1d83e192010-08-17 11:37:00 -0700398 Rect r(left, top, right, bottom);
399 mSnapshot->transform.mapRect(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700400 return !mSnapshot->clipRect.intersects(r);
401}
402
Romain Guy079ba2c2010-07-16 14:12:24 -0700403bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
404 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700405 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700406 setScissorFromClip();
407 }
Romain Guy079ba2c2010-07-16 14:12:24 -0700408 return !mSnapshot->clipRect.isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700409}
410
Romain Guyf6a11b82010-06-23 17:47:49 -0700411///////////////////////////////////////////////////////////////////////////////
412// Drawing
413///////////////////////////////////////////////////////////////////////////////
414
Romain Guyc1396e92010-06-30 17:56:19 -0700415void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700416 const float right = left + bitmap->width();
417 const float bottom = top + bitmap->height();
418
419 if (quickReject(left, top, right, bottom)) {
420 return;
421 }
422
Romain Guy61c8c9c2010-08-09 20:48:09 -0700423 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700424 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700425 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700426 const AutoTexture autoCleanup(texture);
427
Romain Guy6926c722010-07-12 20:20:03 -0700428 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700429}
430
Romain Guyf86ef572010-07-01 11:05:42 -0700431void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
432 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
433 const mat4 transform(*matrix);
434 transform.mapRect(r);
435
Romain Guy6926c722010-07-12 20:20:03 -0700436 if (quickReject(r.left, r.top, r.right, r.bottom)) {
437 return;
438 }
439
Romain Guy61c8c9c2010-08-09 20:48:09 -0700440 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700441 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700442 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700443 const AutoTexture autoCleanup(texture);
444
Romain Guy82ba8142010-07-09 13:25:56 -0700445 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700446}
447
Romain Guy8ba548f2010-06-30 19:21:21 -0700448void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
449 float srcLeft, float srcTop, float srcRight, float srcBottom,
450 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700451 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700452 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
453 return;
454 }
455
Romain Guy61c8c9c2010-08-09 20:48:09 -0700456 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700457 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700458 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700459 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700460
Romain Guy8ba548f2010-06-30 19:21:21 -0700461 const float width = texture->width;
462 const float height = texture->height;
463
464 const float u1 = srcLeft / width;
465 const float v1 = srcTop / height;
466 const float u2 = srcRight / width;
467 const float v2 = srcBottom / height;
468
469 resetDrawTextureTexCoords(u1, v1, u2, v2);
470
Romain Guy82ba8142010-07-09 13:25:56 -0700471 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700472
473 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
474}
475
Romain Guydeba7852010-07-07 17:54:48 -0700476void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
477 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700478 if (quickReject(left, top, right, bottom)) {
479 return;
480 }
481
Romain Guy61c8c9c2010-08-09 20:48:09 -0700482 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700483 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700484 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700485 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700486
487 int alpha;
488 SkXfermode::Mode mode;
489 getAlphaAndMode(paint, &alpha, &mode);
490
Romain Guyfb8b7632010-08-23 21:05:08 -0700491 Patch* mesh = mCaches.patchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700492 mesh->updateVertices(bitmap, left, top, right, bottom,
493 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700494
495 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
496 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700497 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
498 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy16202fc2010-07-09 16:13:28 -0700499 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700500}
501
Romain Guy85bf02f2010-06-22 13:11:24 -0700502void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy8b55f372010-08-18 17:10:07 -0700503 if (mSnapshot->invisible) return;
Romain Guy079ba2c2010-07-16 14:12:24 -0700504 const Rect& clip = mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700505 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700506}
Romain Guy9d5316e2010-06-24 19:30:36 -0700507
Romain Guybd6b79b2010-06-26 00:13:53 -0700508void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700509 if (quickReject(left, top, right, bottom)) {
510 return;
511 }
512
Romain Guy026c5e162010-06-28 17:12:22 -0700513 SkXfermode::Mode mode;
514
515 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
516 if (!isMode) {
517 // Assume SRC_OVER
518 mode = SkXfermode::kSrcOver_Mode;
519 }
520
521 // Skia draws using the color's alpha channel if < 255
522 // Otherwise, it uses the paint's alpha
523 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700524 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700525 color |= p->getAlpha() << 24;
526 }
527
528 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700529}
Romain Guy9d5316e2010-06-24 19:30:36 -0700530
Romain Guye8e62a42010-07-23 18:55:21 -0700531void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
532 float x, float y, SkPaint* paint) {
Romain Guy8b55f372010-08-18 17:10:07 -0700533 if (mSnapshot->invisible || text == NULL || count == 0 ||
534 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700535 return;
536 }
537
Romain Guya80d32f2010-08-20 18:42:37 -0700538 float scaleX = paint->getTextScaleX();
539 bool applyScaleX = scaleX < 0.9999f || scaleX > 1.0001f;
540 if (applyScaleX) {
541 save(0);
542 translate(x - (x * scaleX), 0.0f);
543 scale(scaleX, 1.0f);
544 }
545
Romain Guya674ab72010-08-10 17:26:42 -0700546 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700547 switch (paint->getTextAlign()) {
548 case SkPaint::kCenter_Align:
549 length = paint->measureText(text, bytesCount);
550 x -= length / 2.0f;
551 break;
552 case SkPaint::kRight_Align:
553 length = paint->measureText(text, bytesCount);
554 x -= length;
555 break;
556 default:
557 break;
558 }
559
Romain Guy694b5192010-07-21 21:33:20 -0700560 int alpha;
561 SkXfermode::Mode mode;
562 getAlphaAndMode(paint, &alpha, &mode);
563
Romain Guy2542d192010-08-18 11:47:12 -0700564 uint32_t color = paint->getColor();
565 const GLfloat a = alpha / 255.0f;
566 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
567 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
568 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
569
Romain Guyb45c0c92010-08-26 20:35:23 -0700570 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
571 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -0700572 paint->getTextSize());
Romain Guy1e45aae2010-08-13 19:39:53 -0700573 if (mHasShadow) {
574 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -0700575 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -0700576 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700577 count, mShadowRadius);
578 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700579
Romain Guy2542d192010-08-18 11:47:12 -0700580 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700581
582 // Draw the mesh
583 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700584 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700585 }
586
Romain Guy06f96e22010-07-30 19:18:16 -0700587 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700588 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700589
Romain Guyb45c0c92010-08-26 20:35:23 -0700590 setupTextureAlpha8(fontRenderer.getTexture(), 0, 0, textureUnit, x, y, r, g, b, a,
Romain Guy0a417492010-08-16 20:26:20 -0700591 mode, false, true);
Romain Guy06f96e22010-07-30 19:18:16 -0700592
Romain Guy09147fb2010-07-22 13:08:20 -0700593 const Rect& clip = mSnapshot->getLocalClip();
Romain Guyb45c0c92010-08-26 20:35:23 -0700594 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700595
596 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700597 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700598
Romain Guy0a417492010-08-16 20:26:20 -0700599 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guya80d32f2010-08-20 18:42:37 -0700600
601 if (applyScaleX) {
602 restore();
603 }
Romain Guy694b5192010-07-21 21:33:20 -0700604}
605
Romain Guy7fbcc042010-08-04 15:40:07 -0700606void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guy8b55f372010-08-18 17:10:07 -0700607 if (mSnapshot->invisible) return;
608
Romain Guy7fbcc042010-08-04 15:40:07 -0700609 GLuint textureUnit = 0;
610 glActiveTexture(gTextureUnits[textureUnit]);
611
Romain Guyfb8b7632010-08-23 21:05:08 -0700612 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700613 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700614 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700615
Romain Guy8b55f372010-08-18 17:10:07 -0700616 const float x = texture->left - texture->offset;
617 const float y = texture->top - texture->offset;
618
619 if (quickReject(x, y, x + texture->width, y + texture->height)) {
620 return;
621 }
622
Romain Guy7fbcc042010-08-04 15:40:07 -0700623 int alpha;
624 SkXfermode::Mode mode;
625 getAlphaAndMode(paint, &alpha, &mode);
626
627 uint32_t color = paint->getColor();
628 const GLfloat a = alpha / 255.0f;
629 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
630 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
631 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
632
Romain Guy0a417492010-08-16 20:26:20 -0700633 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
634
635 // Draw the mesh
636 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700637 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700638}
639
Romain Guy6926c722010-07-12 20:20:03 -0700640///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700641// Shaders
642///////////////////////////////////////////////////////////////////////////////
643
644void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700645 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700646}
647
Romain Guy06f96e22010-07-30 19:18:16 -0700648void OpenGLRenderer::setupShader(SkiaShader* shader) {
649 mShader = shader;
650 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700651 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700652 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700653}
654
Romain Guyd27977d2010-07-14 19:18:51 -0700655///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700656// Color filters
657///////////////////////////////////////////////////////////////////////////////
658
659void OpenGLRenderer::resetColorFilter() {
660 mColorFilter = NULL;
661}
662
663void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
664 mColorFilter = filter;
665}
666
667///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700668// Drop shadow
669///////////////////////////////////////////////////////////////////////////////
670
671void OpenGLRenderer::resetShadow() {
672 mHasShadow = false;
673}
674
675void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
676 mHasShadow = true;
677 mShadowRadius = radius;
678 mShadowDx = dx;
679 mShadowDy = dy;
680 mShadowColor = color;
681}
682
683///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700684// Drawing implementation
685///////////////////////////////////////////////////////////////////////////////
686
Romain Guy0a417492010-08-16 20:26:20 -0700687void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700688 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700689 const float sx = x - texture->left + mShadowDx;
690 const float sy = y - texture->top + mShadowDy;
691
Romain Guy2542d192010-08-18 11:47:12 -0700692 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
693 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700694 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
695 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
696 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
697
698 GLuint textureUnit = 0;
699 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
700}
701
702void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
703 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
704 bool transforms, bool applyFilters) {
705 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
706 x, y, r, g, b, a, mode, transforms, applyFilters);
707}
708
709void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
710 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
711 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
712 // Describe the required shaders
713 ProgramDescription description;
714 description.hasTexture = true;
715 description.hasAlpha8Texture = true;
716
717 if (applyFilters) {
718 if (mShader) {
719 mShader->describe(description, mExtensions);
720 }
721 if (mColorFilter) {
722 mColorFilter->describe(description, mExtensions);
723 }
724 }
725
726 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700727 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -0700728
729 // Setup the blending mode
730 chooseBlending(true, mode);
731 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
Romain Guyfb8b7632010-08-23 21:05:08 -0700732 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700733
Romain Guyfb8b7632010-08-23 21:05:08 -0700734 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -0700735 glEnableVertexAttribArray(texCoordsSlot);
736
737 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700738 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy0a417492010-08-16 20:26:20 -0700739 gMeshStride, &mMeshVertices[0].position[0]);
740 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
741 gMeshStride, &mMeshVertices[0].texture[0]);
742
743 // Setup uniforms
744 if (transforms) {
745 mModelView.loadTranslate(x, y, 0.0f);
746 mModelView.scale(width, height, 1.0f);
747 } else {
748 mModelView.loadIdentity();
749 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700750 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
751 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -0700752
753 textureUnit++;
754 if (applyFilters) {
755 // Setup attributes and uniforms required by the shaders
756 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700757 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700758 }
759 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700760 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -0700761 }
762 }
763}
764
765#define kStdStrikeThru_Offset (-6.0f / 21.0f)
766#define kStdUnderline_Offset (1.0f / 9.0f)
767#define kStdUnderline_Thickness (1.0f / 18.0f)
768
769void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
770 float x, float y, SkPaint* paint) {
771 // Handle underline and strike-through
772 uint32_t flags = paint->getFlags();
773 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
774 float underlineWidth = length;
775 // If length is > 0.0f, we already measured the text for the text alignment
776 if (length <= 0.0f) {
777 underlineWidth = paint->measureText(text, bytesCount);
778 }
779
780 float offsetX = 0;
781 switch (paint->getTextAlign()) {
782 case SkPaint::kCenter_Align:
783 offsetX = underlineWidth * 0.5f;
784 break;
785 case SkPaint::kRight_Align:
786 offsetX = underlineWidth;
787 break;
788 default:
789 break;
790 }
791
792 if (underlineWidth > 0.0f) {
793 float textSize = paint->getTextSize();
794 float height = textSize * kStdUnderline_Thickness;
795
796 float left = x - offsetX;
797 float top = 0.0f;
798 float right = left + underlineWidth;
799 float bottom = 0.0f;
800
801 if (flags & SkPaint::kUnderlineText_Flag) {
802 top = y + textSize * kStdUnderline_Offset;
803 bottom = top + height;
804 drawRect(left, top, right, bottom, paint);
805 }
806
807 if (flags & SkPaint::kStrikeThruText_Flag) {
808 top = y + textSize * kStdStrikeThru_Offset;
809 bottom = top + height;
810 drawRect(left, top, right, bottom, paint);
811 }
812 }
813 }
814}
815
Romain Guy026c5e162010-06-28 17:12:22 -0700816void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700817 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -0700818 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -0700819 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -0700820 color |= 0x00ffffff;
821 }
822
823 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700824 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700825 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -0700826 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
827 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
828 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
829
Romain Guy06f96e22010-07-30 19:18:16 -0700830 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -0700831
Romain Guy06f96e22010-07-30 19:18:16 -0700832 // Setup the blending mode
833 chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode);
Romain Guy9d5316e2010-06-24 19:30:36 -0700834
Romain Guy06f96e22010-07-30 19:18:16 -0700835 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -0700836 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -0700837 if (mShader) {
838 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -0700839 }
Romain Guydb1938e2010-08-02 18:50:22 -0700840 if (mColorFilter) {
841 mColorFilter->describe(description, mExtensions);
842 }
Romain Guyd27977d2010-07-14 19:18:51 -0700843
Romain Guy06f96e22010-07-30 19:18:16 -0700844 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700845 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -0700846
847 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700848 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy06f96e22010-07-30 19:18:16 -0700849 gMeshStride, &mMeshVertices[0].position[0]);
850
851 // Setup uniforms
852 mModelView.loadTranslate(left, top, 0.0f);
853 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -0700854 if (!ignoreTransform) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700855 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700856 } else {
857 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -0700858 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700859 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700860 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700861
Romain Guy06f96e22010-07-30 19:18:16 -0700862 // Setup attributes and uniforms required by the shaders
863 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700864 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -0700865 }
Romain Guydb1938e2010-08-02 18:50:22 -0700866 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700867 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -0700868 }
Romain Guyc0ac1932010-07-19 18:43:02 -0700869
Romain Guy06f96e22010-07-30 19:18:16 -0700870 // Draw the mesh
Romain Guy889f8d12010-07-29 14:37:42 -0700871 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyd27977d2010-07-14 19:18:51 -0700872}
873
Romain Guy82ba8142010-07-09 13:25:56 -0700874void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700875 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700876 int alpha;
877 SkXfermode::Mode mode;
878 getAlphaAndMode(paint, &alpha, &mode);
879
Romain Guya9794742010-07-13 11:37:54 -0700880 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guyac670c02010-07-27 17:39:27 -0700881 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700882}
883
Romain Guybd6b79b2010-06-26 00:13:53 -0700884void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700885 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
886 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyac670c02010-07-27 17:39:27 -0700887 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guyf7f93552010-07-08 19:17:03 -0700888}
889
890void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700891 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700892 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guy889f8d12010-07-29 14:37:42 -0700893 ProgramDescription description;
894 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -0700895 if (mColorFilter) {
896 mColorFilter->describe(description, mExtensions);
897 }
Romain Guy889f8d12010-07-29 14:37:42 -0700898
Romain Guybd6b79b2010-06-26 00:13:53 -0700899 mModelView.loadTranslate(left, top, 0.0f);
900 mModelView.scale(right - left, bottom - top, 1.0f);
901
Romain Guyfb8b7632010-08-23 21:05:08 -0700902 useProgram(mCaches.programCache.get(description));
903 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700904
Romain Guya9794742010-07-13 11:37:54 -0700905 chooseBlending(blend || alpha < 1.0f, mode);
Romain Guy889f8d12010-07-29 14:37:42 -0700906
907 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -0700908 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700909 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -0700910
Romain Guya9794742010-07-13 11:37:54 -0700911 // Always premultiplied
Romain Guyfb8b7632010-08-23 21:05:08 -0700912 glUniform4f(mCaches.currentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700913
Romain Guy889f8d12010-07-29 14:37:42 -0700914 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -0700915 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -0700916 glEnableVertexAttribArray(texCoordsSlot);
Romain Guyfb8b7632010-08-23 21:05:08 -0700917 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700918 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -0700919 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700920
Romain Guydb1938e2010-08-02 18:50:22 -0700921 // Color filter
922 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700923 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -0700924 }
925
Romain Guyf7f93552010-07-08 19:17:03 -0700926 if (!indices) {
Romain Guyac670c02010-07-27 17:39:27 -0700927 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700928 } else {
929 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
930 }
Romain Guy889f8d12010-07-29 14:37:42 -0700931 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -0700932}
933
934void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
Romain Guy82ba8142010-07-09 13:25:56 -0700935 blend = blend || mode != SkXfermode::kSrcOver_Mode;
936 if (blend) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700937 if (!mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -0700938 glEnable(GL_BLEND);
939 }
940
941 GLenum sourceMode = gBlends[mode].src;
942 GLenum destMode = gBlends[mode].dst;
943 if (!isPremultiplied && sourceMode == GL_ONE) {
944 sourceMode = GL_SRC_ALPHA;
945 }
946
Romain Guyfb8b7632010-08-23 21:05:08 -0700947 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
Romain Guy82ba8142010-07-09 13:25:56 -0700948 glBlendFunc(sourceMode, destMode);
Romain Guyfb8b7632010-08-23 21:05:08 -0700949 mCaches.lastSrcMode = sourceMode;
950 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -0700951 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700952 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -0700953 glDisable(GL_BLEND);
954 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700955 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700956}
957
Romain Guy889f8d12010-07-29 14:37:42 -0700958bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -0700959 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700960 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -0700961 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -0700962 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -0700963 return false;
Romain Guy260e1022010-07-12 14:41:06 -0700964 }
Romain Guy6926c722010-07-12 20:20:03 -0700965 return true;
Romain Guy260e1022010-07-12 14:41:06 -0700966}
967
Romain Guy026c5e162010-06-28 17:12:22 -0700968void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -0700969 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -0700970 TextureVertex::setUV(v++, u1, v1);
971 TextureVertex::setUV(v++, u2, v1);
972 TextureVertex::setUV(v++, u1, v2);
973 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -0700974}
975
976void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
977 if (paint) {
978 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
979 if (!isMode) {
980 // Assume SRC_OVER
981 *mode = SkXfermode::kSrcOver_Mode;
982 }
983
984 // Skia draws using the color's alpha channel if < 255
985 // Otherwise, it uses the paint's alpha
986 int color = paint->getColor();
987 *alpha = (color >> 24) & 0xFF;
988 if (*alpha == 255) {
989 *alpha = paint->getAlpha();
990 }
991 } else {
992 *mode = SkXfermode::kSrcOver_Mode;
993 *alpha = 255;
994 }
Romain Guy026c5e162010-06-28 17:12:22 -0700995}
996
Romain Guy889f8d12010-07-29 14:37:42 -0700997void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
998 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -0700999 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001000 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1001 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1002}
1003
Romain Guy9d5316e2010-06-24 19:30:36 -07001004}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001005}; // namespace android