1 // Copyright 2023 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 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 expresso 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 <memory> 18 19 #include "Handle.h" 20 #include "aemu/base/files/Stream.h" 21 #include "gl/BufferGl.h" 22 #include "snapshot/LazySnapshotObj.h" 23 24 namespace gfxstream { 25 namespace gl { 26 class EmulationGl; 27 } // namespace gl 28 } // namespace gfxstream 29 30 namespace gfxstream { 31 namespace vk { 32 class BufferVk; 33 struct VkEmulation; 34 } // namespace vk 35 } // namespace gfxstream 36 37 namespace gfxstream { 38 39 class Buffer : public android::snapshot::LazySnapshotObj<Buffer> { 40 public: 41 static std::shared_ptr<Buffer> create(gl::EmulationGl* emulationGl, 42 vk::VkEmulation* emulationVk, uint64_t size, 43 HandleType handle); 44 45 static std::shared_ptr<Buffer> onLoad(gl::EmulationGl* emulationGl, 46 vk::VkEmulation* emulationVk, 47 android::base::Stream* stream); 48 49 void onSave(android::base::Stream* stream); 50 void restore(); 51 getHndl()52 HandleType getHndl() const { return mHandle; } getSize()53 uint64_t getSize() const { return mSize; } 54 55 void readToBytes(uint64_t offset, uint64_t size, void* outBytes); 56 bool updateFromBytes(uint64_t offset, uint64_t size, const void* bytes); 57 58 private: 59 Buffer(HandleType handle, uint64_t size); 60 61 const uint64_t mSize; 62 const HandleType mHandle; 63 64 // If GL emulation is enabled. 65 std::unique_ptr<gl::BufferGl> mBufferGl; 66 67 // If Vk emulation is enabled. 68 std::unique_ptr<vk::BufferVk> mBufferVk; 69 }; 70 71 typedef std::shared_ptr<Buffer> BufferPtr; 72 73 } // namespace gfxstream 74