Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1 | /* |
| 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 Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 17 | #define LOG_TAG "OpenGLRenderer" |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
| 22 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 23 | #include <SkCanvas.h> |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 24 | #include <SkTypeface.h> |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 25 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 26 | #include <utils/Log.h> |
| 27 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 28 | #include "OpenGLRenderer.h" |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 29 | |
| 30 | namespace android { |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 31 | namespace uirenderer { |
| 32 | |
| 33 | /////////////////////////////////////////////////////////////////////////////// |
| 34 | // Defines |
| 35 | /////////////////////////////////////////////////////////////////////////////// |
| 36 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 37 | #define REQUIRED_TEXTURE_UNITS_COUNT 3 |
| 38 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 39 | // Generates simple and textured vertices |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 40 | #define FV(x, y, u, v) { { x, y }, { u, v } } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 41 | |
| 42 | /////////////////////////////////////////////////////////////////////////////// |
| 43 | // Globals |
| 44 | /////////////////////////////////////////////////////////////////////////////// |
| 45 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 46 | // This array is never used directly but used as a memcpy source in the |
| 47 | // OpenGLRenderer constructor |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 48 | static const TextureVertex gMeshVertices[] = { |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 49 | 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 Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 53 | }; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 54 | static const GLsizei gMeshStride = sizeof(TextureVertex); |
| 55 | static const GLsizei gMeshCount = 4; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 56 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 57 | /** |
| 58 | * Structure mapping Skia xfermodes to OpenGL blending factors. |
| 59 | */ |
| 60 | struct Blender { |
| 61 | SkXfermode::Mode mode; |
| 62 | GLenum src; |
| 63 | GLenum dst; |
| 64 | }; // struct Blender |
| 65 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 66 | // In this array, the index of each Blender equals the value of the first |
| 67 | // entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode] |
| 68 | static 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 Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 82 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 83 | static const GLenum gTextureUnits[] = { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 84 | GL_TEXTURE0, |
| 85 | GL_TEXTURE1, |
| 86 | GL_TEXTURE2 |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 89 | /////////////////////////////////////////////////////////////////////////////// |
| 90 | // Constructors/destructor |
| 91 | /////////////////////////////////////////////////////////////////////////////// |
| 92 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 93 | OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) { |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 94 | LOGD("Create OpenGLRenderer"); |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 95 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 96 | mShader = NULL; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 97 | mColorFilter = NULL; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 98 | mHasShadow = false; |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 99 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 100 | memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices)); |
| 101 | |
Romain Guy | ae5575b | 2010-07-29 18:48:04 -0700 | [diff] [blame] | 102 | mFirstSnapshot = new Snapshot; |
| 103 | |
| 104 | GLint maxTextureUnits; |
| 105 | glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits); |
| 106 | if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) { |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 107 | LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT); |
| 108 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 111 | OpenGLRenderer::~OpenGLRenderer() { |
| 112 | LOGD("Destroy OpenGLRenderer"); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 115 | /////////////////////////////////////////////////////////////////////////////// |
| 116 | // Setup |
| 117 | /////////////////////////////////////////////////////////////////////////////// |
| 118 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 119 | void OpenGLRenderer::setViewport(int width, int height) { |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 120 | glViewport(0, 0, width, height); |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 121 | mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 122 | |
| 123 | mWidth = width; |
| 124 | mHeight = height; |
Romain Guy | ae5575b | 2010-07-29 18:48:04 -0700 | [diff] [blame] | 125 | mFirstSnapshot->height = height; |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 126 | mFirstSnapshot->viewport.set(0, 0, width, height); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 129 | void OpenGLRenderer::prepare() { |
Romain Guy | b82da65 | 2010-07-30 11:36:12 -0700 | [diff] [blame] | 130 | mSnapshot = new Snapshot(mFirstSnapshot); |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 131 | mSaveCount = 1; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 132 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 133 | glViewport(0, 0, mWidth, mHeight); |
| 134 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 135 | glDisable(GL_SCISSOR_TEST); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 136 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 137 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 138 | glClear(GL_COLOR_BUFFER_BIT); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 139 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 140 | glEnable(GL_SCISSOR_TEST); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 141 | glScissor(0, 0, mWidth, mHeight); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 142 | |
Romain Guy | b82da65 | 2010-07-30 11:36:12 -0700 | [diff] [blame] | 143 | mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 146 | /////////////////////////////////////////////////////////////////////////////// |
| 147 | // State management |
| 148 | /////////////////////////////////////////////////////////////////////////////// |
| 149 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 150 | int OpenGLRenderer::getSaveCount() const { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 151 | return mSaveCount; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | int OpenGLRenderer::save(int flags) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 155 | return saveSnapshot(); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void OpenGLRenderer::restore() { |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 159 | if (mSaveCount > 1) { |
| 160 | restoreSnapshot(); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 161 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void OpenGLRenderer::restoreToCount(int saveCount) { |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 165 | if (saveCount < 1) saveCount = 1; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 166 | |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 167 | while (mSaveCount > saveCount) { |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 168 | restoreSnapshot(); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 169 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | int OpenGLRenderer::saveSnapshot() { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 173 | mSnapshot = new Snapshot(mSnapshot); |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 174 | return mSaveCount++; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | bool OpenGLRenderer::restoreSnapshot() { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 178 | bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 179 | bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer; |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 180 | bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 181 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 182 | sp<Snapshot> current = mSnapshot; |
| 183 | sp<Snapshot> previous = mSnapshot->previous; |
| 184 | |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 185 | if (restoreOrtho) { |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 186 | Rect& r = previous->viewport; |
| 187 | glViewport(r.left, r.top, r.right, r.bottom); |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 188 | mOrthoMatrix.load(current->orthoMatrix); |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 191 | mSaveCount--; |
| 192 | mSnapshot = previous; |
| 193 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 194 | if (restoreLayer) { |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 195 | composeLayer(current, previous); |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 198 | if (restoreClip) { |
| 199 | setScissorFromClip(); |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 200 | } |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 201 | |
| 202 | return restoreClip; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 205 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 206 | // Layers |
| 207 | /////////////////////////////////////////////////////////////////////////////// |
| 208 | |
| 209 | int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom, |
| 210 | const SkPaint* p, int flags) { |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 211 | 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 Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 227 | if (alpha > 0 && !mSnapshot->invisible) { |
| 228 | createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags); |
| 229 | } else { |
| 230 | mSnapshot->invisible = true; |
| 231 | } |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 232 | |
| 233 | return count; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom, |
| 237 | int alpha, int flags) { |
| 238 | int count = saveSnapshot(); |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 239 | 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 Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 244 | return count; |
| 245 | } |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 246 | |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 247 | bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top, |
| 248 | float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 249 | LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 250 | LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize()); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 251 | |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 252 | GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0; |
| 253 | LayerSize size(right - left, bottom - top); |
| 254 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 255 | Layer* layer = mCaches.layerCache.get(size, previousFbo); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 256 | if (!layer) { |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 257 | return false; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 260 | glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo); |
| 261 | |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 262 | // 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 Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 268 | layer->mode = mode; |
| 269 | layer->alpha = alpha / 255.0f; |
| 270 | layer->layer.set(left, top, right, bottom); |
| 271 | |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 272 | // Save the layer in the snapshot |
| 273 | snapshot->flags |= Snapshot::kFlagIsLayer; |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 274 | snapshot->layer = layer; |
| 275 | snapshot->fbo = layer->fbo; |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 276 | 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 Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 282 | |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 283 | setScissorFromClip(); |
| 284 | |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 285 | // Change the ortho projection |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 286 | glViewport(0, 0, right - left, bottom - top); |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 287 | mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, -1.0f, 1.0f); |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 288 | |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 289 | return true; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 292 | void 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 Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 309 | // 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 Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 312 | drawTextureRect(rect.left, rect.top, rect.right, rect.bottom, |
| 313 | layer->texture, layer->alpha, layer->mode, layer->blend); |
| 314 | |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 315 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
| 316 | |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 317 | 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 Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 320 | if (!mCaches.layerCache.put(size, layer)) { |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 321 | LAYER_LOGD("Deleting layer"); |
| 322 | |
| 323 | glDeleteFramebuffers(1, &layer->fbo); |
| 324 | glDeleteTextures(1, &layer->texture); |
| 325 | |
| 326 | delete layer; |
| 327 | } |
| 328 | } |
| 329 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 330 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 331 | // Transforms |
| 332 | /////////////////////////////////////////////////////////////////////////////// |
| 333 | |
| 334 | void OpenGLRenderer::translate(float dx, float dy) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 335 | mSnapshot->transform.translate(dx, dy, 0.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | void OpenGLRenderer::rotate(float degrees) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 339 | mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | void OpenGLRenderer::scale(float sx, float sy) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 343 | mSnapshot->transform.scale(sx, sy, 1.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | void OpenGLRenderer::setMatrix(SkMatrix* matrix) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 347 | mSnapshot->transform.load(*matrix); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | void OpenGLRenderer::getMatrix(SkMatrix* matrix) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 351 | mSnapshot->transform.copyTo(*matrix); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 355 | mat4 m(*matrix); |
| 356 | mSnapshot->transform.multiply(m); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /////////////////////////////////////////////////////////////////////////////// |
| 360 | // Clipping |
| 361 | /////////////////////////////////////////////////////////////////////////////// |
| 362 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 363 | void OpenGLRenderer::setScissorFromClip() { |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 364 | const Rect& clip = mSnapshot->clipRect; |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 365 | glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight()); |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | const Rect& OpenGLRenderer::getClipBounds() { |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 369 | return mSnapshot->getLocalClip(); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 372 | bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) { |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 373 | if (mSnapshot->invisible) return true; |
| 374 | |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 375 | Rect r(left, top, right, bottom); |
| 376 | mSnapshot->transform.mapRect(r); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 377 | return !mSnapshot->clipRect.intersects(r); |
| 378 | } |
| 379 | |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 380 | bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) { |
| 381 | bool clipped = mSnapshot->clip(left, top, right, bottom, op); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 382 | if (clipped) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 383 | setScissorFromClip(); |
| 384 | } |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 385 | return !mSnapshot->clipRect.isEmpty(); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 388 | /////////////////////////////////////////////////////////////////////////////// |
| 389 | // Drawing |
| 390 | /////////////////////////////////////////////////////////////////////////////// |
| 391 | |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 392 | void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 393 | 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 Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 400 | glActiveTexture(GL_TEXTURE0); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 401 | const Texture* texture = mCaches.textureCache.get(bitmap); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 402 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 403 | const AutoTexture autoCleanup(texture); |
| 404 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 405 | drawTextureRect(left, top, right, bottom, texture, paint); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 408 | void 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 Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 413 | if (quickReject(r.left, r.top, r.right, r.bottom)) { |
| 414 | return; |
| 415 | } |
| 416 | |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 417 | glActiveTexture(GL_TEXTURE0); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 418 | const Texture* texture = mCaches.textureCache.get(bitmap); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 419 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 420 | const AutoTexture autoCleanup(texture); |
| 421 | |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 422 | drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint); |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 425 | void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, |
| 426 | float srcLeft, float srcTop, float srcRight, float srcBottom, |
| 427 | float dstLeft, float dstTop, float dstRight, float dstBottom, |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 428 | const SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 429 | if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) { |
| 430 | return; |
| 431 | } |
| 432 | |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 433 | glActiveTexture(GL_TEXTURE0); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 434 | const Texture* texture = mCaches.textureCache.get(bitmap); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 435 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 436 | const AutoTexture autoCleanup(texture); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 437 | |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 438 | 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 Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 448 | drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 449 | |
| 450 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
| 451 | } |
| 452 | |
Romain Guy | deba785 | 2010-07-07 17:54:48 -0700 | [diff] [blame] | 453 | void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch, |
| 454 | float left, float top, float right, float bottom, const SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 455 | if (quickReject(left, top, right, bottom)) { |
| 456 | return; |
| 457 | } |
| 458 | |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 459 | glActiveTexture(GL_TEXTURE0); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 460 | const Texture* texture = mCaches.textureCache.get(bitmap); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 461 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 462 | const AutoTexture autoCleanup(texture); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 463 | |
| 464 | int alpha; |
| 465 | SkXfermode::Mode mode; |
| 466 | getAlphaAndMode(paint, &alpha, &mode); |
| 467 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 468 | Patch* mesh = mCaches.patchCache.get(patch); |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 469 | mesh->updateVertices(bitmap, left, top, right, bottom, |
| 470 | &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 471 | |
| 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 Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 474 | drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f, |
| 475 | mode, texture->blend, &mesh->vertices[0].position[0], |
Romain Guy | 16202fc | 2010-07-09 16:13:28 -0700 | [diff] [blame] | 476 | &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 479 | void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 480 | if (mSnapshot->invisible) return; |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 481 | const Rect& clip = mSnapshot->clipRect; |
Romain Guy | 3d58c03 | 2010-07-14 16:34:53 -0700 | [diff] [blame] | 482 | drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 483 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 484 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 485 | void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 486 | if (quickReject(left, top, right, bottom)) { |
| 487 | return; |
| 488 | } |
| 489 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 490 | 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 Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 501 | if (((color >> 24) & 0xff) == 255) { |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 502 | color |= p->getAlpha() << 24; |
| 503 | } |
| 504 | |
| 505 | drawColorRect(left, top, right, bottom, color, mode); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 506 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 507 | |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 508 | void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, |
| 509 | float x, float y, SkPaint* paint) { |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 510 | if (mSnapshot->invisible || text == NULL || count == 0 || |
| 511 | (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) { |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 512 | return; |
| 513 | } |
| 514 | |
Romain Guy | a80d32f | 2010-08-20 18:42:37 -0700 | [diff] [blame] | 515 | 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 Guy | a674ab7 | 2010-08-10 17:26:42 -0700 | [diff] [blame] | 523 | float length = -1.0f; |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 524 | 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 Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 537 | int alpha; |
| 538 | SkXfermode::Mode mode; |
| 539 | getAlphaAndMode(paint, &alpha, &mode); |
| 540 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 541 | 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 Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame^] | 547 | FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint); |
| 548 | fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 549 | paint->getTextSize()); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 550 | if (mHasShadow) { |
| 551 | glActiveTexture(gTextureUnits[0]); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame^] | 552 | mCaches.dropShadowCache.setFontRenderer(fontRenderer); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 553 | const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount, |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 554 | count, mShadowRadius); |
| 555 | const AutoTexture autoCleanup(shadow); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 556 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 557 | setupShadow(shadow, x, y, mode, a); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 558 | |
| 559 | // Draw the mesh |
| 560 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 561 | glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 564 | GLuint textureUnit = 0; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 565 | glActiveTexture(gTextureUnits[textureUnit]); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 566 | |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame^] | 567 | setupTextureAlpha8(fontRenderer.getTexture(), 0, 0, textureUnit, x, y, r, g, b, a, |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 568 | mode, false, true); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 569 | |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 570 | const Rect& clip = mSnapshot->getLocalClip(); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame^] | 571 | fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 572 | |
| 573 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 574 | glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); |
Romain Guy | a674ab7 | 2010-08-10 17:26:42 -0700 | [diff] [blame] | 575 | |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 576 | drawTextDecorations(text, bytesCount, length, x, y, paint); |
Romain Guy | a80d32f | 2010-08-20 18:42:37 -0700 | [diff] [blame] | 577 | |
| 578 | if (applyScaleX) { |
| 579 | restore(); |
| 580 | } |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 581 | } |
| 582 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 583 | void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) { |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 584 | if (mSnapshot->invisible) return; |
| 585 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 586 | GLuint textureUnit = 0; |
| 587 | glActiveTexture(gTextureUnits[textureUnit]); |
| 588 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 589 | const PathTexture* texture = mCaches.pathCache.get(path, paint); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 590 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 591 | const AutoTexture autoCleanup(texture); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 592 | |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 593 | 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 Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 600 | 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 Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 610 | 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 Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 614 | glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 617 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 618 | // Shaders |
| 619 | /////////////////////////////////////////////////////////////////////////////// |
| 620 | |
| 621 | void OpenGLRenderer::resetShader() { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 622 | mShader = NULL; |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 625 | void OpenGLRenderer::setupShader(SkiaShader* shader) { |
| 626 | mShader = shader; |
| 627 | if (mShader) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 628 | mShader->set(&mCaches.textureCache, &mCaches.gradientCache); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 629 | } |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 632 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 633 | // Color filters |
| 634 | /////////////////////////////////////////////////////////////////////////////// |
| 635 | |
| 636 | void OpenGLRenderer::resetColorFilter() { |
| 637 | mColorFilter = NULL; |
| 638 | } |
| 639 | |
| 640 | void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) { |
| 641 | mColorFilter = filter; |
| 642 | } |
| 643 | |
| 644 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 645 | // Drop shadow |
| 646 | /////////////////////////////////////////////////////////////////////////////// |
| 647 | |
| 648 | void OpenGLRenderer::resetShadow() { |
| 649 | mHasShadow = false; |
| 650 | } |
| 651 | |
| 652 | void 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 Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 661 | // Drawing implementation |
| 662 | /////////////////////////////////////////////////////////////////////////////// |
| 663 | |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 664 | void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y, |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 665 | SkXfermode::Mode mode, float alpha) { |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 666 | const float sx = x - texture->left + mShadowDx; |
| 667 | const float sy = y - texture->top + mShadowDy; |
| 668 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 669 | const int shadowAlpha = ((mShadowColor >> 24) & 0xFF); |
| 670 | const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 671 | 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 | |
| 679 | void 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 | |
| 686 | void 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 Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 704 | useProgram(mCaches.programCache.get(description)); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 705 | |
| 706 | // Setup the blending mode |
| 707 | chooseBlending(true, mode); |
| 708 | bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 709 | glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 710 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 711 | int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords"); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 712 | glEnableVertexAttribArray(texCoordsSlot); |
| 713 | |
| 714 | // Setup attributes |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 715 | glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 716 | 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 Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 727 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform); |
| 728 | glUniform4f(mCaches.currentProgram->color, r, g, b, a); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 729 | |
| 730 | textureUnit++; |
| 731 | if (applyFilters) { |
| 732 | // Setup attributes and uniforms required by the shaders |
| 733 | if (mShader) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 734 | mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 735 | } |
| 736 | if (mColorFilter) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 737 | mColorFilter->setupProgram(mCaches.currentProgram); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 738 | } |
| 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 | |
| 746 | void 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 Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 793 | void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, |
Romain Guy | 3d58c03 | 2010-07-14 16:34:53 -0700 | [diff] [blame] | 794 | int color, SkXfermode::Mode mode, bool ignoreTransform) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 795 | // If a shader is set, preserve only the alpha |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 796 | if (mShader) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 797 | color |= 0x00ffffff; |
| 798 | } |
| 799 | |
| 800 | // Render using pre-multiplied alpha |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 801 | const int alpha = (color >> 24) & 0xFF; |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 802 | const GLfloat a = alpha / 255.0f; |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 803 | 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 Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 807 | GLuint textureUnit = 0; |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 808 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 809 | // Setup the blending mode |
| 810 | chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode); |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 811 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 812 | // Describe the required shaders |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 813 | ProgramDescription description; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 814 | if (mShader) { |
| 815 | mShader->describe(description, mExtensions); |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 816 | } |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 817 | if (mColorFilter) { |
| 818 | mColorFilter->describe(description, mExtensions); |
| 819 | } |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 820 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 821 | // Build and use the appropriate shader |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 822 | useProgram(mCaches.programCache.get(description)); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 823 | |
| 824 | // Setup attributes |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 825 | glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 826 | 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 Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 831 | if (!ignoreTransform) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 832 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 833 | } else { |
| 834 | mat4 identity; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 835 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 836 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 837 | glUniform4f(mCaches.currentProgram->color, r, g, b, a); |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 838 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 839 | // Setup attributes and uniforms required by the shaders |
| 840 | if (mShader) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 841 | mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 842 | } |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 843 | if (mColorFilter) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 844 | mColorFilter->setupProgram(mCaches.currentProgram); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 845 | } |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 846 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 847 | // Draw the mesh |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 848 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 849 | } |
| 850 | |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 851 | void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 852 | const Texture* texture, const SkPaint* paint) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 853 | int alpha; |
| 854 | SkXfermode::Mode mode; |
| 855 | getAlphaAndMode(paint, &alpha, &mode); |
| 856 | |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 857 | drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend, |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 858 | &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL); |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 859 | } |
| 860 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 861 | void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 862 | GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) { |
| 863 | drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 864 | &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 868 | GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 869 | GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) { |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 870 | ProgramDescription description; |
| 871 | description.hasTexture = true; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 872 | if (mColorFilter) { |
| 873 | mColorFilter->describe(description, mExtensions); |
| 874 | } |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 875 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 876 | mModelView.loadTranslate(left, top, 0.0f); |
| 877 | mModelView.scale(right - left, bottom - top, 1.0f); |
| 878 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 879 | useProgram(mCaches.programCache.get(description)); |
| 880 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform); |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 881 | |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 882 | chooseBlending(blend || alpha < 1.0f, mode); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 883 | |
| 884 | // Texture |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 885 | bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 886 | glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 887 | |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 888 | // Always premultiplied |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 889 | glUniform4f(mCaches.currentProgram->color, alpha, alpha, alpha, alpha); |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 890 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 891 | // Mesh |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 892 | int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords"); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 893 | glEnableVertexAttribArray(texCoordsSlot); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 894 | glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 895 | gMeshStride, vertices); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 896 | glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords); |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 897 | |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 898 | // Color filter |
| 899 | if (mColorFilter) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 900 | mColorFilter->setupProgram(mCaches.currentProgram); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 901 | } |
| 902 | |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 903 | if (!indices) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 904 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 905 | } else { |
| 906 | glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices); |
| 907 | } |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 908 | glDisableVertexAttribArray(texCoordsSlot); |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 912 | blend = blend || mode != SkXfermode::kSrcOver_Mode; |
| 913 | if (blend) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 914 | if (!mCaches.blend) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 915 | 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 Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 924 | if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 925 | glBlendFunc(sourceMode, destMode); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 926 | mCaches.lastSrcMode = sourceMode; |
| 927 | mCaches.lastDstMode = destMode; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 928 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 929 | } else if (mCaches.blend) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 930 | glDisable(GL_BLEND); |
| 931 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 932 | mCaches.blend = blend; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 933 | } |
| 934 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 935 | bool OpenGLRenderer::useProgram(Program* program) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 936 | if (!program->isInUse()) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 937 | if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove(); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 938 | program->use(); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 939 | mCaches.currentProgram = program; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 940 | return false; |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 941 | } |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 942 | return true; |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 943 | } |
| 944 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 945 | void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 946 | TextureVertex* v = &mMeshVertices[0]; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 947 | TextureVertex::setUV(v++, u1, v1); |
| 948 | TextureVertex::setUV(v++, u2, v1); |
| 949 | TextureVertex::setUV(v++, u1, v2); |
| 950 | TextureVertex::setUV(v++, u2, v2); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | void 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 Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 972 | } |
| 973 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 974 | void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) { |
| 975 | glActiveTexture(gTextureUnits[textureUnit]); |
Romain Guy | ae5575b | 2010-07-29 18:48:04 -0700 | [diff] [blame] | 976 | glBindTexture(GL_TEXTURE_2D, texture); |
Romain Guy | a1db574 | 2010-07-20 13:09:13 -0700 | [diff] [blame] | 977 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS); |
| 978 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT); |
| 979 | } |
| 980 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 981 | }; // namespace uirenderer |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 982 | }; // namespace android |