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 namespace Gralloc2 {
38 class Allocator;
39 }
40 
41 class GraphicBufferMapper;
42 class String8;
43 
44 class GraphicBufferAllocator : public Singleton<GraphicBufferAllocator>
45 {
46 public:
get()47     static inline GraphicBufferAllocator& get() { return getInstance(); }
48 
49     status_t allocate(uint32_t w, uint32_t h, PixelFormat format,
50             uint32_t layerCount, uint64_t usage,
51             buffer_handle_t* handle, uint32_t* stride, uint64_t graphicBufferId,
52             std::string requestorName);
53 
54     status_t free(buffer_handle_t handle);
55 
56     void dump(String8& res) const;
57     static void dumpToSystemLog();
58 
59 private:
60     struct alloc_rec_t {
61         uint32_t width;
62         uint32_t height;
63         uint32_t stride;
64         PixelFormat format;
65         uint32_t layerCount;
66         uint64_t usage;
67         size_t size;
68         std::string requestorName;
69     };
70 
71     static Mutex sLock;
72     static KeyedVector<buffer_handle_t, alloc_rec_t> sAllocList;
73 
74     friend class Singleton<GraphicBufferAllocator>;
75     GraphicBufferAllocator();
76     ~GraphicBufferAllocator();
77 
78     GraphicBufferMapper& mMapper;
79     const std::unique_ptr<const Gralloc2::Allocator> mAllocator;
80 };
81 
82 // ---------------------------------------------------------------------------
83 }; // namespace android
84 
85 #endif // ANDROID_BUFFER_ALLOCATOR_H
86