Romain Guy | ce0537b | 2010-06-29 21:05:21 -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 | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 17 | #define LOG_TAG "OpenGLRenderer" |
| 18 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 19 | #include <GLES2/gl2.h> |
| 20 | |
| 21 | #include "TextureCache.h" |
| 22 | |
| 23 | namespace android { |
| 24 | namespace uirenderer { |
| 25 | |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | // Constructors/destructor |
| 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 30 | TextureCache::TextureCache(uint32_t maxByteSize): |
Romain Guy | 6c81893 | 2010-07-07 15:15:32 -0700 | [diff] [blame] | 31 | mCache(GenerationCache<SkBitmap*, Texture*>::kUnlimitedCapacity), |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 32 | mSize(0), mMaxSize(maxByteSize) { |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 33 | mCache.setOnEntryRemovedListener(this); |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame^] | 34 | |
| 35 | GLint maxTextureSize; |
| 36 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize); |
| 37 | LOGD("Maximum texture dimension is %d pixels", maxTextureSize); |
| 38 | mMaxTextureSize = maxTextureSize; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | TextureCache::~TextureCache() { |
| 42 | mCache.clear(); |
| 43 | } |
| 44 | |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 45 | /////////////////////////////////////////////////////////////////////////////// |
| 46 | // Size management |
| 47 | /////////////////////////////////////////////////////////////////////////////// |
| 48 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 49 | uint32_t TextureCache::getSize() { |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 50 | return mSize; |
| 51 | } |
| 52 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 53 | uint32_t TextureCache::getMaxSize() { |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 54 | return mMaxSize; |
| 55 | } |
| 56 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 57 | void TextureCache::setMaxSize(uint32_t maxSize) { |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 58 | mMaxSize = maxSize; |
| 59 | while (mSize > mMaxSize) { |
| 60 | mCache.removeOldest(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 64 | /////////////////////////////////////////////////////////////////////////////// |
| 65 | // Callbacks |
| 66 | /////////////////////////////////////////////////////////////////////////////// |
| 67 | |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 68 | void TextureCache::operator()(SkBitmap*& bitmap, Texture*& texture) { |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 69 | if (bitmap) { |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 70 | const uint32_t size = bitmap->rowBytes() * bitmap->height(); |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 71 | mSize -= size; |
| 72 | } |
| 73 | |
| 74 | if (texture) { |
| 75 | glDeleteTextures(1, &texture->id); |
| 76 | delete texture; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /////////////////////////////////////////////////////////////////////////////// |
| 81 | // Caching |
| 82 | /////////////////////////////////////////////////////////////////////////////// |
| 83 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 84 | Texture* TextureCache::get(SkBitmap* bitmap) { |
| 85 | Texture* texture = mCache.get(bitmap); |
| 86 | if (!texture) { |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame^] | 87 | if (bitmap->width() > mMaxTextureSize || bitmap->height() > mMaxTextureSize) { |
| 88 | LOGW("Bitmap too large to be uploaded into a texture"); |
| 89 | return NULL; |
| 90 | } |
| 91 | |
Romain Guy | 7d139ba | 2010-07-02 11:20:34 -0700 | [diff] [blame] | 92 | const uint32_t size = bitmap->rowBytes() * bitmap->height(); |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 93 | // Don't even try to cache a bitmap that's bigger than the cache |
| 94 | if (size < mMaxSize) { |
| 95 | while (mSize + size > mMaxSize) { |
| 96 | mCache.removeOldest(); |
| 97 | } |
| 98 | } |
| 99 | |
Romain Guy | 364703c | 2010-06-30 15:51:03 -0700 | [diff] [blame] | 100 | texture = new Texture; |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 101 | generateTexture(bitmap, texture, false); |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 102 | |
| 103 | if (size < mMaxSize) { |
| 104 | mSize += size; |
| 105 | mCache.put(bitmap, texture); |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 106 | } else { |
| 107 | texture->cleanup = true; |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 108 | } |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 109 | } else if (bitmap->getGenerationID() != texture->generation) { |
| 110 | generateTexture(bitmap, texture, true); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 111 | } |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 112 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 113 | return texture; |
| 114 | } |
| 115 | |
Romain Guy | 121e224 | 2010-07-01 18:26:52 -0700 | [diff] [blame] | 116 | void TextureCache::remove(SkBitmap* bitmap) { |
| 117 | mCache.remove(bitmap); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void TextureCache::clear() { |
| 121 | mCache.clear(); |
| 122 | } |
| 123 | |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 124 | void TextureCache::generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate) { |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 125 | SkAutoLockPixels alp(*bitmap); |
| 126 | if (!bitmap->readyToDraw()) { |
| 127 | LOGE("Cannot generate texture from bitmap"); |
| 128 | return; |
| 129 | } |
| 130 | |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 131 | if (!regenerate) { |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 132 | texture->generation = bitmap->getGenerationID(); |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 133 | texture->width = bitmap->width(); |
| 134 | texture->height = bitmap->height(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 135 | |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 136 | glGenTextures(1, &texture->id); |
| 137 | } |
| 138 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 139 | glBindTexture(GL_TEXTURE_2D, texture->id); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 140 | glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel()); |
| 141 | |
| 142 | switch (bitmap->getConfig()) { |
Romain Guy | bd0e6aa | 2010-07-22 18:50:12 -0700 | [diff] [blame] | 143 | case SkBitmap::kA8_Config: |
| 144 | texture->blend = true; |
| 145 | glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, bitmap->rowBytesAsPixels(), texture->height, 0, |
| 146 | GL_ALPHA, GL_UNSIGNED_BYTE, bitmap->getPixels()); |
| 147 | break; |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 148 | case SkBitmap::kRGB_565_Config: |
| 149 | texture->blend = false; |
| 150 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmap->rowBytesAsPixels(), texture->height, 0, |
| 151 | GL_RGB, GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels()); |
| 152 | break; |
| 153 | case SkBitmap::kARGB_8888_Config: |
Romain Guy | 594f406 | 2010-07-13 17:41:31 -0700 | [diff] [blame] | 154 | texture->blend = !bitmap->isOpaque(); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 155 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height, 0, |
| 156 | GL_RGBA, GL_UNSIGNED_BYTE, bitmap->getPixels()); |
| 157 | break; |
| 158 | default: |
| 159 | break; |
| 160 | } |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 161 | |
| 162 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 163 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 164 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 165 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 166 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | }; // namespace uirenderer |
| 170 | }; // namespace android |