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 | |
| 17 | #include <GLES2/gl2.h> |
| 18 | |
| 19 | #include "TextureCache.h" |
| 20 | |
| 21 | namespace android { |
| 22 | namespace uirenderer { |
| 23 | |
| 24 | TextureCache::TextureCache(unsigned int maxEntries): mCache(maxEntries) { |
| 25 | mCache.setOnEntryRemovedListener(this); |
| 26 | } |
| 27 | |
| 28 | TextureCache::~TextureCache() { |
| 29 | mCache.clear(); |
| 30 | } |
| 31 | |
| 32 | void TextureCache::operator()(SkBitmap* key, Texture* value) { |
| 33 | LOGD("Entry removed"); |
| 34 | if (value) { |
| 35 | glDeleteTextures(1, &value->id); |
| 36 | delete value; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | Texture* TextureCache::get(SkBitmap* bitmap) { |
| 41 | Texture* texture = mCache.get(bitmap); |
| 42 | if (!texture) { |
Romain Guy | 364703c | 2010-06-30 15:51:03 -0700 | [diff] [blame] | 43 | texture = new Texture; |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame^] | 44 | generateTexture(bitmap, texture, false); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 45 | mCache.put(bitmap, texture); |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 46 | } else if (bitmap->getGenerationID() != texture->generation) { |
| 47 | generateTexture(bitmap, texture, true); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 48 | } |
| 49 | return texture; |
| 50 | } |
| 51 | |
| 52 | Texture* TextureCache::remove(SkBitmap* bitmap) { |
| 53 | return mCache.remove(bitmap); |
| 54 | } |
| 55 | |
| 56 | void TextureCache::clear() { |
| 57 | mCache.clear(); |
| 58 | } |
| 59 | |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 60 | void TextureCache::generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate) { |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame^] | 61 | SkAutoLockPixels alp(*bitmap); |
| 62 | if (!bitmap->readyToDraw()) { |
| 63 | LOGE("Cannot generate texture from bitmap"); |
| 64 | return; |
| 65 | } |
| 66 | |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 67 | if (!regenerate) { |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame^] | 68 | texture->generation = bitmap->getGenerationID(); |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 69 | texture->width = bitmap->width(); |
| 70 | texture->height = bitmap->height(); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 71 | |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 72 | glGenTextures(1, &texture->id); |
| 73 | } |
| 74 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 75 | glBindTexture(GL_TEXTURE_2D, texture->id); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame^] | 76 | glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel()); |
| 77 | |
| 78 | switch (bitmap->getConfig()) { |
| 79 | case SkBitmap::kRGB_565_Config: |
| 80 | texture->blend = false; |
| 81 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmap->rowBytesAsPixels(), texture->height, 0, |
| 82 | GL_RGB, GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels()); |
| 83 | break; |
| 84 | case SkBitmap::kARGB_8888_Config: |
| 85 | texture->blend = true; |
| 86 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height, 0, |
| 87 | GL_RGBA, GL_UNSIGNED_BYTE, bitmap->getPixels()); |
| 88 | break; |
| 89 | default: |
| 90 | break; |
| 91 | } |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 92 | |
| 93 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 94 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 95 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 96 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 97 | |
Romain Guy | fe88094 | 2010-06-30 16:05:32 -0700 | [diff] [blame] | 98 | glBindTexture(GL_TEXTURE_2D, 0); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | }; // namespace uirenderer |
| 102 | }; // namespace android |