blob: e97122bbdf0f10ea684153568ae357eaa32e2ace [file] [log] [blame]
Dan Stoza303b9a52014-11-17 12:03:59 -08001/*
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002**
3** Copyright 2009, The Android Open Source Project
4**
Dan Stoza303b9a52014-11-17 12:03:59 -08005** 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
Mathias Agopian076b1cc2009-04-10 14:24:30 -07008**
Dan Stoza303b9a52014-11-17 12:03:59 -08009** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopian076b1cc2009-04-10 14:24:30 -070010**
Dan Stoza303b9a52014-11-17 12:03:59 -080011** 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
Mathias Agopian076b1cc2009-04-10 14:24:30 -070015** limitations under the License.
16*/
17
18#ifndef ANDROID_BUFFER_ALLOCATOR_H
19#define ANDROID_BUFFER_ALLOCATOR_H
20
21#include <stdint.h>
22
Mathias Agopianfe2f54f2017-02-15 19:48:58 -080023#include <memory>
24#include <string>
25
Mathias Agopian076b1cc2009-04-10 14:24:30 -070026#include <cutils/native_handle.h>
27
28#include <utils/Errors.h>
29#include <utils/KeyedVector.h>
Mathias Agopianfe2f54f2017-02-15 19:48:58 -080030#include <utils/Mutex.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070031#include <utils/Singleton.h>
32
Dan Stoza8deb4da2016-06-01 18:21:44 -070033#include <ui/Gralloc1.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070034#include <ui/PixelFormat.h>
35
Mathias Agopian076b1cc2009-04-10 14:24:30 -070036namespace android {
Mathias Agopian076b1cc2009-04-10 14:24:30 -070037
Chia-I Wu9ba189d2016-09-22 17:13:08 +080038namespace Gralloc2 {
39class Allocator;
40}
41
Chia-I Wu9ba189d2016-09-22 17:13:08 +080042class GraphicBufferMapper;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070043class String8;
44
Mathias Agopian3330b202009-10-05 17:07:12 -070045class GraphicBufferAllocator : public Singleton<GraphicBufferAllocator>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070046{
47public:
48 enum {
Dan Stoza8deb4da2016-06-01 18:21:44 -070049 USAGE_SW_READ_NEVER = GRALLOC1_CONSUMER_USAGE_CPU_READ_NEVER,
50 USAGE_SW_READ_RARELY = GRALLOC1_CONSUMER_USAGE_CPU_READ,
51 USAGE_SW_READ_OFTEN = GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN,
52 USAGE_SW_READ_MASK = GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN,
Dan Stoza303b9a52014-11-17 12:03:59 -080053
Dan Stoza8deb4da2016-06-01 18:21:44 -070054 USAGE_SW_WRITE_NEVER = GRALLOC1_PRODUCER_USAGE_CPU_WRITE_NEVER,
55 USAGE_SW_WRITE_RARELY = GRALLOC1_PRODUCER_USAGE_CPU_WRITE,
56 USAGE_SW_WRITE_OFTEN = GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN,
57 USAGE_SW_WRITE_MASK = GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN,
Dan Stoza303b9a52014-11-17 12:03:59 -080058
Mathias Agopian076b1cc2009-04-10 14:24:30 -070059 USAGE_SOFTWARE_MASK = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK,
Dan Stoza303b9a52014-11-17 12:03:59 -080060
Dan Stoza8deb4da2016-06-01 18:21:44 -070061 USAGE_HW_TEXTURE = GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE,
62 USAGE_HW_RENDER = GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET,
63 USAGE_HW_2D = 0x00000400, // Deprecated
64 USAGE_HW_MASK = 0x00071F00, // Deprecated
Mathias Agopian076b1cc2009-04-10 14:24:30 -070065 };
66
Mathias Agopian3330b202009-10-05 17:07:12 -070067 static inline GraphicBufferAllocator& get() { return getInstance(); }
Mathias Agopian076b1cc2009-04-10 14:24:30 -070068
Dan Stoza8deb4da2016-06-01 18:21:44 -070069 status_t allocate(uint32_t w, uint32_t h, PixelFormat format,
Craig Donnere96a3252017-02-02 12:13:34 -080070 uint32_t layerCount, uint64_t producerUsage, uint64_t consumerUsage,
71 buffer_handle_t* handle, uint32_t* stride, uint64_t graphicBufferId,
Craig Donner6ebc46a2016-10-21 15:23:44 -070072 std::string requestorName);
Mathias Agopian076b1cc2009-04-10 14:24:30 -070073
74 status_t free(buffer_handle_t handle);
75
Mathias Agopian076b1cc2009-04-10 14:24:30 -070076 void dump(String8& res) const;
Mathias Agopian678bdd62010-12-03 17:33:09 -080077 static void dumpToSystemLog();
Mathias Agopian076b1cc2009-04-10 14:24:30 -070078
79private:
80 struct alloc_rec_t {
Dan Stoza303b9a52014-11-17 12:03:59 -080081 uint32_t width;
82 uint32_t height;
83 uint32_t stride;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070084 PixelFormat format;
Craig Donner6ebc46a2016-10-21 15:23:44 -070085 uint32_t layerCount;
Craig Donnere96a3252017-02-02 12:13:34 -080086 uint64_t producerUsage;
87 uint64_t consumerUsage;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070088 size_t size;
Dan Stoza024e9312016-08-24 12:17:29 -070089 std::string requestorName;
Mathias Agopian076b1cc2009-04-10 14:24:30 -070090 };
Dan Stoza303b9a52014-11-17 12:03:59 -080091
Mathias Agopian076b1cc2009-04-10 14:24:30 -070092 static Mutex sLock;
93 static KeyedVector<buffer_handle_t, alloc_rec_t> sAllocList;
Dan Stoza303b9a52014-11-17 12:03:59 -080094
Mathias Agopian3330b202009-10-05 17:07:12 -070095 friend class Singleton<GraphicBufferAllocator>;
96 GraphicBufferAllocator();
97 ~GraphicBufferAllocator();
Dan Stoza303b9a52014-11-17 12:03:59 -080098
Chia-I Wu9ba189d2016-09-22 17:13:08 +080099 const std::unique_ptr<const Gralloc2::Allocator> mAllocator;
100 GraphicBufferMapper& mMapper;
101
Dan Stoza8deb4da2016-06-01 18:21:44 -0700102 std::unique_ptr<Gralloc1::Loader> mLoader;
103 std::unique_ptr<Gralloc1::Device> mDevice;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700104};
105
106// ---------------------------------------------------------------------------
107}; // namespace android
108
109#endif // ANDROID_BUFFER_ALLOCATOR_H