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