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)59 bool ColorBufferVk::readToBytes(uint32_t x, uint32_t y, uint32_t w, uint32_t h, void* outBytes) {
60 return readColorBufferToBytes(mHandle, x, y, w, h, outBytes);
61 }
62
updateFromBytes(const std::vector<uint8_t> & bytes)63 bool ColorBufferVk::updateFromBytes(const std::vector<uint8_t>& bytes) {
64 return updateColorBufferFromBytes(mHandle, bytes);
65 }
66
updateFromBytes(uint32_t x,uint32_t y,uint32_t w,uint32_t h,const void * bytes)67 bool ColorBufferVk::updateFromBytes(uint32_t x, uint32_t y, uint32_t w, uint32_t h,
68 const void* bytes) {
69 return updateColorBufferFromBytes(mHandle, x, y, w, h, bytes);
70 }
71
importExtMemoryHandle(void * nativeResource,uint32_t type,bool preserveContent)72 bool ColorBufferVk::importExtMemoryHandle(void* nativeResource, uint32_t type,
73 bool preserveContent) {
74 // TODO: Any need to support preserveContent?
75 assert(!preserveContent);
76 VK_EXT_MEMORY_HANDLE extMemoryHandle =
77 *reinterpret_cast<VK_EXT_MEMORY_HANDLE*>(&nativeResource);
78 return importExtMemoryHandleToVkColorBuffer(mHandle, type, extMemoryHandle);
79 }
80
81 } // namespace vk
82 } // namespace gfxstream
83