blob: a3a8c43e0e081108377727558243e8f2faba7f2c [file] [log] [blame]
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05301/*
Tharaga Balachandran576571c2020-01-23 18:41:10 -05002 * Copyright (c) 2011-2018, 2020 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>
Tharaga Balachandran74ab1112020-01-08 17:17:56 -050024
Naseer Ahmede36f2242017-12-01 15:33:56 -050025#include <mutex>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053026#include <unordered_map>
Naseer Ahmede69031e2016-11-22 20:05:16 -050027#include <unordered_set>
28#include <utility>
Tharaga Balachandran74ab1112020-01-08 17:17:56 -050029#include <vector>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053030
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053031#include "gr_allocator.h"
Naseer Ahmede69031e2016-11-22 20:05:16 -050032#include "gr_buf_descriptor.h"
Tharaga Balachandran74ab1112020-01-08 17:17:56 -050033#include "gr_utils.h"
Naseer Ahmede36f2242017-12-01 15:33:56 -050034#include "gralloc_priv.h"
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053035
Naseer Ahmede36f2242017-12-01 15:33:56 -050036namespace gralloc {
Tharaga Balachandran74ab1112020-01-08 17:17:56 -050037using gralloc::Error;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053038class BufferManager {
39 public:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053040 ~BufferManager();
Naseer Ahmede69031e2016-11-22 20:05:16 -050041
Naseer Ahmede36f2242017-12-01 15:33:56 -050042 Error AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle,
Tharaga Balachandranab150ab2019-09-26 19:17:58 -040043 unsigned int bufferSize = 0, bool testAlloc = false);
Naseer Ahmede36f2242017-12-01 15:33:56 -050044 Error RetainBuffer(private_handle_t const *hnd);
45 Error ReleaseBuffer(private_handle_t const *hnd);
46 Error LockBuffer(const private_handle_t *hnd, uint64_t usage);
47 Error UnlockBuffer(const private_handle_t *hnd);
48 Error Dump(std::ostringstream *os);
Naseer Ahmed920d71b2018-03-08 16:54:28 -050049 Error ValidateBufferSize(private_handle_t const *hnd, BufferInfo info);
50 Error IsBufferImported(const private_handle_t *hnd);
Naseer Ahmede36f2242017-12-01 15:33:56 -050051 static BufferManager *GetInstance();
Tharaga Balachandran74ab1112020-01-08 17:17:56 -050052 Error GetMetadata(private_handle_t *handle, int64_t metadatatype_value, hidl_vec<uint8_t> *out);
53 Error SetMetadata(private_handle_t *handle, int64_t metadatatype_value, hidl_vec<uint8_t> in);
54 Error GetReservedRegion(private_handle_t *handle, void **reserved_region,
55 uint64_t *reserved_region_size);
56 Error FlushBuffer(const private_handle_t *handle);
57 Error RereadBuffer(const private_handle_t *handle);
58 Error GetAllHandles(std::vector<const private_handle_t *> *out_handle_list);
Tharaga Balachandran576571c2020-01-23 18:41:10 -050059 void SetGrallocDebugProperties(gralloc::GrallocProperties props);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053060
61 private:
Naseer Ahmede69031e2016-11-22 20:05:16 -050062 BufferManager();
Naseer Ahmede36f2242017-12-01 15:33:56 -050063 Error MapBuffer(private_handle_t const *hnd);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053064
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -040065 // Imports the ion fds into the current process. Returns an error for invalid handles
Naseer Ahmede36f2242017-12-01 15:33:56 -050066 Error ImportHandleLocked(private_handle_t *hnd);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -040067
68 // Creates a Buffer from the valid private handle and adds it to the map
Naseer Ahmed378d8582017-03-28 21:56:08 -040069 void RegisterHandleLocked(const private_handle_t *hnd, int ion_handle, int ion_handle_meta);
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -040070
Naseer Ahmede69031e2016-11-22 20:05:16 -050071 // Wrapper structure over private handle
72 // Values associated with the private handle
73 // that do not need to go over IPC can be placed here
74 // This structure is also not expected to be ABI stable
75 // unlike private_handle_t
76 struct Buffer {
77 const private_handle_t *handle = nullptr;
78 int ref_count = 1;
79 // Hold the main and metadata ion handles
80 // Freed from the allocator process
81 // and unused in the mapping process
82 int ion_handle_main = -1;
83 int ion_handle_meta = -1;
84
85 Buffer() = delete;
Naseer Ahmede36f2242017-12-01 15:33:56 -050086 explicit Buffer(const private_handle_t *h, int ih_main = -1, int ih_meta = -1)
87 : handle(h), ion_handle_main(ih_main), ion_handle_meta(ih_meta) {}
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -040088 void IncRef() { ++ref_count; }
89 bool DecRef() { return --ref_count == 0; }
Tharaga Balachandran40648032020-05-01 10:54:31 -040090 uint64_t reserved_size = 0;
91 void *reserved_region_ptr = nullptr;
Naseer Ahmede69031e2016-11-22 20:05:16 -050092 };
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -040093
Naseer Ahmede36f2242017-12-01 15:33:56 -050094 Error FreeBuffer(std::shared_ptr<Buffer> buf);
Naseer Ahmede69031e2016-11-22 20:05:16 -050095
Naseer Ahmed3a9d53a2017-03-15 19:21:40 -040096 // Get the wrapper Buffer object from the handle, returns nullptr if handle is not found
Naseer Ahmed378d8582017-03-28 21:56:08 -040097 std::shared_ptr<Buffer> GetBufferFromHandleLocked(const private_handle_t *hnd);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053098 Allocator *allocator_ = NULL;
Naseer Ahmed378d8582017-03-28 21:56:08 -040099 std::mutex buffer_lock_;
Naseer Ahmede36f2242017-12-01 15:33:56 -0500100 std::unordered_map<const private_handle_t *, std::shared_ptr<Buffer>> handles_map_ = {};
Naseer Ahmede69031e2016-11-22 20:05:16 -0500101 std::atomic<uint64_t> next_id_;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530102};
103
Naseer Ahmede36f2242017-12-01 15:33:56 -0500104} // namespace gralloc
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530105
106#endif // __GR_BUF_MGR_H__