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