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
22059e12ccd20f5c249724a8362d6bac325334ea76Romain Guy#include <utils/LruCache.h>
23029f64303b8fe506ef34c12777da86b830d3bf7aDerek Sollenberger#include <utils/Mutex.h>
24fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guy
25c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy#include "Debug.h"
26ce0537b80087a6225273040a987414b1dd081aa0Romain Guy
27272a685f17cc4828257e521a6f62b7b17870f75eJohn Reck#include <vector>
2838e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck#include <unordered_map>
29272a685f17cc4828257e521a6f62b7b17870f75eJohn Reck
30ce0537b80087a6225273040a987414b1dd081aa0Romain Guynamespace android {
31ce0537b80087a6225273040a987414b1dd081aa0Romain Guynamespace uirenderer {
32ce0537b80087a6225273040a987414b1dd081aa0Romain Guy
332dc236b2bae13b9a0ed9b3f7320502aecd7983b3Tom Hudsonclass Texture;
342dc236b2bae13b9a0ed9b3f7320502aecd7983b3Tom Hudson
35d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase///////////////////////////////////////////////////////////////////////////////
36d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase// Defines
37d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase///////////////////////////////////////////////////////////////////////////////
38d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase
39d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase// Debug
40d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase#if DEBUG_TEXTURES
415baa3a62a97544669fba6d65a11c07f252e654ddSteve Block    #define TEXTURE_LOGD(...) ALOGD(__VA_ARGS__)
42d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase#else
43d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase    #define TEXTURE_LOGD(...)
44d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase#endif
45d98aa2de9ab18e09c2be1997f41212740f51f6e6Chet Haase
469e10841c27d973b930e1b49a099c69d866659505Romain Guy///////////////////////////////////////////////////////////////////////////////
479e10841c27d973b930e1b49a099c69d866659505Romain Guy// Classes
489e10841c27d973b930e1b49a099c69d866659505Romain Guy///////////////////////////////////////////////////////////////////////////////
499e10841c27d973b930e1b49a099c69d866659505Romain Guy
50ebd52610cfeff6e557fde284a7e1efc5e6438285John Reckclass AssetAtlas;
51ebd52610cfeff6e557fde284a7e1efc5e6438285John Reck
52121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy/**
53121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy * A simple LRU texture cache. The cache has a maximum size expressed in bytes.
54121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy * Any texture added to the cache causing the cache to grow beyond the maximum
55121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy * allowed size will also cause the oldest texture to be kicked out.
56121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy */
57117bdbcfa3e8306dad21e7e01fa71b00cdfa7265Chris Craikclass TextureCache : public OnEntryRemoved<uint32_t, Texture*> {
58ce0537b80087a6225273040a987414b1dd081aa0Romain Guypublic:
59fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    TextureCache();
60ce0537b80087a6225273040a987414b1dd081aa0Romain Guy    ~TextureCache();
61ce0537b80087a6225273040a987414b1dd081aa0Romain Guy
62121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy    /**
63121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     * Used as a callback when an entry is removed from the cache.
64121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     * Do not invoke directly.
65121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     */
66e84a208317e0ed388fcdad1e6743c7849acb51b0Chris Craik    void operator()(uint32_t&, Texture*& texture) override;
67ce0537b80087a6225273040a987414b1dd081aa0Romain Guy
68121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy    /**
69860d155f866cc15a725e7ce03763280987f24901John Reck     * Resets all Textures to not be marked as in use
70860d155f866cc15a725e7ce03763280987f24901John Reck     */
7100e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck    void resetMarkInUse(void* ownerToken);
72860d155f866cc15a725e7ce03763280987f24901John Reck
73860d155f866cc15a725e7ce03763280987f24901John Reck    /**
74860d155f866cc15a725e7ce03763280987f24901John Reck     * Attempts to precache the SkBitmap. Returns true if a Texture was successfully
75860d155f866cc15a725e7ce03763280987f24901John Reck     * acquired for the bitmap, false otherwise. If a Texture was acquired it is
76860d155f866cc15a725e7ce03763280987f24901John Reck     * marked as in use.
77860d155f866cc15a725e7ce03763280987f24901John Reck     */
7800e79c9947b741194ff6c0d08ede9b3befbf9c9dJohn Reck    bool prefetchAndMarkInUse(void* ownerToken, const SkBitmap* bitmap);
79860d155f866cc15a725e7ce03763280987f24901John Reck
80860d155f866cc15a725e7ce03763280987f24901John Reck    /**
816ad690e16f8e139bfd29a035b52ab616d813a74bChris Craik     * Returns the texture associated with the specified bitmap from either within the cache, or
826ad690e16f8e139bfd29a035b52ab616d813a74bChris Craik     * the AssetAtlas. If the texture cannot be found in the cache, a new texture is generated.
83121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     */
846ad690e16f8e139bfd29a035b52ab616d813a74bChris Craik    Texture* get(const SkBitmap* bitmap) {
856ad690e16f8e139bfd29a035b52ab616d813a74bChris Craik        return get(bitmap, AtlasUsageType::Use);
866ad690e16f8e139bfd29a035b52ab616d813a74bChris Craik    }
876ad690e16f8e139bfd29a035b52ab616d813a74bChris Craik
886ad690e16f8e139bfd29a035b52ab616d813a74bChris Craik    /**
896ad690e16f8e139bfd29a035b52ab616d813a74bChris Craik     * Returns the texture associated with the specified bitmap. If the texture cannot be found in
906ad690e16f8e139bfd29a035b52ab616d813a74bChris Craik     * the cache, a new texture is generated, even if it resides in the AssetAtlas.
916ad690e16f8e139bfd29a035b52ab616d813a74bChris Craik     */
926ad690e16f8e139bfd29a035b52ab616d813a74bChris Craik    Texture* getAndBypassAtlas(const SkBitmap* bitmap) {
936ad690e16f8e139bfd29a035b52ab616d813a74bChris Craik        return get(bitmap, AtlasUsageType::Bypass);
946ad690e16f8e139bfd29a035b52ab616d813a74bChris Craik    }
9571d08a07189c2b433080203895d9d5e936b16b9eJohn Reck
96121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy    /**
973d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger     * Removes the texture associated with the specified pixelRef. This is meant
98fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guy     * to be called from threads that are not the EGL context thread.
99fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guy     */
1003d4eed7f1aa99401dabe2e45b82f98fb4fc2d754Derek Sollenberger    ANDROID_API void releaseTexture(uint32_t pixelRefStableID);
101fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guy    /**
102fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guy     * Process deferred removals.
103fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guy     */
104fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guy    void clearGarbage();
105fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guy
106fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guy    /**
107121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     * Clears the cache. This causes all textures to be deleted.
108121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     */
109ce0537b80087a6225273040a987414b1dd081aa0Romain Guy    void clear();
110ce0537b80087a6225273040a987414b1dd081aa0Romain Guy
111121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy    /**
112121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     * Returns the maximum size of the cache in bytes.
113121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     */
1147d139ba2c331f11e9b485753cc727a0ff202f2a4Romain Guy    uint32_t getMaxSize();
115121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy    /**
116121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     * Returns the current size of the cache in bytes.
117121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy     */
1187d139ba2c331f11e9b485753cc727a0ff202f2a4Romain Guy    uint32_t getSize();
119121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy
120eca0ca2424afc1e98912405906edfc32f7733e16Romain Guy    /**
121eca0ca2424afc1e98912405906edfc32f7733e16Romain Guy     * Partially flushes the cache. The amount of memory freed by a flush
122eca0ca2424afc1e98912405906edfc32f7733e16Romain Guy     * is defined by the flush rate.
123eca0ca2424afc1e98912405906edfc32f7733e16Romain Guy     */
124eca0ca2424afc1e98912405906edfc32f7733e16Romain Guy    void flush();
125eca0ca2424afc1e98912405906edfc32f7733e16Romain Guy
126ebd52610cfeff6e557fde284a7e1efc5e6438285John Reck    void setAssetAtlas(AssetAtlas* assetAtlas);
127ebd52610cfeff6e557fde284a7e1efc5e6438285John Reck
128ce0537b80087a6225273040a987414b1dd081aa0Romain Guyprivate:
1296ad690e16f8e139bfd29a035b52ab616d813a74bChris Craik    enum class AtlasUsageType {
1306ad690e16f8e139bfd29a035b52ab616d813a74bChris Craik        Use,
1316ad690e16f8e139bfd29a035b52ab616d813a74bChris Craik        Bypass,
1326ad690e16f8e139bfd29a035b52ab616d813a74bChris Craik    };
133860d155f866cc15a725e7ce03763280987f24901John Reck
134860d155f866cc15a725e7ce03763280987f24901John Reck    bool canMakeTextureFromBitmap(const SkBitmap* bitmap);
135860d155f866cc15a725e7ce03763280987f24901John Reck
1366ad690e16f8e139bfd29a035b52ab616d813a74bChris Craik    Texture* get(const SkBitmap* bitmap, AtlasUsageType atlasUsageType);
1376ad690e16f8e139bfd29a035b52ab616d813a74bChris Craik    Texture* getCachedTexture(const SkBitmap* bitmap, AtlasUsageType atlasUsageType);
138860d155f866cc15a725e7ce03763280987f24901John Reck
13971d08a07189c2b433080203895d9d5e936b16b9eJohn Reck    LruCache<uint32_t, Texture*> mCache;
140121e2242565d5f09ad83a2d33ecd2225838802c5Romain Guy
1417d139ba2c331f11e9b485753cc727a0ff202f2a4Romain Guy    uint32_t mSize;
14248a8f431fa52ae2ee25ffba9d20676f03bb710ffChris Craik    const uint32_t mMaxSize;
143163935113919a184122b8b3bd672ef08c8df65dcRomain Guy    GLint mMaxTextureSize;
1449aaa8269a3e7291aab84d01c3fc9c744d8f2d2f4Romain Guy
14548a8f431fa52ae2ee25ffba9d20676f03bb710ffChris Craik    const float mFlushRate;
146eca0ca2424afc1e98912405906edfc32f7733e16Romain Guy
147e190aa69756aecfaffabdd4c6d32cb6b3220d842Romain Guy    bool mDebugEnabled;
148e190aa69756aecfaffabdd4c6d32cb6b3220d842Romain Guy
149272a685f17cc4828257e521a6f62b7b17870f75eJohn Reck    std::vector<uint32_t> mGarbage;
1509aaa8269a3e7291aab84d01c3fc9c744d8f2d2f4Romain Guy    mutable Mutex mLock;
151ebd52610cfeff6e557fde284a7e1efc5e6438285John Reck
152ebd52610cfeff6e557fde284a7e1efc5e6438285John Reck    AssetAtlas* mAssetAtlas;
153ce0537b80087a6225273040a987414b1dd081aa0Romain Guy}; // class TextureCache
154ce0537b80087a6225273040a987414b1dd081aa0Romain Guy
155ce0537b80087a6225273040a987414b1dd081aa0Romain Guy}; // namespace uirenderer
156ce0537b80087a6225273040a987414b1dd081aa0Romain Guy}; // namespace android
157ce0537b80087a6225273040a987414b1dd081aa0Romain Guy
1585b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif // ANDROID_HWUI_TEXTURE_CACHE_H
159