| // Copyright 2019 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #ifndef MSD_IMG_BUFFER_H |
| #define MSD_IMG_BUFFER_H |
| |
| #include "magma_util/macros.h" |
| #include "msd.h" |
| #include "platform_buffer.h" |
| |
| class MsdImgBuffer |
| { |
| public: |
| static std::unique_ptr<MsdImgBuffer> Import(uint32_t handle); |
| static std::unique_ptr<MsdImgBuffer> Create(uint64_t size, const char* name); |
| |
| ~MsdImgBuffer(); |
| |
| magma::PlatformBuffer* platform_buffer() |
| { |
| DASSERT(platform_buf_); |
| return platform_buf_.get(); |
| } |
| |
| bool GetPersistentCpuMap(void** data_out) |
| { |
| if (!data_) |
| { |
| if (!platform_buf_->MapCpu(&data_)) |
| { |
| return DRETF(false, "Failed to map persistent"); |
| } |
| } |
| |
| *data_out = data_; |
| return true; |
| } |
| |
| private: |
| MsdImgBuffer(std::unique_ptr<magma::PlatformBuffer> platform_buf); |
| |
| std::unique_ptr<magma::PlatformBuffer> platform_buf_; |
| void* data_ = nullptr; |
| }; |
| |
| class MsdImgAbiBuffer : public msd_buffer_t |
| { |
| public: |
| MsdImgAbiBuffer(std::shared_ptr<MsdImgBuffer> ptr) : base_ptr_(std::move(ptr)) { magic_ = kMagic; } |
| |
| static MsdImgAbiBuffer* cast(msd_buffer_t* buf) |
| { |
| DASSERT(buf); |
| DASSERT(buf->magic_ == kMagic); |
| return static_cast<MsdImgAbiBuffer*>(buf); |
| } |
| |
| std::shared_ptr<MsdImgBuffer> base_ptr() { return base_ptr_; } |
| |
| private: |
| std::shared_ptr<MsdImgBuffer> base_ptr_; |
| |
| static const uint32_t kMagic = 0x62756666; // "buff" |
| }; |
| |
| #endif // MSD_IMG_BUFFER_H |