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 "ExternalObjectManager.h"
20 #include "Handle.h"
21 #include "aemu/base/files/Stream.h"
22 #include "snapshot/LazySnapshotObj.h"
23 
24 #if GFXSTREAM_ENABLE_HOST_GLES
25 #include "gl/BufferGl.h"
26 #endif
27 
28 namespace gfxstream {
29 namespace gl {
30 class EmulationGl;
31 }  // namespace gl
32 }  // namespace gfxstream
33 
34 namespace gfxstream {
35 namespace vk {
36 class BufferVk;
37 struct VkEmulation;
38 }  // namespace vk
39 }  // namespace gfxstream
40 
41 namespace gfxstream {
42 
43 class Buffer : public android::snapshot::LazySnapshotObj<Buffer> {
44    public:
45     static std::shared_ptr<Buffer> create(gl::EmulationGl* emulationGl,
46                                           vk::VkEmulation* emulationVk, uint64_t size,
47                                           HandleType handle);
48 
49     static std::shared_ptr<Buffer> onLoad(gl::EmulationGl* emulationGl,
50                                           vk::VkEmulation* emulationVk,
51                                           android::base::Stream* stream);
52 
53     void onSave(android::base::Stream* stream);
54     void restore();
55 
getHndl()56     HandleType getHndl() const { return mHandle; }
getSize()57     uint64_t getSize() const { return mSize; }
58 
59     void readToBytes(uint64_t offset, uint64_t size, void* outBytes);
60     bool updateFromBytes(uint64_t offset, uint64_t size, const void* bytes);
61     std::optional<BlobDescriptorInfo> exportBlob();
62 
63    private:
64     Buffer(HandleType handle, uint64_t size);
65 
66     const uint64_t mSize;
67     const HandleType mHandle;
68 
69 #if GFXSTREAM_ENABLE_HOST_GLES
70     // If GL emulation is enabled.
71     std::unique_ptr<gl::BufferGl> mBufferGl;
72 #endif
73 
74     // If Vk emulation is enabled.
75     std::unique_ptr<vk::BufferVk> mBufferVk;
76 };
77 
78 typedef std::shared_ptr<Buffer> BufferPtr;
79 
80 }  // namespace gfxstream
81