blob: 033d8e2efd58658c70353510485ebad1c6531cae [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy694b5192010-07-21 21:33:20 -070024#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070025
Romain Guye4d01122010-06-16 18:44:05 -070026#include <utils/Log.h>
27
Romain Guy85bf02f2010-06-22 13:11:24 -070028#include "OpenGLRenderer.h"
Romain Guye4d01122010-06-16 18:44:05 -070029
30namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070031namespace uirenderer {
32
33///////////////////////////////////////////////////////////////////////////////
34// Defines
35///////////////////////////////////////////////////////////////////////////////
36
Romain Guy06f96e22010-07-30 19:18:16 -070037#define REQUIRED_TEXTURE_UNITS_COUNT 3
38
Romain Guydda570202010-07-06 11:39:32 -070039// Generates simple and textured vertices
Romain Guybd6b79b2010-06-26 00:13:53 -070040#define FV(x, y, u, v) { { x, y }, { u, v } }
Romain Guy9d5316e2010-06-24 19:30:36 -070041
42///////////////////////////////////////////////////////////////////////////////
43// Globals
44///////////////////////////////////////////////////////////////////////////////
45
Romain Guy026c5e162010-06-28 17:12:22 -070046// This array is never used directly but used as a memcpy source in the
47// OpenGLRenderer constructor
Romain Guyac670c02010-07-27 17:39:27 -070048static const TextureVertex gMeshVertices[] = {
Romain Guyc1396e92010-06-30 17:56:19 -070049 FV(0.0f, 0.0f, 0.0f, 0.0f),
50 FV(1.0f, 0.0f, 1.0f, 0.0f),
51 FV(0.0f, 1.0f, 0.0f, 1.0f),
52 FV(1.0f, 1.0f, 1.0f, 1.0f)
Romain Guybd6b79b2010-06-26 00:13:53 -070053};
Romain Guyac670c02010-07-27 17:39:27 -070054static const GLsizei gMeshStride = sizeof(TextureVertex);
55static const GLsizei gMeshCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070056
Romain Guy889f8d12010-07-29 14:37:42 -070057/**
58 * Structure mapping Skia xfermodes to OpenGL blending factors.
59 */
60struct Blender {
61 SkXfermode::Mode mode;
62 GLenum src;
63 GLenum dst;
64}; // struct Blender
65
Romain Guy026c5e162010-06-28 17:12:22 -070066// In this array, the index of each Blender equals the value of the first
67// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
68static const Blender gBlends[] = {
69 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
70 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
71 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
72 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
73 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
74 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
75 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
76 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
77 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
78 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
79 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
80 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
81};
Romain Guye4d01122010-06-16 18:44:05 -070082
Romain Guy889f8d12010-07-29 14:37:42 -070083static const GLenum gTextureUnits[] = {
Romain Guy06f96e22010-07-30 19:18:16 -070084 GL_TEXTURE0,
85 GL_TEXTURE1,
86 GL_TEXTURE2
Romain Guyd27977d2010-07-14 19:18:51 -070087};
88
Romain Guyf6a11b82010-06-23 17:47:49 -070089///////////////////////////////////////////////////////////////////////////////
90// Constructors/destructor
91///////////////////////////////////////////////////////////////////////////////
92
Romain Guyfb8b7632010-08-23 21:05:08 -070093OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
Romain Guy85bf02f2010-06-22 13:11:24 -070094 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -070095
Romain Guy06f96e22010-07-30 19:18:16 -070096 mShader = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -070097 mColorFilter = NULL;
Romain Guy1e45aae2010-08-13 19:39:53 -070098 mHasShadow = false;
Romain Guy026c5e162010-06-28 17:12:22 -070099
Romain Guyac670c02010-07-27 17:39:27 -0700100 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
101
Romain Guyae5575b2010-07-29 18:48:04 -0700102 mFirstSnapshot = new Snapshot;
103
104 GLint maxTextureUnits;
105 glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
106 if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) {
Romain Guy889f8d12010-07-29 14:37:42 -0700107 LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT);
108 }
Romain Guye4d01122010-06-16 18:44:05 -0700109}
110
Romain Guy85bf02f2010-06-22 13:11:24 -0700111OpenGLRenderer::~OpenGLRenderer() {
112 LOGD("Destroy OpenGLRenderer");
Romain Guye4d01122010-06-16 18:44:05 -0700113}
114
Romain Guyf6a11b82010-06-23 17:47:49 -0700115///////////////////////////////////////////////////////////////////////////////
116// Setup
117///////////////////////////////////////////////////////////////////////////////
118
Romain Guy85bf02f2010-06-22 13:11:24 -0700119void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700120 glViewport(0, 0, width, height);
Romain Guy260e1022010-07-12 14:41:06 -0700121 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700122
123 mWidth = width;
124 mHeight = height;
Romain Guyae5575b2010-07-29 18:48:04 -0700125 mFirstSnapshot->height = height;
Romain Guy1d83e192010-08-17 11:37:00 -0700126 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700127}
128
Romain Guy85bf02f2010-06-22 13:11:24 -0700129void OpenGLRenderer::prepare() {
Romain 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 Guyf6a11b82010-06-23 17:47:49 -0700146///////////////////////////////////////////////////////////////////////////////
147// State management
148///////////////////////////////////////////////////////////////////////////////
149
Romain Guybb9524b2010-06-22 18:56:38 -0700150int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700151 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700152}
153
154int OpenGLRenderer::save(int flags) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700155 return saveSnapshot();
Romain Guybb9524b2010-06-22 18:56:38 -0700156}
157
158void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700159 if (mSaveCount > 1) {
160 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700161 }
Romain Guybb9524b2010-06-22 18:56:38 -0700162}
163
164void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700165 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700166
Romain Guy8fb95422010-08-17 18:38:51 -0700167 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700168 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700169 }
Romain Guybb9524b2010-06-22 18:56:38 -0700170}
171
172int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700173 mSnapshot = new Snapshot(mSnapshot);
Romain Guy8fb95422010-08-17 18:38:51 -0700174 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700175}
176
177bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700178 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700179 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700180 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700181
Romain Guybd6b79b2010-06-26 00:13:53 -0700182 sp<Snapshot> current = mSnapshot;
183 sp<Snapshot> previous = mSnapshot->previous;
184
Romain Guyf86ef572010-07-01 11:05:42 -0700185 if (restoreOrtho) {
Romain Guy1d83e192010-08-17 11:37:00 -0700186 Rect& r = previous->viewport;
187 glViewport(r.left, r.top, r.right, r.bottom);
Romain Guy260e1022010-07-12 14:41:06 -0700188 mOrthoMatrix.load(current->orthoMatrix);
Romain Guyf86ef572010-07-01 11:05:42 -0700189 }
190
Romain Guy8b55f372010-08-18 17:10:07 -0700191 mSaveCount--;
192 mSnapshot = previous;
193
Romain Guybd6b79b2010-06-26 00:13:53 -0700194 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700195 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700196 }
197
Romain Guy2542d192010-08-18 11:47:12 -0700198 if (restoreClip) {
199 setScissorFromClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700200 }
Romain Guy2542d192010-08-18 11:47:12 -0700201
202 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700203}
204
Romain Guyf6a11b82010-06-23 17:47:49 -0700205///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700206// Layers
207///////////////////////////////////////////////////////////////////////////////
208
209int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
210 const SkPaint* p, int flags) {
Romain Guyd55a8612010-06-28 17:42:46 -0700211 int count = saveSnapshot();
212
213 int alpha = 255;
214 SkXfermode::Mode mode;
215
216 if (p) {
217 alpha = p->getAlpha();
218 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
219 if (!isMode) {
220 // Assume SRC_OVER
221 mode = SkXfermode::kSrcOver_Mode;
222 }
223 } else {
224 mode = SkXfermode::kSrcOver_Mode;
225 }
226
Romain Guy8b55f372010-08-18 17:10:07 -0700227 if (alpha > 0 && !mSnapshot->invisible) {
228 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
229 } else {
230 mSnapshot->invisible = true;
231 }
Romain Guyd55a8612010-06-28 17:42:46 -0700232
233 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700234}
235
236int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
237 int alpha, int flags) {
238 int count = saveSnapshot();
Romain Guy8b55f372010-08-18 17:10:07 -0700239 if (alpha > 0 && !mSnapshot->invisible) {
240 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
241 } else {
242 mSnapshot->invisible = true;
243 }
Romain Guyd55a8612010-06-28 17:42:46 -0700244 return count;
245}
Romain Guybd6b79b2010-06-26 00:13:53 -0700246
Romain Guyd55a8612010-06-28 17:42:46 -0700247bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
248 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guyd27977d2010-07-14 19:18:51 -0700249 LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700250 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700251
Romain Guyf18fd992010-07-08 11:45:51 -0700252 GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0;
253 LayerSize size(right - left, bottom - top);
254
Romain Guyfb8b7632010-08-23 21:05:08 -0700255 Layer* layer = mCaches.layerCache.get(size, previousFbo);
Romain Guydda570202010-07-06 11:39:32 -0700256 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700257 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700258 }
259
Romain Guyf18fd992010-07-08 11:45:51 -0700260 glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
261
Romain Guyf86ef572010-07-01 11:05:42 -0700262 // Clear the FBO
263 glDisable(GL_SCISSOR_TEST);
264 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
265 glClear(GL_COLOR_BUFFER_BIT);
266 glEnable(GL_SCISSOR_TEST);
267
Romain Guydda570202010-07-06 11:39:32 -0700268 layer->mode = mode;
269 layer->alpha = alpha / 255.0f;
270 layer->layer.set(left, top, right, bottom);
271
Romain Guy8fb95422010-08-17 18:38:51 -0700272 // Save the layer in the snapshot
273 snapshot->flags |= Snapshot::kFlagIsLayer;
Romain Guydda570202010-07-06 11:39:32 -0700274 snapshot->layer = layer;
275 snapshot->fbo = layer->fbo;
Romain Guy2542d192010-08-18 11:47:12 -0700276 snapshot->transform.loadTranslate(-left, -top, 0.0f);
277 snapshot->setClip(0.0f, 0.0f, right - left, bottom - top);
278 snapshot->viewport.set(0.0f, 0.0f, right - left, bottom - top);
279 snapshot->height = bottom - top;
280 snapshot->flags |= Snapshot::kFlagDirtyOrtho;
281 snapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy1d83e192010-08-17 11:37:00 -0700282
Romain Guyf86ef572010-07-01 11:05:42 -0700283 setScissorFromClip();
284
Romain Guyf86ef572010-07-01 11:05:42 -0700285 // Change the ortho projection
Romain Guy1d83e192010-08-17 11:37:00 -0700286 glViewport(0, 0, right - left, bottom - top);
Romain Guy8b55f372010-08-18 17:10:07 -0700287 mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, -1.0f, 1.0f);
Romain Guyf86ef572010-07-01 11:05:42 -0700288
Romain Guyd55a8612010-06-28 17:42:46 -0700289 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700290}
291
Romain Guy1d83e192010-08-17 11:37:00 -0700292void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
293 if (!current->layer) {
294 LOGE("Attempting to compose a layer that does not exist");
295 return;
296 }
297
298 // Unbind current FBO and restore previous one
299 // Most of the time, previous->fbo will be 0 to bind the default buffer
300 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
301
302 // Restore the clip from the previous snapshot
303 const Rect& clip = previous->clipRect;
304 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
305
306 Layer* layer = current->layer;
307 const Rect& rect = layer->layer;
308
Romain Guy8b55f372010-08-18 17:10:07 -0700309 // FBOs are already drawn with a top-left origin, don't flip the texture
310 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
311
Romain Guy1d83e192010-08-17 11:37:00 -0700312 drawTextureRect(rect.left, rect.top, rect.right, rect.bottom,
313 layer->texture, layer->alpha, layer->mode, layer->blend);
314
Romain Guy8b55f372010-08-18 17:10:07 -0700315 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
316
Romain Guy1d83e192010-08-17 11:37:00 -0700317 LayerSize size(rect.getWidth(), rect.getHeight());
318 // Failing to add the layer to the cache should happen only if the
319 // layer is too large
Romain Guyfb8b7632010-08-23 21:05:08 -0700320 if (!mCaches.layerCache.put(size, layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700321 LAYER_LOGD("Deleting layer");
322
323 glDeleteFramebuffers(1, &layer->fbo);
324 glDeleteTextures(1, &layer->texture);
325
326 delete layer;
327 }
328}
329
Romain Guybd6b79b2010-06-26 00:13:53 -0700330///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700331// Transforms
332///////////////////////////////////////////////////////////////////////////////
333
334void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700335 mSnapshot->transform.translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700336}
337
338void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700339 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700340}
341
342void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700343 mSnapshot->transform.scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -0700344}
345
346void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700347 mSnapshot->transform.load(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700348}
349
350void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700351 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700352}
353
354void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700355 mat4 m(*matrix);
356 mSnapshot->transform.multiply(m);
Romain Guyf6a11b82010-06-23 17:47:49 -0700357}
358
359///////////////////////////////////////////////////////////////////////////////
360// Clipping
361///////////////////////////////////////////////////////////////////////////////
362
Romain Guybb9524b2010-06-22 18:56:38 -0700363void OpenGLRenderer::setScissorFromClip() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700364 const Rect& clip = mSnapshot->clipRect;
Romain Guyf86ef572010-07-01 11:05:42 -0700365 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700366}
367
368const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -0700369 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -0700370}
371
Romain Guyc7d53492010-06-25 13:41:57 -0700372bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Romain Guy8b55f372010-08-18 17:10:07 -0700373 if (mSnapshot->invisible) return true;
374
Romain Guy1d83e192010-08-17 11:37:00 -0700375 Rect r(left, top, right, bottom);
376 mSnapshot->transform.mapRect(r);
Romain Guyc7d53492010-06-25 13:41:57 -0700377 return !mSnapshot->clipRect.intersects(r);
378}
379
Romain Guy079ba2c2010-07-16 14:12:24 -0700380bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
381 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700382 if (clipped) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700383 setScissorFromClip();
384 }
Romain Guy079ba2c2010-07-16 14:12:24 -0700385 return !mSnapshot->clipRect.isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -0700386}
387
Romain Guyf6a11b82010-06-23 17:47:49 -0700388///////////////////////////////////////////////////////////////////////////////
389// Drawing
390///////////////////////////////////////////////////////////////////////////////
391
Romain Guyc1396e92010-06-30 17:56:19 -0700392void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700393 const float right = left + bitmap->width();
394 const float bottom = top + bitmap->height();
395
396 if (quickReject(left, top, right, bottom)) {
397 return;
398 }
399
Romain Guy61c8c9c2010-08-09 20:48:09 -0700400 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700401 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700402 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700403 const AutoTexture autoCleanup(texture);
404
Romain Guy6926c722010-07-12 20:20:03 -0700405 drawTextureRect(left, top, right, bottom, texture, paint);
Romain Guyce0537b2010-06-29 21:05:21 -0700406}
407
Romain Guyf86ef572010-07-01 11:05:42 -0700408void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
409 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
410 const mat4 transform(*matrix);
411 transform.mapRect(r);
412
Romain Guy6926c722010-07-12 20:20:03 -0700413 if (quickReject(r.left, r.top, r.right, r.bottom)) {
414 return;
415 }
416
Romain Guy61c8c9c2010-08-09 20:48:09 -0700417 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700418 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700419 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700420 const AutoTexture autoCleanup(texture);
421
Romain Guy82ba8142010-07-09 13:25:56 -0700422 drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700423}
424
Romain Guy8ba548f2010-06-30 19:21:21 -0700425void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
426 float srcLeft, float srcTop, float srcRight, float srcBottom,
427 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700428 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700429 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
430 return;
431 }
432
Romain Guy61c8c9c2010-08-09 20:48:09 -0700433 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700434 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700435 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700436 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -0700437
Romain Guy8ba548f2010-06-30 19:21:21 -0700438 const float width = texture->width;
439 const float height = texture->height;
440
441 const float u1 = srcLeft / width;
442 const float v1 = srcTop / height;
443 const float u2 = srcRight / width;
444 const float v2 = srcBottom / height;
445
446 resetDrawTextureTexCoords(u1, v1, u2, v2);
447
Romain Guy82ba8142010-07-09 13:25:56 -0700448 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700449
450 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
451}
452
Romain Guydeba7852010-07-07 17:54:48 -0700453void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
454 float left, float top, float right, float bottom, const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -0700455 if (quickReject(left, top, right, bottom)) {
456 return;
457 }
458
Romain Guy61c8c9c2010-08-09 20:48:09 -0700459 glActiveTexture(GL_TEXTURE0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700460 const Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700461 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700462 const AutoTexture autoCleanup(texture);
Romain Guyf7f93552010-07-08 19:17:03 -0700463
464 int alpha;
465 SkXfermode::Mode mode;
466 getAlphaAndMode(paint, &alpha, &mode);
467
Romain Guyfb8b7632010-08-23 21:05:08 -0700468 Patch* mesh = mCaches.patchCache.get(patch);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700469 mesh->updateVertices(bitmap, left, top, right, bottom,
470 &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs);
Romain Guyf7f93552010-07-08 19:17:03 -0700471
472 // Specify right and bottom as +1.0f from left/top to prevent scaling since the
473 // patch mesh already defines the final size
Romain Guya9794742010-07-13 11:37:54 -0700474 drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f,
475 mode, texture->blend, &mesh->vertices[0].position[0],
Romain Guy16202fc2010-07-09 16:13:28 -0700476 &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700477}
478
Romain Guy85bf02f2010-06-22 13:11:24 -0700479void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guy8b55f372010-08-18 17:10:07 -0700480 if (mSnapshot->invisible) return;
Romain Guy079ba2c2010-07-16 14:12:24 -0700481 const Rect& clip = mSnapshot->clipRect;
Romain Guy3d58c032010-07-14 16:34:53 -0700482 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Romain Guyc7d53492010-06-25 13:41:57 -0700483}
Romain Guy9d5316e2010-06-24 19:30:36 -0700484
Romain Guybd6b79b2010-06-26 00:13:53 -0700485void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy6926c722010-07-12 20:20:03 -0700486 if (quickReject(left, top, right, bottom)) {
487 return;
488 }
489
Romain Guy026c5e162010-06-28 17:12:22 -0700490 SkXfermode::Mode mode;
491
492 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
493 if (!isMode) {
494 // Assume SRC_OVER
495 mode = SkXfermode::kSrcOver_Mode;
496 }
497
498 // Skia draws using the color's alpha channel if < 255
499 // Otherwise, it uses the paint's alpha
500 int color = p->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -0700501 if (((color >> 24) & 0xff) == 255) {
Romain Guy026c5e162010-06-28 17:12:22 -0700502 color |= p->getAlpha() << 24;
503 }
504
505 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700506}
Romain Guy9d5316e2010-06-24 19:30:36 -0700507
Romain Guye8e62a42010-07-23 18:55:21 -0700508void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
509 float x, float y, SkPaint* paint) {
Romain Guy8b55f372010-08-18 17:10:07 -0700510 if (mSnapshot->invisible || text == NULL || count == 0 ||
511 (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) {
Romain Guye8e62a42010-07-23 18:55:21 -0700512 return;
513 }
514
Romain Guya80d32f2010-08-20 18:42:37 -0700515 float scaleX = paint->getTextScaleX();
516 bool applyScaleX = scaleX < 0.9999f || scaleX > 1.0001f;
517 if (applyScaleX) {
518 save(0);
519 translate(x - (x * scaleX), 0.0f);
520 scale(scaleX, 1.0f);
521 }
522
Romain Guya674ab72010-08-10 17:26:42 -0700523 float length = -1.0f;
Romain Guye8e62a42010-07-23 18:55:21 -0700524 switch (paint->getTextAlign()) {
525 case SkPaint::kCenter_Align:
526 length = paint->measureText(text, bytesCount);
527 x -= length / 2.0f;
528 break;
529 case SkPaint::kRight_Align:
530 length = paint->measureText(text, bytesCount);
531 x -= length;
532 break;
533 default:
534 break;
535 }
536
Romain Guy694b5192010-07-21 21:33:20 -0700537 int alpha;
538 SkXfermode::Mode mode;
539 getAlphaAndMode(paint, &alpha, &mode);
540
Romain Guy2542d192010-08-18 11:47:12 -0700541 uint32_t color = paint->getColor();
542 const GLfloat a = alpha / 255.0f;
543 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
544 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
545 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
546
Romain Guyfb8b7632010-08-23 21:05:08 -0700547 mCaches.fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
548 paint->getTextSize());
Romain Guy1e45aae2010-08-13 19:39:53 -0700549 if (mHasShadow) {
550 glActiveTexture(gTextureUnits[0]);
Romain Guyfb8b7632010-08-23 21:05:08 -0700551 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700552 count, mShadowRadius);
553 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700554
Romain Guy2542d192010-08-18 11:47:12 -0700555 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700556
557 // Draw the mesh
558 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700559 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700560 }
561
Romain Guy06f96e22010-07-30 19:18:16 -0700562 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700563 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700564
Romain Guyfb8b7632010-08-23 21:05:08 -0700565 setupTextureAlpha8(mCaches.fontRenderer.getTexture(), 0, 0, textureUnit, x, y, r, g, b, a,
Romain Guy0a417492010-08-16 20:26:20 -0700566 mode, false, true);
Romain Guy06f96e22010-07-30 19:18:16 -0700567
Romain Guy09147fb2010-07-22 13:08:20 -0700568 const Rect& clip = mSnapshot->getLocalClip();
Romain Guyfb8b7632010-08-23 21:05:08 -0700569 mCaches.fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700570
571 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700572 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700573
Romain Guy0a417492010-08-16 20:26:20 -0700574 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guya80d32f2010-08-20 18:42:37 -0700575
576 if (applyScaleX) {
577 restore();
578 }
Romain Guy694b5192010-07-21 21:33:20 -0700579}
580
Romain Guy7fbcc042010-08-04 15:40:07 -0700581void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guy8b55f372010-08-18 17:10:07 -0700582 if (mSnapshot->invisible) return;
583
Romain Guy7fbcc042010-08-04 15:40:07 -0700584 GLuint textureUnit = 0;
585 glActiveTexture(gTextureUnits[textureUnit]);
586
Romain Guyfb8b7632010-08-23 21:05:08 -0700587 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700588 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700589 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700590
Romain Guy8b55f372010-08-18 17:10:07 -0700591 const float x = texture->left - texture->offset;
592 const float y = texture->top - texture->offset;
593
594 if (quickReject(x, y, x + texture->width, y + texture->height)) {
595 return;
596 }
597
Romain Guy7fbcc042010-08-04 15:40:07 -0700598 int alpha;
599 SkXfermode::Mode mode;
600 getAlphaAndMode(paint, &alpha, &mode);
601
602 uint32_t color = paint->getColor();
603 const GLfloat a = alpha / 255.0f;
604 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
605 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
606 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
607
Romain Guy0a417492010-08-16 20:26:20 -0700608 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
609
610 // Draw the mesh
611 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700612 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700613}
614
Romain Guy6926c722010-07-12 20:20:03 -0700615///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700616// Shaders
617///////////////////////////////////////////////////////////////////////////////
618
619void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700620 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700621}
622
Romain Guy06f96e22010-07-30 19:18:16 -0700623void OpenGLRenderer::setupShader(SkiaShader* shader) {
624 mShader = shader;
625 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700626 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700627 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700628}
629
Romain Guyd27977d2010-07-14 19:18:51 -0700630///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700631// Color filters
632///////////////////////////////////////////////////////////////////////////////
633
634void OpenGLRenderer::resetColorFilter() {
635 mColorFilter = NULL;
636}
637
638void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
639 mColorFilter = filter;
640}
641
642///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700643// Drop shadow
644///////////////////////////////////////////////////////////////////////////////
645
646void OpenGLRenderer::resetShadow() {
647 mHasShadow = false;
648}
649
650void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
651 mHasShadow = true;
652 mShadowRadius = radius;
653 mShadowDx = dx;
654 mShadowDy = dy;
655 mShadowColor = color;
656}
657
658///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700659// Drawing implementation
660///////////////////////////////////////////////////////////////////////////////
661
Romain Guy0a417492010-08-16 20:26:20 -0700662void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700663 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700664 const float sx = x - texture->left + mShadowDx;
665 const float sy = y - texture->top + mShadowDy;
666
Romain Guy2542d192010-08-18 11:47:12 -0700667 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
668 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700669 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
670 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
671 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
672
673 GLuint textureUnit = 0;
674 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
675}
676
677void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
678 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
679 bool transforms, bool applyFilters) {
680 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
681 x, y, r, g, b, a, mode, transforms, applyFilters);
682}
683
684void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
685 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
686 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
687 // Describe the required shaders
688 ProgramDescription description;
689 description.hasTexture = true;
690 description.hasAlpha8Texture = true;
691
692 if (applyFilters) {
693 if (mShader) {
694 mShader->describe(description, mExtensions);
695 }
696 if (mColorFilter) {
697 mColorFilter->describe(description, mExtensions);
698 }
699 }
700
701 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700702 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -0700703
704 // Setup the blending mode
705 chooseBlending(true, mode);
706 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
Romain Guyfb8b7632010-08-23 21:05:08 -0700707 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700708
Romain Guyfb8b7632010-08-23 21:05:08 -0700709 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -0700710 glEnableVertexAttribArray(texCoordsSlot);
711
712 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700713 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy0a417492010-08-16 20:26:20 -0700714 gMeshStride, &mMeshVertices[0].position[0]);
715 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
716 gMeshStride, &mMeshVertices[0].texture[0]);
717
718 // Setup uniforms
719 if (transforms) {
720 mModelView.loadTranslate(x, y, 0.0f);
721 mModelView.scale(width, height, 1.0f);
722 } else {
723 mModelView.loadIdentity();
724 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700725 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
726 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -0700727
728 textureUnit++;
729 if (applyFilters) {
730 // Setup attributes and uniforms required by the shaders
731 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700732 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700733 }
734 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700735 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -0700736 }
737 }
738}
739
740#define kStdStrikeThru_Offset (-6.0f / 21.0f)
741#define kStdUnderline_Offset (1.0f / 9.0f)
742#define kStdUnderline_Thickness (1.0f / 18.0f)
743
744void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
745 float x, float y, SkPaint* paint) {
746 // Handle underline and strike-through
747 uint32_t flags = paint->getFlags();
748 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
749 float underlineWidth = length;
750 // If length is > 0.0f, we already measured the text for the text alignment
751 if (length <= 0.0f) {
752 underlineWidth = paint->measureText(text, bytesCount);
753 }
754
755 float offsetX = 0;
756 switch (paint->getTextAlign()) {
757 case SkPaint::kCenter_Align:
758 offsetX = underlineWidth * 0.5f;
759 break;
760 case SkPaint::kRight_Align:
761 offsetX = underlineWidth;
762 break;
763 default:
764 break;
765 }
766
767 if (underlineWidth > 0.0f) {
768 float textSize = paint->getTextSize();
769 float height = textSize * kStdUnderline_Thickness;
770
771 float left = x - offsetX;
772 float top = 0.0f;
773 float right = left + underlineWidth;
774 float bottom = 0.0f;
775
776 if (flags & SkPaint::kUnderlineText_Flag) {
777 top = y + textSize * kStdUnderline_Offset;
778 bottom = top + height;
779 drawRect(left, top, right, bottom, paint);
780 }
781
782 if (flags & SkPaint::kStrikeThruText_Flag) {
783 top = y + textSize * kStdStrikeThru_Offset;
784 bottom = top + height;
785 drawRect(left, top, right, bottom, paint);
786 }
787 }
788 }
789}
790
Romain Guy026c5e162010-06-28 17:12:22 -0700791void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700792 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -0700793 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -0700794 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -0700795 color |= 0x00ffffff;
796 }
797
798 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700799 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700800 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -0700801 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
802 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
803 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
804
Romain Guy06f96e22010-07-30 19:18:16 -0700805 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -0700806
Romain Guy06f96e22010-07-30 19:18:16 -0700807 // Setup the blending mode
808 chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode);
Romain Guy9d5316e2010-06-24 19:30:36 -0700809
Romain Guy06f96e22010-07-30 19:18:16 -0700810 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -0700811 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -0700812 if (mShader) {
813 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -0700814 }
Romain Guydb1938e2010-08-02 18:50:22 -0700815 if (mColorFilter) {
816 mColorFilter->describe(description, mExtensions);
817 }
Romain Guyd27977d2010-07-14 19:18:51 -0700818
Romain Guy06f96e22010-07-30 19:18:16 -0700819 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700820 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -0700821
822 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700823 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy06f96e22010-07-30 19:18:16 -0700824 gMeshStride, &mMeshVertices[0].position[0]);
825
826 // Setup uniforms
827 mModelView.loadTranslate(left, top, 0.0f);
828 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -0700829 if (!ignoreTransform) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700830 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700831 } else {
832 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -0700833 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700834 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700835 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700836
Romain Guy06f96e22010-07-30 19:18:16 -0700837 // Setup attributes and uniforms required by the shaders
838 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700839 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -0700840 }
Romain Guydb1938e2010-08-02 18:50:22 -0700841 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700842 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -0700843 }
Romain Guyc0ac1932010-07-19 18:43:02 -0700844
Romain Guy06f96e22010-07-30 19:18:16 -0700845 // Draw the mesh
Romain Guy889f8d12010-07-29 14:37:42 -0700846 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyd27977d2010-07-14 19:18:51 -0700847}
848
Romain Guy82ba8142010-07-09 13:25:56 -0700849void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700850 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700851 int alpha;
852 SkXfermode::Mode mode;
853 getAlphaAndMode(paint, &alpha, &mode);
854
Romain Guya9794742010-07-13 11:37:54 -0700855 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guyac670c02010-07-27 17:39:27 -0700856 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700857}
858
Romain Guybd6b79b2010-06-26 00:13:53 -0700859void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700860 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
861 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyac670c02010-07-27 17:39:27 -0700862 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guyf7f93552010-07-08 19:17:03 -0700863}
864
865void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700866 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700867 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guy889f8d12010-07-29 14:37:42 -0700868 ProgramDescription description;
869 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -0700870 if (mColorFilter) {
871 mColorFilter->describe(description, mExtensions);
872 }
Romain Guy889f8d12010-07-29 14:37:42 -0700873
Romain Guybd6b79b2010-06-26 00:13:53 -0700874 mModelView.loadTranslate(left, top, 0.0f);
875 mModelView.scale(right - left, bottom - top, 1.0f);
876
Romain Guyfb8b7632010-08-23 21:05:08 -0700877 useProgram(mCaches.programCache.get(description));
878 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700879
Romain Guya9794742010-07-13 11:37:54 -0700880 chooseBlending(blend || alpha < 1.0f, mode);
Romain Guy889f8d12010-07-29 14:37:42 -0700881
882 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -0700883 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700884 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -0700885
Romain Guya9794742010-07-13 11:37:54 -0700886 // Always premultiplied
Romain Guyfb8b7632010-08-23 21:05:08 -0700887 glUniform4f(mCaches.currentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700888
Romain Guy889f8d12010-07-29 14:37:42 -0700889 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -0700890 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -0700891 glEnableVertexAttribArray(texCoordsSlot);
Romain Guyfb8b7632010-08-23 21:05:08 -0700892 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700893 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -0700894 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700895
Romain Guydb1938e2010-08-02 18:50:22 -0700896 // Color filter
897 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700898 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -0700899 }
900
Romain Guyf7f93552010-07-08 19:17:03 -0700901 if (!indices) {
Romain Guyac670c02010-07-27 17:39:27 -0700902 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700903 } else {
904 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
905 }
Romain Guy889f8d12010-07-29 14:37:42 -0700906 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -0700907}
908
909void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
Romain Guy82ba8142010-07-09 13:25:56 -0700910 blend = blend || mode != SkXfermode::kSrcOver_Mode;
911 if (blend) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700912 if (!mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -0700913 glEnable(GL_BLEND);
914 }
915
916 GLenum sourceMode = gBlends[mode].src;
917 GLenum destMode = gBlends[mode].dst;
918 if (!isPremultiplied && sourceMode == GL_ONE) {
919 sourceMode = GL_SRC_ALPHA;
920 }
921
Romain Guyfb8b7632010-08-23 21:05:08 -0700922 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
Romain Guy82ba8142010-07-09 13:25:56 -0700923 glBlendFunc(sourceMode, destMode);
Romain Guyfb8b7632010-08-23 21:05:08 -0700924 mCaches.lastSrcMode = sourceMode;
925 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -0700926 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700927 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -0700928 glDisable(GL_BLEND);
929 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700930 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700931}
932
Romain Guy889f8d12010-07-29 14:37:42 -0700933bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -0700934 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700935 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -0700936 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -0700937 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -0700938 return false;
Romain Guy260e1022010-07-12 14:41:06 -0700939 }
Romain Guy6926c722010-07-12 20:20:03 -0700940 return true;
Romain Guy260e1022010-07-12 14:41:06 -0700941}
942
Romain Guy026c5e162010-06-28 17:12:22 -0700943void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -0700944 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -0700945 TextureVertex::setUV(v++, u1, v1);
946 TextureVertex::setUV(v++, u2, v1);
947 TextureVertex::setUV(v++, u1, v2);
948 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -0700949}
950
951void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
952 if (paint) {
953 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
954 if (!isMode) {
955 // Assume SRC_OVER
956 *mode = SkXfermode::kSrcOver_Mode;
957 }
958
959 // Skia draws using the color's alpha channel if < 255
960 // Otherwise, it uses the paint's alpha
961 int color = paint->getColor();
962 *alpha = (color >> 24) & 0xFF;
963 if (*alpha == 255) {
964 *alpha = paint->getAlpha();
965 }
966 } else {
967 *mode = SkXfermode::kSrcOver_Mode;
968 *alpha = 255;
969 }
Romain Guy026c5e162010-06-28 17:12:22 -0700970}
971
Romain Guy889f8d12010-07-29 14:37:42 -0700972void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
973 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -0700974 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -0700975 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
976 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
977}
978
Romain Guy9d5316e2010-06-24 19:30:36 -0700979}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700980}; // namespace android