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 #include <vector>
26 
27 #include <cutils/native_handle.h>
28 
29 #include <ui/PixelFormat.h>
30 
31 #include <utils/Errors.h>
32 #include <utils/KeyedVector.h>
33 #include <utils/Mutex.h>
34 #include <utils/Singleton.h>
35 
36 namespace android {
37 
38 class GrallocAllocator;
39 class GraphicBufferMapper;
40 
41 class GraphicBufferAllocator : public Singleton<GraphicBufferAllocator>
42 {
43 public:
get()44     static inline GraphicBufferAllocator& get() { return getInstance(); }
45 
46     struct AdditionalOptions {
47         const char* name;
48         int64_t value;
49     };
50 
51     struct AllocationRequest {
52         bool importBuffer;
53         uint32_t width;
54         uint32_t height;
55         PixelFormat format;
56         uint32_t layerCount;
57         uint64_t usage;
58         std::string requestorName;
59         std::vector<AdditionalOptions> extras;
60     };
61 
62     struct AllocationResult {
63         status_t status;
64         buffer_handle_t handle = nullptr;
65         uint32_t stride = 0;
66 
AllocationResultAllocationResult67         explicit AllocationResult(status_t status) : status(status) {}
68 
AllocationResultAllocationResult69         explicit AllocationResult(buffer_handle_t handle, uint32_t stride)
70               : status(OK), handle(handle), stride(stride) {}
71     };
72 
73     AllocationResult allocate(const AllocationRequest&);
74 
75     /**
76      * Allocates and imports a gralloc buffer.
77      *
78      * The handle must be freed with GraphicBufferAllocator::free() when no longer needed.
79      */
80     status_t allocate(uint32_t w, uint32_t h, PixelFormat format, uint32_t layerCount,
81                       uint64_t usage, buffer_handle_t* handle, uint32_t* stride,
82                       std::string requestorName);
83 
84     /**
85      * Allocates and does NOT import a gralloc buffer. Buffers cannot be used until they have
86      * been imported. This function is for advanced use cases only.
87      *
88      * The raw native handle must be freed by calling native_handle_close() followed by
89      * native_handle_delete().
90      */
91     status_t allocateRawHandle(uint32_t w, uint32_t h, PixelFormat format, uint32_t layerCount,
92                                uint64_t usage, buffer_handle_t* handle, uint32_t* stride,
93                                std::string requestorName);
94 
95     /**
96      * DEPRECATED: GraphicBufferAllocator does not use the graphicBufferId.
97      */
98     status_t allocate(uint32_t w, uint32_t h, PixelFormat format,
99             uint32_t layerCount, uint64_t usage,
100             buffer_handle_t* handle, uint32_t* stride, uint64_t graphicBufferId,
101             std::string requestorName);
102 
103     status_t free(buffer_handle_t handle);
104 
105     uint64_t getTotalSize() const;
106 
107     void dump(std::string& res, bool less = true) const;
108     static void dumpToSystemLog(bool less = true);
109 
110     bool supportsAdditionalOptions() const;
111 
112 protected:
113     struct alloc_rec_t {
114         uint32_t width;
115         uint32_t height;
116         uint32_t stride;
117         PixelFormat format;
118         uint32_t layerCount;
119         uint64_t usage;
120         size_t size;
121         std::string requestorName;
122     };
123 
124     status_t allocateHelper(uint32_t w, uint32_t h, PixelFormat format, uint32_t layerCount,
125                             uint64_t usage, buffer_handle_t* handle, uint32_t* stride,
126                             std::string requestorName, bool importBuffer);
127 
128     static Mutex sLock;
129     static KeyedVector<buffer_handle_t, alloc_rec_t> sAllocList;
130 
131     friend class Singleton<GraphicBufferAllocator>;
132     GraphicBufferAllocator();
133     ~GraphicBufferAllocator();
134 
135     GraphicBufferMapper& mMapper;
136     std::unique_ptr<const GrallocAllocator> mAllocator;
137 };
138 
139 // ---------------------------------------------------------------------------
140 }; // namespace android
141 
142 #endif // ANDROID_BUFFER_ALLOCATOR_H
143