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 | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 29 | #include <ui/Rect.h> |
| 30 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 31 | #include "OpenGLRenderer.h" |
Romain Guy | 0fe478e | 2010-11-08 12:08:41 -0800 | [diff] [blame] | 32 | #include "DisplayListRenderer.h" |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 33 | |
| 34 | namespace android { |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 35 | namespace uirenderer { |
| 36 | |
| 37 | /////////////////////////////////////////////////////////////////////////////// |
| 38 | // Defines |
| 39 | /////////////////////////////////////////////////////////////////////////////// |
| 40 | |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 41 | #define RAD_TO_DEG (180.0f / 3.14159265f) |
| 42 | #define MIN_ANGLE 0.001f |
| 43 | |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 44 | // TODO: This should be set in properties |
| 45 | #define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH) |
| 46 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 47 | /////////////////////////////////////////////////////////////////////////////// |
| 48 | // Globals |
| 49 | /////////////////////////////////////////////////////////////////////////////// |
| 50 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Structure mapping Skia xfermodes to OpenGL blending factors. |
| 53 | */ |
| 54 | struct Blender { |
| 55 | SkXfermode::Mode mode; |
| 56 | GLenum src; |
| 57 | GLenum dst; |
| 58 | }; // struct Blender |
| 59 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 60 | // In this array, the index of each Blender equals the value of the first |
| 61 | // entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode] |
| 62 | static const Blender gBlends[] = { |
| 63 | { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO }, |
| 64 | { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO }, |
| 65 | { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE }, |
| 66 | { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, |
| 67 | { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, |
| 68 | { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO }, |
| 69 | { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA }, |
| 70 | { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, |
| 71 | { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, |
| 72 | { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, |
| 73 | { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, |
| 74 | { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA } |
| 75 | }; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 76 | |
Romain Guy | 87a7657 | 2010-09-13 18:11:21 -0700 | [diff] [blame] | 77 | // This array contains the swapped version of each SkXfermode. For instance |
| 78 | // this array's SrcOver blending mode is actually DstOver. You can refer to |
| 79 | // createLayer() for more information on the purpose of this array. |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 80 | static const Blender gBlendsSwap[] = { |
| 81 | { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO }, |
| 82 | { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE }, |
| 83 | { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO }, |
| 84 | { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, |
| 85 | { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, |
| 86 | { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA }, |
| 87 | { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO }, |
| 88 | { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, |
| 89 | { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, |
| 90 | { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, |
| 91 | { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, |
| 92 | { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA } |
| 93 | }; |
| 94 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 95 | static const GLenum gTextureUnits[] = { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 96 | GL_TEXTURE0, |
| 97 | GL_TEXTURE1, |
| 98 | GL_TEXTURE2 |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 101 | /////////////////////////////////////////////////////////////////////////////// |
| 102 | // Constructors/destructor |
| 103 | /////////////////////////////////////////////////////////////////////////////// |
| 104 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 105 | OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 106 | mShader = NULL; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 107 | mColorFilter = NULL; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 108 | mHasShadow = false; |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 109 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 110 | memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices)); |
| 111 | |
Romain Guy | ae5575b | 2010-07-29 18:48:04 -0700 | [diff] [blame] | 112 | mFirstSnapshot = new Snapshot; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 115 | OpenGLRenderer::~OpenGLRenderer() { |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 116 | // The context has already been destroyed at this point, do not call |
| 117 | // GL APIs. All GL state should be kept in Caches.h |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 120 | /////////////////////////////////////////////////////////////////////////////// |
| 121 | // Setup |
| 122 | /////////////////////////////////////////////////////////////////////////////// |
| 123 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 124 | void OpenGLRenderer::setViewport(int width, int height) { |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 125 | glViewport(0, 0, width, height); |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 126 | mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 127 | |
| 128 | mWidth = width; |
| 129 | mHeight = height; |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 130 | |
| 131 | mFirstSnapshot->height = height; |
| 132 | mFirstSnapshot->viewport.set(0, 0, width, height); |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 133 | |
| 134 | mDirtyClip = false; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 137 | void OpenGLRenderer::prepare(bool opaque) { |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 138 | mCaches.clearGarbage(); |
| 139 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 140 | mSnapshot = new Snapshot(mFirstSnapshot, |
| 141 | SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 142 | mSaveCount = 1; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 143 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 144 | glViewport(0, 0, mWidth, mHeight); |
| 145 | |
Romain Guy | d90f23e | 2010-09-09 11:47:54 -0700 | [diff] [blame] | 146 | glDisable(GL_DITHER); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 147 | |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 148 | if (!opaque) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 149 | glDisable(GL_SCISSOR_TEST); |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 150 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 151 | glClear(GL_COLOR_BUFFER_BIT); |
| 152 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 153 | |
Chet Haase | 0d20083 | 2010-11-05 15:36:16 -0700 | [diff] [blame] | 154 | glEnable(GL_SCISSOR_TEST); |
| 155 | glScissor(0, 0, mWidth, mHeight); |
Romain Guy | b82da65 | 2010-07-30 11:36:12 -0700 | [diff] [blame] | 156 | mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 159 | void OpenGLRenderer::finish() { |
| 160 | #if DEBUG_OPENGL |
| 161 | GLenum status = GL_NO_ERROR; |
| 162 | while ((status = glGetError()) != GL_NO_ERROR) { |
| 163 | LOGD("GL error from OpenGLRenderer: 0x%x", status); |
| 164 | } |
| 165 | #endif |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 166 | #if DEBUG_MEMORY_USAGE |
| 167 | mCaches.dumpMemoryUsage(); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 168 | #else |
| 169 | if (mCaches.getDebugLevel() & kDebugMemory) { |
| 170 | mCaches.dumpMemoryUsage(); |
| 171 | } |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 172 | #endif |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 175 | void OpenGLRenderer::acquireContext() { |
| 176 | if (mCaches.currentProgram) { |
| 177 | if (mCaches.currentProgram->isInUse()) { |
| 178 | mCaches.currentProgram->remove(); |
| 179 | mCaches.currentProgram = NULL; |
| 180 | } |
| 181 | } |
Romain Guy | 50c0f09 | 2010-10-19 11:42:22 -0700 | [diff] [blame] | 182 | mCaches.unbindMeshBuffer(); |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void OpenGLRenderer::releaseContext() { |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 186 | glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight()); |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 187 | |
| 188 | glEnable(GL_SCISSOR_TEST); |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 189 | dirtyClip(); |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 190 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 191 | glDisable(GL_DITHER); |
| 192 | |
| 193 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 194 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 195 | |
Romain Guy | 50c0f09 | 2010-10-19 11:42:22 -0700 | [diff] [blame] | 196 | mCaches.blend = true; |
| 197 | glEnable(GL_BLEND); |
| 198 | glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode); |
| 199 | glBlendEquation(GL_FUNC_ADD); |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 202 | /////////////////////////////////////////////////////////////////////////////// |
| 203 | // State management |
| 204 | /////////////////////////////////////////////////////////////////////////////// |
| 205 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 206 | int OpenGLRenderer::getSaveCount() const { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 207 | return mSaveCount; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | int OpenGLRenderer::save(int flags) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 211 | return saveSnapshot(flags); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void OpenGLRenderer::restore() { |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 215 | if (mSaveCount > 1) { |
| 216 | restoreSnapshot(); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 217 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void OpenGLRenderer::restoreToCount(int saveCount) { |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 221 | if (saveCount < 1) saveCount = 1; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 222 | |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 223 | while (mSaveCount > saveCount) { |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 224 | restoreSnapshot(); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 225 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 228 | int OpenGLRenderer::saveSnapshot(int flags) { |
| 229 | mSnapshot = new Snapshot(mSnapshot, flags); |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 230 | return mSaveCount++; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | bool OpenGLRenderer::restoreSnapshot() { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 234 | bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 235 | bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer; |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 236 | bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 237 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 238 | sp<Snapshot> current = mSnapshot; |
| 239 | sp<Snapshot> previous = mSnapshot->previous; |
| 240 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 241 | if (restoreOrtho) { |
| 242 | Rect& r = previous->viewport; |
| 243 | glViewport(r.left, r.top, r.right, r.bottom); |
| 244 | mOrthoMatrix.load(current->orthoMatrix); |
| 245 | } |
| 246 | |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 247 | mSaveCount--; |
| 248 | mSnapshot = previous; |
| 249 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 250 | if (restoreClip) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 251 | dirtyClip(); |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 252 | } |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 253 | |
Romain Guy | 5ec9924 | 2010-11-03 16:19:08 -0700 | [diff] [blame] | 254 | if (restoreLayer) { |
| 255 | composeLayer(current, previous); |
| 256 | } |
| 257 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 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, |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 266 | SkPaint* p, int flags) { |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 267 | const GLuint previousFbo = mSnapshot->fbo; |
| 268 | const int count = saveSnapshot(flags); |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 269 | |
Romain Guy | e45362c | 2010-11-03 19:58:32 -0700 | [diff] [blame] | 270 | if (!mSnapshot->invisible) { |
| 271 | int alpha = 255; |
| 272 | SkXfermode::Mode mode; |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 273 | |
Romain Guy | e45362c | 2010-11-03 19:58:32 -0700 | [diff] [blame] | 274 | if (p) { |
| 275 | alpha = p->getAlpha(); |
| 276 | if (!mCaches.extensions.hasFramebufferFetch()) { |
| 277 | const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); |
| 278 | if (!isMode) { |
| 279 | // Assume SRC_OVER |
| 280 | mode = SkXfermode::kSrcOver_Mode; |
| 281 | } |
| 282 | } else { |
| 283 | mode = getXfermode(p->getXfermode()); |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 284 | } |
| 285 | } else { |
Romain Guy | e45362c | 2010-11-03 19:58:32 -0700 | [diff] [blame] | 286 | mode = SkXfermode::kSrcOver_Mode; |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 287 | } |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 288 | |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 289 | createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo); |
| 290 | } |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 291 | |
| 292 | return count; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom, |
| 296 | int alpha, int flags) { |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 297 | if (alpha >= 255 - ALPHA_THRESHOLD) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 298 | return saveLayer(left, top, right, bottom, NULL, flags); |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 299 | } else { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 300 | SkPaint paint; |
| 301 | paint.setAlpha(alpha); |
| 302 | return saveLayer(left, top, right, bottom, &paint, flags); |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 303 | } |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 304 | } |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 305 | |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 306 | /** |
| 307 | * Layers are viewed by Skia are slightly different than layers in image editing |
| 308 | * programs (for instance.) When a layer is created, previously created layers |
| 309 | * and the frame buffer still receive every drawing command. For instance, if a |
| 310 | * layer is created and a shape intersecting the bounds of the layers and the |
| 311 | * framebuffer is draw, the shape will be drawn on both (unless the layer was |
| 312 | * created with the SkCanvas::kClipToLayer_SaveFlag flag.) |
| 313 | * |
| 314 | * A way to implement layers is to create an FBO for each layer, backed by an RGBA |
| 315 | * texture. Unfortunately, this is inefficient as it requires every primitive to |
| 316 | * be drawn n + 1 times, where n is the number of active layers. In practice this |
| 317 | * means, for every primitive: |
| 318 | * - Switch active frame buffer |
| 319 | * - Change viewport, clip and projection matrix |
| 320 | * - Issue the drawing |
| 321 | * |
| 322 | * Switching rendering target n + 1 times per drawn primitive is extremely costly. |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 323 | * To avoid this, layers are implemented in a different way here, at least in the |
| 324 | * general case. FBOs are used, as an optimization, when the "clip to layer" flag |
| 325 | * is set. When this flag is set we can redirect all drawing operations into a |
| 326 | * single FBO. |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 327 | * |
| 328 | * This implementation relies on the frame buffer being at least RGBA 8888. When |
| 329 | * a layer is created, only a texture is created, not an FBO. The content of the |
| 330 | * 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] | 331 | * 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] | 332 | * buffer and drawing continues as normal. This technique therefore treats the |
| 333 | * frame buffer as a scratch buffer for the layers. |
| 334 | * |
| 335 | * To compose the layers back onto the frame buffer, each layer texture |
| 336 | * (containing the original frame buffer data) is drawn as a simple quad over |
| 337 | * the frame buffer. The trick is that the quad is set as the composition |
| 338 | * destination in the blending equation, and the frame buffer becomes the source |
| 339 | * of the composition. |
| 340 | * |
| 341 | * Drawing layers with an alpha value requires an extra step before composition. |
| 342 | * An empty quad is drawn over the layer's region in the frame buffer. This quad |
| 343 | * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the |
| 344 | * quad is used to multiply the colors in the frame buffer. This is achieved by |
| 345 | * changing the GL blend functions for the GL_FUNC_ADD blend equation to |
| 346 | * GL_ZERO, GL_SRC_ALPHA. |
| 347 | * |
| 348 | * Because glCopyTexImage2D() can be slow, an alternative implementation might |
| 349 | * be use to draw a single clipped layer. The implementation described above |
| 350 | * is correct in every case. |
Romain Guy | 87a7657 | 2010-09-13 18:11:21 -0700 | [diff] [blame] | 351 | * |
| 352 | * (1) The frame buffer is actually not cleared right away. To allow the GPU |
| 353 | * to potentially optimize series of calls to glCopyTexImage2D, the frame |
| 354 | * buffer is left untouched until the first drawing operation. Only when |
| 355 | * something actually gets drawn are the layers regions cleared. |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 356 | */ |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 357 | bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top, |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 358 | float right, float bottom, int alpha, SkXfermode::Mode mode, |
| 359 | int flags, GLuint previousFbo) { |
| 360 | LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 361 | LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize()); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 362 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 363 | const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag; |
| 364 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 365 | // Window coordinates of the layer |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 366 | Rect bounds(left, top, right, bottom); |
Romain Guy | ae88e5e | 2010-10-22 17:49:18 -0700 | [diff] [blame] | 367 | if (fboLayer) { |
| 368 | // Clear the previous layer regions before we change the viewport |
| 369 | clearLayerRegions(); |
| 370 | } else { |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 371 | mSnapshot->transform->mapRect(bounds); |
Romain Guy | ae51759 | 2010-10-22 10:40:27 -0700 | [diff] [blame] | 372 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 373 | // Layers only make sense if they are in the framebuffer's bounds |
Romain Guy | 58ae6db | 2010-10-22 11:16:37 -0700 | [diff] [blame] | 374 | bounds.intersect(*snapshot->clipRect); |
Romain Guy | ae51759 | 2010-10-22 10:40:27 -0700 | [diff] [blame] | 375 | |
Romain Guy | ae88e5e | 2010-10-22 17:49:18 -0700 | [diff] [blame] | 376 | // We cannot work with sub-pixels in this case |
| 377 | bounds.snapToPixelBoundaries(); |
| 378 | |
Romain Guy | ae51759 | 2010-10-22 10:40:27 -0700 | [diff] [blame] | 379 | // When the layer is not an FBO, we may use glCopyTexImage so we |
| 380 | // need to make sure the layer does not extend outside the bounds |
| 381 | // of the framebuffer |
| 382 | bounds.intersect(snapshot->previous->viewport); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 383 | } |
Romain Guy | bf43411 | 2010-09-16 14:40:17 -0700 | [diff] [blame] | 384 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 385 | if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize || |
| 386 | bounds.getHeight() > mCaches.maxTextureSize) { |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 387 | snapshot->invisible = true; |
| 388 | } else { |
Romain Guy | e45362c | 2010-11-03 19:58:32 -0700 | [diff] [blame] | 389 | snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer); |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | // Bail out if we won't draw in this snapshot |
| 393 | if (snapshot->invisible) { |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 394 | return false; |
| 395 | } |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 396 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 397 | glActiveTexture(gTextureUnits[0]); |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 398 | Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight()); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 399 | if (!layer) { |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 400 | return false; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 403 | layer->mode = mode; |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 404 | layer->alpha = alpha; |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 405 | layer->layer.set(bounds); |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 406 | layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height), |
| 407 | bounds.getWidth() / float(layer->width), 0.0f); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 408 | |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 409 | // Save the layer in the snapshot |
| 410 | snapshot->flags |= Snapshot::kFlagIsLayer; |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 411 | snapshot->layer = layer; |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 412 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 413 | if (fboLayer) { |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 414 | return createFboLayer(layer, bounds, snapshot, previousFbo); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 415 | } else { |
| 416 | // Copy the framebuffer into the layer |
| 417 | glBindTexture(GL_TEXTURE_2D, layer->texture); |
| 418 | |
Romain Guy | ae88e5e | 2010-10-22 17:49:18 -0700 | [diff] [blame] | 419 | if (layer->empty) { |
| 420 | glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, |
| 421 | snapshot->height - bounds.bottom, layer->width, layer->height, 0); |
| 422 | layer->empty = false; |
| 423 | } else { |
| 424 | glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left, |
| 425 | snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight()); |
| 426 | } |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 427 | |
| 428 | // Enqueue the buffer coordinates to clear the corresponding region later |
| 429 | mLayers.push(new Rect(bounds)); |
| 430 | } |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 431 | |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 432 | return true; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 435 | bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot, |
| 436 | GLuint previousFbo) { |
| 437 | layer->fbo = mCaches.fboCache.get(); |
| 438 | |
| 439 | #if RENDER_LAYERS_AS_REGIONS |
| 440 | snapshot->region = &snapshot->layer->region; |
| 441 | snapshot->flags |= Snapshot::kFlagFboTarget; |
| 442 | #endif |
| 443 | |
| 444 | Rect clip(bounds); |
| 445 | snapshot->transform->mapRect(clip); |
| 446 | clip.intersect(*snapshot->clipRect); |
| 447 | clip.snapToPixelBoundaries(); |
| 448 | clip.intersect(snapshot->previous->viewport); |
| 449 | |
| 450 | mat4 inverse; |
| 451 | inverse.loadInverse(*mSnapshot->transform); |
| 452 | |
| 453 | inverse.mapRect(clip); |
| 454 | clip.snapToPixelBoundaries(); |
| 455 | clip.intersect(bounds); |
Romain Guy | 5ec9924 | 2010-11-03 16:19:08 -0700 | [diff] [blame] | 456 | clip.translate(-bounds.left, -bounds.top); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 457 | |
| 458 | snapshot->flags |= Snapshot::kFlagIsFboLayer; |
| 459 | snapshot->fbo = layer->fbo; |
| 460 | snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f); |
| 461 | //snapshot->resetClip(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight()); |
| 462 | snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom); |
| 463 | snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight()); |
| 464 | snapshot->height = bounds.getHeight(); |
| 465 | snapshot->flags |= Snapshot::kFlagDirtyOrtho; |
| 466 | snapshot->orthoMatrix.load(mOrthoMatrix); |
| 467 | |
| 468 | // Bind texture to FBO |
| 469 | glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo); |
| 470 | glBindTexture(GL_TEXTURE_2D, layer->texture); |
| 471 | |
| 472 | // Initialize the texture if needed |
| 473 | if (layer->empty) { |
| 474 | layer->empty = false; |
| 475 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0, |
| 476 | GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
| 477 | } |
| 478 | |
| 479 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
| 480 | layer->texture, 0); |
| 481 | |
| 482 | #if DEBUG_LAYERS_AS_REGIONS |
| 483 | GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 484 | if (status != GL_FRAMEBUFFER_COMPLETE) { |
| 485 | LOGE("Framebuffer incomplete (GL error code 0x%x)", status); |
| 486 | |
| 487 | glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); |
| 488 | glDeleteTextures(1, &layer->texture); |
| 489 | mCaches.fboCache.put(layer->fbo); |
| 490 | |
| 491 | delete layer; |
| 492 | |
| 493 | return false; |
| 494 | } |
| 495 | #endif |
| 496 | |
| 497 | // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering |
| 498 | glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f, |
| 499 | clip.getWidth() + 2.0f, clip.getHeight() + 2.0f); |
| 500 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 501 | glClear(GL_COLOR_BUFFER_BIT); |
| 502 | |
| 503 | dirtyClip(); |
| 504 | |
| 505 | // Change the ortho projection |
| 506 | glViewport(0, 0, bounds.getWidth(), bounds.getHeight()); |
| 507 | mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f); |
| 508 | |
| 509 | return true; |
| 510 | } |
| 511 | |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 512 | /** |
| 513 | * Read the documentation of createLayer() before doing anything in this method. |
| 514 | */ |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 515 | void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) { |
| 516 | if (!current->layer) { |
| 517 | LOGE("Attempting to compose a layer that does not exist"); |
| 518 | return; |
| 519 | } |
| 520 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 521 | const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer; |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 522 | |
| 523 | if (fboLayer) { |
| 524 | // Unbind current FBO and restore previous one |
| 525 | glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); |
| 526 | } |
| 527 | |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 528 | Layer* layer = current->layer; |
| 529 | const Rect& rect = layer->layer; |
| 530 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 531 | if (!fboLayer && layer->alpha < 255) { |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 532 | drawColorRect(rect.left, rect.top, rect.right, rect.bottom, |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 533 | layer->alpha << 24, SkXfermode::kDstIn_Mode, true); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 534 | // Required below, composeLayerRect() will divide by 255 |
| 535 | layer->alpha = 255; |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 538 | mCaches.unbindMeshBuffer(); |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 539 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 540 | glActiveTexture(gTextureUnits[0]); |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 541 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 542 | // When the layer is stored in an FBO, we can save a bit of fillrate by |
| 543 | // drawing only the dirty region |
| 544 | if (fboLayer) { |
| 545 | dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform); |
| 546 | composeLayerRegion(layer, rect); |
| 547 | } else { |
| 548 | dirtyLayer(rect.left, rect.top, rect.right, rect.bottom); |
| 549 | composeLayerRect(layer, rect, true); |
| 550 | } |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 551 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 552 | if (fboLayer) { |
| 553 | // Detach the texture from the FBO |
| 554 | glBindFramebuffer(GL_FRAMEBUFFER, current->fbo); |
| 555 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); |
| 556 | glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); |
| 557 | |
| 558 | // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed |
| 559 | mCaches.fboCache.put(current->fbo); |
| 560 | } |
| 561 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 562 | dirtyClip(); |
| 563 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 564 | // Failing to add the layer to the cache should happen only if the layer is too large |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 565 | if (!mCaches.layerCache.put(layer)) { |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 566 | LAYER_LOGD("Deleting layer"); |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 567 | glDeleteTextures(1, &layer->texture); |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 568 | delete layer; |
| 569 | } |
| 570 | } |
| 571 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 572 | void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) { |
| 573 | const Rect& texCoords = layer->texCoords; |
| 574 | resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom); |
| 575 | |
| 576 | drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture, |
| 577 | layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0], |
| 578 | &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap); |
| 579 | |
| 580 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
| 581 | } |
| 582 | |
| 583 | void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) { |
| 584 | #if RENDER_LAYERS_AS_REGIONS |
| 585 | if (layer->region.isRect()) { |
| 586 | composeLayerRect(layer, rect); |
| 587 | layer->region.clear(); |
| 588 | return; |
| 589 | } |
| 590 | |
| 591 | if (!layer->region.isEmpty()) { |
| 592 | size_t count; |
| 593 | const android::Rect* rects = layer->region.getArray(&count); |
| 594 | |
| 595 | setupDraw(); |
| 596 | |
| 597 | ProgramDescription description; |
| 598 | description.hasTexture = true; |
| 599 | |
| 600 | const float alpha = layer->alpha / 255.0f; |
| 601 | const bool setColor = description.setColor(alpha, alpha, alpha, alpha); |
| 602 | chooseBlending(layer->blend || layer->alpha < 255, layer->mode, description, false); |
| 603 | |
| 604 | useProgram(mCaches.programCache.get(description)); |
| 605 | |
| 606 | // Texture |
| 607 | bindTexture(layer->texture); |
| 608 | glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0); |
| 609 | |
| 610 | // Always premultiplied |
| 611 | if (setColor) { |
| 612 | mCaches.currentProgram->setColor(alpha, alpha, alpha, alpha); |
| 613 | } |
| 614 | |
| 615 | // Mesh |
| 616 | int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords"); |
| 617 | glEnableVertexAttribArray(texCoordsSlot); |
| 618 | |
| 619 | mModelView.loadIdentity(); |
| 620 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); |
| 621 | |
| 622 | const float texX = 1.0f / float(layer->width); |
| 623 | const float texY = 1.0f / float(layer->height); |
| 624 | |
| 625 | TextureVertex* mesh = mCaches.getRegionMesh(); |
| 626 | GLsizei numQuads = 0; |
| 627 | |
| 628 | glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, |
| 629 | gMeshStride, &mesh[0].position[0]); |
| 630 | glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, |
| 631 | gMeshStride, &mesh[0].texture[0]); |
| 632 | |
| 633 | for (size_t i = 0; i < count; i++) { |
| 634 | const android::Rect* r = &rects[i]; |
| 635 | |
| 636 | const float u1 = r->left * texX; |
| 637 | const float v1 = (rect.getHeight() - r->top) * texY; |
| 638 | const float u2 = r->right * texX; |
| 639 | const float v2 = (rect.getHeight() - r->bottom) * texY; |
| 640 | |
| 641 | // TODO: Reject quads outside of the clip |
| 642 | TextureVertex::set(mesh++, r->left, r->top, u1, v1); |
| 643 | TextureVertex::set(mesh++, r->right, r->top, u2, v1); |
| 644 | TextureVertex::set(mesh++, r->left, r->bottom, u1, v2); |
| 645 | TextureVertex::set(mesh++, r->right, r->bottom, u2, v2); |
| 646 | |
| 647 | numQuads++; |
| 648 | |
| 649 | if (numQuads >= REGION_MESH_QUAD_COUNT) { |
| 650 | glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL); |
| 651 | numQuads = 0; |
| 652 | mesh = mCaches.getRegionMesh(); |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | if (numQuads > 0) { |
| 657 | glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL); |
| 658 | } |
| 659 | |
| 660 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
| 661 | glDisableVertexAttribArray(texCoordsSlot); |
| 662 | |
| 663 | #if DEBUG_LAYERS_AS_REGIONS |
| 664 | uint32_t colors[] = { |
| 665 | 0x7fff0000, 0x7f00ff00, |
| 666 | 0x7f0000ff, 0x7fff00ff, |
| 667 | }; |
| 668 | |
| 669 | int offset = 0; |
| 670 | int32_t top = rects[0].top; |
| 671 | int i = 0; |
| 672 | |
| 673 | for (size_t i = 0; i < count; i++) { |
| 674 | if (top != rects[i].top) { |
| 675 | offset ^= 0x2; |
| 676 | top = rects[i].top; |
| 677 | } |
| 678 | |
| 679 | Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom); |
| 680 | drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)], |
| 681 | SkXfermode::kSrcOver_Mode); |
| 682 | } |
| 683 | #endif |
| 684 | |
| 685 | layer->region.clear(); |
| 686 | } |
| 687 | #else |
| 688 | composeLayerRect(layer, rect); |
| 689 | #endif |
| 690 | } |
| 691 | |
| 692 | void OpenGLRenderer::dirtyLayer(const float left, const float top, |
| 693 | const float right, const float bottom, const mat4 transform) { |
| 694 | #if RENDER_LAYERS_AS_REGIONS |
| 695 | if ((mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region) { |
| 696 | Rect bounds(left, top, right, bottom); |
| 697 | transform.mapRect(bounds); |
| 698 | bounds.intersect(*mSnapshot->clipRect); |
| 699 | bounds.snapToPixelBoundaries(); |
| 700 | |
| 701 | android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom); |
| 702 | if (!dirty.isEmpty()) { |
| 703 | mSnapshot->region->orSelf(dirty); |
| 704 | } |
| 705 | } |
| 706 | #endif |
| 707 | } |
| 708 | |
| 709 | void OpenGLRenderer::dirtyLayer(const float left, const float top, |
| 710 | const float right, const float bottom) { |
| 711 | #if RENDER_LAYERS_AS_REGIONS |
| 712 | if ((mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region) { |
| 713 | Rect bounds(left, top, right, bottom); |
| 714 | bounds.intersect(*mSnapshot->clipRect); |
| 715 | bounds.snapToPixelBoundaries(); |
| 716 | |
| 717 | android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom); |
| 718 | if (!dirty.isEmpty()) { |
| 719 | mSnapshot->region->orSelf(dirty); |
| 720 | } |
| 721 | } |
| 722 | #endif |
| 723 | } |
| 724 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 725 | void OpenGLRenderer::setupDraw() { |
| 726 | clearLayerRegions(); |
| 727 | if (mDirtyClip) { |
| 728 | setScissorFromClip(); |
| 729 | } |
| 730 | } |
| 731 | |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 732 | void OpenGLRenderer::clearLayerRegions() { |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 733 | if (mLayers.size() == 0 || mSnapshot->invisible) return; |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 734 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 735 | Rect clipRect(*mSnapshot->clipRect); |
| 736 | clipRect.snapToPixelBoundaries(); |
| 737 | |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 738 | for (uint32_t i = 0; i < mLayers.size(); i++) { |
| 739 | Rect* bounds = mLayers.itemAt(i); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 740 | if (clipRect.intersects(*bounds)) { |
| 741 | // Clear the framebuffer where the layer will draw |
| 742 | glScissor(bounds->left, mSnapshot->height - bounds->bottom, |
| 743 | bounds->getWidth(), bounds->getHeight()); |
| 744 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 745 | glClear(GL_COLOR_BUFFER_BIT); |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 746 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 747 | // Restore the clip |
| 748 | dirtyClip(); |
| 749 | } |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 750 | |
| 751 | delete bounds; |
| 752 | } |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 753 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 754 | mLayers.clear(); |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 755 | } |
| 756 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 757 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 758 | // Transforms |
| 759 | /////////////////////////////////////////////////////////////////////////////// |
| 760 | |
| 761 | void OpenGLRenderer::translate(float dx, float dy) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 762 | mSnapshot->transform->translate(dx, dy, 0.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | void OpenGLRenderer::rotate(float degrees) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 766 | mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | void OpenGLRenderer::scale(float sx, float sy) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 770 | mSnapshot->transform->scale(sx, sy, 1.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | void OpenGLRenderer::setMatrix(SkMatrix* matrix) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 774 | mSnapshot->transform->load(*matrix); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Romain Guy | 41030da | 2010-10-13 13:40:37 -0700 | [diff] [blame] | 777 | const float* OpenGLRenderer::getMatrix() const { |
Romain Guy | 99bcdc5 | 2010-10-13 15:17:00 -0700 | [diff] [blame] | 778 | if (mSnapshot->fbo != 0) { |
| 779 | return &mSnapshot->transform->data[0]; |
| 780 | } |
| 781 | return &mIdentity.data[0]; |
Romain Guy | 41030da | 2010-10-13 13:40:37 -0700 | [diff] [blame] | 782 | } |
| 783 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 784 | void OpenGLRenderer::getMatrix(SkMatrix* matrix) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 785 | mSnapshot->transform->copyTo(*matrix); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { |
Romain Guy | e5ebcb0 | 2010-10-15 13:57:28 -0700 | [diff] [blame] | 789 | SkMatrix transform; |
| 790 | mSnapshot->transform->copyTo(transform); |
| 791 | transform.preConcat(*matrix); |
| 792 | mSnapshot->transform->load(transform); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | /////////////////////////////////////////////////////////////////////////////// |
| 796 | // Clipping |
| 797 | /////////////////////////////////////////////////////////////////////////////// |
| 798 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 799 | void OpenGLRenderer::setScissorFromClip() { |
Romain Guy | e5ebcb0 | 2010-10-15 13:57:28 -0700 | [diff] [blame] | 800 | Rect clip(*mSnapshot->clipRect); |
| 801 | clip.snapToPixelBoundaries(); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 802 | glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight()); |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 803 | mDirtyClip = false; |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | const Rect& OpenGLRenderer::getClipBounds() { |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 807 | return mSnapshot->getLocalClip(); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 810 | bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) { |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 811 | if (mSnapshot->invisible) { |
| 812 | return true; |
| 813 | } |
| 814 | |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 815 | Rect r(left, top, right, bottom); |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 816 | mSnapshot->transform->mapRect(r); |
Romain Guy | d2a1ff0 | 2010-10-14 14:46:44 -0700 | [diff] [blame] | 817 | r.snapToPixelBoundaries(); |
| 818 | |
| 819 | Rect clipRect(*mSnapshot->clipRect); |
| 820 | clipRect.snapToPixelBoundaries(); |
| 821 | |
| 822 | return !clipRect.intersects(r); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 825 | bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) { |
| 826 | bool clipped = mSnapshot->clip(left, top, right, bottom, op); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 827 | if (clipped) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 828 | dirtyClip(); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 829 | } |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 830 | return !mSnapshot->clipRect->isEmpty(); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 831 | } |
| 832 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 833 | /////////////////////////////////////////////////////////////////////////////// |
| 834 | // Drawing |
| 835 | /////////////////////////////////////////////////////////////////////////////// |
| 836 | |
Romain Guy | 0fe478e | 2010-11-08 12:08:41 -0800 | [diff] [blame] | 837 | void OpenGLRenderer::drawDisplayList(DisplayList* displayList) { |
| 838 | // All the usual checks and setup operations (quickReject, setupDraw, etc.) |
| 839 | // will be performed by the display list itself |
| 840 | if (displayList) { |
| 841 | displayList->replay(*this); |
| 842 | } |
| 843 | } |
| 844 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 845 | void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 846 | const float right = left + bitmap->width(); |
| 847 | const float bottom = top + bitmap->height(); |
| 848 | |
| 849 | if (quickReject(left, top, right, bottom)) { |
| 850 | return; |
| 851 | } |
| 852 | |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 853 | glActiveTexture(GL_TEXTURE0); |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 854 | Texture* texture = mCaches.textureCache.get(bitmap); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 855 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 856 | const AutoTexture autoCleanup(texture); |
| 857 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 858 | drawTextureRect(left, top, right, bottom, texture, paint); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 859 | } |
| 860 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 861 | void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) { |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 862 | Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height()); |
| 863 | const mat4 transform(*matrix); |
| 864 | transform.mapRect(r); |
| 865 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 866 | if (quickReject(r.left, r.top, r.right, r.bottom)) { |
| 867 | return; |
| 868 | } |
| 869 | |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 870 | glActiveTexture(GL_TEXTURE0); |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 871 | Texture* texture = mCaches.textureCache.get(bitmap); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 872 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 873 | const AutoTexture autoCleanup(texture); |
| 874 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 875 | // This could be done in a cheaper way, all we need is pass the matrix |
| 876 | // to the vertex shader. The save/restore is a bit overkill. |
| 877 | save(SkCanvas::kMatrix_SaveFlag); |
| 878 | concatMatrix(matrix); |
| 879 | drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint); |
| 880 | restore(); |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 881 | } |
| 882 | |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 883 | void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, |
| 884 | float srcLeft, float srcTop, float srcRight, float srcBottom, |
| 885 | float dstLeft, float dstTop, float dstRight, float dstBottom, |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 886 | SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 887 | if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) { |
| 888 | return; |
| 889 | } |
| 890 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 891 | glActiveTexture(gTextureUnits[0]); |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 892 | Texture* texture = mCaches.textureCache.get(bitmap); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 893 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 894 | const AutoTexture autoCleanup(texture); |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 895 | setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 896 | |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 897 | const float width = texture->width; |
| 898 | const float height = texture->height; |
| 899 | |
| 900 | const float u1 = srcLeft / width; |
| 901 | const float v1 = srcTop / height; |
| 902 | const float u2 = srcRight / width; |
| 903 | const float v2 = srcBottom / height; |
| 904 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 905 | mCaches.unbindMeshBuffer(); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 906 | resetDrawTextureTexCoords(u1, v1, u2, v2); |
| 907 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 908 | int alpha; |
| 909 | SkXfermode::Mode mode; |
| 910 | getAlphaAndMode(paint, &alpha, &mode); |
| 911 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 912 | if (mSnapshot->transform->isPureTranslate()) { |
| 913 | const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f); |
| 914 | const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f); |
| 915 | |
| 916 | drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop), |
| 917 | texture->id, alpha / 255.0f, mode, texture->blend, |
| 918 | &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], |
| 919 | GL_TRIANGLE_STRIP, gMeshCount, false, true); |
| 920 | } else { |
| 921 | drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f, |
| 922 | mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], |
| 923 | GL_TRIANGLE_STRIP, gMeshCount); |
| 924 | } |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 925 | |
| 926 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
| 927 | } |
| 928 | |
Romain Guy | 4aa9057 | 2010-09-26 18:40:37 -0700 | [diff] [blame] | 929 | void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs, |
Romain Guy | 4bb9420 | 2010-10-12 15:59:26 -0700 | [diff] [blame] | 930 | const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors, |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 931 | float left, float top, float right, float bottom, SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 932 | if (quickReject(left, top, right, bottom)) { |
| 933 | return; |
| 934 | } |
| 935 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 936 | glActiveTexture(gTextureUnits[0]); |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 937 | Texture* texture = mCaches.textureCache.get(bitmap); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 938 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 939 | const AutoTexture autoCleanup(texture); |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 940 | setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 941 | |
| 942 | int alpha; |
| 943 | SkXfermode::Mode mode; |
| 944 | getAlphaAndMode(paint, &alpha, &mode); |
| 945 | |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame] | 946 | const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(), |
Romain Guy | 4bb9420 | 2010-10-12 15:59:26 -0700 | [diff] [blame] | 947 | right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 948 | |
Romain Guy | a5ef39a | 2010-12-03 16:48:20 -0800 | [diff] [blame] | 949 | if (mesh && mesh->verticesCount > 0) { |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 950 | const bool pureTranslate = mSnapshot->transform->isPureTranslate(); |
Romain Guy | a5ef39a | 2010-12-03 16:48:20 -0800 | [diff] [blame] | 951 | #if RENDER_LAYERS_AS_REGIONS |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 952 | // Mark the current layer dirty where we are going to draw the patch |
| 953 | if ((mSnapshot->flags & Snapshot::kFlagFboTarget) && |
| 954 | mSnapshot->region && mesh->hasEmptyQuads) { |
| 955 | const size_t count = mesh->quads.size(); |
| 956 | for (size_t i = 0; i < count; i++) { |
Romain Guy | 8ab4079 | 2010-12-07 13:30:10 -0800 | [diff] [blame^] | 957 | const Rect& bounds = mesh->quads.itemAt(i); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 958 | if (pureTranslate) { |
| 959 | const float x = (int) floorf(bounds.left + 0.5f); |
| 960 | const float y = (int) floorf(bounds.top + 0.5f); |
Romain Guy | 8ab4079 | 2010-12-07 13:30:10 -0800 | [diff] [blame^] | 961 | dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight(), |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 962 | *mSnapshot->transform); |
| 963 | } else { |
| 964 | dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, |
| 965 | *mSnapshot->transform); |
| 966 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 967 | } |
| 968 | } |
Romain Guy | a5ef39a | 2010-12-03 16:48:20 -0800 | [diff] [blame] | 969 | #endif |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 970 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 971 | if (pureTranslate) { |
| 972 | const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); |
| 973 | const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); |
| 974 | |
| 975 | drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f, |
| 976 | mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, |
| 977 | GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer, |
| 978 | true, !mesh->hasEmptyQuads); |
| 979 | } else { |
| 980 | drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, |
| 981 | mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, |
| 982 | GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer, |
| 983 | true, !mesh->hasEmptyQuads); |
| 984 | } |
Romain Guy | 054dc18 | 2010-10-15 17:55:25 -0700 | [diff] [blame] | 985 | } |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 986 | } |
| 987 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 988 | void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) { |
Romain Guy | 7f78b0c | 2010-11-04 14:36:26 -0700 | [diff] [blame] | 989 | // TODO: Should do quickReject for each line |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 990 | if (mSnapshot->invisible) return; |
| 991 | |
Romain Guy | 7f78b0c | 2010-11-04 14:36:26 -0700 | [diff] [blame] | 992 | setupDraw(); |
| 993 | |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 994 | int alpha; |
| 995 | SkXfermode::Mode mode; |
| 996 | getAlphaAndMode(paint, &alpha, &mode); |
| 997 | |
| 998 | uint32_t color = paint->getColor(); |
| 999 | const GLfloat a = alpha / 255.0f; |
| 1000 | const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; |
| 1001 | const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; |
| 1002 | const GLfloat b = a * ((color ) & 0xFF) / 255.0f; |
| 1003 | |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 1004 | const bool isAA = paint->isAntiAlias(); |
| 1005 | if (isAA) { |
| 1006 | GLuint textureUnit = 0; |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1007 | glActiveTexture(gTextureUnits[textureUnit]); |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 1008 | setupTextureAlpha8(mCaches.line.getTexture(), 0, 0, textureUnit, 0.0f, 0.0f, r, g, b, a, |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 1009 | mode, false, true, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, |
| 1010 | mCaches.line.getMeshBuffer()); |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 1011 | } else { |
Romain Guy | 7f78b0c | 2010-11-04 14:36:26 -0700 | [diff] [blame] | 1012 | setupColorRect(0.0f, 0.0f, 1.0f, 1.0f, r, g, b, a, mode, false, true); |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | const float strokeWidth = paint->getStrokeWidth(); |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 1016 | const GLsizei elementsCount = isAA ? mCaches.line.getElementsCount() : gMeshCount; |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 1017 | const GLenum drawMode = isAA ? GL_TRIANGLES : GL_TRIANGLE_STRIP; |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 1018 | |
| 1019 | for (int i = 0; i < count; i += 4) { |
| 1020 | float tx = 0.0f; |
| 1021 | float ty = 0.0f; |
| 1022 | |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 1023 | if (isAA) { |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 1024 | 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] | 1025 | strokeWidth, tx, ty); |
| 1026 | } else { |
Romain Guy | b5ab417 | 2010-09-17 15:36:56 -0700 | [diff] [blame] | 1027 | ty = strokeWidth <= 1.0f ? 0.0f : -strokeWidth * 0.5f; |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 1028 | } |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 1029 | |
| 1030 | const float dx = points[i + 2] - points[i]; |
| 1031 | const float dy = points[i + 3] - points[i + 1]; |
| 1032 | const float mag = sqrtf(dx * dx + dy * dy); |
| 1033 | const float angle = acos(dx / mag); |
| 1034 | |
| 1035 | mModelView.loadTranslate(points[i], points[i + 1], 0.0f); |
| 1036 | if (angle > MIN_ANGLE || angle < -MIN_ANGLE) { |
| 1037 | mModelView.rotate(angle * RAD_TO_DEG, 0.0f, 0.0f, 1.0f); |
| 1038 | } |
| 1039 | mModelView.translate(tx, ty, 0.0f); |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 1040 | if (!isAA) { |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 1041 | float length = mCaches.line.getLength(points[i], points[i + 1], |
| 1042 | points[i + 2], points[i + 3]); |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 1043 | mModelView.scale(length, strokeWidth, 1.0f); |
| 1044 | } |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 1045 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1046 | // TODO: Add bounds to the layer's region |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 1047 | |
| 1048 | if (mShader) { |
| 1049 | mShader->updateTransforms(mCaches.currentProgram, mModelView, *mSnapshot); |
| 1050 | } |
| 1051 | |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 1052 | glDrawArrays(drawMode, 0, elementsCount); |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 1055 | if (isAA) { |
| 1056 | glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); |
| 1057 | } |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 1060 | void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { |
Romain Guy | e45362c | 2010-11-03 19:58:32 -0700 | [diff] [blame] | 1061 | // No need to check against the clip, we fill the clip region |
| 1062 | if (mSnapshot->invisible) return; |
| 1063 | |
Romain Guy | ae88e5e | 2010-10-22 17:49:18 -0700 | [diff] [blame] | 1064 | Rect& clip(*mSnapshot->clipRect); |
| 1065 | clip.snapToPixelBoundaries(); |
Romain Guy | 3d58c03 | 2010-07-14 16:34:53 -0700 | [diff] [blame] | 1066 | drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 1067 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 1068 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 1069 | void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1070 | if (quickReject(left, top, right, bottom)) { |
| 1071 | return; |
| 1072 | } |
| 1073 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 1074 | SkXfermode::Mode mode; |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1075 | if (!mCaches.extensions.hasFramebufferFetch()) { |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1076 | const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); |
| 1077 | if (!isMode) { |
| 1078 | // Assume SRC_OVER |
| 1079 | mode = SkXfermode::kSrcOver_Mode; |
| 1080 | } |
| 1081 | } else { |
| 1082 | mode = getXfermode(p->getXfermode()); |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | // Skia draws using the color's alpha channel if < 255 |
| 1086 | // Otherwise, it uses the paint's alpha |
| 1087 | int color = p->getColor(); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1088 | if (((color >> 24) & 0xff) == 255) { |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 1089 | color |= p->getAlpha() << 24; |
| 1090 | } |
| 1091 | |
| 1092 | drawColorRect(left, top, right, bottom, color, mode); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 1093 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 1094 | |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 1095 | void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, |
| 1096 | float x, float y, SkPaint* paint) { |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1097 | if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) { |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 1098 | return; |
| 1099 | } |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 1100 | if (mSnapshot->invisible) return; |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 1101 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1102 | paint->setAntiAlias(true); |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 1103 | |
Romain Guy | a674ab7 | 2010-08-10 17:26:42 -0700 | [diff] [blame] | 1104 | float length = -1.0f; |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 1105 | switch (paint->getTextAlign()) { |
| 1106 | case SkPaint::kCenter_Align: |
| 1107 | length = paint->measureText(text, bytesCount); |
| 1108 | x -= length / 2.0f; |
| 1109 | break; |
| 1110 | case SkPaint::kRight_Align: |
| 1111 | length = paint->measureText(text, bytesCount); |
| 1112 | x -= length; |
| 1113 | break; |
| 1114 | default: |
| 1115 | break; |
| 1116 | } |
| 1117 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1118 | // TODO: Handle paint->getTextScaleX() |
Romain Guy | 3a3fa1b | 2010-12-06 18:47:50 -0800 | [diff] [blame] | 1119 | const float oldX = x; |
| 1120 | const float oldY = y; |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1121 | const bool pureTranslate = mSnapshot->transform->isPureTranslate(); |
| 1122 | if (pureTranslate) { |
| 1123 | x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f); |
| 1124 | y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f); |
| 1125 | } |
| 1126 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 1127 | int alpha; |
| 1128 | SkXfermode::Mode mode; |
| 1129 | getAlphaAndMode(paint, &alpha, &mode); |
| 1130 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 1131 | uint32_t color = paint->getColor(); |
| 1132 | const GLfloat a = alpha / 255.0f; |
| 1133 | const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; |
| 1134 | const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; |
| 1135 | const GLfloat b = a * ((color ) & 0xFF) / 255.0f; |
| 1136 | |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 1137 | FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint); |
| 1138 | fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1139 | paint->getTextSize()); |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 1140 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1141 | setupDraw(); |
Romain Guy | 9d13fe25 | 2010-10-15 16:06:03 -0700 | [diff] [blame] | 1142 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 1143 | if (mHasShadow) { |
| 1144 | glActiveTexture(gTextureUnits[0]); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 1145 | mCaches.dropShadowCache.setFontRenderer(fontRenderer); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1146 | const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount, |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 1147 | count, mShadowRadius); |
| 1148 | const AutoTexture autoCleanup(shadow); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1149 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1150 | setupShadow(shadow, x, y, mode, a, pureTranslate); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1151 | |
| 1152 | // Draw the mesh |
| 1153 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1154 | glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1157 | GLuint textureUnit = 0; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1158 | glActiveTexture(gTextureUnits[textureUnit]); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1159 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1160 | // Pick the appropriate texture filtering |
| 1161 | bool linearFilter = mSnapshot->transform->changesBounds(); |
| 1162 | if (pureTranslate && !linearFilter) { |
| 1163 | linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f; |
| 1164 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1165 | |
| 1166 | // Dimensions are set to (0,0), the layer (if any) won't be dirtied |
Romain Guy | e8cb9c14 | 2010-10-04 14:14:11 -0700 | [diff] [blame] | 1167 | setupTextureAlpha8(fontRenderer.getTexture(linearFilter), 0, 0, textureUnit, |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1168 | x, y, r, g, b, a, mode, false, true, NULL, NULL, 0, pureTranslate); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1169 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1170 | const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1171 | Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); |
| 1172 | |
| 1173 | #if RENDER_LAYERS_AS_REGIONS |
| 1174 | bool hasLayer = (mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region; |
| 1175 | #else |
| 1176 | bool hasLayer = false; |
| 1177 | #endif |
Romain Guy | 9d13fe25 | 2010-10-15 16:06:03 -0700 | [diff] [blame] | 1178 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 1179 | mCaches.unbindMeshBuffer(); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1180 | if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y, |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1181 | hasLayer ? &bounds : NULL)) { |
| 1182 | #if RENDER_LAYERS_AS_REGIONS |
| 1183 | if (hasLayer) { |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1184 | if (!pureTranslate) { |
| 1185 | mSnapshot->transform->mapRect(bounds); |
| 1186 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1187 | bounds.intersect(*mSnapshot->clipRect); |
| 1188 | bounds.snapToPixelBoundaries(); |
| 1189 | |
| 1190 | android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom); |
| 1191 | mSnapshot->region->orSelf(dirty); |
| 1192 | } |
| 1193 | #endif |
| 1194 | } |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 1195 | |
| 1196 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1197 | glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); |
Romain Guy | a674ab7 | 2010-08-10 17:26:42 -0700 | [diff] [blame] | 1198 | |
Romain Guy | 3a3fa1b | 2010-12-06 18:47:50 -0800 | [diff] [blame] | 1199 | drawTextDecorations(text, bytesCount, length, oldX, oldY, paint); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 1202 | void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) { |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 1203 | if (mSnapshot->invisible) return; |
| 1204 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 1205 | GLuint textureUnit = 0; |
| 1206 | glActiveTexture(gTextureUnits[textureUnit]); |
| 1207 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1208 | const PathTexture* texture = mCaches.pathCache.get(path, paint); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 1209 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 1210 | const AutoTexture autoCleanup(texture); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 1211 | |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 1212 | const float x = texture->left - texture->offset; |
| 1213 | const float y = texture->top - texture->offset; |
| 1214 | |
| 1215 | if (quickReject(x, y, x + texture->width, y + texture->height)) { |
| 1216 | return; |
| 1217 | } |
| 1218 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 1219 | int alpha; |
| 1220 | SkXfermode::Mode mode; |
| 1221 | getAlphaAndMode(paint, &alpha, &mode); |
| 1222 | |
| 1223 | uint32_t color = paint->getColor(); |
| 1224 | const GLfloat a = alpha / 255.0f; |
| 1225 | const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; |
| 1226 | const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; |
| 1227 | const GLfloat b = a * ((color ) & 0xFF) / 255.0f; |
| 1228 | |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1229 | setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true); |
| 1230 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1231 | setupDraw(); |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 1232 | |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1233 | // Draw the mesh |
| 1234 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1235 | glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 1236 | } |
| 1237 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1238 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1239 | // Shaders |
| 1240 | /////////////////////////////////////////////////////////////////////////////// |
| 1241 | |
| 1242 | void OpenGLRenderer::resetShader() { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1243 | mShader = NULL; |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1246 | void OpenGLRenderer::setupShader(SkiaShader* shader) { |
| 1247 | mShader = shader; |
| 1248 | if (mShader) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1249 | mShader->set(&mCaches.textureCache, &mCaches.gradientCache); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1250 | } |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 1251 | } |
| 1252 | |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1253 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1254 | // Color filters |
| 1255 | /////////////////////////////////////////////////////////////////////////////// |
| 1256 | |
| 1257 | void OpenGLRenderer::resetColorFilter() { |
| 1258 | mColorFilter = NULL; |
| 1259 | } |
| 1260 | |
| 1261 | void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) { |
| 1262 | mColorFilter = filter; |
| 1263 | } |
| 1264 | |
| 1265 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 1266 | // Drop shadow |
| 1267 | /////////////////////////////////////////////////////////////////////////////// |
| 1268 | |
| 1269 | void OpenGLRenderer::resetShadow() { |
| 1270 | mHasShadow = false; |
| 1271 | } |
| 1272 | |
| 1273 | void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) { |
| 1274 | mHasShadow = true; |
| 1275 | mShadowRadius = radius; |
| 1276 | mShadowDx = dx; |
| 1277 | mShadowDy = dy; |
| 1278 | mShadowColor = color; |
| 1279 | } |
| 1280 | |
| 1281 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1282 | // Drawing implementation |
| 1283 | /////////////////////////////////////////////////////////////////////////////// |
| 1284 | |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1285 | void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y, |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1286 | SkXfermode::Mode mode, float alpha, bool ignoreTransforms) { |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1287 | const float sx = x - texture->left + mShadowDx; |
| 1288 | const float sy = y - texture->top + mShadowDy; |
| 1289 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 1290 | const int shadowAlpha = ((mShadowColor >> 24) & 0xFF); |
| 1291 | const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1292 | const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f; |
| 1293 | const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f; |
| 1294 | const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f; |
| 1295 | |
| 1296 | GLuint textureUnit = 0; |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1297 | setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit, |
| 1298 | sx, sy, r, g, b, a, mode, true, false, |
| 1299 | (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, 0, ignoreTransforms); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit, |
| 1303 | float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode, |
| 1304 | bool transforms, bool applyFilters) { |
| 1305 | setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit, |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 1306 | x, y, r, g, b, a, mode, transforms, applyFilters, |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 1307 | (GLvoid*) 0, (GLvoid*) gMeshTextureOffset); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height, |
| 1311 | GLuint& textureUnit, float x, float y, float r, float g, float b, float a, |
| 1312 | SkXfermode::Mode mode, bool transforms, bool applyFilters) { |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 1313 | setupTextureAlpha8(texture, width, height, textureUnit, x, y, r, g, b, a, mode, |
| 1314 | transforms, applyFilters, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset); |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height, |
| 1318 | GLuint& textureUnit, float x, float y, float r, float g, float b, float a, |
| 1319 | SkXfermode::Mode mode, bool transforms, bool applyFilters, |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1320 | GLvoid* vertices, GLvoid* texCoords, GLuint vbo, bool ignoreTransform) { |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1321 | // Describe the required shaders |
| 1322 | ProgramDescription description; |
| 1323 | description.hasTexture = true; |
| 1324 | description.hasAlpha8Texture = true; |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 1325 | const bool setColor = description.setAlpha8Color(r, g, b, a); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1326 | |
| 1327 | if (applyFilters) { |
| 1328 | if (mShader) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1329 | mShader->describe(description, mCaches.extensions); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1330 | } |
| 1331 | if (mColorFilter) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1332 | mColorFilter->describe(description, mCaches.extensions); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1333 | } |
| 1334 | } |
| 1335 | |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1336 | // Setup the blending mode |
| 1337 | chooseBlending(true, mode, description); |
| 1338 | |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1339 | // Build and use the appropriate shader |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1340 | useProgram(mCaches.programCache.get(description)); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1341 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1342 | bindTexture(texture); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1343 | glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1344 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1345 | int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords"); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1346 | glEnableVertexAttribArray(texCoordsSlot); |
| 1347 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 1348 | if (texCoords) { |
| 1349 | // Setup attributes |
| 1350 | if (!vertices) { |
| 1351 | mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo); |
| 1352 | } else { |
| 1353 | mCaches.unbindMeshBuffer(); |
| 1354 | } |
| 1355 | glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, |
| 1356 | gMeshStride, vertices); |
| 1357 | glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords); |
| 1358 | } |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1359 | |
| 1360 | // Setup uniforms |
| 1361 | if (transforms) { |
| 1362 | mModelView.loadTranslate(x, y, 0.0f); |
| 1363 | mModelView.scale(width, height, 1.0f); |
| 1364 | } else { |
| 1365 | mModelView.loadIdentity(); |
| 1366 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1367 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1368 | mat4 t; |
| 1369 | if (!ignoreTransform) { |
| 1370 | t.load(*mSnapshot->transform); |
| 1371 | } |
| 1372 | |
| 1373 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, t); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1374 | if (width > 0 && height > 0) { |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1375 | dirtyLayer(x, y, x + width, y + height, t); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1376 | } |
| 1377 | |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 1378 | if (setColor) { |
| 1379 | mCaches.currentProgram->setColor(r, g, b, a); |
| 1380 | } |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1381 | |
| 1382 | textureUnit++; |
| 1383 | if (applyFilters) { |
| 1384 | // Setup attributes and uniforms required by the shaders |
| 1385 | if (mShader) { |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1386 | if (ignoreTransform) { |
| 1387 | mModelView.loadInverse(*mSnapshot->transform); |
| 1388 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1389 | mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1390 | } |
| 1391 | if (mColorFilter) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1392 | mColorFilter->setupProgram(mCaches.currentProgram); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1393 | } |
| 1394 | } |
| 1395 | } |
| 1396 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1397 | // Same values used by Skia |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1398 | #define kStdStrikeThru_Offset (-6.0f / 21.0f) |
| 1399 | #define kStdUnderline_Offset (1.0f / 9.0f) |
| 1400 | #define kStdUnderline_Thickness (1.0f / 18.0f) |
| 1401 | |
| 1402 | void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length, |
| 1403 | float x, float y, SkPaint* paint) { |
| 1404 | // Handle underline and strike-through |
| 1405 | uint32_t flags = paint->getFlags(); |
| 1406 | if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) { |
| 1407 | float underlineWidth = length; |
| 1408 | // If length is > 0.0f, we already measured the text for the text alignment |
| 1409 | if (length <= 0.0f) { |
| 1410 | underlineWidth = paint->measureText(text, bytesCount); |
| 1411 | } |
| 1412 | |
| 1413 | float offsetX = 0; |
| 1414 | switch (paint->getTextAlign()) { |
| 1415 | case SkPaint::kCenter_Align: |
| 1416 | offsetX = underlineWidth * 0.5f; |
| 1417 | break; |
| 1418 | case SkPaint::kRight_Align: |
| 1419 | offsetX = underlineWidth; |
| 1420 | break; |
| 1421 | default: |
| 1422 | break; |
| 1423 | } |
| 1424 | |
| 1425 | if (underlineWidth > 0.0f) { |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 1426 | const float textSize = paint->getTextSize(); |
| 1427 | const float strokeWidth = textSize * kStdUnderline_Thickness; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1428 | |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 1429 | const float left = x - offsetX; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1430 | float top = 0.0f; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 1431 | |
| 1432 | const int pointsCount = 4 * (flags & SkPaint::kStrikeThruText_Flag ? 2 : 1); |
| 1433 | float points[pointsCount]; |
| 1434 | int currentPoint = 0; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1435 | |
| 1436 | if (flags & SkPaint::kUnderlineText_Flag) { |
| 1437 | top = y + textSize * kStdUnderline_Offset; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 1438 | points[currentPoint++] = left; |
| 1439 | points[currentPoint++] = top; |
| 1440 | points[currentPoint++] = left + underlineWidth; |
| 1441 | points[currentPoint++] = top; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | if (flags & SkPaint::kStrikeThruText_Flag) { |
| 1445 | top = y + textSize * kStdStrikeThru_Offset; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 1446 | points[currentPoint++] = left; |
| 1447 | points[currentPoint++] = top; |
| 1448 | points[currentPoint++] = left + underlineWidth; |
| 1449 | points[currentPoint++] = top; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1450 | } |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 1451 | |
| 1452 | SkPaint linesPaint(*paint); |
| 1453 | linesPaint.setStrokeWidth(strokeWidth); |
| 1454 | |
| 1455 | drawLines(&points[0], pointsCount, &linesPaint); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1456 | } |
| 1457 | } |
| 1458 | } |
| 1459 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 1460 | void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 1461 | int color, SkXfermode::Mode mode, bool ignoreTransform) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1462 | setupDraw(); |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 1463 | |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1464 | // If a shader is set, preserve only the alpha |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1465 | if (mShader) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1466 | color |= 0x00ffffff; |
| 1467 | } |
| 1468 | |
| 1469 | // Render using pre-multiplied alpha |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 1470 | const int alpha = (color >> 24) & 0xFF; |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1471 | const GLfloat a = alpha / 255.0f; |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 1472 | const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; |
| 1473 | const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; |
| 1474 | const GLfloat b = a * ((color ) & 0xFF) / 255.0f; |
| 1475 | |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 1476 | setupColorRect(left, top, right, bottom, r, g, b, a, mode, ignoreTransform); |
| 1477 | |
| 1478 | // Draw the mesh |
| 1479 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
| 1480 | } |
| 1481 | |
| 1482 | void OpenGLRenderer::setupColorRect(float left, float top, float right, float bottom, |
Romain Guy | 7f78b0c | 2010-11-04 14:36:26 -0700 | [diff] [blame] | 1483 | float r, float g, float b, float a, SkXfermode::Mode mode, |
| 1484 | bool ignoreTransform, bool ignoreMatrix) { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1485 | GLuint textureUnit = 0; |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 1486 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1487 | // Describe the required shaders |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1488 | ProgramDescription description; |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 1489 | const bool setColor = description.setColor(r, g, b, a); |
| 1490 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1491 | if (mShader) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1492 | mShader->describe(description, mCaches.extensions); |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1493 | } |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1494 | if (mColorFilter) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1495 | mColorFilter->describe(description, mCaches.extensions); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1496 | } |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1497 | |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 1498 | // Setup the blending mode |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 1499 | chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description); |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1500 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1501 | // Build and use the appropriate shader |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1502 | useProgram(mCaches.programCache.get(description)); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1503 | |
| 1504 | // Setup attributes |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 1505 | mCaches.bindMeshBuffer(); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1506 | glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 1507 | gMeshStride, 0); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1508 | |
Romain Guy | 7f78b0c | 2010-11-04 14:36:26 -0700 | [diff] [blame] | 1509 | if (!ignoreMatrix) { |
| 1510 | // Setup uniforms |
| 1511 | mModelView.loadTranslate(left, top, 0.0f); |
| 1512 | mModelView.scale(right - left, bottom - top, 1.0f); |
| 1513 | if (!ignoreTransform) { |
| 1514 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); |
| 1515 | dirtyLayer(left, top, right, bottom, *mSnapshot->transform); |
| 1516 | } else { |
| 1517 | mat4 identity; |
| 1518 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity); |
| 1519 | dirtyLayer(left, top, right, bottom); |
| 1520 | } |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1521 | } |
Romain Guy | 01d0657 | 2010-11-11 12:06:27 -0800 | [diff] [blame] | 1522 | if (!mShader || (mShader && setColor)) { |
| 1523 | mCaches.currentProgram->setColor(r, g, b, a); |
| 1524 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 1525 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1526 | // Setup attributes and uniforms required by the shaders |
| 1527 | if (mShader) { |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1528 | if (ignoreMatrix) { |
| 1529 | mModelView.loadIdentity(); |
| 1530 | } else if (ignoreTransform) { |
| 1531 | mModelView.loadInverse(*mSnapshot->transform); |
| 1532 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1533 | mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 1534 | } |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1535 | if (mColorFilter) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1536 | mColorFilter->setupProgram(mCaches.currentProgram); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1537 | } |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1538 | } |
| 1539 | |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1540 | void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 1541 | Texture* texture, SkPaint* paint) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1542 | int alpha; |
| 1543 | SkXfermode::Mode mode; |
| 1544 | getAlphaAndMode(paint, &alpha, &mode); |
| 1545 | |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 1546 | setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); |
| 1547 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1548 | if (mSnapshot->transform->isPureTranslate()) { |
| 1549 | const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); |
| 1550 | const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); |
| 1551 | |
| 1552 | drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id, |
| 1553 | alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL, |
| 1554 | (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true); |
| 1555 | } else { |
| 1556 | drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, |
| 1557 | texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, |
| 1558 | GL_TRIANGLE_STRIP, gMeshCount); |
| 1559 | } |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 1560 | } |
| 1561 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 1562 | void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 1563 | GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) { |
| 1564 | drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 1565 | (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 1569 | GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 1570 | GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1571 | bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1572 | setupDraw(); |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 1573 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1574 | ProgramDescription description; |
| 1575 | description.hasTexture = true; |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 1576 | const bool setColor = description.setColor(alpha, alpha, alpha, alpha); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1577 | if (mColorFilter) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1578 | mColorFilter->describe(description, mCaches.extensions); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1579 | } |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1580 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 1581 | mModelView.loadTranslate(left, top, 0.0f); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1582 | if (!ignoreScale) { |
| 1583 | mModelView.scale(right - left, bottom - top, 1.0f); |
| 1584 | } |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 1585 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1586 | chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst); |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1587 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1588 | useProgram(mCaches.programCache.get(description)); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1589 | if (!ignoreTransform) { |
| 1590 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1591 | if (dirty) dirtyLayer(left, top, right, bottom, *mSnapshot->transform); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1592 | } else { |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1593 | mat4 identity; |
| 1594 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity); |
| 1595 | if (dirty) dirtyLayer(left, top, right, bottom); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1596 | } |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 1597 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1598 | // Texture |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 1599 | bindTexture(texture); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1600 | glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 1601 | |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 1602 | // Always premultiplied |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 1603 | if (setColor) { |
| 1604 | mCaches.currentProgram->setColor(alpha, alpha, alpha, alpha); |
| 1605 | } |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 1606 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1607 | // Mesh |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1608 | int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords"); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1609 | glEnableVertexAttribArray(texCoordsSlot); |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 1610 | |
| 1611 | if (!vertices) { |
| 1612 | mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo); |
| 1613 | } else { |
| 1614 | mCaches.unbindMeshBuffer(); |
| 1615 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1616 | glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 1617 | gMeshStride, vertices); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1618 | glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords); |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 1619 | |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1620 | // Color filter |
| 1621 | if (mColorFilter) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1622 | mColorFilter->setupProgram(mCaches.currentProgram); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1623 | } |
| 1624 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 1625 | glDrawArrays(drawMode, 0, elementsCount); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1626 | glDisableVertexAttribArray(texCoordsSlot); |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1627 | } |
| 1628 | |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1629 | void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1630 | ProgramDescription& description, bool swapSrcDst) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1631 | blend = blend || mode != SkXfermode::kSrcOver_Mode; |
| 1632 | if (blend) { |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1633 | if (mode < SkXfermode::kPlus_Mode) { |
| 1634 | if (!mCaches.blend) { |
| 1635 | glEnable(GL_BLEND); |
| 1636 | } |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1637 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1638 | GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src; |
| 1639 | GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1640 | |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1641 | if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) { |
| 1642 | glBlendFunc(sourceMode, destMode); |
| 1643 | mCaches.lastSrcMode = sourceMode; |
| 1644 | mCaches.lastDstMode = destMode; |
| 1645 | } |
| 1646 | } else { |
| 1647 | // These blend modes are not supported by OpenGL directly and have |
| 1648 | // to be implemented using shaders. Since the shader will perform |
| 1649 | // the blending, turn blending off here |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1650 | if (mCaches.extensions.hasFramebufferFetch()) { |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1651 | description.framebufferMode = mode; |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1652 | description.swapSrcDst = swapSrcDst; |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | if (mCaches.blend) { |
| 1656 | glDisable(GL_BLEND); |
| 1657 | } |
| 1658 | blend = false; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1659 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1660 | } else if (mCaches.blend) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1661 | glDisable(GL_BLEND); |
| 1662 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1663 | mCaches.blend = blend; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 1664 | } |
| 1665 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1666 | bool OpenGLRenderer::useProgram(Program* program) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1667 | if (!program->isInUse()) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1668 | if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove(); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1669 | program->use(); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1670 | mCaches.currentProgram = program; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1671 | return false; |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 1672 | } |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1673 | return true; |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 1674 | } |
| 1675 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 1676 | void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 1677 | TextureVertex* v = &mMeshVertices[0]; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1678 | TextureVertex::setUV(v++, u1, v1); |
| 1679 | TextureVertex::setUV(v++, u2, v1); |
| 1680 | TextureVertex::setUV(v++, u1, v2); |
| 1681 | TextureVertex::setUV(v++, u2, v2); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 1682 | } |
| 1683 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 1684 | void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) { |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 1685 | if (paint) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1686 | if (!mCaches.extensions.hasFramebufferFetch()) { |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1687 | const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode); |
| 1688 | if (!isMode) { |
| 1689 | // Assume SRC_OVER |
| 1690 | *mode = SkXfermode::kSrcOver_Mode; |
| 1691 | } |
| 1692 | } else { |
| 1693 | *mode = getXfermode(paint->getXfermode()); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | // Skia draws using the color's alpha channel if < 255 |
| 1697 | // Otherwise, it uses the paint's alpha |
| 1698 | int color = paint->getColor(); |
| 1699 | *alpha = (color >> 24) & 0xFF; |
| 1700 | if (*alpha == 255) { |
| 1701 | *alpha = paint->getAlpha(); |
| 1702 | } |
| 1703 | } else { |
| 1704 | *mode = SkXfermode::kSrcOver_Mode; |
| 1705 | *alpha = 255; |
| 1706 | } |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 1707 | } |
| 1708 | |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1709 | SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) { |
| 1710 | if (mode == NULL) { |
| 1711 | return SkXfermode::kSrcOver_Mode; |
| 1712 | } |
| 1713 | return mode->fMode; |
| 1714 | } |
| 1715 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1716 | void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) { |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 1717 | bool bound = false; |
| 1718 | if (wrapS != texture->wrapS) { |
| 1719 | glBindTexture(GL_TEXTURE_2D, texture->id); |
| 1720 | bound = true; |
| 1721 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS); |
| 1722 | texture->wrapS = wrapS; |
| 1723 | } |
| 1724 | if (wrapT != texture->wrapT) { |
Romain Guy | 3e3ba15 | 2010-10-25 18:33:16 -0700 | [diff] [blame] | 1725 | if (!bound) { |
Romain Guy | 3e3ba15 | 2010-10-25 18:33:16 -0700 | [diff] [blame] | 1726 | glBindTexture(GL_TEXTURE_2D, texture->id); |
| 1727 | } |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 1728 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT); |
| 1729 | texture->wrapT = wrapT; |
| 1730 | } |
Romain Guy | a1db574 | 2010-07-20 13:09:13 -0700 | [diff] [blame] | 1731 | } |
| 1732 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 1733 | }; // namespace uirenderer |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1734 | }; // namespace android |