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     /**
46      * Allocates and imports a gralloc buffer.
47      *
48      * The handle must be freed with GraphicBufferAllocator::free() when no longer needed.
49      */
50     status_t allocate(uint32_t w, uint32_t h, PixelFormat format, uint32_t layerCount,
51                       uint64_t usage, buffer_handle_t* handle, uint32_t* stride,
52                       std::string requestorName);
53 
54     /**
55      * Allocates and does NOT import a gralloc buffer. Buffers cannot be used until they have
56      * been imported. This function is for advanced use cases only.
57      *
58      * The raw native handle must be freed by calling native_handle_close() followed by
59      * native_handle_delete().
60      */
61     status_t allocateRawHandle(uint32_t w, uint32_t h, PixelFormat format, uint32_t layerCount,
62                                uint64_t usage, buffer_handle_t* handle, uint32_t* stride,
63                                std::string requestorName);
64 
65     /**
66      * DEPRECATED: GraphicBufferAllocator does not use the graphicBufferId.
67      */
68     status_t allocate(uint32_t w, uint32_t h, PixelFormat format,
69             uint32_t layerCount, uint64_t usage,
70             buffer_handle_t* handle, uint32_t* stride, uint64_t graphicBufferId,
71             std::string requestorName);
72 
73     status_t free(buffer_handle_t handle);
74 
75     uint64_t getTotalSize() const;
76 
77     void dump(std::string& res, bool less = true) const;
78     static void dumpToSystemLog(bool less = true);
79 
80 protected:
81     struct alloc_rec_t {
82         uint32_t width;
83         uint32_t height;
84         uint32_t stride;
85         PixelFormat format;
86         uint32_t layerCount;
87         uint64_t usage;
88         size_t size;
89         std::string requestorName;
90     };
91 
92     status_t allocateHelper(uint32_t w, uint32_t h, PixelFormat format, uint32_t layerCount,
93                             uint64_t usage, buffer_handle_t* handle, uint32_t* stride,
94                             std::string requestorName, bool importBuffer);
95 
96     static Mutex sLock;
97     static KeyedVector<buffer_handle_t, alloc_rec_t> sAllocList;
98 
99     friend class Singleton<GraphicBufferAllocator>;
100     GraphicBufferAllocator();
101     ~GraphicBufferAllocator();
102 
103     GraphicBufferMapper& mMapper;
104     std::unique_ptr<const GrallocAllocator> mAllocator;
105 };
106 
107 // ---------------------------------------------------------------------------
108 }; // namespace android
109 
110 #endif // ANDROID_BUFFER_ALLOCATOR_H
111