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 #include "ColorBufferVk.h"
16 
17 #include "VkCommonOperations.h"
18 
19 namespace gfxstream {
20 namespace vk {
21 
22 /*static*/
create(uint32_t handle,uint32_t width,uint32_t height,GLenum format,FrameworkFormat frameworkFormat,bool vulkanOnly,uint32_t memoryProperty,android::base::Stream * stream)23 std::unique_ptr<ColorBufferVk> ColorBufferVk::create(uint32_t handle, uint32_t width,
24                                                      uint32_t height, GLenum format,
25                                                      FrameworkFormat frameworkFormat,
26                                                      bool vulkanOnly, uint32_t memoryProperty,
27                                                      android::base::Stream* stream) {
28     if (!createVkColorBuffer(width, height, format, frameworkFormat, handle, vulkanOnly,
29                              memoryProperty)) {
30         GL_LOG("Failed to create ColorBufferVk:%d", handle);
31         return nullptr;
32     }
33     if (getGlobalVkEmulation()->features.VulkanSnapshots.enabled && stream) {
34         VkImageLayout currentLayout = static_cast<VkImageLayout>(stream->getBe32());
35         setColorBufferCurrentLayout(handle, currentLayout);
36     }
37     return std::unique_ptr<ColorBufferVk>(new ColorBufferVk(handle));
38 }
39 
onSave(android::base::Stream * stream)40 void ColorBufferVk::onSave(android::base::Stream* stream) {
41     if (!getGlobalVkEmulation()->features.VulkanSnapshots.enabled) {
42         return;
43     }
44     stream->putBe32(static_cast<uint32_t>(getColorBufferCurrentLayout(mHandle)));
45 }
46 
ColorBufferVk(uint32_t handle)47 ColorBufferVk::ColorBufferVk(uint32_t handle) : mHandle(handle) {}
48 
~ColorBufferVk()49 ColorBufferVk::~ColorBufferVk() {
50     if (!teardownVkColorBuffer(mHandle)) {
51         ERR("Failed to destroy ColorBufferVk:%d", mHandle);
52     }
53 }
54 
readToBytes(std::vector<uint8_t> * outBytes)55 bool ColorBufferVk::readToBytes(std::vector<uint8_t>* outBytes) {
56     return readColorBufferToBytes(mHandle, outBytes);
57 }
58 
readToBytes(uint32_t x,uint32_t y,uint32_t w,uint32_t h,void * outBytes,uint64_t outBytesSize)59 bool ColorBufferVk::readToBytes(uint32_t x, uint32_t y, uint32_t w, uint32_t h, void* outBytes,
60                                 uint64_t outBytesSize) {
61     return readColorBufferToBytes(mHandle, x, y, w, h, outBytes, outBytesSize);
62 }
63 
updateFromBytes(const std::vector<uint8_t> & bytes)64 bool ColorBufferVk::updateFromBytes(const std::vector<uint8_t>& bytes) {
65     return updateColorBufferFromBytes(mHandle, bytes);
66 }
67 
updateFromBytes(uint32_t x,uint32_t y,uint32_t w,uint32_t h,const void * bytes)68 bool ColorBufferVk::updateFromBytes(uint32_t x, uint32_t y, uint32_t w, uint32_t h,
69                                     const void* bytes) {
70     return updateColorBufferFromBytes(mHandle, x, y, w, h, bytes);
71 }
72 
importExtMemoryHandle(void * nativeResource,uint32_t type,bool preserveContent)73 bool ColorBufferVk::importExtMemoryHandle(void* nativeResource, uint32_t type,
74                                           bool preserveContent) {
75     // TODO: Any need to support preserveContent?
76     assert(!preserveContent);
77     VK_EXT_MEMORY_HANDLE extMemoryHandle =
78         *reinterpret_cast<VK_EXT_MEMORY_HANDLE*>(&nativeResource);
79     return importExtMemoryHandleToVkColorBuffer(mHandle, type, extMemoryHandle);
80 }
81 
waitSync()82 int ColorBufferVk::waitSync() { return waitSyncVkColorBuffer(mHandle); }
83 
exportBlob()84 std::optional<BlobDescriptorInfo> ColorBufferVk::exportBlob() {
85     auto info = exportColorBufferMemory(mHandle);
86     if (info) {
87         return BlobDescriptorInfo{
88             .descriptor = std::move((*info).descriptor),
89             .handleType = (*info).streamHandleType,
90             .caching = 0,
91             .vulkanInfoOpt = std::nullopt,
92         };
93     } else {
94         return std::nullopt;
95     }
96 }
97 
98 }  // namespace vk
99 }  // namespace gfxstream
100