blob: dffa788f46b51b8f64b323080015743ce5525c4e [file] [log] [blame]
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001/*
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/PixelFormat.h>
31
32#include <hardware/gralloc.h>
33
34
35namespace android {
36// ---------------------------------------------------------------------------
37
38class String8;
39
Mathias Agopian3330b202009-10-05 17:07:12 -070040class GraphicBufferAllocator : public Singleton<GraphicBufferAllocator>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070041{
42public:
43 enum {
44 USAGE_SW_READ_NEVER = GRALLOC_USAGE_SW_READ_NEVER,
45 USAGE_SW_READ_RARELY = GRALLOC_USAGE_SW_READ_RARELY,
46 USAGE_SW_READ_OFTEN = GRALLOC_USAGE_SW_READ_OFTEN,
47 USAGE_SW_READ_MASK = GRALLOC_USAGE_SW_READ_MASK,
48
49 USAGE_SW_WRITE_NEVER = GRALLOC_USAGE_SW_WRITE_NEVER,
50 USAGE_SW_WRITE_RARELY = GRALLOC_USAGE_SW_WRITE_RARELY,
51 USAGE_SW_WRITE_OFTEN = GRALLOC_USAGE_SW_WRITE_OFTEN,
52 USAGE_SW_WRITE_MASK = GRALLOC_USAGE_SW_WRITE_MASK,
53
54 USAGE_SOFTWARE_MASK = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK,
55
56 USAGE_HW_TEXTURE = GRALLOC_USAGE_HW_TEXTURE,
57 USAGE_HW_RENDER = GRALLOC_USAGE_HW_RENDER,
58 USAGE_HW_2D = GRALLOC_USAGE_HW_2D,
59 USAGE_HW_MASK = GRALLOC_USAGE_HW_MASK
60 };
61
Mathias Agopian3330b202009-10-05 17:07:12 -070062 static inline GraphicBufferAllocator& get() { return getInstance(); }
Mathias Agopian076b1cc2009-04-10 14:24:30 -070063
64
65 status_t alloc(uint32_t w, uint32_t h, PixelFormat format, int usage,
66 buffer_handle_t* handle, int32_t* stride);
67
68 status_t free(buffer_handle_t handle);
69
Mathias Agopian076b1cc2009-04-10 14:24:30 -070070 void dump(String8& res) const;
Mathias Agopian678bdd62010-12-03 17:33:09 -080071 static void dumpToSystemLog();
Mathias Agopian076b1cc2009-04-10 14:24:30 -070072
73private:
74 struct alloc_rec_t {
75 uint32_t w;
76 uint32_t h;
Mathias Agopian5629eb12010-04-15 14:57:39 -070077 uint32_t s;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070078 PixelFormat format;
79 uint32_t usage;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070080 size_t size;
81 };
82
83 static Mutex sLock;
84 static KeyedVector<buffer_handle_t, alloc_rec_t> sAllocList;
85
Mathias Agopian3330b202009-10-05 17:07:12 -070086 friend class Singleton<GraphicBufferAllocator>;
87 GraphicBufferAllocator();
88 ~GraphicBufferAllocator();
Mathias Agopian076b1cc2009-04-10 14:24:30 -070089
Mathias Agopian076b1cc2009-04-10 14:24:30 -070090 alloc_device_t *mAllocDev;
91};
92
93// ---------------------------------------------------------------------------
94}; // namespace android
95
96#endif // ANDROID_BUFFER_ALLOCATOR_H