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