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 | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 83 | static const Blender gBlendsSwap[] = { |
| 84 | { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO }, |
| 85 | { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE }, |
| 86 | { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO }, |
| 87 | { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, |
| 88 | { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, |
| 89 | { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA }, |
| 90 | { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO }, |
| 91 | { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, |
| 92 | { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, |
| 93 | { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, |
| 94 | { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, |
| 95 | { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA } |
| 96 | }; |
| 97 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 98 | static const GLenum gTextureUnits[] = { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 99 | GL_TEXTURE0, |
| 100 | GL_TEXTURE1, |
| 101 | GL_TEXTURE2 |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 104 | /////////////////////////////////////////////////////////////////////////////// |
| 105 | // Constructors/destructor |
| 106 | /////////////////////////////////////////////////////////////////////////////// |
| 107 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 108 | OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) { |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 109 | LOGD("Create OpenGLRenderer"); |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 110 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 111 | mShader = NULL; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 112 | mColorFilter = NULL; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 113 | mHasShadow = false; |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 114 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 115 | memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices)); |
| 116 | |
Romain Guy | ae5575b | 2010-07-29 18:48:04 -0700 | [diff] [blame] | 117 | mFirstSnapshot = new Snapshot; |
| 118 | |
| 119 | GLint maxTextureUnits; |
| 120 | glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits); |
| 121 | if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) { |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 122 | LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT); |
| 123 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 126 | OpenGLRenderer::~OpenGLRenderer() { |
| 127 | LOGD("Destroy OpenGLRenderer"); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 130 | /////////////////////////////////////////////////////////////////////////////// |
| 131 | // Setup |
| 132 | /////////////////////////////////////////////////////////////////////////////// |
| 133 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 134 | void OpenGLRenderer::setViewport(int width, int height) { |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 135 | glViewport(0, 0, width, height); |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 136 | mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 137 | |
| 138 | mWidth = width; |
| 139 | mHeight = height; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 142 | void OpenGLRenderer::prepare() { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 143 | mSnapshot = new Snapshot(mFirstSnapshot, |
| 144 | SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 145 | mSaveCount = 1; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 146 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 147 | glViewport(0, 0, mWidth, mHeight); |
| 148 | |
Romain Guy | d90f23e | 2010-09-09 11:47:54 -0700 | [diff] [blame] | 149 | glDisable(GL_DITHER); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 150 | glDisable(GL_SCISSOR_TEST); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 151 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 152 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 153 | glClear(GL_COLOR_BUFFER_BIT); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 154 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 155 | glEnable(GL_SCISSOR_TEST); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 156 | glScissor(0, 0, mWidth, mHeight); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 157 | |
Romain Guy | b82da65 | 2010-07-30 11:36:12 -0700 | [diff] [blame] | 158 | mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 161 | void OpenGLRenderer::acquireContext() { |
| 162 | if (mCaches.currentProgram) { |
| 163 | if (mCaches.currentProgram->isInUse()) { |
| 164 | mCaches.currentProgram->remove(); |
| 165 | mCaches.currentProgram = NULL; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | void OpenGLRenderer::releaseContext() { |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 171 | glViewport(0, 0, mWidth, mHeight); |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 172 | |
| 173 | glEnable(GL_SCISSOR_TEST); |
| 174 | setScissorFromClip(); |
| 175 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 176 | glDisable(GL_DITHER); |
| 177 | |
| 178 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 179 | |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 180 | if (mCaches.blend) { |
| 181 | glEnable(GL_BLEND); |
| 182 | glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 183 | glBlendEquation(GL_FUNC_ADD); |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 184 | } else { |
| 185 | glDisable(GL_BLEND); |
| 186 | } |
| 187 | } |
| 188 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 189 | /////////////////////////////////////////////////////////////////////////////// |
| 190 | // State management |
| 191 | /////////////////////////////////////////////////////////////////////////////// |
| 192 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 193 | int OpenGLRenderer::getSaveCount() const { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 194 | return mSaveCount; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | int OpenGLRenderer::save(int flags) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 198 | return saveSnapshot(flags); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void OpenGLRenderer::restore() { |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 202 | if (mSaveCount > 1) { |
| 203 | restoreSnapshot(); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 204 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void OpenGLRenderer::restoreToCount(int saveCount) { |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 208 | if (saveCount < 1) saveCount = 1; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 209 | |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 210 | while (mSaveCount > saveCount) { |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 211 | restoreSnapshot(); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 212 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 215 | int OpenGLRenderer::saveSnapshot(int flags) { |
| 216 | mSnapshot = new Snapshot(mSnapshot, flags); |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 217 | return mSaveCount++; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | bool OpenGLRenderer::restoreSnapshot() { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 221 | bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 222 | bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 223 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 224 | sp<Snapshot> current = mSnapshot; |
| 225 | sp<Snapshot> previous = mSnapshot->previous; |
| 226 | |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 227 | mSaveCount--; |
| 228 | mSnapshot = previous; |
| 229 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 230 | if (restoreLayer) { |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 231 | composeLayer(current, previous); |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 234 | if (restoreClip) { |
| 235 | setScissorFromClip(); |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 236 | } |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 237 | |
| 238 | return restoreClip; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 241 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 242 | // Layers |
| 243 | /////////////////////////////////////////////////////////////////////////////// |
| 244 | |
| 245 | int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom, |
| 246 | const SkPaint* p, int flags) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 247 | int count = saveSnapshot(flags); |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 248 | |
| 249 | int alpha = 255; |
| 250 | SkXfermode::Mode mode; |
| 251 | |
| 252 | if (p) { |
| 253 | alpha = p->getAlpha(); |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 254 | if (!mExtensions.hasFramebufferFetch()) { |
| 255 | const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); |
| 256 | if (!isMode) { |
| 257 | // Assume SRC_OVER |
| 258 | mode = SkXfermode::kSrcOver_Mode; |
| 259 | } |
| 260 | } else { |
| 261 | mode = getXfermode(p->getXfermode()); |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 262 | } |
| 263 | } else { |
| 264 | mode = SkXfermode::kSrcOver_Mode; |
| 265 | } |
| 266 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 267 | createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags); |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 268 | |
| 269 | return count; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom, |
| 273 | int alpha, int flags) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 274 | if (alpha == 0xff) { |
| 275 | return saveLayer(left, top, right, bottom, NULL, flags); |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 276 | } else { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 277 | SkPaint paint; |
| 278 | paint.setAlpha(alpha); |
| 279 | return saveLayer(left, top, right, bottom, &paint, flags); |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 280 | } |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 281 | } |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 282 | |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 283 | /** |
| 284 | * Layers are viewed by Skia are slightly different than layers in image editing |
| 285 | * programs (for instance.) When a layer is created, previously created layers |
| 286 | * and the frame buffer still receive every drawing command. For instance, if a |
| 287 | * layer is created and a shape intersecting the bounds of the layers and the |
| 288 | * framebuffer is draw, the shape will be drawn on both (unless the layer was |
| 289 | * created with the SkCanvas::kClipToLayer_SaveFlag flag.) |
| 290 | * |
| 291 | * A way to implement layers is to create an FBO for each layer, backed by an RGBA |
| 292 | * texture. Unfortunately, this is inefficient as it requires every primitive to |
| 293 | * be drawn n + 1 times, where n is the number of active layers. In practice this |
| 294 | * means, for every primitive: |
| 295 | * - Switch active frame buffer |
| 296 | * - Change viewport, clip and projection matrix |
| 297 | * - Issue the drawing |
| 298 | * |
| 299 | * Switching rendering target n + 1 times per drawn primitive is extremely costly. |
| 300 | * To avoid this, layers are implemented in a different way here. |
| 301 | * |
| 302 | * This implementation relies on the frame buffer being at least RGBA 8888. When |
| 303 | * a layer is created, only a texture is created, not an FBO. The content of the |
| 304 | * frame buffer contained within the layer's bounds is copied into this texture |
| 305 | * using glCopyTexImage2D(). The layer's region is then cleared in the frame |
| 306 | * buffer and drawing continues as normal. This technique therefore treats the |
| 307 | * frame buffer as a scratch buffer for the layers. |
| 308 | * |
| 309 | * To compose the layers back onto the frame buffer, each layer texture |
| 310 | * (containing the original frame buffer data) is drawn as a simple quad over |
| 311 | * the frame buffer. The trick is that the quad is set as the composition |
| 312 | * destination in the blending equation, and the frame buffer becomes the source |
| 313 | * of the composition. |
| 314 | * |
| 315 | * Drawing layers with an alpha value requires an extra step before composition. |
| 316 | * An empty quad is drawn over the layer's region in the frame buffer. This quad |
| 317 | * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the |
| 318 | * quad is used to multiply the colors in the frame buffer. This is achieved by |
| 319 | * changing the GL blend functions for the GL_FUNC_ADD blend equation to |
| 320 | * GL_ZERO, GL_SRC_ALPHA. |
| 321 | * |
| 322 | * Because glCopyTexImage2D() can be slow, an alternative implementation might |
| 323 | * be use to draw a single clipped layer. The implementation described above |
| 324 | * is correct in every case. |
| 325 | */ |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 326 | bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top, |
| 327 | float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 328 | LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 329 | LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize()); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 330 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 331 | // Window coordinates of the layer |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 332 | Rect bounds(left, top, right, bottom); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 333 | mSnapshot->transform->mapRect(bounds); |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 334 | |
Romain Guy | 8411f33 | 2010-09-13 17:27:57 -0700 | [diff] [blame] | 335 | // Layers only make sense if they are in the framebuffer's bounds |
| 336 | bounds.intersect(*mSnapshot->clipRect); |
Romain Guy | 81ab046 | 2010-09-13 17:32:34 -0700 | [diff] [blame] | 337 | if (bounds.isEmpty()) return false; |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 338 | |
Romain Guy | 8411f33 | 2010-09-13 17:27:57 -0700 | [diff] [blame] | 339 | LayerSize size(bounds.getWidth(), bounds.getHeight()); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 340 | Layer* layer = mCaches.layerCache.get(size); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 341 | if (!layer) { |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 342 | return false; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 345 | layer->mode = mode; |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 346 | layer->alpha = alpha; |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 347 | layer->layer.set(bounds); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 348 | |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 349 | // Save the layer in the snapshot |
| 350 | snapshot->flags |= Snapshot::kFlagIsLayer; |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 351 | snapshot->layer = layer; |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 352 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 353 | // Copy the framebuffer into the layer |
| 354 | glBindTexture(GL_TEXTURE_2D, layer->texture); |
| 355 | glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, mHeight - bounds.bottom, |
| 356 | bounds.getWidth(), bounds.getHeight(), 0); |
| 357 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 358 | if (flags & SkCanvas::kClipToLayer_SaveFlag) { |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 359 | if (mSnapshot->clipTransformed(bounds)) setScissorFromClip(); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 362 | // Enqueue the buffer coordinates to clear the corresponding region later |
| 363 | mLayers.push(new Rect(bounds)); |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 364 | |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 365 | return true; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 368 | /** |
| 369 | * Read the documentation of createLayer() before doing anything in this method. |
| 370 | */ |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 371 | void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) { |
| 372 | if (!current->layer) { |
| 373 | LOGE("Attempting to compose a layer that does not exist"); |
| 374 | return; |
| 375 | } |
| 376 | |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 377 | // Restore the clip from the previous snapshot |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 378 | const Rect& clip = *previous->clipRect; |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 379 | glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight()); |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 380 | |
| 381 | Layer* layer = current->layer; |
| 382 | const Rect& rect = layer->layer; |
| 383 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 384 | if (layer->alpha < 255) { |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 385 | drawColorRect(rect.left, rect.top, rect.right, rect.bottom, |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 386 | layer->alpha << 24, SkXfermode::kDstIn_Mode, true); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | // Layers are already drawn with a top-left origin, don't flip the texture |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 390 | resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f); |
| 391 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 392 | drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture, |
| 393 | 1.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0], |
| 394 | &mMeshVertices[0].texture[0], NULL, 0, true, true); |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 395 | |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 396 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
| 397 | |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 398 | LayerSize size(rect.getWidth(), rect.getHeight()); |
| 399 | // Failing to add the layer to the cache should happen only if the |
| 400 | // layer is too large |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 401 | if (!mCaches.layerCache.put(size, layer)) { |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 402 | LAYER_LOGD("Deleting layer"); |
| 403 | |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 404 | glDeleteTextures(1, &layer->texture); |
| 405 | |
| 406 | delete layer; |
| 407 | } |
| 408 | } |
| 409 | |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 410 | void OpenGLRenderer::clearLayerRegions() { |
| 411 | if (mLayers.size() == 0) return; |
| 412 | |
| 413 | for (uint32_t i = 0; i < mLayers.size(); i++) { |
| 414 | Rect* bounds = mLayers.itemAt(i); |
| 415 | |
| 416 | // Clear the framebuffer where the layer will draw |
| 417 | glScissor(bounds->left, mHeight - bounds->bottom, |
| 418 | bounds->getWidth(), bounds->getHeight()); |
| 419 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 420 | glClear(GL_COLOR_BUFFER_BIT); |
| 421 | |
| 422 | delete bounds; |
| 423 | } |
| 424 | mLayers.clear(); |
| 425 | |
| 426 | // Restore the clip |
| 427 | setScissorFromClip(); |
| 428 | } |
| 429 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 430 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 431 | // Transforms |
| 432 | /////////////////////////////////////////////////////////////////////////////// |
| 433 | |
| 434 | void OpenGLRenderer::translate(float dx, float dy) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 435 | mSnapshot->transform->translate(dx, dy, 0.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | void OpenGLRenderer::rotate(float degrees) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 439 | mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | void OpenGLRenderer::scale(float sx, float sy) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 443 | mSnapshot->transform->scale(sx, sy, 1.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | void OpenGLRenderer::setMatrix(SkMatrix* matrix) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 447 | mSnapshot->transform->load(*matrix); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | void OpenGLRenderer::getMatrix(SkMatrix* matrix) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 451 | mSnapshot->transform->copyTo(*matrix); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 455 | mat4 m(*matrix); |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 456 | mSnapshot->transform->multiply(m); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | /////////////////////////////////////////////////////////////////////////////// |
| 460 | // Clipping |
| 461 | /////////////////////////////////////////////////////////////////////////////// |
| 462 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 463 | void OpenGLRenderer::setScissorFromClip() { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 464 | const Rect& clip = *mSnapshot->clipRect; |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 465 | glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight()); |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | const Rect& OpenGLRenderer::getClipBounds() { |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 469 | return mSnapshot->getLocalClip(); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 472 | bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) { |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 473 | Rect r(left, top, right, bottom); |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 474 | mSnapshot->transform->mapRect(r); |
| 475 | return !mSnapshot->clipRect->intersects(r); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 478 | bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) { |
| 479 | bool clipped = mSnapshot->clip(left, top, right, bottom, op); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 480 | if (clipped) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 481 | setScissorFromClip(); |
| 482 | } |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 483 | return !mSnapshot->clipRect->isEmpty(); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 486 | /////////////////////////////////////////////////////////////////////////////// |
| 487 | // Drawing |
| 488 | /////////////////////////////////////////////////////////////////////////////// |
| 489 | |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 490 | void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 491 | const float right = left + bitmap->width(); |
| 492 | const float bottom = top + bitmap->height(); |
| 493 | |
| 494 | if (quickReject(left, top, right, bottom)) { |
| 495 | return; |
| 496 | } |
| 497 | |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 498 | glActiveTexture(GL_TEXTURE0); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 499 | const Texture* texture = mCaches.textureCache.get(bitmap); |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 500 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 501 | const AutoTexture autoCleanup(texture); |
| 502 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 503 | drawTextureRect(left, top, right, bottom, texture, paint); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 506 | void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) { |
| 507 | Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height()); |
| 508 | const mat4 transform(*matrix); |
| 509 | transform.mapRect(r); |
| 510 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 511 | if (quickReject(r.left, r.top, r.right, r.bottom)) { |
| 512 | return; |
| 513 | } |
| 514 | |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 515 | glActiveTexture(GL_TEXTURE0); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 516 | const Texture* texture = mCaches.textureCache.get(bitmap); |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 517 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 518 | const AutoTexture autoCleanup(texture); |
| 519 | |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 520 | drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint); |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 523 | void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, |
| 524 | float srcLeft, float srcTop, float srcRight, float srcBottom, |
| 525 | float dstLeft, float dstTop, float dstRight, float dstBottom, |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 526 | const SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 527 | if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) { |
| 528 | return; |
| 529 | } |
| 530 | |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 531 | glActiveTexture(GL_TEXTURE0); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 532 | const Texture* texture = mCaches.textureCache.get(bitmap); |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 533 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 534 | const AutoTexture autoCleanup(texture); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 535 | |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 536 | const float width = texture->width; |
| 537 | const float height = texture->height; |
| 538 | |
| 539 | const float u1 = srcLeft / width; |
| 540 | const float v1 = srcTop / height; |
| 541 | const float u2 = srcRight / width; |
| 542 | const float v2 = srcBottom / height; |
| 543 | |
| 544 | resetDrawTextureTexCoords(u1, v1, u2, v2); |
| 545 | |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 546 | drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 547 | |
| 548 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
| 549 | } |
| 550 | |
Romain Guy | deba785 | 2010-07-07 17:54:48 -0700 | [diff] [blame] | 551 | void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch, |
| 552 | float left, float top, float right, float bottom, const SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 553 | if (quickReject(left, top, right, bottom)) { |
| 554 | return; |
| 555 | } |
| 556 | |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 557 | glActiveTexture(GL_TEXTURE0); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 558 | const Texture* texture = mCaches.textureCache.get(bitmap); |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 559 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 560 | const AutoTexture autoCleanup(texture); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 561 | |
| 562 | int alpha; |
| 563 | SkXfermode::Mode mode; |
| 564 | getAlphaAndMode(paint, &alpha, &mode); |
| 565 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 566 | Patch* mesh = mCaches.patchCache.get(patch); |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 567 | mesh->updateVertices(bitmap, left, top, right, bottom, |
| 568 | &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs); |
Romain Guy | 8411f33 | 2010-09-13 17:27:57 -0700 | [diff] [blame] | 569 | mesh->dump(); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 570 | |
| 571 | // Specify right and bottom as +1.0f from left/top to prevent scaling since the |
| 572 | // patch mesh already defines the final size |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 573 | drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f, |
| 574 | mode, texture->blend, &mesh->vertices[0].position[0], |
Romain Guy | 16202fc | 2010-07-09 16:13:28 -0700 | [diff] [blame] | 575 | &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 578 | void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 579 | const Rect& clip = *mSnapshot->clipRect; |
Romain Guy | 3d58c03 | 2010-07-14 16:34:53 -0700 | [diff] [blame] | 580 | drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 581 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 582 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 583 | 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] | 584 | if (quickReject(left, top, right, bottom)) { |
| 585 | return; |
| 586 | } |
| 587 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 588 | SkXfermode::Mode mode; |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 589 | if (!mExtensions.hasFramebufferFetch()) { |
| 590 | const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); |
| 591 | if (!isMode) { |
| 592 | // Assume SRC_OVER |
| 593 | mode = SkXfermode::kSrcOver_Mode; |
| 594 | } |
| 595 | } else { |
| 596 | mode = getXfermode(p->getXfermode()); |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | // Skia draws using the color's alpha channel if < 255 |
| 600 | // Otherwise, it uses the paint's alpha |
| 601 | int color = p->getColor(); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 602 | if (((color >> 24) & 0xff) == 255) { |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 603 | color |= p->getAlpha() << 24; |
| 604 | } |
| 605 | |
| 606 | drawColorRect(left, top, right, bottom, color, mode); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 607 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 608 | |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 609 | void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, |
| 610 | float x, float y, SkPaint* paint) { |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 611 | if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) { |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 612 | return; |
| 613 | } |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 614 | paint->setAntiAlias(true); |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 615 | |
Romain Guy | a674ab7 | 2010-08-10 17:26:42 -0700 | [diff] [blame] | 616 | float length = -1.0f; |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 617 | switch (paint->getTextAlign()) { |
| 618 | case SkPaint::kCenter_Align: |
| 619 | length = paint->measureText(text, bytesCount); |
| 620 | x -= length / 2.0f; |
| 621 | break; |
| 622 | case SkPaint::kRight_Align: |
| 623 | length = paint->measureText(text, bytesCount); |
| 624 | x -= length; |
| 625 | break; |
| 626 | default: |
| 627 | break; |
| 628 | } |
| 629 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 630 | int alpha; |
| 631 | SkXfermode::Mode mode; |
| 632 | getAlphaAndMode(paint, &alpha, &mode); |
| 633 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 634 | uint32_t color = paint->getColor(); |
| 635 | const GLfloat a = alpha / 255.0f; |
| 636 | const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; |
| 637 | const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; |
| 638 | const GLfloat b = a * ((color ) & 0xFF) / 255.0f; |
| 639 | |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 640 | FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint); |
| 641 | fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 642 | paint->getTextSize()); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 643 | if (mHasShadow) { |
| 644 | glActiveTexture(gTextureUnits[0]); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 645 | mCaches.dropShadowCache.setFontRenderer(fontRenderer); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 646 | const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount, |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 647 | count, mShadowRadius); |
| 648 | const AutoTexture autoCleanup(shadow); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 649 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 650 | setupShadow(shadow, x, y, mode, a); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 651 | |
| 652 | // Draw the mesh |
| 653 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 654 | glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 657 | GLuint textureUnit = 0; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 658 | glActiveTexture(gTextureUnits[textureUnit]); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 659 | |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 660 | setupTextureAlpha8(fontRenderer.getTexture(), 0, 0, textureUnit, x, y, r, g, b, a, |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 661 | mode, false, true); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 662 | |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 663 | const Rect& clip = mSnapshot->getLocalClip(); |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 664 | clearLayerRegions(); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 665 | fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 666 | |
| 667 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 668 | glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); |
Romain Guy | a674ab7 | 2010-08-10 17:26:42 -0700 | [diff] [blame] | 669 | |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 670 | drawTextDecorations(text, bytesCount, length, x, y, paint); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 673 | void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) { |
| 674 | GLuint textureUnit = 0; |
| 675 | glActiveTexture(gTextureUnits[textureUnit]); |
| 676 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 677 | const PathTexture* texture = mCaches.pathCache.get(path, paint); |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 678 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 679 | const AutoTexture autoCleanup(texture); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 680 | |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 681 | const float x = texture->left - texture->offset; |
| 682 | const float y = texture->top - texture->offset; |
| 683 | |
| 684 | if (quickReject(x, y, x + texture->width, y + texture->height)) { |
| 685 | return; |
| 686 | } |
| 687 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 688 | int alpha; |
| 689 | SkXfermode::Mode mode; |
| 690 | getAlphaAndMode(paint, &alpha, &mode); |
| 691 | |
| 692 | uint32_t color = paint->getColor(); |
| 693 | const GLfloat a = alpha / 255.0f; |
| 694 | const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; |
| 695 | const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; |
| 696 | const GLfloat b = a * ((color ) & 0xFF) / 255.0f; |
| 697 | |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 698 | setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true); |
| 699 | |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 700 | clearLayerRegions(); |
| 701 | |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 702 | // Draw the mesh |
| 703 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 704 | glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 707 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 708 | // Shaders |
| 709 | /////////////////////////////////////////////////////////////////////////////// |
| 710 | |
| 711 | void OpenGLRenderer::resetShader() { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 712 | mShader = NULL; |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 715 | void OpenGLRenderer::setupShader(SkiaShader* shader) { |
| 716 | mShader = shader; |
| 717 | if (mShader) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 718 | mShader->set(&mCaches.textureCache, &mCaches.gradientCache); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 719 | } |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 722 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 723 | // Color filters |
| 724 | /////////////////////////////////////////////////////////////////////////////// |
| 725 | |
| 726 | void OpenGLRenderer::resetColorFilter() { |
| 727 | mColorFilter = NULL; |
| 728 | } |
| 729 | |
| 730 | void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) { |
| 731 | mColorFilter = filter; |
| 732 | } |
| 733 | |
| 734 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 735 | // Drop shadow |
| 736 | /////////////////////////////////////////////////////////////////////////////// |
| 737 | |
| 738 | void OpenGLRenderer::resetShadow() { |
| 739 | mHasShadow = false; |
| 740 | } |
| 741 | |
| 742 | void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) { |
| 743 | mHasShadow = true; |
| 744 | mShadowRadius = radius; |
| 745 | mShadowDx = dx; |
| 746 | mShadowDy = dy; |
| 747 | mShadowColor = color; |
| 748 | } |
| 749 | |
| 750 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 751 | // Drawing implementation |
| 752 | /////////////////////////////////////////////////////////////////////////////// |
| 753 | |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 754 | void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y, |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 755 | SkXfermode::Mode mode, float alpha) { |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 756 | const float sx = x - texture->left + mShadowDx; |
| 757 | const float sy = y - texture->top + mShadowDy; |
| 758 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 759 | const int shadowAlpha = ((mShadowColor >> 24) & 0xFF); |
| 760 | const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 761 | const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f; |
| 762 | const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f; |
| 763 | const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f; |
| 764 | |
| 765 | GLuint textureUnit = 0; |
| 766 | setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false); |
| 767 | } |
| 768 | |
| 769 | void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit, |
| 770 | float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode, |
| 771 | bool transforms, bool applyFilters) { |
| 772 | setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit, |
| 773 | x, y, r, g, b, a, mode, transforms, applyFilters); |
| 774 | } |
| 775 | |
| 776 | void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height, |
| 777 | GLuint& textureUnit, float x, float y, float r, float g, float b, float a, |
| 778 | SkXfermode::Mode mode, bool transforms, bool applyFilters) { |
| 779 | // Describe the required shaders |
| 780 | ProgramDescription description; |
| 781 | description.hasTexture = true; |
| 782 | description.hasAlpha8Texture = true; |
| 783 | |
| 784 | if (applyFilters) { |
| 785 | if (mShader) { |
| 786 | mShader->describe(description, mExtensions); |
| 787 | } |
| 788 | if (mColorFilter) { |
| 789 | mColorFilter->describe(description, mExtensions); |
| 790 | } |
| 791 | } |
| 792 | |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 793 | // Setup the blending mode |
| 794 | chooseBlending(true, mode, description); |
| 795 | |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 796 | // Build and use the appropriate shader |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 797 | useProgram(mCaches.programCache.get(description)); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 798 | |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 799 | bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 800 | glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 801 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 802 | int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords"); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 803 | glEnableVertexAttribArray(texCoordsSlot); |
| 804 | |
| 805 | // Setup attributes |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 806 | glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 807 | gMeshStride, &mMeshVertices[0].position[0]); |
| 808 | glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, |
| 809 | gMeshStride, &mMeshVertices[0].texture[0]); |
| 810 | |
| 811 | // Setup uniforms |
| 812 | if (transforms) { |
| 813 | mModelView.loadTranslate(x, y, 0.0f); |
| 814 | mModelView.scale(width, height, 1.0f); |
| 815 | } else { |
| 816 | mModelView.loadIdentity(); |
| 817 | } |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 818 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 819 | glUniform4f(mCaches.currentProgram->color, r, g, b, a); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 820 | |
| 821 | textureUnit++; |
| 822 | if (applyFilters) { |
| 823 | // Setup attributes and uniforms required by the shaders |
| 824 | if (mShader) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 825 | mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 826 | } |
| 827 | if (mColorFilter) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 828 | mColorFilter->setupProgram(mCaches.currentProgram); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | } |
| 832 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 833 | // Same values used by Skia |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 834 | #define kStdStrikeThru_Offset (-6.0f / 21.0f) |
| 835 | #define kStdUnderline_Offset (1.0f / 9.0f) |
| 836 | #define kStdUnderline_Thickness (1.0f / 18.0f) |
| 837 | |
| 838 | void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length, |
| 839 | float x, float y, SkPaint* paint) { |
| 840 | // Handle underline and strike-through |
| 841 | uint32_t flags = paint->getFlags(); |
| 842 | if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) { |
| 843 | float underlineWidth = length; |
| 844 | // If length is > 0.0f, we already measured the text for the text alignment |
| 845 | if (length <= 0.0f) { |
| 846 | underlineWidth = paint->measureText(text, bytesCount); |
| 847 | } |
| 848 | |
| 849 | float offsetX = 0; |
| 850 | switch (paint->getTextAlign()) { |
| 851 | case SkPaint::kCenter_Align: |
| 852 | offsetX = underlineWidth * 0.5f; |
| 853 | break; |
| 854 | case SkPaint::kRight_Align: |
| 855 | offsetX = underlineWidth; |
| 856 | break; |
| 857 | default: |
| 858 | break; |
| 859 | } |
| 860 | |
| 861 | if (underlineWidth > 0.0f) { |
| 862 | float textSize = paint->getTextSize(); |
| 863 | float height = textSize * kStdUnderline_Thickness; |
| 864 | |
| 865 | float left = x - offsetX; |
| 866 | float top = 0.0f; |
| 867 | float right = left + underlineWidth; |
| 868 | float bottom = 0.0f; |
| 869 | |
| 870 | if (flags & SkPaint::kUnderlineText_Flag) { |
| 871 | top = y + textSize * kStdUnderline_Offset; |
| 872 | bottom = top + height; |
| 873 | drawRect(left, top, right, bottom, paint); |
| 874 | } |
| 875 | |
| 876 | if (flags & SkPaint::kStrikeThruText_Flag) { |
| 877 | top = y + textSize * kStdStrikeThru_Offset; |
| 878 | bottom = top + height; |
| 879 | drawRect(left, top, right, bottom, paint); |
| 880 | } |
| 881 | } |
| 882 | } |
| 883 | } |
| 884 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 885 | void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 886 | int color, SkXfermode::Mode mode, bool ignoreTransform) { |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 887 | clearLayerRegions(); |
| 888 | |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 889 | // If a shader is set, preserve only the alpha |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 890 | if (mShader) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 891 | color |= 0x00ffffff; |
| 892 | } |
| 893 | |
| 894 | // Render using pre-multiplied alpha |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 895 | const int alpha = (color >> 24) & 0xFF; |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 896 | const GLfloat a = alpha / 255.0f; |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 897 | const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; |
| 898 | const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; |
| 899 | const GLfloat b = a * ((color ) & 0xFF) / 255.0f; |
| 900 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 901 | GLuint textureUnit = 0; |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 902 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 903 | // Describe the required shaders |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 904 | ProgramDescription description; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 905 | if (mShader) { |
| 906 | mShader->describe(description, mExtensions); |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 907 | } |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 908 | if (mColorFilter) { |
| 909 | mColorFilter->describe(description, mExtensions); |
| 910 | } |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 911 | |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 912 | // Setup the blending mode |
| 913 | chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode, description); |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 914 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 915 | // Build and use the appropriate shader |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 916 | useProgram(mCaches.programCache.get(description)); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 917 | |
| 918 | // Setup attributes |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 919 | glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 920 | gMeshStride, &mMeshVertices[0].position[0]); |
| 921 | |
| 922 | // Setup uniforms |
| 923 | mModelView.loadTranslate(left, top, 0.0f); |
| 924 | mModelView.scale(right - left, bottom - top, 1.0f); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 925 | if (!ignoreTransform) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 926 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 927 | } else { |
| 928 | mat4 identity; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 929 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 930 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 931 | glUniform4f(mCaches.currentProgram->color, r, g, b, a); |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 932 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 933 | // Setup attributes and uniforms required by the shaders |
| 934 | if (mShader) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 935 | mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 936 | } |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 937 | if (mColorFilter) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 938 | mColorFilter->setupProgram(mCaches.currentProgram); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 939 | } |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 940 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 941 | // Draw the mesh |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 942 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 943 | } |
| 944 | |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 945 | void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 946 | const Texture* texture, const SkPaint* paint) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 947 | int alpha; |
| 948 | SkXfermode::Mode mode; |
| 949 | getAlphaAndMode(paint, &alpha, &mode); |
| 950 | |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 951 | 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] | 952 | &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL); |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 953 | } |
| 954 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 955 | void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 956 | GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) { |
| 957 | drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 958 | &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 962 | GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 963 | GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount, |
| 964 | bool swapSrcDst, bool ignoreTransform) { |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 965 | clearLayerRegions(); |
| 966 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 967 | ProgramDescription description; |
| 968 | description.hasTexture = true; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 969 | if (mColorFilter) { |
| 970 | mColorFilter->describe(description, mExtensions); |
| 971 | } |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 972 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 973 | mModelView.loadTranslate(left, top, 0.0f); |
| 974 | mModelView.scale(right - left, bottom - top, 1.0f); |
| 975 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 976 | chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst); |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 977 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 978 | useProgram(mCaches.programCache.get(description)); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 979 | if (!ignoreTransform) { |
| 980 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); |
| 981 | } else { |
| 982 | mat4 m; |
| 983 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, m); |
| 984 | } |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 985 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 986 | // Texture |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 987 | bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 988 | glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 989 | |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 990 | // Always premultiplied |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 991 | glUniform4f(mCaches.currentProgram->color, alpha, alpha, alpha, alpha); |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 992 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 993 | // Mesh |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 994 | int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords"); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 995 | glEnableVertexAttribArray(texCoordsSlot); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 996 | glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 997 | gMeshStride, vertices); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 998 | glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords); |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 999 | |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1000 | // Color filter |
| 1001 | if (mColorFilter) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1002 | mColorFilter->setupProgram(mCaches.currentProgram); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1003 | } |
| 1004 | |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 1005 | if (!indices) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 1006 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 1007 | } else { |
| 1008 | glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices); |
| 1009 | } |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1010 | glDisableVertexAttribArray(texCoordsSlot); |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1013 | void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1014 | ProgramDescription& description, bool swapSrcDst) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1015 | blend = blend || mode != SkXfermode::kSrcOver_Mode; |
| 1016 | if (blend) { |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1017 | if (mode < SkXfermode::kPlus_Mode) { |
| 1018 | if (!mCaches.blend) { |
| 1019 | glEnable(GL_BLEND); |
| 1020 | } |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1021 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1022 | GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src; |
| 1023 | GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1024 | |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1025 | if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) { |
| 1026 | glBlendFunc(sourceMode, destMode); |
| 1027 | mCaches.lastSrcMode = sourceMode; |
| 1028 | mCaches.lastDstMode = destMode; |
| 1029 | } |
| 1030 | } else { |
| 1031 | // These blend modes are not supported by OpenGL directly and have |
| 1032 | // to be implemented using shaders. Since the shader will perform |
| 1033 | // the blending, turn blending off here |
| 1034 | if (mExtensions.hasFramebufferFetch()) { |
| 1035 | description.framebufferMode = mode; |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1036 | description.swapSrcDst = swapSrcDst; |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | if (mCaches.blend) { |
| 1040 | glDisable(GL_BLEND); |
| 1041 | } |
| 1042 | blend = false; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1043 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1044 | } else if (mCaches.blend) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1045 | glDisable(GL_BLEND); |
| 1046 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1047 | mCaches.blend = blend; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1050 | bool OpenGLRenderer::useProgram(Program* program) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1051 | if (!program->isInUse()) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1052 | if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove(); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1053 | program->use(); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1054 | mCaches.currentProgram = program; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1055 | return false; |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 1056 | } |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1057 | return true; |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 1060 | void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 1061 | TextureVertex* v = &mMeshVertices[0]; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1062 | TextureVertex::setUV(v++, u1, v1); |
| 1063 | TextureVertex::setUV(v++, u2, v1); |
| 1064 | TextureVertex::setUV(v++, u1, v2); |
| 1065 | TextureVertex::setUV(v++, u2, v2); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) { |
| 1069 | if (paint) { |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1070 | if (!mExtensions.hasFramebufferFetch()) { |
| 1071 | const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode); |
| 1072 | if (!isMode) { |
| 1073 | // Assume SRC_OVER |
| 1074 | *mode = SkXfermode::kSrcOver_Mode; |
| 1075 | } |
| 1076 | } else { |
| 1077 | *mode = getXfermode(paint->getXfermode()); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | // Skia draws using the color's alpha channel if < 255 |
| 1081 | // Otherwise, it uses the paint's alpha |
| 1082 | int color = paint->getColor(); |
| 1083 | *alpha = (color >> 24) & 0xFF; |
| 1084 | if (*alpha == 255) { |
| 1085 | *alpha = paint->getAlpha(); |
| 1086 | } |
| 1087 | } else { |
| 1088 | *mode = SkXfermode::kSrcOver_Mode; |
| 1089 | *alpha = 255; |
| 1090 | } |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1093 | SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) { |
| 1094 | if (mode == NULL) { |
| 1095 | return SkXfermode::kSrcOver_Mode; |
| 1096 | } |
| 1097 | return mode->fMode; |
| 1098 | } |
| 1099 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1100 | void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) { |
| 1101 | glActiveTexture(gTextureUnits[textureUnit]); |
Romain Guy | ae5575b | 2010-07-29 18:48:04 -0700 | [diff] [blame] | 1102 | glBindTexture(GL_TEXTURE_2D, texture); |
Romain Guy | a1db574 | 2010-07-20 13:09:13 -0700 | [diff] [blame] | 1103 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS); |
| 1104 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT); |
| 1105 | } |
| 1106 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 1107 | }; // namespace uirenderer |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1108 | }; // namespace android |