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 | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 26 | #include <cutils/properties.h> |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 27 | #include <utils/Log.h> |
| 28 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 29 | #include "OpenGLRenderer.h" |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 30 | #include "Properties.h" |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 31 | |
| 32 | namespace android { |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 33 | namespace uirenderer { |
| 34 | |
| 35 | /////////////////////////////////////////////////////////////////////////////// |
| 36 | // Defines |
| 37 | /////////////////////////////////////////////////////////////////////////////// |
| 38 | |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 39 | #define DEFAULT_TEXTURE_CACHE_SIZE 20.0f |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 40 | #define DEFAULT_LAYER_CACHE_SIZE 6.0f |
| 41 | #define DEFAULT_PATH_CACHE_SIZE 6.0f |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 42 | #define DEFAULT_PATCH_CACHE_SIZE 100 |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 43 | #define DEFAULT_GRADIENT_CACHE_SIZE 0.5f |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 44 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 45 | #define REQUIRED_TEXTURE_UNITS_COUNT 3 |
| 46 | |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 47 | // Converts a number of mega-bytes into bytes |
| 48 | #define MB(s) s * 1024 * 1024 |
| 49 | |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 50 | // Generates simple and textured vertices |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 51 | #define FV(x, y, u, v) { { x, y }, { u, v } } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 52 | |
| 53 | /////////////////////////////////////////////////////////////////////////////// |
| 54 | // Globals |
| 55 | /////////////////////////////////////////////////////////////////////////////// |
| 56 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 57 | // This array is never used directly but used as a memcpy source in the |
| 58 | // OpenGLRenderer constructor |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 59 | static const TextureVertex gMeshVertices[] = { |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 60 | FV(0.0f, 0.0f, 0.0f, 0.0f), |
| 61 | FV(1.0f, 0.0f, 1.0f, 0.0f), |
| 62 | FV(0.0f, 1.0f, 0.0f, 1.0f), |
| 63 | FV(1.0f, 1.0f, 1.0f, 1.0f) |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 64 | }; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 65 | static const GLsizei gMeshStride = sizeof(TextureVertex); |
| 66 | static const GLsizei gMeshCount = 4; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 67 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 68 | /** |
| 69 | * Structure mapping Skia xfermodes to OpenGL blending factors. |
| 70 | */ |
| 71 | struct Blender { |
| 72 | SkXfermode::Mode mode; |
| 73 | GLenum src; |
| 74 | GLenum dst; |
| 75 | }; // struct Blender |
| 76 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 77 | // In this array, the index of each Blender equals the value of the first |
| 78 | // entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode] |
| 79 | static const Blender gBlends[] = { |
| 80 | { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO }, |
| 81 | { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO }, |
| 82 | { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE }, |
| 83 | { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, |
| 84 | { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, |
| 85 | { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO }, |
| 86 | { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA }, |
| 87 | { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, |
| 88 | { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, |
| 89 | { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, |
| 90 | { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, |
| 91 | { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA } |
| 92 | }; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 93 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 94 | static const GLenum gTextureUnits[] = { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 95 | GL_TEXTURE0, |
| 96 | GL_TEXTURE1, |
| 97 | GL_TEXTURE2 |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 100 | /////////////////////////////////////////////////////////////////////////////// |
| 101 | // Constructors/destructor |
| 102 | /////////////////////////////////////////////////////////////////////////////// |
| 103 | |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 104 | OpenGLRenderer::OpenGLRenderer(): |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 105 | mBlend(false), mLastSrcMode(GL_ZERO), mLastDstMode(GL_ZERO), |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 106 | mTextureCache(MB(DEFAULT_TEXTURE_CACHE_SIZE)), |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 107 | mLayerCache(MB(DEFAULT_LAYER_CACHE_SIZE)), |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 108 | mGradientCache(MB(DEFAULT_GRADIENT_CACHE_SIZE)), |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 109 | mPathCache(MB(DEFAULT_PATH_CACHE_SIZE)), |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 110 | mPatchCache(DEFAULT_PATCH_CACHE_SIZE) { |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 111 | LOGD("Create OpenGLRenderer"); |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 112 | |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 113 | char property[PROPERTY_VALUE_MAX]; |
| 114 | if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) { |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 115 | LOGD(" Setting texture cache size to %sMB", property); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 116 | mTextureCache.setMaxSize(MB(atof(property))); |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 117 | } else { |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 118 | LOGD(" Using default texture cache size of %.2fMB", DEFAULT_TEXTURE_CACHE_SIZE); |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | if (property_get(PROPERTY_LAYER_CACHE_SIZE, property, NULL) > 0) { |
| 122 | LOGD(" Setting layer cache size to %sMB", property); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 123 | mLayerCache.setMaxSize(MB(atof(property))); |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 124 | } else { |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 125 | LOGD(" Using default layer cache size of %.2fMB", DEFAULT_LAYER_CACHE_SIZE); |
| 126 | } |
| 127 | |
| 128 | if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) { |
| 129 | LOGD(" Setting gradient cache size to %sMB", property); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 130 | mGradientCache.setMaxSize(MB(atof(property))); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 131 | } else { |
| 132 | LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE); |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 135 | if (property_get(PROPERTY_PATH_CACHE_SIZE, property, NULL) > 0) { |
| 136 | LOGD(" Setting path cache size to %sMB", property); |
| 137 | mPathCache.setMaxSize(MB(atof(property))); |
| 138 | } else { |
| 139 | LOGD(" Using default path cache size of %.2fMB", DEFAULT_PATH_CACHE_SIZE); |
| 140 | } |
| 141 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 142 | mCurrentProgram = NULL; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 143 | mShader = NULL; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 144 | mColorFilter = NULL; |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 145 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 146 | memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices)); |
| 147 | |
Romain Guy | ae5575b | 2010-07-29 18:48:04 -0700 | [diff] [blame] | 148 | mFirstSnapshot = new Snapshot; |
| 149 | |
| 150 | GLint maxTextureUnits; |
| 151 | glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits); |
| 152 | if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) { |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 153 | LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT); |
| 154 | } |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 157 | OpenGLRenderer::~OpenGLRenderer() { |
| 158 | LOGD("Destroy OpenGLRenderer"); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 159 | |
| 160 | mTextureCache.clear(); |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 161 | mLayerCache.clear(); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 162 | mGradientCache.clear(); |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 163 | mPatchCache.clear(); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 166 | /////////////////////////////////////////////////////////////////////////////// |
| 167 | // Setup |
| 168 | /////////////////////////////////////////////////////////////////////////////// |
| 169 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 170 | void OpenGLRenderer::setViewport(int width, int height) { |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 171 | glViewport(0, 0, width, height); |
| 172 | |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 173 | mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 174 | |
| 175 | mWidth = width; |
| 176 | mHeight = height; |
Romain Guy | ae5575b | 2010-07-29 18:48:04 -0700 | [diff] [blame] | 177 | mFirstSnapshot->height = height; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 180 | void OpenGLRenderer::prepare() { |
Romain Guy | b82da65 | 2010-07-30 11:36:12 -0700 | [diff] [blame] | 181 | mSnapshot = new Snapshot(mFirstSnapshot); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 182 | mSaveCount = 0; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 183 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 184 | glDisable(GL_SCISSOR_TEST); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 185 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 186 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 187 | glClear(GL_COLOR_BUFFER_BIT); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 188 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 189 | glEnable(GL_SCISSOR_TEST); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 190 | glScissor(0, 0, mWidth, mHeight); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 191 | |
Romain Guy | b82da65 | 2010-07-30 11:36:12 -0700 | [diff] [blame] | 192 | mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 195 | /////////////////////////////////////////////////////////////////////////////// |
| 196 | // State management |
| 197 | /////////////////////////////////////////////////////////////////////////////// |
| 198 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 199 | int OpenGLRenderer::getSaveCount() const { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 200 | return mSaveCount; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | int OpenGLRenderer::save(int flags) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 204 | return saveSnapshot(); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void OpenGLRenderer::restore() { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 208 | if (mSaveCount == 0) return; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 209 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 210 | if (restoreSnapshot()) { |
| 211 | setScissorFromClip(); |
| 212 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void OpenGLRenderer::restoreToCount(int saveCount) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 216 | if (saveCount <= 0 || saveCount > mSaveCount) return; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 217 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 218 | bool restoreClip = false; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 219 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 220 | while (mSaveCount != saveCount - 1) { |
| 221 | restoreClip |= restoreSnapshot(); |
| 222 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 223 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 224 | if (restoreClip) { |
| 225 | setScissorFromClip(); |
| 226 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | int OpenGLRenderer::saveSnapshot() { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 230 | mSnapshot = new Snapshot(mSnapshot); |
| 231 | return ++mSaveCount; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | bool OpenGLRenderer::restoreSnapshot() { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 235 | bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 236 | bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer; |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 237 | bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 238 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 239 | sp<Snapshot> current = mSnapshot; |
| 240 | sp<Snapshot> previous = mSnapshot->previous; |
| 241 | |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 242 | if (restoreOrtho) { |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 243 | mOrthoMatrix.load(current->orthoMatrix); |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 246 | if (restoreLayer) { |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 247 | composeLayer(current, previous); |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | mSnapshot = previous; |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 251 | mSaveCount--; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 252 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 253 | return restoreClip; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 256 | void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) { |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 257 | if (!current->layer) { |
| 258 | LOGE("Attempting to compose a layer that does not exist"); |
| 259 | return; |
| 260 | } |
| 261 | |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 262 | // Unbind current FBO and restore previous one |
| 263 | // Most of the time, previous->fbo will be 0 to bind the default buffer |
| 264 | glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); |
| 265 | |
| 266 | // Restore the clip from the previous snapshot |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 267 | const Rect& clip = previous->clipRect; |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 268 | glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight()); |
| 269 | |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 270 | Layer* layer = current->layer; |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 271 | const Rect& rect = layer->layer; |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 272 | |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 273 | drawTextureRect(rect.left, rect.top, rect.right, rect.bottom, |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 274 | layer->texture, layer->alpha, layer->mode, layer->blend); |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 275 | |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 276 | LayerSize size(rect.getWidth(), rect.getHeight()); |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 277 | // Failing to add the layer to the cache should happen only if the |
| 278 | // layer is too large |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 279 | if (!mLayerCache.put(size, layer)) { |
| 280 | LAYER_LOGD("Deleting layer"); |
| 281 | |
| 282 | glDeleteFramebuffers(1, &layer->fbo); |
| 283 | glDeleteTextures(1, &layer->texture); |
| 284 | |
| 285 | delete layer; |
| 286 | } |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 289 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 290 | // Layers |
| 291 | /////////////////////////////////////////////////////////////////////////////// |
| 292 | |
| 293 | int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom, |
| 294 | const SkPaint* p, int flags) { |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 295 | int count = saveSnapshot(); |
| 296 | |
| 297 | int alpha = 255; |
| 298 | SkXfermode::Mode mode; |
| 299 | |
| 300 | if (p) { |
| 301 | alpha = p->getAlpha(); |
| 302 | const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); |
| 303 | if (!isMode) { |
| 304 | // Assume SRC_OVER |
| 305 | mode = SkXfermode::kSrcOver_Mode; |
| 306 | } |
| 307 | } else { |
| 308 | mode = SkXfermode::kSrcOver_Mode; |
| 309 | } |
| 310 | |
| 311 | createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags); |
| 312 | |
| 313 | return count; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom, |
| 317 | int alpha, int flags) { |
| 318 | int count = saveSnapshot(); |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 319 | createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags); |
| 320 | return count; |
| 321 | } |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 322 | |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 323 | bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top, |
| 324 | float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 325 | LAYER_LOGD("Requesting layer %fx%f", right - left, bottom - top); |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 326 | LAYER_LOGD("Layer cache size = %d", mLayerCache.getSize()); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 327 | |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 328 | GLuint previousFbo = snapshot->previous.get() ? snapshot->previous->fbo : 0; |
| 329 | LayerSize size(right - left, bottom - top); |
| 330 | |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 331 | Layer* layer = mLayerCache.get(size, previousFbo); |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 332 | if (!layer) { |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 333 | return false; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 336 | glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo); |
| 337 | |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 338 | // Clear the FBO |
| 339 | glDisable(GL_SCISSOR_TEST); |
| 340 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 341 | glClear(GL_COLOR_BUFFER_BIT); |
| 342 | glEnable(GL_SCISSOR_TEST); |
| 343 | |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 344 | // Save the layer in the snapshot |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 345 | snapshot->flags |= Snapshot::kFlagIsLayer; |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 346 | layer->mode = mode; |
| 347 | layer->alpha = alpha / 255.0f; |
| 348 | layer->layer.set(left, top, right, bottom); |
| 349 | |
| 350 | snapshot->layer = layer; |
| 351 | snapshot->fbo = layer->fbo; |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 352 | |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 353 | // Creates a new snapshot to draw into the FBO |
| 354 | saveSnapshot(); |
| 355 | // TODO: This doesn't preserve other transformations (check Skia first) |
| 356 | mSnapshot->transform.loadTranslate(-left, -top, 0.0f); |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 357 | mSnapshot->setClip(0.0f, 0.0f, right - left, bottom - top); |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 358 | mSnapshot->height = bottom - top; |
| 359 | setScissorFromClip(); |
| 360 | |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 361 | mSnapshot->flags = Snapshot::kFlagDirtyOrtho | Snapshot::kFlagClipSet; |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 362 | mSnapshot->orthoMatrix.load(mOrthoMatrix); |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 363 | |
| 364 | // Change the ortho projection |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 365 | mOrthoMatrix.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f); |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 366 | |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 367 | return true; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 371 | // Transforms |
| 372 | /////////////////////////////////////////////////////////////////////////////// |
| 373 | |
| 374 | void OpenGLRenderer::translate(float dx, float dy) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 375 | mSnapshot->transform.translate(dx, dy, 0.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | void OpenGLRenderer::rotate(float degrees) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 379 | mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | void OpenGLRenderer::scale(float sx, float sy) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 383 | mSnapshot->transform.scale(sx, sy, 1.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | void OpenGLRenderer::setMatrix(SkMatrix* matrix) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 387 | mSnapshot->transform.load(*matrix); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | void OpenGLRenderer::getMatrix(SkMatrix* matrix) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 391 | mSnapshot->transform.copyTo(*matrix); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 395 | mat4 m(*matrix); |
| 396 | mSnapshot->transform.multiply(m); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /////////////////////////////////////////////////////////////////////////////// |
| 400 | // Clipping |
| 401 | /////////////////////////////////////////////////////////////////////////////// |
| 402 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 403 | void OpenGLRenderer::setScissorFromClip() { |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 404 | const Rect& clip = mSnapshot->clipRect; |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 405 | glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight()); |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | const Rect& OpenGLRenderer::getClipBounds() { |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 409 | return mSnapshot->getLocalClip(); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 412 | bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) { |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 413 | Rect r(left, top, right, bottom); |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 414 | mSnapshot->transform.mapRect(r); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 415 | return !mSnapshot->clipRect.intersects(r); |
| 416 | } |
| 417 | |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 418 | bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) { |
| 419 | bool clipped = mSnapshot->clip(left, top, right, bottom, op); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 420 | if (clipped) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 421 | setScissorFromClip(); |
| 422 | } |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 423 | return !mSnapshot->clipRect.isEmpty(); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 426 | /////////////////////////////////////////////////////////////////////////////// |
| 427 | // Drawing |
| 428 | /////////////////////////////////////////////////////////////////////////////// |
| 429 | |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 430 | void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 431 | const float right = left + bitmap->width(); |
| 432 | const float bottom = top + bitmap->height(); |
| 433 | |
| 434 | if (quickReject(left, top, right, bottom)) { |
| 435 | return; |
| 436 | } |
| 437 | |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame^] | 438 | glActiveTexture(GL_TEXTURE0); |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 439 | const Texture* texture = mTextureCache.get(bitmap); |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 440 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 441 | const AutoTexture autoCleanup(texture); |
| 442 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 443 | drawTextureRect(left, top, right, bottom, texture, paint); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 446 | void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) { |
| 447 | Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height()); |
| 448 | const mat4 transform(*matrix); |
| 449 | transform.mapRect(r); |
| 450 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 451 | if (quickReject(r.left, r.top, r.right, r.bottom)) { |
| 452 | return; |
| 453 | } |
| 454 | |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame^] | 455 | glActiveTexture(GL_TEXTURE0); |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 456 | const Texture* texture = mTextureCache.get(bitmap); |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 457 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 458 | const AutoTexture autoCleanup(texture); |
| 459 | |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 460 | drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint); |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 463 | void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, |
| 464 | float srcLeft, float srcTop, float srcRight, float srcBottom, |
| 465 | float dstLeft, float dstTop, float dstRight, float dstBottom, |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 466 | const SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 467 | if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) { |
| 468 | return; |
| 469 | } |
| 470 | |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame^] | 471 | glActiveTexture(GL_TEXTURE0); |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 472 | const Texture* texture = mTextureCache.get(bitmap); |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 473 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 474 | const AutoTexture autoCleanup(texture); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 475 | |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 476 | const float width = texture->width; |
| 477 | const float height = texture->height; |
| 478 | |
| 479 | const float u1 = srcLeft / width; |
| 480 | const float v1 = srcTop / height; |
| 481 | const float u2 = srcRight / width; |
| 482 | const float v2 = srcBottom / height; |
| 483 | |
| 484 | resetDrawTextureTexCoords(u1, v1, u2, v2); |
| 485 | |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 486 | drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 487 | |
| 488 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
| 489 | } |
| 490 | |
Romain Guy | deba785 | 2010-07-07 17:54:48 -0700 | [diff] [blame] | 491 | void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch, |
| 492 | float left, float top, float right, float bottom, const SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 493 | if (quickReject(left, top, right, bottom)) { |
| 494 | return; |
| 495 | } |
| 496 | |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame^] | 497 | glActiveTexture(GL_TEXTURE0); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 498 | const Texture* texture = mTextureCache.get(bitmap); |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 499 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 500 | const AutoTexture autoCleanup(texture); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 501 | |
| 502 | int alpha; |
| 503 | SkXfermode::Mode mode; |
| 504 | getAlphaAndMode(paint, &alpha, &mode); |
| 505 | |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 506 | Patch* mesh = mPatchCache.get(patch); |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 507 | mesh->updateVertices(bitmap, left, top, right, bottom, |
| 508 | &patch->xDivs[0], &patch->yDivs[0], patch->numXDivs, patch->numYDivs); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 509 | |
| 510 | // Specify right and bottom as +1.0f from left/top to prevent scaling since the |
| 511 | // patch mesh already defines the final size |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 512 | drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f, |
| 513 | mode, texture->blend, &mesh->vertices[0].position[0], |
Romain Guy | 16202fc | 2010-07-09 16:13:28 -0700 | [diff] [blame] | 514 | &mesh->vertices[0].texture[0], mesh->indices, mesh->indicesCount); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 517 | void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 518 | const Rect& clip = mSnapshot->clipRect; |
Romain Guy | 3d58c03 | 2010-07-14 16:34:53 -0700 | [diff] [blame] | 519 | drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 520 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 521 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 522 | void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 523 | if (quickReject(left, top, right, bottom)) { |
| 524 | return; |
| 525 | } |
| 526 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 527 | SkXfermode::Mode mode; |
| 528 | |
| 529 | const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); |
| 530 | if (!isMode) { |
| 531 | // Assume SRC_OVER |
| 532 | mode = SkXfermode::kSrcOver_Mode; |
| 533 | } |
| 534 | |
| 535 | // Skia draws using the color's alpha channel if < 255 |
| 536 | // Otherwise, it uses the paint's alpha |
| 537 | int color = p->getColor(); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 538 | if (((color >> 24) & 0xff) == 255) { |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 539 | color |= p->getAlpha() << 24; |
| 540 | } |
| 541 | |
| 542 | drawColorRect(left, top, right, bottom, color, mode); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 543 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 544 | |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 545 | void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, |
| 546 | float x, float y, SkPaint* paint) { |
| 547 | if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) { |
| 548 | return; |
| 549 | } |
| 550 | |
| 551 | float length; |
| 552 | switch (paint->getTextAlign()) { |
| 553 | case SkPaint::kCenter_Align: |
| 554 | length = paint->measureText(text, bytesCount); |
| 555 | x -= length / 2.0f; |
| 556 | break; |
| 557 | case SkPaint::kRight_Align: |
| 558 | length = paint->measureText(text, bytesCount); |
| 559 | x -= length; |
| 560 | break; |
| 561 | default: |
| 562 | break; |
| 563 | } |
| 564 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 565 | int alpha; |
| 566 | SkXfermode::Mode mode; |
| 567 | getAlphaAndMode(paint, &alpha, &mode); |
| 568 | |
| 569 | uint32_t color = paint->getColor(); |
| 570 | const GLfloat a = alpha / 255.0f; |
| 571 | const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; |
| 572 | const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; |
| 573 | const GLfloat b = a * ((color ) & 0xFF) / 255.0f; |
| 574 | |
| 575 | mModelView.loadIdentity(); |
| 576 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 577 | GLuint textureUnit = 0; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 578 | // Needs to be set prior to calling FontRenderer::getTexture() |
| 579 | glActiveTexture(gTextureUnits[textureUnit]); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 580 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 581 | ProgramDescription description; |
| 582 | description.hasTexture = true; |
| 583 | description.hasAlpha8Texture = true; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 584 | if (mShader) { |
| 585 | mShader->describe(description, mExtensions); |
| 586 | } |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 587 | if (mColorFilter) { |
| 588 | mColorFilter->describe(description, mExtensions); |
| 589 | } |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 590 | |
| 591 | useProgram(mProgramCache.get(description)); |
| 592 | mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 593 | |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 594 | // Text is always blended, no need to check the shader |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 595 | chooseBlending(true, mode); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 596 | bindTexture(mFontRenderer.getTexture(), GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit); |
| 597 | glUniform1i(mCurrentProgram->getUniform("sampler"), textureUnit); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 598 | |
| 599 | int texCoordsSlot = mCurrentProgram->getAttrib("texCoords"); |
| 600 | glEnableVertexAttribArray(texCoordsSlot); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 601 | |
| 602 | // Always premultiplied |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 603 | glUniform4f(mCurrentProgram->color, r, g, b, a); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 604 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 605 | textureUnit++; |
| 606 | // Setup attributes and uniforms required by the shaders |
| 607 | if (mShader) { |
| 608 | mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit); |
| 609 | } |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 610 | if (mColorFilter) { |
| 611 | mColorFilter->setupProgram(mCurrentProgram); |
| 612 | } |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 613 | |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 614 | // TODO: Implement scale properly |
| 615 | const Rect& clip = mSnapshot->getLocalClip(); |
Alex Sakhartchouk | 65ef909 | 2010-07-26 11:09:33 -0700 | [diff] [blame] | 616 | mFontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), paint->getTextSize()); |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 617 | mFontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 618 | |
| 619 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 620 | glDisableVertexAttribArray(texCoordsSlot); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 623 | void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) { |
| 624 | GLuint textureUnit = 0; |
| 625 | glActiveTexture(gTextureUnits[textureUnit]); |
| 626 | |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 627 | const PathTexture* texture = mPathCache.get(path, paint); |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 628 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 629 | const AutoTexture autoCleanup(texture); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 630 | |
| 631 | int alpha; |
| 632 | SkXfermode::Mode mode; |
| 633 | getAlphaAndMode(paint, &alpha, &mode); |
| 634 | |
| 635 | uint32_t color = paint->getColor(); |
| 636 | const GLfloat a = alpha / 255.0f; |
| 637 | const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; |
| 638 | const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; |
| 639 | const GLfloat b = a * ((color ) & 0xFF) / 255.0f; |
| 640 | |
| 641 | // Describe the required shaders |
| 642 | ProgramDescription description; |
| 643 | description.hasTexture = true; |
| 644 | description.hasAlpha8Texture = true; |
| 645 | if (mShader) { |
| 646 | mShader->describe(description, mExtensions); |
| 647 | } |
| 648 | if (mColorFilter) { |
| 649 | mColorFilter->describe(description, mExtensions); |
| 650 | } |
| 651 | |
| 652 | // Build and use the appropriate shader |
| 653 | useProgram(mProgramCache.get(description)); |
| 654 | |
| 655 | // Setup the blending mode |
| 656 | chooseBlending(true, mode); |
| 657 | bindTexture(texture->id, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit); |
| 658 | glUniform1i(mCurrentProgram->getUniform("sampler"), textureUnit); |
| 659 | |
| 660 | int texCoordsSlot = mCurrentProgram->getAttrib("texCoords"); |
| 661 | glEnableVertexAttribArray(texCoordsSlot); |
| 662 | |
| 663 | // Setup attributes |
| 664 | glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE, |
| 665 | gMeshStride, &mMeshVertices[0].position[0]); |
| 666 | glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, |
| 667 | gMeshStride, &mMeshVertices[0].texture[0]); |
| 668 | |
| 669 | // Setup uniforms |
| 670 | mModelView.loadTranslate(texture->left - texture->offset, |
| 671 | texture->top - texture->offset, 0.0f); |
| 672 | mModelView.scale(texture->width, texture->height, 1.0f); |
| 673 | mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform); |
| 674 | |
| 675 | glUniform4f(mCurrentProgram->color, r, g, b, a); |
| 676 | |
| 677 | textureUnit++; |
| 678 | // Setup attributes and uniforms required by the shaders |
| 679 | if (mShader) { |
| 680 | mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit); |
| 681 | } |
| 682 | if (mColorFilter) { |
| 683 | mColorFilter->setupProgram(mCurrentProgram); |
| 684 | } |
| 685 | |
| 686 | // Draw the mesh |
| 687 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
| 688 | |
| 689 | glDisableVertexAttribArray(texCoordsSlot); |
| 690 | } |
| 691 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 692 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 693 | // Shaders |
| 694 | /////////////////////////////////////////////////////////////////////////////// |
| 695 | |
| 696 | void OpenGLRenderer::resetShader() { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 697 | mShader = NULL; |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 700 | void OpenGLRenderer::setupShader(SkiaShader* shader) { |
| 701 | mShader = shader; |
| 702 | if (mShader) { |
| 703 | mShader->set(&mTextureCache, &mGradientCache); |
| 704 | } |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 707 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 708 | // Color filters |
| 709 | /////////////////////////////////////////////////////////////////////////////// |
| 710 | |
| 711 | void OpenGLRenderer::resetColorFilter() { |
| 712 | mColorFilter = NULL; |
| 713 | } |
| 714 | |
| 715 | void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) { |
| 716 | mColorFilter = filter; |
| 717 | } |
| 718 | |
| 719 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 720 | // Drawing implementation |
| 721 | /////////////////////////////////////////////////////////////////////////////// |
| 722 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 723 | void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, |
Romain Guy | 3d58c03 | 2010-07-14 16:34:53 -0700 | [diff] [blame] | 724 | int color, SkXfermode::Mode mode, bool ignoreTransform) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 725 | // If a shader is set, preserve only the alpha |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 726 | if (mShader) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 727 | color |= 0x00ffffff; |
| 728 | } |
| 729 | |
| 730 | // Render using pre-multiplied alpha |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 731 | const int alpha = (color >> 24) & 0xFF; |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 732 | const GLfloat a = alpha / 255.0f; |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 733 | const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; |
| 734 | const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; |
| 735 | const GLfloat b = a * ((color ) & 0xFF) / 255.0f; |
| 736 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 737 | GLuint textureUnit = 0; |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 738 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 739 | // Setup the blending mode |
| 740 | chooseBlending(alpha < 255 || (mShader && mShader->blend()), mode); |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 741 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 742 | // Describe the required shaders |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 743 | ProgramDescription description; |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 744 | if (mShader) { |
| 745 | mShader->describe(description, mExtensions); |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 746 | } |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 747 | if (mColorFilter) { |
| 748 | mColorFilter->describe(description, mExtensions); |
| 749 | } |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 750 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 751 | // Build and use the appropriate shader |
| 752 | useProgram(mProgramCache.get(description)); |
| 753 | |
| 754 | // Setup attributes |
| 755 | glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE, |
| 756 | gMeshStride, &mMeshVertices[0].position[0]); |
| 757 | |
| 758 | // Setup uniforms |
| 759 | mModelView.loadTranslate(left, top, 0.0f); |
| 760 | mModelView.scale(right - left, bottom - top, 1.0f); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 761 | if (!ignoreTransform) { |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 762 | mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 763 | } else { |
| 764 | mat4 identity; |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 765 | mCurrentProgram->set(mOrthoMatrix, mModelView, identity); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 766 | } |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 767 | glUniform4f(mCurrentProgram->color, r, g, b, a); |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 768 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 769 | // Setup attributes and uniforms required by the shaders |
| 770 | if (mShader) { |
| 771 | mShader->setupProgram(mCurrentProgram, mModelView, *mSnapshot, &textureUnit); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 772 | } |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 773 | if (mColorFilter) { |
| 774 | mColorFilter->setupProgram(mCurrentProgram); |
| 775 | } |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 776 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 777 | // Draw the mesh |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 778 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 781 | void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 782 | const Texture* texture, const SkPaint* paint) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 783 | int alpha; |
| 784 | SkXfermode::Mode mode; |
| 785 | getAlphaAndMode(paint, &alpha, &mode); |
| 786 | |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 787 | drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, texture->blend, |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 788 | &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL); |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 789 | } |
| 790 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 791 | void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 792 | GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) { |
| 793 | drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 794 | &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], NULL); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 798 | GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 799 | GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount) { |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 800 | ProgramDescription description; |
| 801 | description.hasTexture = true; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 802 | if (mColorFilter) { |
| 803 | mColorFilter->describe(description, mExtensions); |
| 804 | } |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 805 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 806 | mModelView.loadTranslate(left, top, 0.0f); |
| 807 | mModelView.scale(right - left, bottom - top, 1.0f); |
| 808 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 809 | useProgram(mProgramCache.get(description)); |
| 810 | mCurrentProgram->set(mOrthoMatrix, mModelView, mSnapshot->transform); |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 811 | |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 812 | chooseBlending(blend || alpha < 1.0f, mode); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 813 | |
| 814 | // Texture |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 815 | bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 816 | glUniform1i(mCurrentProgram->getUniform("sampler"), 0); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 817 | |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 818 | // Always premultiplied |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 819 | glUniform4f(mCurrentProgram->color, alpha, alpha, alpha, alpha); |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 820 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 821 | // Mesh |
| 822 | int texCoordsSlot = mCurrentProgram->getAttrib("texCoords"); |
| 823 | glEnableVertexAttribArray(texCoordsSlot); |
| 824 | glVertexAttribPointer(mCurrentProgram->position, 2, GL_FLOAT, GL_FALSE, |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 825 | gMeshStride, vertices); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 826 | glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords); |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 827 | |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 828 | // Color filter |
| 829 | if (mColorFilter) { |
| 830 | mColorFilter->setupProgram(mCurrentProgram); |
| 831 | } |
| 832 | |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 833 | if (!indices) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 834 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 835 | } else { |
| 836 | glDrawElements(GL_TRIANGLES, elementsCount, GL_UNSIGNED_SHORT, indices); |
| 837 | } |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 838 | glDisableVertexAttribArray(texCoordsSlot); |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 842 | blend = blend || mode != SkXfermode::kSrcOver_Mode; |
| 843 | if (blend) { |
| 844 | if (!mBlend) { |
| 845 | glEnable(GL_BLEND); |
| 846 | } |
| 847 | |
| 848 | GLenum sourceMode = gBlends[mode].src; |
| 849 | GLenum destMode = gBlends[mode].dst; |
| 850 | if (!isPremultiplied && sourceMode == GL_ONE) { |
| 851 | sourceMode = GL_SRC_ALPHA; |
| 852 | } |
| 853 | |
| 854 | if (sourceMode != mLastSrcMode || destMode != mLastDstMode) { |
| 855 | glBlendFunc(sourceMode, destMode); |
| 856 | mLastSrcMode = sourceMode; |
| 857 | mLastDstMode = destMode; |
| 858 | } |
| 859 | } else if (mBlend) { |
| 860 | glDisable(GL_BLEND); |
| 861 | } |
| 862 | mBlend = blend; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 865 | bool OpenGLRenderer::useProgram(Program* program) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 866 | if (!program->isInUse()) { |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 867 | if (mCurrentProgram != NULL) mCurrentProgram->remove(); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 868 | program->use(); |
| 869 | mCurrentProgram = program; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 870 | return false; |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 871 | } |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 872 | return true; |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 873 | } |
| 874 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 875 | void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 876 | TextureVertex* v = &mMeshVertices[0]; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 877 | TextureVertex::setUV(v++, u1, v1); |
| 878 | TextureVertex::setUV(v++, u2, v1); |
| 879 | TextureVertex::setUV(v++, u1, v2); |
| 880 | TextureVertex::setUV(v++, u2, v2); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) { |
| 884 | if (paint) { |
| 885 | const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode); |
| 886 | if (!isMode) { |
| 887 | // Assume SRC_OVER |
| 888 | *mode = SkXfermode::kSrcOver_Mode; |
| 889 | } |
| 890 | |
| 891 | // Skia draws using the color's alpha channel if < 255 |
| 892 | // Otherwise, it uses the paint's alpha |
| 893 | int color = paint->getColor(); |
| 894 | *alpha = (color >> 24) & 0xFF; |
| 895 | if (*alpha == 255) { |
| 896 | *alpha = paint->getAlpha(); |
| 897 | } |
| 898 | } else { |
| 899 | *mode = SkXfermode::kSrcOver_Mode; |
| 900 | *alpha = 255; |
| 901 | } |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 902 | } |
| 903 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 904 | void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) { |
| 905 | glActiveTexture(gTextureUnits[textureUnit]); |
Romain Guy | ae5575b | 2010-07-29 18:48:04 -0700 | [diff] [blame] | 906 | glBindTexture(GL_TEXTURE_2D, texture); |
Romain Guy | a1db574 | 2010-07-20 13:09:13 -0700 | [diff] [blame] | 907 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS); |
| 908 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT); |
| 909 | } |
| 910 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 911 | }; // namespace uirenderer |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 912 | }; // namespace android |