blob: 08ff2ef314009012031b1c8057ab0c54f78e3b94 [file] [log] [blame]
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05301/*
Naseer Ahmede69031e2016-11-22 20:05:16 -05002 * Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05303 * Not a Contribution
4 *
5 * Copyright (C) 2008 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#ifndef __GR_BUF_MGR_H__
21#define __GR_BUF_MGR_H__
22
23#include <pthread.h>
24#include <unordered_map>
Naseer Ahmede69031e2016-11-22 20:05:16 -050025#include <unordered_set>
26#include <utility>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053027#include <mutex>
28
29#include "gralloc_priv.h"
30#include "gr_allocator.h"
Naseer Ahmede69031e2016-11-22 20:05:16 -050031#include "gr_buf_descriptor.h"
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053032
33namespace gralloc1 {
34
35class BufferManager {
36 public:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053037 ~BufferManager();
Naseer Ahmede69031e2016-11-22 20:05:16 -050038 gralloc1_error_t CreateBufferDescriptor(gralloc1_buffer_descriptor_t *descriptor_id);
39 gralloc1_error_t DestroyBufferDescriptor(gralloc1_buffer_descriptor_t descriptor_id);
40 gralloc1_error_t AllocateBuffers(uint32_t num_descriptors,
41 const gralloc1_buffer_descriptor_t *descriptor_ids,
42 buffer_handle_t *out_buffers);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053043 gralloc1_error_t RetainBuffer(private_handle_t const *hnd);
44 gralloc1_error_t ReleaseBuffer(private_handle_t const *hnd);
45 gralloc1_error_t LockBuffer(const private_handle_t *hnd, gralloc1_producer_usage_t prod_usage,
46 gralloc1_consumer_usage_t cons_usage);
47 gralloc1_error_t UnlockBuffer(const private_handle_t *hnd);
48 gralloc1_error_t Perform(int operation, va_list args);
Naseer Ahmede69031e2016-11-22 20:05:16 -050049 gralloc1_error_t GetFlexLayout(const private_handle_t *hnd, struct android_flex_layout *layout);
50 gralloc1_error_t GetNumFlexPlanes(const private_handle_t *hnd, uint32_t *out_num_planes);
51
52 template <typename... Args>
53 gralloc1_error_t CallBufferDescriptorFunction(gralloc1_buffer_descriptor_t descriptor_id,
54 void (BufferDescriptor::*member)(Args...),
55 Args... args) {
56 std::lock_guard<std::mutex> lock(locker_);
57 const auto map_descriptor = descriptors_map_.find(descriptor_id);
58 if (map_descriptor == descriptors_map_.end()) {
59 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
60 }
61 const auto descriptor = map_descriptor->second;
62 (descriptor.get()->*member)(std::forward<Args>(args)...);
63 return GRALLOC1_ERROR_NONE;
64 }
65
66 static BufferManager* GetInstance() {
67 static BufferManager *instance = new BufferManager();
68 return instance;
69 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053070
71 private:
Naseer Ahmede69031e2016-11-22 20:05:16 -050072 BufferManager();
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053073 gralloc1_error_t MapBuffer(private_handle_t const *hnd);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053074 int GetBufferType(int format);
75 int AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle,
76 unsigned int bufferSize = 0);
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -070077 int AllocateBuffer(unsigned int size, int aligned_w, int aligned_h, int unaligned_w,
78 int unaligned_h, int format, int bufferType,
79 gralloc1_producer_usage_t prod_usage, gralloc1_consumer_usage_t cons_usage,
80 buffer_handle_t *handle);
Naseer Ahmede69031e2016-11-22 20:05:16 -050081 uint32_t GetDataAlignment(int format, gralloc1_producer_usage_t prod_usage,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053082 gralloc1_consumer_usage_t cons_usage);
83 int GetHandleFlags(int format, gralloc1_producer_usage_t prod_usage,
84 gralloc1_consumer_usage_t cons_usage);
85 void CreateSharedHandle(buffer_handle_t inbuffer, const BufferDescriptor &descriptor,
86 buffer_handle_t *out_buffer);
87
Naseer Ahmede69031e2016-11-22 20:05:16 -050088 // Wrapper structure over private handle
89 // Values associated with the private handle
90 // that do not need to go over IPC can be placed here
91 // This structure is also not expected to be ABI stable
92 // unlike private_handle_t
93 struct Buffer {
94 const private_handle_t *handle = nullptr;
95 int ref_count = 1;
96 // Hold the main and metadata ion handles
97 // Freed from the allocator process
98 // and unused in the mapping process
99 int ion_handle_main = -1;
100 int ion_handle_meta = -1;
101
102 Buffer() = delete;
103 explicit Buffer(const private_handle_t* h, int ih_main = -1, int ih_meta = -1):
104 handle(h),
105 ion_handle_main(ih_main),
106 ion_handle_meta(ih_meta) {
107 }
108 };
109 gralloc1_error_t FreeBuffer(std::shared_ptr<Buffer> buf);
110
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530111 bool map_fb_mem_ = false;
112 bool ubwc_for_fb_ = false;
113 Allocator *allocator_ = NULL;
114 std::mutex locker_;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500115 std::unordered_map<uint64_t, std::shared_ptr<Buffer>> handles_map_ = {};
116 std::unordered_map<gralloc1_buffer_descriptor_t,
117 std::shared_ptr<BufferDescriptor>> descriptors_map_ = {};
118 std::atomic<uint64_t> next_id_;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530119};
120
121} // namespace gralloc1
122
123#endif // __GR_BUF_MGR_H__