1 /*
2 **
3 ** Copyright 2009, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 #ifndef ANDROID_BUFFER_ALLOCATOR_H
19 #define ANDROID_BUFFER_ALLOCATOR_H
20 
21 #include <stdint.h>
22 
23 #include <memory>
24 #include <string>
25 
26 #include <cutils/native_handle.h>
27 
28 #include <ui/PixelFormat.h>
29 
30 #include <utils/Errors.h>
31 #include <utils/KeyedVector.h>
32 #include <utils/Mutex.h>
33 #include <utils/Singleton.h>
34 
35 namespace android {
36 
37 class GrallocAllocator;
38 class GraphicBufferMapper;
39 
40 class GraphicBufferAllocator : public Singleton<GraphicBufferAllocator>
41 {
42 public:
get()43     static inline GraphicBufferAllocator& get() { return getInstance(); }
44 
45     status_t allocate(uint32_t w, uint32_t h, PixelFormat format,
46             uint32_t layerCount, uint64_t usage,
47             buffer_handle_t* handle, uint32_t* stride, uint64_t graphicBufferId,
48             std::string requestorName);
49 
50     status_t free(buffer_handle_t handle);
51 
52     uint64_t getTotalSize() const;
53 
54     void dump(std::string& res) const;
55     static void dumpToSystemLog();
56 
57 protected:
58     struct alloc_rec_t {
59         uint32_t width;
60         uint32_t height;
61         uint32_t stride;
62         PixelFormat format;
63         uint32_t layerCount;
64         uint64_t usage;
65         size_t size;
66         std::string requestorName;
67     };
68 
69     static Mutex sLock;
70     static KeyedVector<buffer_handle_t, alloc_rec_t> sAllocList;
71 
72     friend class Singleton<GraphicBufferAllocator>;
73     GraphicBufferAllocator();
74     ~GraphicBufferAllocator();
75 
76     GraphicBufferMapper& mMapper;
77     std::unique_ptr<const GrallocAllocator> mAllocator;
78 };
79 
80 // ---------------------------------------------------------------------------
81 }; // namespace android
82 
83 #endif // ANDROID_BUFFER_ALLOCATOR_H
84