• 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 
18 #ifndef ANDROID_BUFFER_ALLOCATOR_H
19 #define ANDROID_BUFFER_ALLOCATOR_H
20 
21 #include <stdint.h>
22 
23 #include <cutils/native_handle.h>
24 
25 #include <utils/Errors.h>
26 #include <utils/KeyedVector.h>
27 #include <utils/threads.h>
28 #include <utils/Singleton.h>
29 
30 #include <ui/Gralloc1.h>
31 #include <ui/PixelFormat.h>
32 
33 namespace android {
34 
35 class Gralloc1Loader;
36 class String8;
37 
38 class GraphicBufferAllocator : public Singleton<GraphicBufferAllocator>
39 {
40 public:
41     enum {
42         USAGE_SW_READ_NEVER     = GRALLOC1_CONSUMER_USAGE_CPU_READ_NEVER,
43         USAGE_SW_READ_RARELY    = GRALLOC1_CONSUMER_USAGE_CPU_READ,
44         USAGE_SW_READ_OFTEN     = GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN,
45         USAGE_SW_READ_MASK      = GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN,
46 
47         USAGE_SW_WRITE_NEVER    = GRALLOC1_PRODUCER_USAGE_CPU_WRITE_NEVER,
48         USAGE_SW_WRITE_RARELY   = GRALLOC1_PRODUCER_USAGE_CPU_WRITE,
49         USAGE_SW_WRITE_OFTEN    = GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN,
50         USAGE_SW_WRITE_MASK     = GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN,
51 
52         USAGE_SOFTWARE_MASK     = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK,
53 
54         USAGE_HW_TEXTURE        = GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE,
55         USAGE_HW_RENDER         = GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET,
56         USAGE_HW_2D             = 0x00000400, // Deprecated
57         USAGE_HW_MASK           = 0x00071F00, // Deprecated
58     };
59 
get()60     static inline GraphicBufferAllocator& get() { return getInstance(); }
61 
62     status_t allocate(uint32_t w, uint32_t h, PixelFormat format,
63             uint32_t usage, buffer_handle_t* handle, uint32_t* stride,
64             uint64_t graphicBufferId, std::string requestorName);
65 
66     status_t free(buffer_handle_t handle);
67 
68     void dump(String8& res) const;
69     static void dumpToSystemLog();
70 
71 private:
72     struct alloc_rec_t {
73         uint32_t width;
74         uint32_t height;
75         uint32_t stride;
76         PixelFormat format;
77         uint32_t usage;
78         size_t size;
79         std::string requestorName;
80     };
81 
82     static Mutex sLock;
83     static KeyedVector<buffer_handle_t, alloc_rec_t> sAllocList;
84 
85     friend class Singleton<GraphicBufferAllocator>;
86     GraphicBufferAllocator();
87     ~GraphicBufferAllocator();
88 
89     std::unique_ptr<Gralloc1::Loader> mLoader;
90     std::unique_ptr<Gralloc1::Device> mDevice;
91 };
92 
93 // ---------------------------------------------------------------------------
94 }; // namespace android
95 
96 #endif // ANDROID_BUFFER_ALLOCATOR_H
97