PatchCache.h revision 253f2c213f6ecda63b6872aee77bd30d5ec07c82
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#pragma once
18
19#include <GLES2/gl2.h>
20
21#include <utils/LruCache.h>
22
23#include <androidfw/ResourceTypes.h>
24
25#include "Debug.h"
26#include "utils/Pair.h"
27
28namespace android {
29namespace uirenderer {
30
31class Patch;
32
33///////////////////////////////////////////////////////////////////////////////
34// Defines
35///////////////////////////////////////////////////////////////////////////////
36
37// Debug
38#if DEBUG_PATCHES
39    #define PATCH_LOGD(...) ALOGD(__VA_ARGS__)
40#else
41    #define PATCH_LOGD(...)
42#endif
43
44///////////////////////////////////////////////////////////////////////////////
45// Cache
46///////////////////////////////////////////////////////////////////////////////
47
48class Caches;
49class RenderState;
50
51class PatchCache {
52public:
53    explicit PatchCache(RenderState& renderState);
54    ~PatchCache();
55
56    const Patch* get(const uint32_t bitmapWidth, const uint32_t bitmapHeight,
57            const float pixelWidth, const float pixelHeight, const Res_png_9patch* patch);
58    void clear();
59
60    uint32_t getSize() const {
61        return mSize;
62    }
63
64    uint32_t getMaxSize() const {
65        return mMaxSize;
66    }
67
68    GLuint getMeshBuffer() const {
69        return mMeshBuffer;
70    }
71
72    uint32_t getGenerationId() const {
73        return mGenerationId;
74    }
75
76    /**
77     * Removes the entries associated with the specified 9-patch. This is meant
78     * to be called from threads that are not the EGL context thread (GC thread
79     * on the VM side for instance.)
80     */
81    void removeDeferred(Res_png_9patch* patch);
82
83    /**
84     * Process deferred removals.
85     */
86    void clearGarbage();
87
88
89private:
90    struct PatchDescription {
91        PatchDescription(): mPatch(nullptr), mBitmapWidth(0), mBitmapHeight(0),
92                mPixelWidth(0), mPixelHeight(0) {
93        }
94
95        PatchDescription(const uint32_t bitmapWidth, const uint32_t bitmapHeight,
96                const float pixelWidth, const float pixelHeight, const Res_png_9patch* patch):
97                mPatch(patch), mBitmapWidth(bitmapWidth), mBitmapHeight(bitmapHeight),
98                mPixelWidth(pixelWidth), mPixelHeight(pixelHeight) {
99        }
100
101        hash_t hash() const;
102
103        const Res_png_9patch* getPatch() const { return mPatch; }
104
105        static int compare(const PatchDescription& lhs, const PatchDescription& rhs);
106
107        bool operator==(const PatchDescription& other) const {
108            return compare(*this, other) == 0;
109        }
110
111        bool operator!=(const PatchDescription& other) const {
112            return compare(*this, other) != 0;
113        }
114
115        friend inline int strictly_order_type(const PatchDescription& lhs,
116                const PatchDescription& rhs) {
117            return PatchDescription::compare(lhs, rhs) < 0;
118        }
119
120        friend inline int compare_type(const PatchDescription& lhs,
121                const PatchDescription& rhs) {
122            return PatchDescription::compare(lhs, rhs);
123        }
124
125        friend inline hash_t hash_type(const PatchDescription& entry) {
126            return entry.hash();
127        }
128
129    private:
130        const Res_png_9patch* mPatch;
131        uint32_t mBitmapWidth;
132        uint32_t mBitmapHeight;
133        float mPixelWidth;
134        float mPixelHeight;
135
136    }; // struct PatchDescription
137
138    /**
139     * A buffer block represents an empty range in the mesh buffer
140     * that can be used to store vertices.
141     *
142     * The patch cache maintains a linked-list of buffer blocks
143     * to track available regions of memory in the VBO.
144     */
145    struct BufferBlock {
146        BufferBlock(uint32_t offset, uint32_t size): offset(offset), size(size), next(nullptr) {
147        }
148
149        uint32_t offset;
150        uint32_t size;
151
152        BufferBlock* next;
153    }; // struct BufferBlock
154
155    typedef Pair<const PatchDescription*, Patch*> patch_pair_t;
156
157    void clearCache();
158    void createVertexBuffer();
159
160    void setupMesh(Patch* newMesh);
161
162    void remove(Vector<patch_pair_t>& patchesToRemove, Res_png_9patch* patch);
163
164#if DEBUG_PATCHES
165    void dumpFreeBlocks(const char* prefix);
166#endif
167
168    RenderState& mRenderState;
169    const uint32_t mMaxSize;
170    uint32_t mSize;
171
172    LruCache<PatchDescription, Patch*> mCache;
173
174    GLuint mMeshBuffer;
175    // First available free block inside the mesh buffer
176    BufferBlock* mFreeBlocks;
177
178    uint32_t mGenerationId;
179
180    // Garbage tracking, required to handle GC events on the VM side
181    Vector<Res_png_9patch*> mGarbage;
182    mutable Mutex mLock;
183}; // class PatchCache
184
185}; // namespace uirenderer
186}; // namespace android
187