blob: 4ce30b0078a06b67e2afbbc6cd1ebc312cc3048c [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 Guyb45c0c92010-08-26 20:35:23 -0700547 FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
548 fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
Romain Guyfb8b7632010-08-23 21:05:08 -0700549 paint->getTextSize());
Romain Guy1e45aae2010-08-13 19:39:53 -0700550 if (mHasShadow) {
551 glActiveTexture(gTextureUnits[0]);
Romain Guyb45c0c92010-08-26 20:35:23 -0700552 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
Romain Guyfb8b7632010-08-23 21:05:08 -0700553 const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
Romain Guy1e45aae2010-08-13 19:39:53 -0700554 count, mShadowRadius);
555 const AutoTexture autoCleanup(shadow);
Romain Guy0a417492010-08-16 20:26:20 -0700556
Romain Guy2542d192010-08-18 11:47:12 -0700557 setupShadow(shadow, x, y, mode, a);
Romain Guy0a417492010-08-16 20:26:20 -0700558
559 // Draw the mesh
560 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700561 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy1e45aae2010-08-13 19:39:53 -0700562 }
563
Romain Guy06f96e22010-07-30 19:18:16 -0700564 GLuint textureUnit = 0;
Romain Guydb1938e2010-08-02 18:50:22 -0700565 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guy06f96e22010-07-30 19:18:16 -0700566
Romain Guyb45c0c92010-08-26 20:35:23 -0700567 setupTextureAlpha8(fontRenderer.getTexture(), 0, 0, textureUnit, x, y, r, g, b, a,
Romain Guy0a417492010-08-16 20:26:20 -0700568 mode, false, true);
Romain Guy06f96e22010-07-30 19:18:16 -0700569
Romain Guy09147fb2010-07-22 13:08:20 -0700570 const Rect& clip = mSnapshot->getLocalClip();
Romain Guyb45c0c92010-08-26 20:35:23 -0700571 fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y);
Romain Guy694b5192010-07-21 21:33:20 -0700572
573 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700574 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guya674ab72010-08-10 17:26:42 -0700575
Romain Guy0a417492010-08-16 20:26:20 -0700576 drawTextDecorations(text, bytesCount, length, x, y, paint);
Romain Guya80d32f2010-08-20 18:42:37 -0700577
578 if (applyScaleX) {
579 restore();
580 }
Romain Guy694b5192010-07-21 21:33:20 -0700581}
582
Romain Guy7fbcc042010-08-04 15:40:07 -0700583void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
Romain Guy8b55f372010-08-18 17:10:07 -0700584 if (mSnapshot->invisible) return;
585
Romain Guy7fbcc042010-08-04 15:40:07 -0700586 GLuint textureUnit = 0;
587 glActiveTexture(gTextureUnits[textureUnit]);
588
Romain Guyfb8b7632010-08-23 21:05:08 -0700589 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Romain Guy9cccc2b92010-08-07 23:46:15 -0700590 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -0700591 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -0700592
Romain Guy8b55f372010-08-18 17:10:07 -0700593 const float x = texture->left - texture->offset;
594 const float y = texture->top - texture->offset;
595
596 if (quickReject(x, y, x + texture->width, y + texture->height)) {
597 return;
598 }
599
Romain Guy7fbcc042010-08-04 15:40:07 -0700600 int alpha;
601 SkXfermode::Mode mode;
602 getAlphaAndMode(paint, &alpha, &mode);
603
604 uint32_t color = paint->getColor();
605 const GLfloat a = alpha / 255.0f;
606 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
607 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
608 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
609
Romain Guy0a417492010-08-16 20:26:20 -0700610 setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true);
611
612 // Draw the mesh
613 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyfb8b7632010-08-23 21:05:08 -0700614 glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
Romain Guy7fbcc042010-08-04 15:40:07 -0700615}
616
Romain Guy6926c722010-07-12 20:20:03 -0700617///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -0700618// Shaders
619///////////////////////////////////////////////////////////////////////////////
620
621void OpenGLRenderer::resetShader() {
Romain Guy06f96e22010-07-30 19:18:16 -0700622 mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -0700623}
624
Romain Guy06f96e22010-07-30 19:18:16 -0700625void OpenGLRenderer::setupShader(SkiaShader* shader) {
626 mShader = shader;
627 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700628 mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -0700629 }
Romain Guy7fac2e12010-07-16 17:10:13 -0700630}
631
Romain Guyd27977d2010-07-14 19:18:51 -0700632///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -0700633// Color filters
634///////////////////////////////////////////////////////////////////////////////
635
636void OpenGLRenderer::resetColorFilter() {
637 mColorFilter = NULL;
638}
639
640void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
641 mColorFilter = filter;
642}
643
644///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -0700645// Drop shadow
646///////////////////////////////////////////////////////////////////////////////
647
648void OpenGLRenderer::resetShadow() {
649 mHasShadow = false;
650}
651
652void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
653 mHasShadow = true;
654 mShadowRadius = radius;
655 mShadowDx = dx;
656 mShadowDy = dy;
657 mShadowColor = color;
658}
659
660///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -0700661// Drawing implementation
662///////////////////////////////////////////////////////////////////////////////
663
Romain Guy0a417492010-08-16 20:26:20 -0700664void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y,
Romain Guy2542d192010-08-18 11:47:12 -0700665 SkXfermode::Mode mode, float alpha) {
Romain Guy0a417492010-08-16 20:26:20 -0700666 const float sx = x - texture->left + mShadowDx;
667 const float sy = y - texture->top + mShadowDy;
668
Romain Guy2542d192010-08-18 11:47:12 -0700669 const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
670 const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha;
Romain Guy0a417492010-08-16 20:26:20 -0700671 const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f;
672 const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f;
673 const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f;
674
675 GLuint textureUnit = 0;
676 setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false);
677}
678
679void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit,
680 float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode,
681 bool transforms, bool applyFilters) {
682 setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit,
683 x, y, r, g, b, a, mode, transforms, applyFilters);
684}
685
686void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height,
687 GLuint& textureUnit, float x, float y, float r, float g, float b, float a,
688 SkXfermode::Mode mode, bool transforms, bool applyFilters) {
689 // Describe the required shaders
690 ProgramDescription description;
691 description.hasTexture = true;
692 description.hasAlpha8Texture = true;
693
694 if (applyFilters) {
695 if (mShader) {
696 mShader->describe(description, mExtensions);
697 }
698 if (mColorFilter) {
699 mColorFilter->describe(description, mExtensions);
700 }
701 }
702
703 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700704 useProgram(mCaches.programCache.get(description));
Romain Guy0a417492010-08-16 20:26:20 -0700705
706 // Setup the blending mode
707 chooseBlending(true, mode);
708 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit);
Romain Guyfb8b7632010-08-23 21:05:08 -0700709 glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700710
Romain Guyfb8b7632010-08-23 21:05:08 -0700711 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy0a417492010-08-16 20:26:20 -0700712 glEnableVertexAttribArray(texCoordsSlot);
713
714 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700715 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy0a417492010-08-16 20:26:20 -0700716 gMeshStride, &mMeshVertices[0].position[0]);
717 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE,
718 gMeshStride, &mMeshVertices[0].texture[0]);
719
720 // Setup uniforms
721 if (transforms) {
722 mModelView.loadTranslate(x, y, 0.0f);
723 mModelView.scale(width, height, 1.0f);
724 } else {
725 mModelView.loadIdentity();
726 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700727 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
728 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy0a417492010-08-16 20:26:20 -0700729
730 textureUnit++;
731 if (applyFilters) {
732 // Setup attributes and uniforms required by the shaders
733 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700734 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guy0a417492010-08-16 20:26:20 -0700735 }
736 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700737 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy0a417492010-08-16 20:26:20 -0700738 }
739 }
740}
741
742#define kStdStrikeThru_Offset (-6.0f / 21.0f)
743#define kStdUnderline_Offset (1.0f / 9.0f)
744#define kStdUnderline_Thickness (1.0f / 18.0f)
745
746void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
747 float x, float y, SkPaint* paint) {
748 // Handle underline and strike-through
749 uint32_t flags = paint->getFlags();
750 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
751 float underlineWidth = length;
752 // If length is > 0.0f, we already measured the text for the text alignment
753 if (length <= 0.0f) {
754 underlineWidth = paint->measureText(text, bytesCount);
755 }
756
757 float offsetX = 0;
758 switch (paint->getTextAlign()) {
759 case SkPaint::kCenter_Align:
760 offsetX = underlineWidth * 0.5f;
761 break;
762 case SkPaint::kRight_Align:
763 offsetX = underlineWidth;
764 break;
765 default:
766 break;
767 }
768
769 if (underlineWidth > 0.0f) {
770 float textSize = paint->getTextSize();
771 float height = textSize * kStdUnderline_Thickness;
772
773 float left = x - offsetX;
774 float top = 0.0f;
775 float right = left + underlineWidth;
776 float bottom = 0.0f;
777
778 if (flags & SkPaint::kUnderlineText_Flag) {
779 top = y + textSize * kStdUnderline_Offset;
780 bottom = top + height;
781 drawRect(left, top, right, bottom, paint);
782 }
783
784 if (flags & SkPaint::kStrikeThruText_Flag) {
785 top = y + textSize * kStdStrikeThru_Offset;
786 bottom = top + height;
787 drawRect(left, top, right, bottom, paint);
788 }
789 }
790 }
791}
792
Romain Guy026c5e162010-06-28 17:12:22 -0700793void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy3d58c032010-07-14 16:34:53 -0700794 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -0700795 // If a shader is set, preserve only the alpha
Romain Guy06f96e22010-07-30 19:18:16 -0700796 if (mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -0700797 color |= 0x00ffffff;
798 }
799
800 // Render using pre-multiplied alpha
Romain Guy026c5e162010-06-28 17:12:22 -0700801 const int alpha = (color >> 24) & 0xFF;
Romain Guyd27977d2010-07-14 19:18:51 -0700802 const GLfloat a = alpha / 255.0f;
Romain Guyc0ac1932010-07-19 18:43:02 -0700803 const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f;
804 const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f;
805 const GLfloat b = a * ((color ) & 0xFF) / 255.0f;
806
Romain Guy06f96e22010-07-30 19:18:16 -0700807 GLuint textureUnit = 0;
Romain Guy9d5316e2010-06-24 19:30:36 -0700808
Romain Guy06f96e22010-07-30 19:18:16 -0700809 // Setup the blending mode
810 chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode);
Romain Guy9d5316e2010-06-24 19:30:36 -0700811
Romain Guy06f96e22010-07-30 19:18:16 -0700812 // Describe the required shaders
Romain Guy889f8d12010-07-29 14:37:42 -0700813 ProgramDescription description;
Romain Guy06f96e22010-07-30 19:18:16 -0700814 if (mShader) {
815 mShader->describe(description, mExtensions);
Romain Guy6926c722010-07-12 20:20:03 -0700816 }
Romain Guydb1938e2010-08-02 18:50:22 -0700817 if (mColorFilter) {
818 mColorFilter->describe(description, mExtensions);
819 }
Romain Guyd27977d2010-07-14 19:18:51 -0700820
Romain Guy06f96e22010-07-30 19:18:16 -0700821 // Build and use the appropriate shader
Romain Guyfb8b7632010-08-23 21:05:08 -0700822 useProgram(mCaches.programCache.get(description));
Romain Guy06f96e22010-07-30 19:18:16 -0700823
824 // Setup attributes
Romain Guyfb8b7632010-08-23 21:05:08 -0700825 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guy06f96e22010-07-30 19:18:16 -0700826 gMeshStride, &mMeshVertices[0].position[0]);
827
828 // Setup uniforms
829 mModelView.loadTranslate(left, top, 0.0f);
830 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guyd27977d2010-07-14 19:18:51 -0700831 if (!ignoreTransform) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700832 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guyd27977d2010-07-14 19:18:51 -0700833 } else {
834 mat4 identity;
Romain Guyfb8b7632010-08-23 21:05:08 -0700835 mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity);
Romain Guyd27977d2010-07-14 19:18:51 -0700836 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700837 glUniform4f(mCaches.currentProgram->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700838
Romain Guy06f96e22010-07-30 19:18:16 -0700839 // Setup attributes and uniforms required by the shaders
840 if (mShader) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700841 mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit);
Romain Guyc0ac1932010-07-19 18:43:02 -0700842 }
Romain Guydb1938e2010-08-02 18:50:22 -0700843 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700844 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -0700845 }
Romain Guyc0ac1932010-07-19 18:43:02 -0700846
Romain Guy06f96e22010-07-30 19:18:16 -0700847 // Draw the mesh
Romain Guy889f8d12010-07-29 14:37:42 -0700848 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyd27977d2010-07-14 19:18:51 -0700849}
850
Romain Guy82ba8142010-07-09 13:25:56 -0700851void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700852 const Texture* texture, const SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -0700853 int alpha;
854 SkXfermode::Mode mode;
855 getAlphaAndMode(paint, &alpha, &mode);
856
Romain Guya9794742010-07-13 11:37:54 -0700857 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guyac670c02010-07-27 17:39:27 -0700858 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guy85bf02f2010-06-22 13:11:24 -0700859}
860
Romain Guybd6b79b2010-06-26 00:13:53 -0700861void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700862 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
863 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guyac670c02010-07-27 17:39:27 -0700864 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL);
Romain Guyf7f93552010-07-08 19:17:03 -0700865}
866
867void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -0700868 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guyf7f93552010-07-08 19:17:03 -0700869 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) {
Romain Guy889f8d12010-07-29 14:37:42 -0700870 ProgramDescription description;
871 description.hasTexture = true;
Romain Guydb1938e2010-08-02 18:50:22 -0700872 if (mColorFilter) {
873 mColorFilter->describe(description, mExtensions);
874 }
Romain Guy889f8d12010-07-29 14:37:42 -0700875
Romain Guybd6b79b2010-06-26 00:13:53 -0700876 mModelView.loadTranslate(left, top, 0.0f);
877 mModelView.scale(right - left, bottom - top, 1.0f);
878
Romain Guyfb8b7632010-08-23 21:05:08 -0700879 useProgram(mCaches.programCache.get(description));
880 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform);
Romain Guybd6b79b2010-06-26 00:13:53 -0700881
Romain Guya9794742010-07-13 11:37:54 -0700882 chooseBlending(blend || alpha < 1.0f, mode);
Romain Guy889f8d12010-07-29 14:37:42 -0700883
884 // Texture
Romain Guy06f96e22010-07-30 19:18:16 -0700885 bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0);
Romain Guyfb8b7632010-08-23 21:05:08 -0700886 glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0);
Romain Guyc1396e92010-06-30 17:56:19 -0700887
Romain Guya9794742010-07-13 11:37:54 -0700888 // Always premultiplied
Romain Guyfb8b7632010-08-23 21:05:08 -0700889 glUniform4f(mCaches.currentProgram->color, alpha, alpha, alpha, alpha);
Romain Guybd6b79b2010-06-26 00:13:53 -0700890
Romain Guy889f8d12010-07-29 14:37:42 -0700891 // Mesh
Romain Guyfb8b7632010-08-23 21:05:08 -0700892 int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
Romain Guy889f8d12010-07-29 14:37:42 -0700893 glEnableVertexAttribArray(texCoordsSlot);
Romain Guyfb8b7632010-08-23 21:05:08 -0700894 glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
Romain Guyac670c02010-07-27 17:39:27 -0700895 gMeshStride, vertices);
Romain Guy889f8d12010-07-29 14:37:42 -0700896 glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
Romain Guybd6b79b2010-06-26 00:13:53 -0700897
Romain Guydb1938e2010-08-02 18:50:22 -0700898 // Color filter
899 if (mColorFilter) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700900 mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guydb1938e2010-08-02 18:50:22 -0700901 }
902
Romain Guyf7f93552010-07-08 19:17:03 -0700903 if (!indices) {
Romain Guyac670c02010-07-27 17:39:27 -0700904 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -0700905 } else {
906 glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices);
907 }
Romain Guy889f8d12010-07-29 14:37:42 -0700908 glDisableVertexAttribArray(texCoordsSlot);
Romain Guy82ba8142010-07-09 13:25:56 -0700909}
910
911void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) {
Romain Guy82ba8142010-07-09 13:25:56 -0700912 blend = blend || mode != SkXfermode::kSrcOver_Mode;
913 if (blend) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700914 if (!mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -0700915 glEnable(GL_BLEND);
916 }
917
918 GLenum sourceMode = gBlends[mode].src;
919 GLenum destMode = gBlends[mode].dst;
920 if (!isPremultiplied && sourceMode == GL_ONE) {
921 sourceMode = GL_SRC_ALPHA;
922 }
923
Romain Guyfb8b7632010-08-23 21:05:08 -0700924 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
Romain Guy82ba8142010-07-09 13:25:56 -0700925 glBlendFunc(sourceMode, destMode);
Romain Guyfb8b7632010-08-23 21:05:08 -0700926 mCaches.lastSrcMode = sourceMode;
927 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -0700928 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700929 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -0700930 glDisable(GL_BLEND);
931 }
Romain Guyfb8b7632010-08-23 21:05:08 -0700932 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -0700933}
934
Romain Guy889f8d12010-07-29 14:37:42 -0700935bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -0700936 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -0700937 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -0700938 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -0700939 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -0700940 return false;
Romain Guy260e1022010-07-12 14:41:06 -0700941 }
Romain Guy6926c722010-07-12 20:20:03 -0700942 return true;
Romain Guy260e1022010-07-12 14:41:06 -0700943}
944
Romain Guy026c5e162010-06-28 17:12:22 -0700945void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -0700946 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -0700947 TextureVertex::setUV(v++, u1, v1);
948 TextureVertex::setUV(v++, u2, v1);
949 TextureVertex::setUV(v++, u1, v2);
950 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -0700951}
952
953void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
954 if (paint) {
955 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
956 if (!isMode) {
957 // Assume SRC_OVER
958 *mode = SkXfermode::kSrcOver_Mode;
959 }
960
961 // Skia draws using the color's alpha channel if < 255
962 // Otherwise, it uses the paint's alpha
963 int color = paint->getColor();
964 *alpha = (color >> 24) & 0xFF;
965 if (*alpha == 255) {
966 *alpha = paint->getAlpha();
967 }
968 } else {
969 *mode = SkXfermode::kSrcOver_Mode;
970 *alpha = 255;
971 }
Romain Guy026c5e162010-06-28 17:12:22 -0700972}
973
Romain Guy889f8d12010-07-29 14:37:42 -0700974void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) {
975 glActiveTexture(gTextureUnits[textureUnit]);
Romain Guyae5575b2010-07-29 18:48:04 -0700976 glBindTexture(GL_TEXTURE_2D, texture);
Romain Guya1db5742010-07-20 13:09:13 -0700977 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
978 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
979}
980
Romain Guy9d5316e2010-06-24 19:30:36 -0700981}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700982}; // namespace android