1 /* 2 * Copyright (c) 2011-2018, 2020 The Linux Foundation. All rights reserved. 3 * 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 25 #include <mutex> 26 #include <unordered_map> 27 #include <unordered_set> 28 #include <utility> 29 #include <vector> 30 31 #include "gr_allocator.h" 32 #include "gr_buf_descriptor.h" 33 #include "gr_utils.h" 34 #include "gralloc_priv.h" 35 36 namespace gralloc { 37 using gralloc::Error; 38 class BufferManager { 39 public: 40 ~BufferManager(); 41 42 Error AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle, 43 unsigned int bufferSize = 0, bool testAlloc = false); 44 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); 49 void BuffersDump(); 50 Error ValidateBufferSize(private_handle_t const *hnd, BufferInfo info); 51 Error IsBufferImported(const private_handle_t *hnd); 52 static BufferManager *GetInstance(); 53 void SetGrallocDebugProperties(gralloc::GrallocProperties props); 54 Error GetMetadata(private_handle_t *handle, int64_t metadatatype_value, hidl_vec<uint8_t> *out); 55 Error SetMetadata(private_handle_t *handle, int64_t metadatatype_value, hidl_vec<uint8_t> in); 56 Error GetReservedRegion(private_handle_t *handle, void **reserved_region, 57 uint64_t *reserved_region_size); 58 Error FlushBuffer(const private_handle_t *handle); 59 Error RereadBuffer(const private_handle_t *handle); 60 Error GetAllHandles(std::vector<const private_handle_t *> *out_handle_list); 61 62 private: 63 BufferManager(); 64 65 Error MapBuffer(private_handle_t const *hnd); 66 67 // Imports the ion fds into the current process. Returns an error for invalid handles 68 Error ImportHandleLocked(private_handle_t *hnd); 69 70 // Creates a Buffer from the valid private handle and adds it to the map 71 void RegisterHandleLocked(const private_handle_t *hnd, int ion_handle, int ion_handle_meta); 72 73 // Wrapper structure over private handle 74 // Values associated with the private handle 75 // that do not need to go over IPC can be placed here 76 // This structure is also not expected to be ABI stable 77 // unlike private_handle_t 78 struct Buffer { 79 const private_handle_t *handle = nullptr; 80 int ref_count = 1; 81 // Hold the main and metadata ion handles 82 // Freed from the allocator process 83 // and unused in the mapping process 84 int ion_handle_main = -1; 85 int ion_handle_meta = -1; 86 87 Buffer() = delete; 88 explicit Buffer(const private_handle_t *h, int ih_main = -1, int ih_meta = -1) handleBuffer89 : handle(h), ion_handle_main(ih_main), ion_handle_meta(ih_meta) {} IncRefBuffer90 void IncRef() { ++ref_count; } DecRefBuffer91 bool DecRef() { return --ref_count == 0; } 92 uint64_t reserved_size = 0; 93 void *reserved_region_ptr = nullptr; 94 }; 95 96 Error FreeBuffer(std::shared_ptr<Buffer> buf); 97 98 // Get the wrapper Buffer object from the handle, returns nullptr if handle is not found 99 std::shared_ptr<Buffer> GetBufferFromHandleLocked(const private_handle_t *hnd); 100 Allocator *allocator_ = NULL; 101 std::mutex buffer_lock_; 102 std::unordered_map<const private_handle_t *, std::shared_ptr<Buffer>> handles_map_ = {}; 103 std::atomic<uint64_t> next_id_; 104 uint64_t allocated_ = 0; 105 uint64_t kAllocThreshold = (uint64_t)2*1024*1024*1024; 106 const uint64_t kMemoryOffset = 50*1024*1024; 107 struct { 108 const char *kDumpFile = "/data/misc/wmtrace/bufferdump.txt"; 109 uint64_t position = 0; 110 } file_dump_; 111 }; 112 113 } // namespace gralloc 114 115 #endif // __GR_BUF_MGR_H__ 116