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