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