blob: c3b14630612426b4fdfba51252be8ed496a325aa [file] [log] [blame]
Romain Guyce0537b2010-06-29 21:05:21 -07001/*
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
21namespace android {
22namespace uirenderer {
23
24TextureCache::TextureCache(unsigned int maxEntries): mCache(maxEntries) {
25 mCache.setOnEntryRemovedListener(this);
26}
27
28TextureCache::~TextureCache() {
29 mCache.clear();
30}
31
32void TextureCache::operator()(SkBitmap* key, Texture* value) {
33 LOGD("Entry removed");
34 if (value) {
35 glDeleteTextures(1, &value->id);
36 delete value;
37 }
38}
39
40Texture* TextureCache::get(SkBitmap* bitmap) {
41 Texture* texture = mCache.get(bitmap);
42 if (!texture) {
Romain Guy364703c2010-06-30 15:51:03 -070043 texture = new Texture;
44 generateTexture(bitmap, texture);
Romain Guyce0537b2010-06-29 21:05:21 -070045 mCache.put(bitmap, texture);
Romain Guyfe880942010-06-30 16:05:32 -070046 } else if (bitmap->getGenerationID() != texture->generation) {
47 generateTexture(bitmap, texture, true);
Romain Guyce0537b2010-06-29 21:05:21 -070048 }
49 return texture;
50}
51
52Texture* TextureCache::remove(SkBitmap* bitmap) {
53 return mCache.remove(bitmap);
54}
55
56void TextureCache::clear() {
57 mCache.clear();
58}
59
Romain Guyfe880942010-06-30 16:05:32 -070060void TextureCache::generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate) {
61 if (!regenerate) {
62 texture->width = bitmap->width();
63 texture->height = bitmap->height();
Romain Guyce0537b2010-06-29 21:05:21 -070064
Romain Guyfe880942010-06-30 16:05:32 -070065 glGenTextures(1, &texture->id);
66 }
67
Romain Guyce0537b2010-06-29 21:05:21 -070068 glBindTexture(GL_TEXTURE_2D, texture->id);
69
70 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
71 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
72 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
73 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
74
75 switch (bitmap->getConfig()) {
76 case SkBitmap::kRGB_565_Config:
77 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB565, texture->width, texture->height,
78 0, GL_RGB565, GL_UNSIGNED_SHORT_5_6_5, bitmap->getPixels());
79 break;
80 case SkBitmap::kARGB_8888_Config:
81 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->width, texture->height,
82 0, GL_RGBA, GL_UNSIGNED_BYTE, bitmap->getPixels());
83 break;
Romain Guy364703c2010-06-30 15:51:03 -070084 default:
85 break;
Romain Guyce0537b2010-06-29 21:05:21 -070086 }
87
Romain Guyfe880942010-06-30 16:05:32 -070088 glBindTexture(GL_TEXTURE_2D, 0);
Romain Guyce0537b2010-06-29 21:05:21 -070089}
90
91}; // namespace uirenderer
92}; // namespace android