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