1f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy/*
2f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy * Copyright (C) 2010 The Android Open Source Project
3f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy *
4f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy * Licensed under the Apache License, Version 2.0 (the "License");
5f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy * you may not use this file except in compliance with the License.
6f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy * You may obtain a copy of the License at
7f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy *
8f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy *      http://www.apache.org/licenses/LICENSE-2.0
9f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy *
10f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy * Unless required by applicable law or agreed to in writing, software
11f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy * distributed under the License is distributed on an "AS IS" BASIS,
12f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy * See the License for the specific language governing permissions and
14f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy * limitations under the License.
15f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy */
16f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
175e00c7ce063116c11315639f0035aca8ad73e8ccChris Craik#pragma once
18f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
193b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy#include <GLES2/gl2.h>
202728f961614a385df1f056fc24803a9f65c90fabRomain Guy
213b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy#include <utils/LruCache.h>
223b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
233b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy#include <androidfw/ResourceTypes.h>
243b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
25c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy#include "Debug.h"
26e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy#include "utils/Pair.h"
27f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
28f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guynamespace android {
29f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guynamespace uirenderer {
30f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
312dc236b2bae13b9a0ed9b3f7320502aecd7983b3Tom Hudsonclass Patch;
322dc236b2bae13b9a0ed9b3f7320502aecd7983b3Tom Hudson
33f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy///////////////////////////////////////////////////////////////////////////////
34f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy// Defines
35f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy///////////////////////////////////////////////////////////////////////////////
36f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
37f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy// Debug
38f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy#if DEBUG_PATCHES
395baa3a62a97544669fba6d65a11c07f252e654ddSteve Block    #define PATCH_LOGD(...) ALOGD(__VA_ARGS__)
40f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy#else
41f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy    #define PATCH_LOGD(...)
42f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy#endif
43f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
44f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy///////////////////////////////////////////////////////////////////////////////
45f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy// Cache
46f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy///////////////////////////////////////////////////////////////////////////////
47f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
483b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guyclass Caches;
495e00c7ce063116c11315639f0035aca8ad73e8ccChris Craikclass RenderState;
503b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
512728f961614a385df1f056fc24803a9f65c90fabRomain Guyclass PatchCache {
52f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guypublic:
53faecb78a6b11c780db47bc940ca7662899ab5d5eChih-Hung Hsieh    explicit PatchCache(RenderState& renderState);
54f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy    ~PatchCache();
55f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
56253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy    const Patch* get(const uint32_t bitmapWidth, const uint32_t bitmapHeight,
573b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy            const float pixelWidth, const float pixelHeight, const Res_png_9patch* patch);
58f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy    void clear();
59f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
60c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy    uint32_t getSize() const {
613b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        return mSize;
62c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy    }
63c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy
64c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy    uint32_t getMaxSize() const {
653b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        return mMaxSize;
663b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    }
673b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
683b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    GLuint getMeshBuffer() const {
693b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        return mMeshBuffer;
70c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy    }
71c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy
72e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy    /**
73e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy     * Removes the entries associated with the specified 9-patch. This is meant
74e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy     * to be called from threads that are not the EGL context thread (GC thread
75e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy     * on the VM side for instance.)
76e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy     */
77e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy    void removeDeferred(Res_png_9patch* patch);
783b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
79e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy    /**
80e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy     * Process deferred removals.
81e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy     */
82e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy    void clearGarbage();
83e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy
84e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy
85e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guyprivate:
866f72bebe92a4db7b5dc83f4ac5b5fd02e3b4e2cdRomain Guy    struct PatchDescription {
87e84a208317e0ed388fcdad1e6743c7849acb51b0Chris Craik        PatchDescription(): mPatch(nullptr), mBitmapWidth(0), mBitmapHeight(0),
883b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy                mPixelWidth(0), mPixelHeight(0) {
896f72bebe92a4db7b5dc83f4ac5b5fd02e3b4e2cdRomain Guy        }
906f72bebe92a4db7b5dc83f4ac5b5fd02e3b4e2cdRomain Guy
9113ba0054846ce630ca31e8f26169fd9388faee02Romain Guy        PatchDescription(const uint32_t bitmapWidth, const uint32_t bitmapHeight,
923b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy                const float pixelWidth, const float pixelHeight, const Res_png_9patch* patch):
933b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy                mPatch(patch), mBitmapWidth(bitmapWidth), mBitmapHeight(bitmapHeight),
943b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy                mPixelWidth(pixelWidth), mPixelHeight(pixelHeight) {
956f72bebe92a4db7b5dc83f4ac5b5fd02e3b4e2cdRomain Guy        }
966f72bebe92a4db7b5dc83f4ac5b5fd02e3b4e2cdRomain Guy
973b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        hash_t hash() const;
983b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
99e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy        const Res_png_9patch* getPatch() const { return mPatch; }
100e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy
10113ba0054846ce630ca31e8f26169fd9388faee02Romain Guy        static int compare(const PatchDescription& lhs, const PatchDescription& rhs);
10213ba0054846ce630ca31e8f26169fd9388faee02Romain Guy
10313ba0054846ce630ca31e8f26169fd9388faee02Romain Guy        bool operator==(const PatchDescription& other) const {
10413ba0054846ce630ca31e8f26169fd9388faee02Romain Guy            return compare(*this, other) == 0;
10513ba0054846ce630ca31e8f26169fd9388faee02Romain Guy        }
10613ba0054846ce630ca31e8f26169fd9388faee02Romain Guy
10713ba0054846ce630ca31e8f26169fd9388faee02Romain Guy        bool operator!=(const PatchDescription& other) const {
10813ba0054846ce630ca31e8f26169fd9388faee02Romain Guy            return compare(*this, other) != 0;
10913ba0054846ce630ca31e8f26169fd9388faee02Romain Guy        }
11013ba0054846ce630ca31e8f26169fd9388faee02Romain Guy
11113ba0054846ce630ca31e8f26169fd9388faee02Romain Guy        friend inline int strictly_order_type(const PatchDescription& lhs,
11213ba0054846ce630ca31e8f26169fd9388faee02Romain Guy                const PatchDescription& rhs) {
11313ba0054846ce630ca31e8f26169fd9388faee02Romain Guy            return PatchDescription::compare(lhs, rhs) < 0;
11413ba0054846ce630ca31e8f26169fd9388faee02Romain Guy        }
11513ba0054846ce630ca31e8f26169fd9388faee02Romain Guy
11613ba0054846ce630ca31e8f26169fd9388faee02Romain Guy        friend inline int compare_type(const PatchDescription& lhs,
11713ba0054846ce630ca31e8f26169fd9388faee02Romain Guy                const PatchDescription& rhs) {
11813ba0054846ce630ca31e8f26169fd9388faee02Romain Guy            return PatchDescription::compare(lhs, rhs);
1196f72bebe92a4db7b5dc83f4ac5b5fd02e3b4e2cdRomain Guy        }
1206f72bebe92a4db7b5dc83f4ac5b5fd02e3b4e2cdRomain Guy
1213b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        friend inline hash_t hash_type(const PatchDescription& entry) {
1223b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy            return entry.hash();
1233b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        }
1243b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy
1256f72bebe92a4db7b5dc83f4ac5b5fd02e3b4e2cdRomain Guy    private:
1263b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        const Res_png_9patch* mPatch;
1273b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        uint32_t mBitmapWidth;
1283b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        uint32_t mBitmapHeight;
1293b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        float mPixelWidth;
1303b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy        float mPixelHeight;
1316f72bebe92a4db7b5dc83f4ac5b5fd02e3b4e2cdRomain Guy
1326f72bebe92a4db7b5dc83f4ac5b5fd02e3b4e2cdRomain Guy    }; // struct PatchDescription
1336f72bebe92a4db7b5dc83f4ac5b5fd02e3b4e2cdRomain Guy
134e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy    /**
135e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy     * A buffer block represents an empty range in the mesh buffer
136e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy     * that can be used to store vertices.
137e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy     *
138e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy     * The patch cache maintains a linked-list of buffer blocks
139e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy     * to track available regions of memory in the VBO.
140e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy     */
141e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy    struct BufferBlock {
142e84a208317e0ed388fcdad1e6743c7849acb51b0Chris Craik        BufferBlock(uint32_t offset, uint32_t size): offset(offset), size(size), next(nullptr) {
143e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy        }
144e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy
145e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy        uint32_t offset;
146e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy        uint32_t size;
147e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy
148e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy        BufferBlock* next;
149e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy    }; // struct BufferBlock
150e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy
151e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy    typedef Pair<const PatchDescription*, Patch*> patch_pair_t;
152e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy
153e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy    void clearCache();
154e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy    void createVertexBuffer();
155e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy
1568820fd1d82acaefda98ae73ccf61413d5044f9f3Chris Craik    void setupMesh(Patch* newMesh);
157e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy
158e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy    void remove(Vector<patch_pair_t>& patchesToRemove, Res_png_9patch* patch);
159e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy
160e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy#if DEBUG_PATCHES
161e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy    void dumpFreeBlocks(const char* prefix);
162e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy#endif
163e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy
16496a5c4c7bab6718524de7253da8309143ab48befChris Craik    RenderState& mRenderState;
16548a8f431fa52ae2ee25ffba9d20676f03bb710ffChris Craik    const uint32_t mMaxSize;
1663b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    uint32_t mSize;
1676f72bebe92a4db7b5dc83f4ac5b5fd02e3b4e2cdRomain Guy
1684c2547fa9244e78115cde0a259291053108c3dc7Romain Guy    LruCache<PatchDescription, Patch*> mCache;
1694c2547fa9244e78115cde0a259291053108c3dc7Romain Guy
1703b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy    GLuint mMeshBuffer;
171e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy    // First available free block inside the mesh buffer
172e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy    BufferBlock* mFreeBlocks;
1737d9b1b3c02eb1ffd99742ecb7b69e3ab97d2ba18Romain Guy
174e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy    // Garbage tracking, required to handle GC events on the VM side
175e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy    Vector<Res_png_9patch*> mGarbage;
176e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy    mutable Mutex mLock;
177f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy}; // class PatchCache
178f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
179f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy}; // namespace uirenderer
180f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy}; // namespace android
181