blob: 47ab35506cc59ebe7157991780e29e3d59c84449 [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 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 Guy08ae3172010-06-21 19:35:50 -0700136 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700137
Romain Guy08ae3172010-06-21 19:35:50 -0700138 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
139 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700140
Romain Guy08ae3172010-06-21 19:35:50 -0700141 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700142 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700143
Romain Guyb82da652010-07-30 11:36:12 -0700144 mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight);
Romain Guybb9524b2010-06-22 18:56:38 -0700145}
146
Romain Guyda8532c2010-08-31 11:50:35 -0700147void OpenGLRenderer::acquireContext() {
148 if (mCaches.currentProgram) {
149 if (mCaches.currentProgram->isInUse()) {
150 mCaches.currentProgram->remove();
151 mCaches.currentProgram = NULL;
152 }
153 }
154}
155
156void OpenGLRenderer::releaseContext() {
157 glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
158
159 glEnable(GL_SCISSOR_TEST);
160 setScissorFromClip();
161
162 if (mCaches.blend) {
163 glEnable(GL_BLEND);
164 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
165 } else {
166 glDisable(GL_BLEND);
167 }
168}
169
Romain Guyf6a11b82010-06-23 17:47:49 -0700170///////////////////////////////////////////////////////////////////////////////
171// State management
172///////////////////////////////////////////////////////////////////////////////
173
Romain Guybb9524b2010-06-22 18:56:38 -0700174int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700175 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700176}
177
178int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700179 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700180}
181
182void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700183 if (mSaveCount > 1) {
184 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700185 }
Romain Guybb9524b2010-06-22 18:56:38 -0700186}
187
188void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700189 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700190
Romain Guy8fb95422010-08-17 18:38:51 -0700191 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700192 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700193 }
Romain Guybb9524b2010-06-22 18:56:38 -0700194}
195
Romain Guy8aef54f2010-09-01 15:13:49 -0700196int OpenGLRenderer::saveSnapshot(int flags) {
197 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700198 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700199}
200
201bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700202 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700203 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700204 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700205
Romain Guybd6b79b2010-06-26 00:13:53 -0700206 sp<Snapshot> current = mSnapshot;
207 sp<Snapshot> previous = mSnapshot->previous;
208
Romain Guyf86ef572010-07-01 11:05:42 -0700209 if (restoreOrtho) {
Romain Guy1d83e192010-08-17 11:37:00 -0700210 Rect& r = previous->viewport;
211 glViewport(r.left, r.top, r.right, r.bottom);
Romain Guy260e1022010-07-12 14:41:06 -0700212 mOrthoMatrix.load(current->orthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700213 }
214
Romain Guy8b55f372010-08-18 17:10:07 -0700215 mSaveCount--;
216 mSnapshot = previous;
217
Romain Guybd6b79b2010-06-26 00:13:53 -0700218 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700219 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700220 }
221
Romain Guy2542d192010-08-18 11:47:12 -0700222 if (restoreClip) {
223 setScissorFromClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700224 }
Romain Guy2542d192010-08-18 11:47:12 -0700225
226 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700227}
228
Romain Guyf6a11b82010-06-23 17:47:49 -0700229///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700230// Layers
231///////////////////////////////////////////////////////////////////////////////
232
233int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
234 const SkPaint* p, int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700235 int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700236
237 int alpha = 255;
238 SkXfermode::Mode mode;
239
240 if (p) {
241 alpha = p->getAlpha();
242 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
243 if (!isMode) {
244 // Assume SRC_OVER
245 mode = SkXfermode::kSrcOver_Mode;
246 }
247 } else {
248 mode = SkXfermode::kSrcOver_Mode;
249 }
250
Romain Guy8b55f372010-08-18 17:10:07 -0700251 if (alpha > 0 && !mSnapshot->invisible) {
252 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
253 } else {
254 mSnapshot->invisible = true;
255 }
Romain Guyd55a8612010-06-28 17:42:46 -0700256
257 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700258}
259
260int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
261 int alpha, int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700262 if (alpha == 0xff) {
263 return saveLayer(left, top, right, bottom, NULL, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700264 } else {
Romain Guy8aef54f2010-09-01 15:13:49 -0700265 SkPaint paint;
266 paint.setAlpha(alpha);
267 return saveLayer(left, top, right, bottom, &paint, flags);
Romain Guy8b55f372010-08-18 17:10:07 -0700268 }
Romain Guyd55a8612010-06-28 17:42:46 -0700269}
Romain Guybd6b79b2010-06-26 00:13:53 -0700270
Romain Guyd55a8612010-06-28 17:42:46 -0700271bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
272 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guyd27977d2010-07-14 19:18:51 -0700273 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700274 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700275
Romain Guy8aef54f2010-09-01 15:13:49 -0700276 Rect bounds(left, top, right, bottom);
277 // TODO: Apply transformations and treat layers in screen coordinates
278 // mSnapshot->transform->mapRect(bounds);
279
Romain Guyf18fd992010-07-08 11:45:51 -0700280 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
Romain Guy8aef54f2010-09-01 15:13:49 -0700281 LayerSize size(bounds.getWidth(), bounds.getHeight());
Romain Guyf18fd992010-07-08 11:45:51 -0700282
Romain Guyfb8b7632010-08-23 21:05:08 -0700283 Layer* layer = mCaches.layerCache.get(size, previousFbo);
Romain Guydda57022010-07-06 11:39:32 -0700284 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700285 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700286 }
287
Romain Guyf18fd992010-07-08 11:45:51 -0700288 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
289
Romain Guyf86ef572010-07-01 11:05:42 -0700290 // Clear the FBO
291 glDisable(GL_SCISSOR_TEST);
292 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
293 glClear(GL_COLOR_BUFFER_BIT);
294 glEnable(GL_SCISSOR_TEST);
295
Romain Guydda57022010-07-06 11:39:32 -0700296 layer->mode = mode;
297 layer->alpha = alpha / 255.0f;
Romain Guy8aef54f2010-09-01 15:13:49 -0700298 layer->layer.set(bounds);
Romain Guydda57022010-07-06 11:39:32 -0700299
Romain Guy8fb95422010-08-17 18:38:51 -0700300 // Save the layer in the snapshot
301 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda57022010-07-06 11:39:32 -0700302 snapshot->layer = layer;
303 snapshot->fbo = layer->fbo;
Romain Guy8aef54f2010-09-01 15:13:49 -0700304 // TODO: Temporary until real layer support is implemented
305 snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
306 // TODO: Temporary until real layer support is implemented
307 snapshot->resetClip(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
308 snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
309 snapshot->height = bounds.getHeight();
Romain Guy2542d192010-08-18 11:47:12 -0700310 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
311 snapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy1d83e192010-08-17 11:37:00 -0700312
Romain Guyf86ef572010-07-01 11:05:42 -0700313 setScissorFromClip();
314
Romain Guyf86ef572010-07-01 11:05:42 -0700315 // Change the ortho projection
Romain Guy8aef54f2010-09-01 15:13:49 -0700316 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
317 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
Romain Guyf86ef572010-07-01 11:05:42 -0700318
Romain Guyd55a8612010-06-28 17:42:46 -0700319 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700320}
321
Romain Guy1d83e192010-08-17 11:37:00 -0700322void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
323 if (!current->layer) {
324 LOGE("Attempting to compose a layer that does not exist");
325 return;
326 }
327
328 // Unbind current FBO and restore previous one
329 // Most of the time, previous->fbo will be 0 to bind the default buffer
330 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
331
332 // Restore the clip from the previous snapshot
Romain Guy8aef54f2010-09-01 15:13:49 -0700333 const Rect& clip = *previous->clipRect;
334 glScissor(clip.left, previous->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy1d83e192010-08-17 11:37:00 -0700335
336 Layer* layer = current->layer;
337 const Rect& rect = layer->layer;
338
Romain Guy8b55f372010-08-18 17:10:07 -0700339 // FBOs are already drawn with a top-left origin, don't flip the texture
340 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
341
Romain Guy1d83e192010-08-17 11:37:00 -0700342 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
343 layer->texture, layer->alpha, layer->mode, layer->blend);
344
Romain Guy8b55f372010-08-18 17:10:07 -0700345 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
346
Romain Guy1d83e192010-08-17 11:37:00 -0700347 LayerSize size(rect.getWidth(), rect.getHeight());
348 // Failing to add the layer to the cache should happen only if the
349 // layer is too large
Romain Guyfb8b7632010-08-23 21:05:08 -0700350 if (!mCaches.layerCache.put(size, layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700351 LAYER_LOGD("Deleting layer");
352
353 glDeleteFramebuffers(1, &layer->fbo);
354 glDeleteTextures(1, &layer->texture);
355
356 delete layer;
357 }
358}
359
Romain Guybd6b79b2010-06-26 00:13:53 -0700360///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700361// Transforms
362///////////////////////////////////////////////////////////////////////////////
363
364void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700365 mSnapshot->transform->translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700366}
367
368void OpenGLRenderer::rotate(float degrees) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700369 mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700370}
371
372void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700373 mSnapshot->transform->scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700374}
375
376void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700377 mSnapshot->transform->load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700378}
379
380void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700381 mSnapshot->transform->copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700382}
383
384void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700385 mat4 m(*matrix);
Romain Guy8aef54f2010-09-01 15:13:49 -0700386 mSnapshot->transform->multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700387}
388
389///////////////////////////////////////////////////////////////////////////////
390// Clipping
391///////////////////////////////////////////////////////////////////////////////
392
Romain Guybb9524b2010-06-22 18:56:38 -0700393void OpenGLRenderer::setScissorFromClip() {
Romain Guy8aef54f2010-09-01 15:13:49 -0700394 const Rect& clip = *mSnapshot->clipRect;
Romain Guyf86ef572010-07-01 11:05:42 -0700395 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700396}
397
398const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700399 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700400}
401
Romain Guyc7d53492010-06-25 13:41:57 -0700402bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guy8b55f372010-08-18 17:10:07 -0700403 if (mSnapshot->invisible) return true;
404
Romain Guy1d83e192010-08-17 11:37:00 -0700405 Rect r(left, top, right, bottom);
Romain Guy8aef54f2010-09-01 15:13:49 -0700406 mSnapshot->transform->mapRect(r);
407 return !mSnapshot->clipRect->intersects(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700408}
409
Romain Guy079ba2c2010-07-16 14:12:24 -0700410bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
411 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700412 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700413 setScissorFromClip();
414 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700415 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700416}
417
Romain Guyf6a11b82010-06-23 17:47:49 -0700418///////////////////////////////////////////////////////////////////////////////
419// Drawing
420///////////////////////////////////////////////////////////////////////////////
421
Romain Guyc1396e92010-06-30 17:56:19 -0700422void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700423 const float right = left + bitmap->width();
424 const float bottom = top + bitmap->height();
425
426 if (quickReject(left, top, right, bottom)) {
427 return;
428 }
429
Romain Guy61c8c9c2010-08-09 20:48:09 -0700430 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700431 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700432 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700433 const AutoTexture autoCleanup(texture);
434
Romain Guy6926c722010-07-12 20:20:03 -0700435 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700436}
437
Romain Guyf86ef572010-07-01 11:05:42 -0700438void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
439 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
440 const mat4 transform(*matrix);
441 transform.mapRect(r);
442
Romain Guy6926c722010-07-12 20:20:03 -0700443 if (quickReject(r.left, r.top, r.right, r.bottom)) {
444 return;
445 }
446
Romain Guy61c8c9c2010-08-09 20:48:09 -0700447 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700448 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700449 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700450 const AutoTexture autoCleanup(texture);
451
Romain Guy82ba8142010-07-09 13:25:56 -0700452 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700453}
454
Romain Guy8ba548f2010-06-30 19:21:21 -0700455void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
456 float srcLeft, float srcTop, float srcRight, float srcBottom,
457 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700458 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700459 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
460 return;
461 }
462
Romain Guy61c8c9c2010-08-09 20:48:09 -0700463 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700464 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700465 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700466 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700467
Romain Guy8ba548f2010-06-30 19:21:21 -0700468 const float width = texture->width;
469 const float height = texture->height;
470
471 const float u1 = srcLeft / width;
472 const float v1 = srcTop / height;
473 const float u2 = srcRight / width;
474 const float v2 = srcBottom / height;
475
476 resetDrawTextureTexCoords(u1, v1, u2, v2);
477
Romain Guy82ba8142010-07-09 13:25:56 -0700478 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700479
480 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
481}
482
Romain Guydeba7852010-07-07 17:54:48 -0700483void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
484 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700485 if (quickReject(left, top, right, bottom)) {
486 return;
487 }
488
Romain Guy61c8c9c2010-08-09 20:48:09 -0700489 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700490 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700491 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700492 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700493
494 int alpha;
495 SkXfermode::Mode mode;
496 getAlphaAndMode(paint, &alpha, &mode);
497
Romain Guyfb8b7632010-08-23 21:05:08 -0700498 Patch* mesh = mCaches.patchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700499 mesh->updateVertices(bitmap, left, top, right, bottom,
500 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700501
502 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
503 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700504 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
505 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy16202fc2010-07-09 16:13:28 -0700506 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700507}
508
Romain Guy85bf02f2010-06-22 13:11:24 -0700509void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy8b55f372010-08-18 17:10:07 -0700510 if (mSnapshot->invisible) return;
Romain Guy8aef54f2010-09-01 15:13:49 -0700511 const Rect& clip = *mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700512 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700513}
Romain Guy9d5316e2010-06-24 19:30:36 -0700514
Romain Guybd6b79b2010-06-26 00:13:53 -0700515void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700516 if (quickReject(left, top, right, bottom)) {
517 return;
518 }
519
Romain Guy026c5e162010-06-28 17:12:22 -0700520 SkXfermode::Mode mode;
521
522 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
523 if (!isMode) {
524 // Assume SRC_OVER
525 mode = SkXfermode::kSrcOver_Mode;
526 }
527
528 // Skia draws using the color's alpha channel if < 255
529 // Otherwise, it uses the paint's alpha
530 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700531 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700532 color |= p->getAlpha() << 24;
533 }
534
535 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700536}
Romain Guy9d5316e2010-06-24 19:30:36 -0700537
Romain Guye8e62a42010-07-23 18:55:21 -0700538void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
539 float x, float y, SkPaint* paint) {
Romain Guy8b55f372010-08-18 17:10:07 -0700540 if (mSnapshot->invisible || text == NULL || count == 0 ||
541 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700542 return;
543 }
544
Romain Guya80d32f2010-08-20 18:42:37 -0700545 float scaleX = paint->getTextScaleX();
546 bool applyScaleX = scaleX < 0.9999f || scaleX > 1.0001f;
547 if (applyScaleX) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700548 save(SkCanvas::kMatrix_SaveFlag);
Romain Guya80d32f2010-08-20 18:42:37 -0700549 translate(x - (x * scaleX), 0.0f);
550 scale(scaleX, 1.0f);
551 }
552
Romain Guya674ab72010-08-10 17:26:42 -0700553 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700554 switch (paint->getTextAlign()) {
555 case SkPaint::kCenter_Align:
556 length = paint->measureText(text, bytesCount);
557 x -= length / 2.0f;
558 break;
559 case SkPaint::kRight_Align:
560 length = paint->measureText(text, bytesCount);
561 x -= length;
562 break;
563 default:
564 break;
565 }
566
Romain Guy694b5192010-07-21 21:33:20 -0700567 int alpha;
568 SkXfermode::Mode mode;
569 getAlphaAndMode(paint, &alpha, &mode);
570
Romain Guy2542d192010-08-18 11:47:12 -0700571 uint32_t color = paint->getColor();
572 const GLfloat a = alpha / 255.0f;
573 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
574 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
575 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
576
Romain Guyb45c0c92010-08-26 20:35:23 -0700577 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
578 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -0700579 paint->getTextSize());
Romain Guy1e45aae2010-08-13 19:39:53 -0700580 if (mHasShadow) {
581 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -0700582 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -0700583 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700584 count, mShadowRadius);
585 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700586
Romain Guy2542d192010-08-18 11:47:12 -0700587 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700588
589 // Draw the mesh
590 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700591 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700592 }
593
Romain Guy06f96e22010-07-30 19:18:16 -0700594 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700595 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700596
Romain Guyb45c0c92010-08-26 20:35:23 -0700597 setupTextureAlpha8(fontRenderer.getTexture(), 0, 0, textureUnit, x, y, r, g, b, a,
Romain Guy0a417492010-08-16 20:26:20 -0700598 mode, false, true);
Romain Guy06f96e22010-07-30 19:18:16 -0700599
Romain Guy09147fb2010-07-22 13:08:20 -0700600 const Rect& clip = mSnapshot->getLocalClip();
Romain Guyb45c0c92010-08-26 20:35:23 -0700601 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700602
603 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700604 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700605
Romain Guy0a417492010-08-16 20:26:20 -0700606 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guya80d32f2010-08-20 18:42:37 -0700607
608 if (applyScaleX) {
609 restore();
610 }
Romain Guy694b5192010-07-21 21:33:20 -0700611}
612
Romain Guy7fbcc042010-08-04 15:40:07 -0700613void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guy8b55f372010-08-18 17:10:07 -0700614 if (mSnapshot->invisible) return;
615
Romain Guy7fbcc042010-08-04 15:40:07 -0700616 GLuint textureUnit = 0;
617 glActiveTexture(gTextureUnits[textureUnit]);
618
Romain Guyfb8b7632010-08-23 21:05:08 -0700619 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700620 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700621 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700622
Romain Guy8b55f372010-08-18 17:10:07 -0700623 const float x = texture->left - texture->offset;
624 const float y = texture->top - texture->offset;
625
626 if (quickReject(x, y, x + texture->width, y + texture->height)) {
627 return;
628 }
629
Romain Guy7fbcc042010-08-04 15:40:07 -0700630 int alpha;
631 SkXfermode::Mode mode;
632 getAlphaAndMode(paint, &alpha, &mode);
633
634 uint32_t color = paint->getColor();
635 const GLfloat a = alpha / 255.0f;
636 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
637 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
638 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
639
Romain Guy0a417492010-08-16 20:26:20 -0700640 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
641
642 // Draw the mesh
643 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700644 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700645}
646
Romain Guy6926c722010-07-12 20:20:03 -0700647///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700648// Shaders
649///////////////////////////////////////////////////////////////////////////////
650
651void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700652 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700653}
654
Romain Guy06f96e22010-07-30 19:18:16 -0700655void OpenGLRenderer::setupShader(SkiaShader* shader) {
656 mShader = shader;
657 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700658 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700659 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700660}
661
Romain Guyd27977d2010-07-14 19:18:51 -0700662///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700663// Color filters
664///////////////////////////////////////////////////////////////////////////////
665
666void OpenGLRenderer::resetColorFilter() {
667 mColorFilter = NULL;
668}
669
670void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
671 mColorFilter = filter;
672}
673
674///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700675// Drop shadow
676///////////////////////////////////////////////////////////////////////////////
677
678void OpenGLRenderer::resetShadow() {
679 mHasShadow = false;
680}
681
682void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
683 mHasShadow = true;
684 mShadowRadius = radius;
685 mShadowDx = dx;
686 mShadowDy = dy;
687 mShadowColor = color;
688}
689
690///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700691// Drawing implementation
692///////////////////////////////////////////////////////////////////////////////
693
Romain Guy0a417492010-08-16 20:26:20 -0700694void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700695 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700696 const float sx = x - texture->left + mShadowDx;
697 const float sy = y - texture->top + mShadowDy;
698
Romain Guy2542d192010-08-18 11:47:12 -0700699 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
700 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700701 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
702 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
703 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
704
705 GLuint textureUnit = 0;
706 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
707}
708
709void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
710 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
711 bool transforms, bool applyFilters) {
712 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
713 x, y, r, g, b, a, mode, transforms, applyFilters);
714}
715
716void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
717 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
718 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
719 // Describe the required shaders
720 ProgramDescription description;
721 description.hasTexture = true;
722 description.hasAlpha8Texture = true;
723
724 if (applyFilters) {
725 if (mShader) {
726 mShader->describe(description, mExtensions);
727 }
728 if (mColorFilter) {
729 mColorFilter->describe(description, mExtensions);
730 }
731 }
732
733 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700734 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -0700735
736 // Setup the blending mode
737 chooseBlending(true, mode);
738 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
Romain Guyfb8b7632010-08-23 21:05:08 -0700739 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700740
Romain Guyfb8b7632010-08-23 21:05:08 -0700741 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -0700742 glEnableVertexAttribArray(texCoordsSlot);
743
744 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700745 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy0a417492010-08-16 20:26:20 -0700746 gMeshStride, &mMeshVertices[0].position[0]);
747 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
748 gMeshStride, &mMeshVertices[0].texture[0]);
749
750 // Setup uniforms
751 if (transforms) {
752 mModelView.loadTranslate(x, y, 0.0f);
753 mModelView.scale(width, height, 1.0f);
754 } else {
755 mModelView.loadIdentity();
756 }
Romain Guy8aef54f2010-09-01 15:13:49 -0700757 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyfb8b7632010-08-23 21:05:08 -0700758 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -0700759
760 textureUnit++;
761 if (applyFilters) {
762 // Setup attributes and uniforms required by the shaders
763 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700764 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700765 }
766 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700767 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -0700768 }
769 }
770}
771
772#define kStdStrikeThru_Offset (-6.0f / 21.0f)
773#define kStdUnderline_Offset (1.0f / 9.0f)
774#define kStdUnderline_Thickness (1.0f / 18.0f)
775
776void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
777 float x, float y, SkPaint* paint) {
778 // Handle underline and strike-through
779 uint32_t flags = paint->getFlags();
780 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
781 float underlineWidth = length;
782 // If length is > 0.0f, we already measured the text for the text alignment
783 if (length <= 0.0f) {
784 underlineWidth = paint->measureText(text, bytesCount);
785 }
786
787 float offsetX = 0;
788 switch (paint->getTextAlign()) {
789 case SkPaint::kCenter_Align:
790 offsetX = underlineWidth * 0.5f;
791 break;
792 case SkPaint::kRight_Align:
793 offsetX = underlineWidth;
794 break;
795 default:
796 break;
797 }
798
799 if (underlineWidth > 0.0f) {
800 float textSize = paint->getTextSize();
801 float height = textSize * kStdUnderline_Thickness;
802
803 float left = x - offsetX;
804 float top = 0.0f;
805 float right = left + underlineWidth;
806 float bottom = 0.0f;
807
808 if (flags & SkPaint::kUnderlineText_Flag) {
809 top = y + textSize * kStdUnderline_Offset;
810 bottom = top + height;
811 drawRect(left, top, right, bottom, paint);
812 }
813
814 if (flags & SkPaint::kStrikeThruText_Flag) {
815 top = y + textSize * kStdStrikeThru_Offset;
816 bottom = top + height;
817 drawRect(left, top, right, bottom, paint);
818 }
819 }
820 }
821}
822
Romain Guy026c5e162010-06-28 17:12:22 -0700823void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700824 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -0700825 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -0700826 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -0700827 color |= 0x00ffffff;
828 }
829
830 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700831 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700832 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -0700833 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
834 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
835 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
836
Romain Guy06f96e22010-07-30 19:18:16 -0700837 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -0700838
Romain Guy06f96e22010-07-30 19:18:16 -0700839 // Setup the blending mode
840 chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode);
Romain Guy9d5316e2010-06-24 19:30:36 -0700841
Romain Guy06f96e22010-07-30 19:18:16 -0700842 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -0700843 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -0700844 if (mShader) {
845 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -0700846 }
Romain Guydb1938e2010-08-02 18:50:22 -0700847 if (mColorFilter) {
848 mColorFilter->describe(description, mExtensions);
849 }
Romain Guyd27977d2010-07-14 19:18:51 -0700850
Romain Guy06f96e22010-07-30 19:18:16 -0700851 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700852 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -0700853
854 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700855 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy06f96e22010-07-30 19:18:16 -0700856 gMeshStride, &mMeshVertices[0].position[0]);
857
858 // Setup uniforms
859 mModelView.loadTranslate(left, top, 0.0f);
860 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -0700861 if (!ignoreTransform) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700862 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700863 } else {
864 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -0700865 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700866 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700867 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700868
Romain Guy06f96e22010-07-30 19:18:16 -0700869 // Setup attributes and uniforms required by the shaders
870 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700871 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -0700872 }
Romain Guydb1938e2010-08-02 18:50:22 -0700873 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700874 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -0700875 }
Romain Guyc0ac1932010-07-19 18:43:02 -0700876
Romain Guy06f96e22010-07-30 19:18:16 -0700877 // Draw the mesh
Romain Guy889f8d12010-07-29 14:37:42 -0700878 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyd27977d2010-07-14 19:18:51 -0700879}
880
Romain Guy82ba8142010-07-09 13:25:56 -0700881void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700882 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700883 int alpha;
884 SkXfermode::Mode mode;
885 getAlphaAndMode(paint, &alpha, &mode);
886
Romain Guya9794742010-07-13 11:37:54 -0700887 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guyac670c02010-07-27 17:39:27 -0700888 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700889}
890
Romain Guybd6b79b2010-06-26 00:13:53 -0700891void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700892 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
893 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyac670c02010-07-27 17:39:27 -0700894 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guyf7f93552010-07-08 19:17:03 -0700895}
896
897void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700898 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700899 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guy889f8d12010-07-29 14:37:42 -0700900 ProgramDescription description;
901 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -0700902 if (mColorFilter) {
903 mColorFilter->describe(description, mExtensions);
904 }
Romain Guy889f8d12010-07-29 14:37:42 -0700905
Romain Guybd6b79b2010-06-26 00:13:53 -0700906 mModelView.loadTranslate(left, top, 0.0f);
907 mModelView.scale(right - left, bottom - top, 1.0f);
908
Romain Guyfb8b7632010-08-23 21:05:08 -0700909 useProgram(mCaches.programCache.get(description));
Romain Guy8aef54f2010-09-01 15:13:49 -0700910 mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700911
Romain Guya9794742010-07-13 11:37:54 -0700912 chooseBlending(blend || alpha < 1.0f, mode);
Romain Guy889f8d12010-07-29 14:37:42 -0700913
914 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -0700915 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700916 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -0700917
Romain Guya9794742010-07-13 11:37:54 -0700918 // Always premultiplied
Romain Guyfb8b7632010-08-23 21:05:08 -0700919 glUniform4f(mCaches.currentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700920
Romain Guy889f8d12010-07-29 14:37:42 -0700921 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -0700922 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -0700923 glEnableVertexAttribArray(texCoordsSlot);
Romain Guyfb8b7632010-08-23 21:05:08 -0700924 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700925 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -0700926 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700927
Romain Guydb1938e2010-08-02 18:50:22 -0700928 // Color filter
929 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700930 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -0700931 }
932
Romain Guyf7f93552010-07-08 19:17:03 -0700933 if (!indices) {
Romain Guyac670c02010-07-27 17:39:27 -0700934 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700935 } else {
936 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
937 }
Romain Guy889f8d12010-07-29 14:37:42 -0700938 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -0700939}
940
941void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
Romain Guy82ba8142010-07-09 13:25:56 -0700942 blend = blend || mode != SkXfermode::kSrcOver_Mode;
943 if (blend) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700944 if (!mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -0700945 glEnable(GL_BLEND);
946 }
947
948 GLenum sourceMode = gBlends[mode].src;
949 GLenum destMode = gBlends[mode].dst;
950 if (!isPremultiplied && sourceMode == GL_ONE) {
951 sourceMode = GL_SRC_ALPHA;
952 }
953
Romain Guyfb8b7632010-08-23 21:05:08 -0700954 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
Romain Guy82ba8142010-07-09 13:25:56 -0700955 glBlendFunc(sourceMode, destMode);
Romain Guyfb8b7632010-08-23 21:05:08 -0700956 mCaches.lastSrcMode = sourceMode;
957 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -0700958 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700959 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -0700960 glDisable(GL_BLEND);
961 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700962 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700963}
964
Romain Guy889f8d12010-07-29 14:37:42 -0700965bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -0700966 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700967 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -0700968 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -0700969 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -0700970 return false;
Romain Guy260e1022010-07-12 14:41:06 -0700971 }
Romain Guy6926c722010-07-12 20:20:03 -0700972 return true;
Romain Guy260e1022010-07-12 14:41:06 -0700973}
974
Romain Guy026c5e162010-06-28 17:12:22 -0700975void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -0700976 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -0700977 TextureVertex::setUV(v++, u1, v1);
978 TextureVertex::setUV(v++, u2, v1);
979 TextureVertex::setUV(v++, u1, v2);
980 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -0700981}
982
983void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
984 if (paint) {
985 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
986 if (!isMode) {
987 // Assume SRC_OVER
988 *mode = SkXfermode::kSrcOver_Mode;
989 }
990
991 // Skia draws using the color's alpha channel if < 255
992 // Otherwise, it uses the paint's alpha
993 int color = paint->getColor();
994 *alpha = (color >> 24) & 0xFF;
995 if (*alpha == 255) {
996 *alpha = paint->getAlpha();
997 }
998 } else {
999 *mode = SkXfermode::kSrcOver_Mode;
1000 *alpha = 255;
1001 }
Romain Guy026c5e162010-06-28 17:12:22 -07001002}
1003
Romain Guy889f8d12010-07-29 14:37:42 -07001004void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
1005 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -07001006 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -07001007 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
1008 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
1009}
1010
Romain Guy9d5316e2010-06-24 19:30:36 -07001011}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001012}; // namespace android