• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef ANDROID_BUFFER_ALLOCATOR_H
18 #define ANDROID_BUFFER_ALLOCATOR_H
19 #include <stdint.h>
20 #include <memory>
21 #include <string>
22 #include <cutils/native_handle.h>
23 #include <ui/PixelFormat.h>
24 #include <utils/Errors.h>
25 #include <utils/Mutex.h>
26 #include <utils/Singleton.h>
27 
28 #include <unordered_map>
29 
30 namespace android {
31 namespace Gralloc2 {
32 class Allocator;
33 }
34 class GraphicBufferMapper;
35 class String8;
36 class GraphicBufferAllocator : public Singleton<GraphicBufferAllocator>
37 {
38 public:
get()39     static inline GraphicBufferAllocator& get() { return getInstance(); }
40     status_t allocate(uint32_t w, uint32_t h, PixelFormat format,
41             uint32_t layerCount, uint64_t usage,
42             buffer_handle_t* handle, uint32_t* stride, uint64_t graphicBufferId,
43             std::string requestorName);
44     status_t free(buffer_handle_t handle);
45     void dump(String8& res) const;
46     static void dumpToSystemLog();
47 private:
48     struct alloc_rec_t {
49         uint32_t width;
50         uint32_t height;
51         uint32_t stride;
52         PixelFormat format;
53         uint32_t layerCount;
54         uint64_t usage;
55         size_t size;
56         std::string requestorName;
57     };
58     static Mutex sLock;
59     static std::unordered_map<buffer_handle_t, alloc_rec_t> sAllocList;
60     friend class Singleton<GraphicBufferAllocator>;
61     GraphicBufferAllocator();
62     ~GraphicBufferAllocator();
63     GraphicBufferMapper& mMapper;
64     const std::unique_ptr<const Gralloc2::Allocator> mAllocator;
65 };
66 // ---------------------------------------------------------------------------
67 }; // namespace android
68 #endif // ANDROID_BUFFER_ALLOCATOR_H
69