• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(VkEmulation & vkEmulation,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(VkEmulation& vkEmulation, uint32_t handle,
24                                                      uint32_t width, uint32_t height, GLenum format,
25                                                      FrameworkFormat frameworkFormat,
26                                                      bool vulkanOnly, uint32_t memoryProperty,
27                                                      android::base::Stream* stream) {
28     if (!vkEmulation.createVkColorBuffer(width, height, format, frameworkFormat, handle, vulkanOnly,
29                                          memoryProperty)) {
30         GL_LOG("Failed to create ColorBufferVk:%d", handle);
31         return nullptr;
32     }
33     if (vkEmulation.getFeatures().VulkanSnapshots.enabled && stream) {
34         VkImageLayout currentLayout = static_cast<VkImageLayout>(stream->getBe32());
35         vkEmulation.setColorBufferCurrentLayout(handle, currentLayout);
36     }
37     return std::unique_ptr<ColorBufferVk>(new ColorBufferVk(vkEmulation, handle));
38 }
39 
onSave(android::base::Stream * stream)40 void ColorBufferVk::onSave(android::base::Stream* stream) {
41     if (!mVkEmulation.getFeatures().VulkanSnapshots.enabled) {
42         return;
43     }
44     stream->putBe32(static_cast<uint32_t>(mVkEmulation.getColorBufferCurrentLayout(mHandle)));
45 }
46 
ColorBufferVk(VkEmulation & vkEmulation,uint32_t handle)47 ColorBufferVk::ColorBufferVk(VkEmulation& vkEmulation, uint32_t handle)
48     : mVkEmulation(vkEmulation), mHandle(handle) {}
49 
~ColorBufferVk()50 ColorBufferVk::~ColorBufferVk() {
51     if (!mVkEmulation.teardownVkColorBuffer(mHandle)) {
52         ERR("Failed to destroy ColorBufferVk:%d", mHandle);
53     }
54 }
55 
readToBytes(std::vector<uint8_t> * outBytes)56 bool ColorBufferVk::readToBytes(std::vector<uint8_t>* outBytes) {
57     return mVkEmulation.readColorBufferToBytes(mHandle, outBytes);
58 }
59 
readToBytes(uint32_t x,uint32_t y,uint32_t w,uint32_t h,void * outBytes,uint64_t outBytesSize)60 bool ColorBufferVk::readToBytes(uint32_t x, uint32_t y, uint32_t w, uint32_t h, void* outBytes,
61                                 uint64_t outBytesSize) {
62     return mVkEmulation.readColorBufferToBytes(mHandle, x, y, w, h, outBytes, outBytesSize);
63 }
64 
updateFromBytes(const std::vector<uint8_t> & bytes)65 bool ColorBufferVk::updateFromBytes(const std::vector<uint8_t>& bytes) {
66     return mVkEmulation.updateColorBufferFromBytes(mHandle, bytes);
67 }
68 
updateFromBytes(uint32_t x,uint32_t y,uint32_t w,uint32_t h,const void * bytes)69 bool ColorBufferVk::updateFromBytes(uint32_t x, uint32_t y, uint32_t w, uint32_t h,
70                                     const void* bytes) {
71     return mVkEmulation.updateColorBufferFromBytes(mHandle, x, y, w, h, bytes);
72 }
73 
borrowForComposition(bool colorBufferIsTarget)74 std::unique_ptr<BorrowedImageInfo> ColorBufferVk::borrowForComposition(bool colorBufferIsTarget) {
75     return mVkEmulation.borrowColorBufferForComposition(mHandle, colorBufferIsTarget);
76 }
77 
borrowForDisplay()78 std::unique_ptr<BorrowedImageInfo> ColorBufferVk::borrowForDisplay() {
79     return mVkEmulation.borrowColorBufferForDisplay(mHandle);
80 }
81 
exportBlob()82 std::optional<BlobDescriptorInfo> ColorBufferVk::exportBlob() {
83     auto info = mVkEmulation.exportColorBufferMemory(mHandle);
84     if (info) {
85         return BlobDescriptorInfo{
86             .descriptorInfo =
87                 {
88 #ifdef _WIN32
89                     .descriptor = ManagedDescriptor(static_cast<DescriptorType>(
90                         reinterpret_cast<void*>(info->handleInfo.handle))),
91 #else
92                     .descriptor =
93                         ManagedDescriptor(static_cast<DescriptorType>(info->handleInfo.handle)),
94 #endif
95                     .streamHandleType = info->handleInfo.streamHandleType,
96                 },
97             .caching = 0,
98             .vulkanInfoOpt = std::nullopt,
99         };
100     } else {
101         return std::nullopt;
102     }
103 }
104 
105 }  // namespace vk
106 }  // namespace gfxstream
107