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