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> |
| 24 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 25 | #include <utils/Log.h> |
| 26 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 27 | #include "OpenGLRenderer.h" |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 30 | namespace uirenderer { |
| 31 | |
| 32 | /////////////////////////////////////////////////////////////////////////////// |
| 33 | // Defines |
| 34 | /////////////////////////////////////////////////////////////////////////////// |
| 35 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 36 | #define SV(x, y) { { x, y } } |
| 37 | #define FV(x, y, u, v) { { x, y }, { u, v } } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 38 | |
| 39 | /////////////////////////////////////////////////////////////////////////////// |
| 40 | // Globals |
| 41 | /////////////////////////////////////////////////////////////////////////////// |
| 42 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 43 | static const SimpleVertex gDrawColorVertices[] = { |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 44 | SV(0.0f, 0.0f), |
| 45 | SV(1.0f, 0.0f), |
| 46 | SV(0.0f, 1.0f), |
| 47 | SV(1.0f, 1.0f) |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 48 | }; |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 49 | static const GLsizei gDrawColorVertexStride = sizeof(SimpleVertex); |
| 50 | static const GLsizei gDrawColorVertexCount = 4; |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 51 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 52 | // This array is never used directly but used as a memcpy source in the |
| 53 | // OpenGLRenderer constructor |
| 54 | static const TextureVertex gDrawTextureVertices[] = { |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 55 | FV(0.0f, 0.0f, 0.0f, 1.0f), |
| 56 | FV(1.0f, 0.0f, 1.0f, 1.0f), |
| 57 | FV(0.0f, 1.0f, 0.0f, 0.0f), |
| 58 | FV(1.0f, 1.0f, 1.0f, 0.0f) |
| 59 | }; |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 60 | static const GLsizei gDrawTextureVertexStride = sizeof(TextureVertex); |
| 61 | static const GLsizei gDrawTextureVertexCount = 4; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 62 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 63 | // In this array, the index of each Blender equals the value of the first |
| 64 | // entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode] |
| 65 | static const Blender gBlends[] = { |
| 66 | { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO }, |
| 67 | { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO }, |
| 68 | { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE }, |
| 69 | { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, |
| 70 | { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, |
| 71 | { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO }, |
| 72 | { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA }, |
| 73 | { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, |
| 74 | { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, |
| 75 | { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, |
| 76 | { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, |
| 77 | { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA } |
| 78 | }; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 79 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 80 | /////////////////////////////////////////////////////////////////////////////// |
| 81 | // Constructors/destructor |
| 82 | /////////////////////////////////////////////////////////////////////////////// |
| 83 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 84 | OpenGLRenderer::OpenGLRenderer() { |
| 85 | LOGD("Create OpenGLRenderer"); |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 86 | |
| 87 | mDrawColorShader = new DrawColorProgram; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 88 | mDrawTextureShader = new DrawTextureProgram; |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 89 | |
| 90 | memcpy(mDrawTextureVertices, gDrawTextureVertices, sizeof(gDrawTextureVertices)); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 93 | OpenGLRenderer::~OpenGLRenderer() { |
| 94 | LOGD("Destroy OpenGLRenderer"); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 97 | /////////////////////////////////////////////////////////////////////////////// |
| 98 | // Setup |
| 99 | /////////////////////////////////////////////////////////////////////////////// |
| 100 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 101 | void OpenGLRenderer::setViewport(int width, int height) { |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 102 | glViewport(0, 0, width, height); |
| 103 | |
| 104 | mat4 ortho; |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 105 | ortho.loadOrtho(0, width, height, 0, -1, 1); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 106 | ortho.copyTo(mOrthoMatrix); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 107 | |
| 108 | mWidth = width; |
| 109 | mHeight = height; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 112 | void OpenGLRenderer::prepare() { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 113 | mSnapshot = &mFirstSnapshot; |
| 114 | mSaveCount = 0; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 115 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 116 | glDisable(GL_SCISSOR_TEST); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 117 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 118 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 119 | glClear(GL_COLOR_BUFFER_BIT); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 120 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 121 | glEnable(GL_SCISSOR_TEST); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 122 | glScissor(0, 0, mWidth, mHeight); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 123 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 124 | mSnapshot->clipRect.set(0.0f, 0.0f, mWidth, mHeight); |
| 125 | } |
| 126 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 127 | /////////////////////////////////////////////////////////////////////////////// |
| 128 | // State management |
| 129 | /////////////////////////////////////////////////////////////////////////////// |
| 130 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 131 | int OpenGLRenderer::getSaveCount() const { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 132 | return mSaveCount; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | int OpenGLRenderer::save(int flags) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 136 | return saveSnapshot(); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void OpenGLRenderer::restore() { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 140 | if (mSaveCount == 0) return; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 141 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 142 | if (restoreSnapshot()) { |
| 143 | setScissorFromClip(); |
| 144 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void OpenGLRenderer::restoreToCount(int saveCount) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 148 | if (saveCount <= 0 || saveCount > mSaveCount) return; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 149 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 150 | bool restoreClip = false; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 151 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 152 | while (mSaveCount != saveCount - 1) { |
| 153 | restoreClip |= restoreSnapshot(); |
| 154 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 155 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 156 | if (restoreClip) { |
| 157 | setScissorFromClip(); |
| 158 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | int OpenGLRenderer::saveSnapshot() { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 162 | mSnapshot = new Snapshot(mSnapshot); |
| 163 | return ++mSaveCount; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | bool OpenGLRenderer::restoreSnapshot() { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 167 | bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 168 | bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 169 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 170 | sp<Snapshot> current = mSnapshot; |
| 171 | sp<Snapshot> previous = mSnapshot->previous; |
| 172 | |
| 173 | if (restoreLayer) { |
| 174 | // Unbind current FBO and restore previous one |
| 175 | // Most of the time, previous->fbo will be 0 to bind the default buffer |
| 176 | glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); |
| 177 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 178 | // Restore the clip from the previous snapshot |
| 179 | const Rect& clip = previous->getMappedClip(); |
| 180 | glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight()); |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 181 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 182 | // Compute the correct texture coordinates for the FBO texture |
| 183 | // The texture is currently as big as the window but drawn with |
| 184 | // a quad of the appropriate size |
| 185 | const Rect& layer = current->layer; |
| 186 | Rect texCoords(current->layer); |
| 187 | mSnapshot->transform.mapRect(texCoords); |
| 188 | |
| 189 | const float u1 = texCoords.left / float(mWidth); |
| 190 | const float v1 = (mHeight - texCoords.top) / float(mHeight); |
| 191 | const float u2 = texCoords.right / float(mWidth); |
| 192 | const float v2 = (mHeight - texCoords.bottom) / float(mHeight); |
| 193 | |
| 194 | resetDrawTextureTexCoords(u1, v1, u2, v1); |
| 195 | |
| 196 | drawTextureRect(layer.left, layer.top, layer.right, layer.bottom, |
| 197 | current->texture, current->alpha); |
| 198 | |
| 199 | resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f); |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 200 | |
| 201 | glDeleteFramebuffers(1, ¤t->fbo); |
| 202 | glDeleteTextures(1, ¤t->texture); |
| 203 | } |
| 204 | |
| 205 | mSnapshot = previous; |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 206 | mSaveCount--; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 207 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 208 | return restoreClip; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 211 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 212 | // Layers |
| 213 | /////////////////////////////////////////////////////////////////////////////// |
| 214 | |
| 215 | int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom, |
| 216 | const SkPaint* p, int flags) { |
| 217 | // TODO Implement |
| 218 | return saveSnapshot(); |
| 219 | } |
| 220 | |
| 221 | int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom, |
| 222 | int alpha, int flags) { |
| 223 | int count = saveSnapshot(); |
| 224 | |
| 225 | mSnapshot->flags |= Snapshot::kFlagIsLayer; |
| 226 | mSnapshot->alpha = alpha / 255.0f; |
| 227 | mSnapshot->layer.set(left, top, right, bottom); |
| 228 | |
| 229 | // Generate the FBO and attach the texture |
| 230 | glGenFramebuffers(1, &mSnapshot->fbo); |
| 231 | glBindFramebuffer(GL_FRAMEBUFFER, mSnapshot->fbo); |
| 232 | |
| 233 | // Generate the texture in which the FBO will draw |
| 234 | glGenTextures(1, &mSnapshot->texture); |
| 235 | glBindTexture(GL_TEXTURE_2D, mSnapshot->texture); |
| 236 | |
| 237 | // The FBO will not be scaled, so we can use lower quality filtering |
| 238 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 239 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 240 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 241 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 242 | |
| 243 | // TODO ***** IMPORTANT ***** |
| 244 | // Creating a texture-backed FBO works only if the texture is the same size |
| 245 | // as the original rendering buffer (in this case, mWidth and mHeight.) |
| 246 | // This is expensive and wasteful and must be fixed. |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 247 | // TODO Additionally we should use an FBO cache |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 248 | |
| 249 | const GLsizei width = mWidth; //right - left; |
| 250 | const GLsizei height = mHeight; //bottom - right; |
| 251 | |
| 252 | const GLint format = (flags & SkCanvas::kHasAlphaLayer_SaveFlag) ? GL_RGBA : GL_RGB; |
| 253 | glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL); |
| 254 | glBindTexture(GL_TEXTURE_2D, 0); |
| 255 | |
| 256 | // Bind texture to FBO |
| 257 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
| 258 | mSnapshot->texture, 0); |
| 259 | |
| 260 | GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 261 | if (status != GL_FRAMEBUFFER_COMPLETE) { |
| 262 | LOGD("Framebuffer incomplete %d", status); |
| 263 | |
| 264 | glDeleteFramebuffers(1, &mSnapshot->fbo); |
| 265 | glDeleteTextures(1, &mSnapshot->texture); |
| 266 | } |
| 267 | |
| 268 | return count; |
| 269 | } |
| 270 | |
| 271 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 272 | // Transforms |
| 273 | /////////////////////////////////////////////////////////////////////////////// |
| 274 | |
| 275 | void OpenGLRenderer::translate(float dx, float dy) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 276 | mSnapshot->transform.translate(dx, dy, 0.0f); |
| 277 | mSnapshot->flags |= Snapshot::kFlagDirtyTransform; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | void OpenGLRenderer::rotate(float degrees) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 281 | mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f); |
| 282 | mSnapshot->flags |= Snapshot::kFlagDirtyTransform; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | void OpenGLRenderer::scale(float sx, float sy) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 286 | mSnapshot->transform.scale(sx, sy, 1.0f); |
| 287 | mSnapshot->flags |= Snapshot::kFlagDirtyTransform; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void OpenGLRenderer::setMatrix(SkMatrix* matrix) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 291 | mSnapshot->transform.load(*matrix); |
| 292 | mSnapshot->flags |= Snapshot::kFlagDirtyTransform; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | void OpenGLRenderer::getMatrix(SkMatrix* matrix) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 296 | mSnapshot->transform.copyTo(*matrix); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 300 | mat4 m(*matrix); |
| 301 | mSnapshot->transform.multiply(m); |
| 302 | mSnapshot->flags |= Snapshot::kFlagDirtyTransform; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /////////////////////////////////////////////////////////////////////////////// |
| 306 | // Clipping |
| 307 | /////////////////////////////////////////////////////////////////////////////// |
| 308 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 309 | void OpenGLRenderer::setScissorFromClip() { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 310 | const Rect& clip = mSnapshot->getMappedClip(); |
| 311 | glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight()); |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | const Rect& OpenGLRenderer::getClipBounds() { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 315 | return mSnapshot->clipRect; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 318 | bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) { |
| 319 | /* |
| 320 | * The documentation of quickReject() indicates that the specified rect |
| 321 | * is transformed before being compared to the clip rect. However, the |
| 322 | * clip rect is not stored transformed in the snapshot and can thus be |
| 323 | * compared directly |
| 324 | * |
| 325 | * The following code can be used instead to performed a mapped comparison: |
| 326 | * |
| 327 | * mSnapshot->transform.mapRect(r); |
| 328 | * const Rect& clip = mSnapshot->getMappedClip(); |
| 329 | * return !clip.intersects(r); |
| 330 | */ |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 331 | Rect r(left, top, right, bottom); |
| 332 | return !mSnapshot->clipRect.intersects(r); |
| 333 | } |
| 334 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 335 | bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 336 | bool clipped = mSnapshot->clipRect.intersect(left, top, right, bottom); |
| 337 | if (clipped) { |
| 338 | mSnapshot->flags |= Snapshot::kFlagClipSet; |
| 339 | setScissorFromClip(); |
| 340 | } |
| 341 | return clipped; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 344 | /////////////////////////////////////////////////////////////////////////////// |
| 345 | // Drawing |
| 346 | /////////////////////////////////////////////////////////////////////////////// |
| 347 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 348 | void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 349 | const Rect& clip = mSnapshot->clipRect; |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 350 | drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 351 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 352 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 353 | void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) { |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 354 | SkXfermode::Mode mode; |
| 355 | |
| 356 | const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); |
| 357 | if (!isMode) { |
| 358 | // Assume SRC_OVER |
| 359 | mode = SkXfermode::kSrcOver_Mode; |
| 360 | } |
| 361 | |
| 362 | // Skia draws using the color's alpha channel if < 255 |
| 363 | // Otherwise, it uses the paint's alpha |
| 364 | int color = p->getColor(); |
| 365 | if (((color >> 24) & 0xFF) == 255) { |
| 366 | color |= p->getAlpha() << 24; |
| 367 | } |
| 368 | |
| 369 | drawColorRect(left, top, right, bottom, color, mode); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 370 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 371 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 372 | void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, |
| 373 | int color, SkXfermode::Mode mode) { |
| 374 | const int alpha = (color >> 24) & 0xFF; |
| 375 | const bool blend = alpha < 255 || mode != SkXfermode::kSrcOver_Mode; |
| 376 | |
| 377 | const GLfloat a = alpha / 255.0f; |
| 378 | const GLfloat r = ((color >> 16) & 0xFF) / 255.0f; |
| 379 | const GLfloat g = ((color >> 8) & 0xFF) / 255.0f; |
| 380 | const GLfloat b = ((color ) & 0xFF) / 255.0f; |
| 381 | |
| 382 | if (blend) { |
| 383 | glEnable(GL_BLEND); |
| 384 | glBlendFunc(gBlends[mode].src, gBlends[mode].dst); |
| 385 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 386 | |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 387 | mModelView.loadTranslate(left, top, 0.0f); |
| 388 | mModelView.scale(right - left, bottom - top, 1.0f); |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 389 | |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 390 | mDrawColorShader->use(&mOrthoMatrix[0], &mModelView.data[0], &mSnapshot->transform.data[0]); |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 391 | |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 392 | const GLvoid* p = &gDrawColorVertices[0].position[0]; |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 393 | |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 394 | glEnableVertexAttribArray(mDrawColorShader->position); |
| 395 | glVertexAttribPointer(mDrawColorShader->position, 2, GL_FLOAT, GL_FALSE, |
| 396 | gDrawColorVertexStride, p); |
| 397 | glVertexAttrib4f(mDrawColorShader->color, r, g, b, a); |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 398 | |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 399 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawColorVertexCount); |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 400 | |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 401 | glDisableVertexAttribArray(mDrawColorShader->position); |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 402 | |
| 403 | if (blend) { |
| 404 | glDisable(GL_BLEND); |
| 405 | } |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 408 | void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, |
| 409 | GLuint texture, float alpha) { |
| 410 | mModelView.loadTranslate(left, top, 0.0f); |
| 411 | mModelView.scale(right - left, bottom - top, 1.0f); |
| 412 | |
| 413 | mDrawTextureShader->use(&mOrthoMatrix[0], &mModelView.data[0], &mSnapshot->transform.data[0]); |
| 414 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 415 | // TODO Correctly set the blend function, based on texture format and xfermode |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 416 | glEnable(GL_BLEND); |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 417 | // For not pre-multiplied sources |
| 418 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 419 | |
| 420 | glBindTexture(GL_TEXTURE_2D, texture); |
| 421 | |
| 422 | glActiveTexture(GL_TEXTURE0); |
| 423 | glUniform1i(mDrawTextureShader->sampler, 0); |
| 424 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 425 | const GLvoid* p = &mDrawTextureVertices[0].position[0]; |
| 426 | const GLvoid* t = &mDrawTextureVertices[0].texture[0]; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 427 | |
| 428 | glEnableVertexAttribArray(mDrawTextureShader->position); |
| 429 | glVertexAttribPointer(mDrawTextureShader->position, 2, GL_FLOAT, GL_FALSE, |
| 430 | gDrawTextureVertexStride, p); |
| 431 | |
| 432 | glEnableVertexAttribArray(mDrawTextureShader->texCoords); |
| 433 | glVertexAttribPointer(mDrawTextureShader->texCoords, 2, GL_FLOAT, GL_FALSE, |
| 434 | gDrawTextureVertexStride, t); |
| 435 | |
| 436 | glVertexAttrib4f(mDrawTextureShader->color, 1.0f, 1.0f, 1.0f, alpha); |
| 437 | |
| 438 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount); |
| 439 | |
| 440 | glDisableVertexAttribArray(mDrawTextureShader->position); |
| 441 | glDisableVertexAttribArray(mDrawTextureShader->texCoords); |
| 442 | |
| 443 | glBindTexture(GL_TEXTURE_2D, 0); |
| 444 | glDisable(GL_BLEND); |
| 445 | } |
| 446 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 447 | void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { |
| 448 | mDrawTextureVertices[0].texture[0] = u1; |
| 449 | mDrawTextureVertices[0].texture[1] = v2; |
| 450 | mDrawTextureVertices[1].texture[0] = u2; |
| 451 | mDrawTextureVertices[1].texture[1] = v2; |
| 452 | mDrawTextureVertices[2].texture[0] = u1; |
| 453 | mDrawTextureVertices[2].texture[1] = v1; |
| 454 | mDrawTextureVertices[3].texture[0] = u2; |
| 455 | mDrawTextureVertices[3].texture[1] = v1; |
| 456 | } |
| 457 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 458 | }; // namespace uirenderer |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 459 | }; // namespace android |