TextureCache.h revision c15008e72ec00ca20a271c3006dac649fd07533b
1ce0537b80087a6225273040a987414b1dd081aa0Romain Guy/*
2ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * Copyright (C) 2010 The Android Open Source Project
3ce0537b80087a6225273040a987414b1dd081aa0Romain Guy *
4ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * Licensed under the Apache License, Version 2.0 (the "License");
5ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * you may not use this file except in compliance with the License.
6ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * You may obtain a copy of the License at
7ce0537b80087a6225273040a987414b1dd081aa0Romain Guy *
8ce0537b80087a6225273040a987414b1dd081aa0Romain Guy *      http://www.apache.org/licenses/LICENSE-2.0
9ce0537b80087a6225273040a987414b1dd081aa0Romain Guy *
10ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * Unless required by applicable law or agreed to in writing, software
11ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * distributed under the License is distributed on an "AS IS" BASIS,
12ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * See the License for the specific language governing permissions and
14ce0537b80087a6225273040a987414b1dd081aa0Romain Guy * limitations under the License.
15ce0537b80087a6225273040a987414b1dd081aa0Romain Guy */
16ce0537b80087a6225273040a987414b1dd081aa0Romain Guy
175b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#ifndef ANDROID_HWUI_TEXTURE_CACHE_H
185b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#define ANDROID_HWUI_TEXTURE_CACHE_H
19ce0537b80087a6225273040a987414b1dd081aa0Romain Guy
20ce0537b80087a6225273040a987414b1dd081aa0Romain Guy#include <SkBitmap.h>
21ce0537b80087a6225273040a987414b1dd081aa0Romain Guy
22c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy#include "Debug.h"
23ce0537b80087a6225273040a987414b1dd081aa0Romain Guy#include "Texture.h"
2421b028a44f3e0bd9b0f0432b8b92c45f661d22a4Romain Guy#include "utils/GenerationCache.h"
25ce0537b80087a6225273040a987414b1dd081aa0Romain Guy
26ce0537b80087a6225273040a987414b1dd081aa0Romain Guynamespace android {
27ce0537b80087a6225273040a987414b1dd081aa0Romain Guynamespace uirenderer {
28ce0537b80087a6225273040a987414b1dd081aa0Romain Guy
29d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase///////////////////////////////////////////////////////////////////////////////
30d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase// Defines
31d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase///////////////////////////////////////////////////////////////////////////////
32d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase
33d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase// Debug
34d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase#if DEBUG_TEXTURES
35d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase    #define TEXTURE_LOGD(...) LOGD(__VA_ARGS__)
36d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase#else
37d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase    #define TEXTURE_LOGD(...)
38d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase#endif
39d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase
409e10841c27d973b930e1b49a099c69d866659505Romain Guy///////////////////////////////////////////////////////////////////////////////
419e10841c27d973b930e1b49a099c69d866659505Romain Guy// Classes
429e10841c27d973b930e1b49a099c69d866659505Romain Guy///////////////////////////////////////////////////////////////////////////////
439e10841c27d973b930e1b49a099c69d866659505Romain Guy
44121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy/**
45121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy * A simple LRU texture cache. The cache has a maximum size expressed in bytes.
46121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy * Any texture added to the cache causing the cache to grow beyond the maximum
47121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy * allowed size will also cause the oldest texture to be kicked out.
48121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy */
49ce0537b80087a6225273040a987414b1dd081aa0Romain Guyclass TextureCache: public OnEntryRemoved<SkBitmap*, Texture*> {
50ce0537b80087a6225273040a987414b1dd081aa0Romain Guypublic:
51fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    TextureCache();
527d139ba2c331f11e9b485753cc727a0ff202f2a4Romain Guy    TextureCache(uint32_t maxByteSize);
53ce0537b80087a6225273040a987414b1dd081aa0Romain Guy    ~TextureCache();
54ce0537b80087a6225273040a987414b1dd081aa0Romain Guy
55121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy    /**
56121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     * Used as a callback when an entry is removed from the cache.
57121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     * Do not invoke directly.
58121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     */
59dda570201ac851dd85af3861f7e575721d3345daRomain Guy    void operator()(SkBitmap*& bitmap, Texture*& texture);
60ce0537b80087a6225273040a987414b1dd081aa0Romain Guy
61121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy    /**
62121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     * Returns the texture associated with the specified bitmap. If the texture
63121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     * cannot be found in the cache, a new texture is generated.
64121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     */
65ce0537b80087a6225273040a987414b1dd081aa0Romain Guy    Texture* get(SkBitmap* bitmap);
66121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy    /**
67121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     * Removes the texture associated with the specified bitmap. Returns NULL
68121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     * if the texture cannot be found. Upon remove the texture is freed.
69121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     */
70121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy    void remove(SkBitmap* bitmap);
71121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy    /**
72121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     * Clears the cache. This causes all textures to be deleted.
73121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     */
74ce0537b80087a6225273040a987414b1dd081aa0Romain Guy    void clear();
75ce0537b80087a6225273040a987414b1dd081aa0Romain Guy
76121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy    /**
77121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     * Sets the maximum size of the cache in bytes.
78121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     */
797d139ba2c331f11e9b485753cc727a0ff202f2a4Romain Guy    void setMaxSize(uint32_t maxSize);
80121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy    /**
81121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     * Returns the maximum size of the cache in bytes.
82121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     */
837d139ba2c331f11e9b485753cc727a0ff202f2a4Romain Guy    uint32_t getMaxSize();
84121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy    /**
85121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     * Returns the current size of the cache in bytes.
86121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     */
877d139ba2c331f11e9b485753cc727a0ff202f2a4Romain Guy    uint32_t getSize();
88121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy
89ce0537b80087a6225273040a987414b1dd081aa0Romain Guyprivate:
90121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy    /**
91121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     * Generates the texture from a bitmap into the specified texture structure.
92121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     *
93121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     * @param regenerate If true, the bitmap data is reuploaded into the texture, but
94121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     *        no new texture is generated.
95121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     */
96fe8809471a40cac8acc984adfa51c39e13e83947Romain Guy    void generateTexture(SkBitmap* bitmap, Texture* texture, bool regenerate = false);
97ce0537b80087a6225273040a987414b1dd081aa0Romain Guy
985b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    void uploadLoFiTexture(bool resize, SkBitmap* bitmap, uint32_t width, uint32_t height);
998c749f87d29e1a363ddf9027c3a51753c612d510Romain Guy    void uploadToTexture(bool resize, GLenum format, GLsizei width, GLsizei height,
1008c749f87d29e1a363ddf9027c3a51753c612d510Romain Guy            GLenum type, const GLvoid * data);
1018c749f87d29e1a363ddf9027c3a51753c612d510Romain Guy
102fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    void init();
103fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy
1046c81893c626499e58c8eeb20d6c35ec4e1ce808bRomain Guy    GenerationCache<SkBitmap*, Texture*> mCache;
105121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy
1067d139ba2c331f11e9b485753cc727a0ff202f2a4Romain Guy    uint32_t mSize;
1077d139ba2c331f11e9b485753cc727a0ff202f2a4Romain Guy    uint32_t mMaxSize;
108163935113919a184122b8b3bd672ef08c8df65dcRomain Guy    GLint mMaxTextureSize;
1099aaa8269a3e7291aab84d01c3fc9c744d8f2d2f4Romain Guy
110a2341a9f6addcd79723965ec5b1a1c5ae0f8bd65Romain Guy    /**
111a2341a9f6addcd79723965ec5b1a1c5ae0f8bd65Romain Guy     * Used to access mCache and mSize. All methods are accessed from a single
112a2341a9f6addcd79723965ec5b1a1c5ae0f8bd65Romain Guy     * thread except for remove().
113a2341a9f6addcd79723965ec5b1a1c5ae0f8bd65Romain Guy     */
1149aaa8269a3e7291aab84d01c3fc9c744d8f2d2f4Romain Guy    mutable Mutex mLock;
115ce0537b80087a6225273040a987414b1dd081aa0Romain Guy}; // class TextureCache
116ce0537b80087a6225273040a987414b1dd081aa0Romain Guy
117ce0537b80087a6225273040a987414b1dd081aa0Romain Guy}; // namespace uirenderer
118ce0537b80087a6225273040a987414b1dd081aa0Romain Guy}; // namespace android
119ce0537b80087a6225273040a987414b1dd081aa0Romain Guy
1205b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif // ANDROID_HWUI_TEXTURE_CACHE_H
121