Matt Sandy | 087f00e | 2023-04-24 13:42:19 -0700 | [diff] [blame] | 1 | // Copyright (C) 2023 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License") override; |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #pragma once |
| 16 | |
| 17 | #include <atomic> |
| 18 | #include <memory> |
| 19 | #include <unordered_set> |
| 20 | |
| 21 | #include "DrmDevice.h" |
| 22 | #include "aemu/base/Compiler.h" |
| 23 | #include "aemu/base/ManagedDescriptor.hpp" |
| 24 | |
| 25 | namespace gfxstream { |
| 26 | namespace magma { |
| 27 | |
| 28 | class DrmDevice; |
| 29 | |
| 30 | // Wraps a linux DRM (GEM) buffer. |
| 31 | class DrmBuffer { |
| 32 | public: |
| 33 | ~DrmBuffer(); |
| 34 | DISALLOW_COPY_AND_ASSIGN(DrmBuffer); |
| 35 | DrmBuffer(DrmBuffer&&) noexcept; |
| 36 | DrmBuffer& operator=(DrmBuffer&&) = delete; |
| 37 | |
| 38 | // Creates a new buffer using the provided device. The device must remain valid for the lifetime |
| 39 | // of the buffer. |
| 40 | static std::unique_ptr<DrmBuffer> create(DrmDevice& device, uint32_t context_id, uint64_t size); |
| 41 | |
| 42 | // Returns the gem handle for the buffer. |
| 43 | uint32_t getHandle(); |
| 44 | |
| 45 | // Returns the allocated size of the buffer. |
| 46 | uint64_t size(); |
| 47 | |
| 48 | // Returns the host address for the mapped buffer. |
| 49 | void* map(); |
| 50 | |
| 51 | // Returns the host-guest shared buffer ID. |
| 52 | uint64_t getId(); |
| 53 | |
| 54 | private: |
| 55 | DrmBuffer(DrmDevice& device); |
| 56 | |
| 57 | DrmDevice& mDevice; |
| 58 | uint32_t mContextId; |
| 59 | uint32_t mGemHandle; |
| 60 | uint64_t mSize; |
| 61 | |
| 62 | static std::atomic_uint64_t mIdNext; |
| 63 | void* mHva = nullptr; |
| 64 | uint64_t mId = 0; |
| 65 | }; |
| 66 | |
| 67 | } // namespace magma |
| 68 | } // namespace gfxstream |