blob: 84459db595c908fc90b86f22769ea973f6291a85 [file] [log] [blame]
Jason Macnaka296e682022-10-27 16:21:13 -07001// 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#include "Buffer.h"
16
Gurchetan Singh95b30dc2024-01-18 18:31:15 -080017#if GFXSTREAM_ENABLE_HOST_GLES
Jason Macnaka296e682022-10-27 16:21:13 -070018#include "gl/EmulationGl.h"
Gurchetan Singh95b30dc2024-01-18 18:31:15 -080019#endif
20
Jason Macnaka296e682022-10-27 16:21:13 -070021#include "vulkan/BufferVk.h"
22#include "vulkan/VkCommonOperations.h"
23
24namespace gfxstream {
25
26using android::base::ManagedDescriptor;
27using emugl::ABORT_REASON_OTHER;
28using emugl::FatalError;
29
30Buffer::Buffer(HandleType handle, uint64_t size) : mHandle(handle), mSize(size) {}
31
32/*static*/
Jason Macnaked0c9e62023-03-30 15:58:24 -070033std::shared_ptr<Buffer> Buffer::create(gl::EmulationGl* emulationGl, vk::VkEmulation* emulationVk,
34 uint64_t size, HandleType handle) {
Jason Macnaka296e682022-10-27 16:21:13 -070035 std::shared_ptr<Buffer> buffer(new Buffer(handle, size));
36
Gurchetan Singh95b30dc2024-01-18 18:31:15 -080037#if GFXSTREAM_ENABLE_HOST_GLES
Jason Macnaka296e682022-10-27 16:21:13 -070038 if (emulationGl) {
39 buffer->mBufferGl = emulationGl->createBuffer(size, handle);
40 if (!buffer->mBufferGl) {
41 ERR("Failed to initialize BufferGl.");
42 return nullptr;
43 }
44 }
Gurchetan Singh95b30dc2024-01-18 18:31:15 -080045#endif
Jason Macnaka296e682022-10-27 16:21:13 -070046
47 if (emulationVk && emulationVk->live) {
48 const bool vulkanOnly = emulationGl == nullptr;
49
Jason Macnaked0c9e62023-03-30 15:58:24 -070050 buffer->mBufferVk = vk::BufferVk::create(handle, size, vulkanOnly);
Jason Macnaka296e682022-10-27 16:21:13 -070051 if (!buffer->mBufferVk) {
52 ERR("Failed to initialize BufferVk.");
53 return nullptr;
54 }
55
56 if (!vulkanOnly) {
Gurchetan Singh95b30dc2024-01-18 18:31:15 -080057#if GFXSTREAM_ENABLE_HOST_GLES
Jason Macnaka296e682022-10-27 16:21:13 -070058 if (!buffer->mBufferGl) {
59 GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER)) << "Missing BufferGl?";
60 }
Gurchetan Singh95b30dc2024-01-18 18:31:15 -080061#endif
Jason Macnaka296e682022-10-27 16:21:13 -070062 // TODO: external memory sharing.
63 }
64 }
65
66 return buffer;
67}
68
69/*static*/
Jason Macnaked0c9e62023-03-30 15:58:24 -070070std::shared_ptr<Buffer> Buffer::onLoad(gl::EmulationGl* emulationGl, vk::VkEmulation*,
71 android::base::Stream* stream) {
Jason Macnaka296e682022-10-27 16:21:13 -070072 const auto handle = static_cast<HandleType>(stream->getBe32());
73 const auto size = static_cast<uint64_t>(stream->getBe64());
74
75 std::shared_ptr<Buffer> buffer(new Buffer(handle, size));
76
Gurchetan Singh95b30dc2024-01-18 18:31:15 -080077#if GFXSTREAM_ENABLE_HOST_GLES
Jason Macnaka296e682022-10-27 16:21:13 -070078 if (emulationGl) {
79 buffer->mBufferGl = emulationGl->loadBuffer(stream);
80 if (!buffer->mBufferGl) {
81 ERR("Failed to load BufferGl.");
82 return nullptr;
83 }
84 }
Gurchetan Singh95b30dc2024-01-18 18:31:15 -080085#endif
Jason Macnaka296e682022-10-27 16:21:13 -070086
87 buffer->mNeedRestore = true;
88
89 return buffer;
90}
91
92void Buffer::onSave(android::base::Stream* stream) {
93 stream->putBe32(mHandle);
94 stream->putBe64(mSize);
95
Gurchetan Singh95b30dc2024-01-18 18:31:15 -080096#if GFXSTREAM_ENABLE_HOST_GLES
Jason Macnaka296e682022-10-27 16:21:13 -070097 if (mBufferGl) {
98 mBufferGl->onSave(stream);
99 }
Gurchetan Singh95b30dc2024-01-18 18:31:15 -0800100#endif
Jason Macnaka296e682022-10-27 16:21:13 -0700101}
102
103void Buffer::restore() {}
104
105void Buffer::readToBytes(uint64_t offset, uint64_t size, void* outBytes) {
106 touch();
107
Gurchetan Singh95b30dc2024-01-18 18:31:15 -0800108#if GFXSTREAM_ENABLE_HOST_GLES
Jason Macnaka296e682022-10-27 16:21:13 -0700109 if (mBufferGl) {
110 mBufferGl->read(offset, size, outBytes);
111 return;
112 }
Gurchetan Singh95b30dc2024-01-18 18:31:15 -0800113#endif
114
Jason Macnaka296e682022-10-27 16:21:13 -0700115 if (mBufferVk) {
116 mBufferVk->readToBytes(offset, size, outBytes);
117 return;
118 }
119
120 GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER)) << "No Buffer impl?";
121}
122
123bool Buffer::updateFromBytes(uint64_t offset, uint64_t size, const void* bytes) {
124 touch();
125
Gurchetan Singh95b30dc2024-01-18 18:31:15 -0800126#if GFXSTREAM_ENABLE_HOST_GLES
Jason Macnaka296e682022-10-27 16:21:13 -0700127 if (mBufferGl) {
128 mBufferGl->subUpdate(offset, size, bytes);
129 return true;
130 }
Gurchetan Singh95b30dc2024-01-18 18:31:15 -0800131#endif
132
Jason Macnaka296e682022-10-27 16:21:13 -0700133 if (mBufferVk) {
134 return mBufferVk->updateFromBytes(offset, size, bytes);
135 }
136
137 GFXSTREAM_ABORT(FatalError(ABORT_REASON_OTHER)) << "No Buffer impl?";
138 return false;
139}
140
Gurchetan Singhb86aa8b2024-06-24 17:19:56 -0700141std::optional<ManagedDescriptorInfo> Buffer::exportBlob() {
142 if (!mBufferVk) {
143 return std::nullopt;
144 }
145
146 return mBufferVk->exportBlob();
147}
148
Jason Macnaka296e682022-10-27 16:21:13 -0700149} // namespace gfxstream