| 1 |
/* |
| 2 |
* Copyright 2018 Google LLC |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
#include "VulkanStreamGuest.h" |
| 6 |
|
| 7 |
#include "util/log.h" |
| 8 |
#include "util/perf/cpu_trace.h" |
| 9 |
|
| 10 |
namespace gfxstream { |
| 11 |
namespace vk { |
| 12 |
|
| 13 |
VulkanStreamGuest::VulkanStreamGuest(gfxstream::guest::IOStream* stream) : mStream(stream) { |
| 14 |
unsetHandleMapping(); |
| 15 |
mFeatureBits = ResourceTracker::get()->getStreamFeatures(); |
| 16 |
} |
| 17 |
|
| 18 |
VulkanStreamGuest::~VulkanStreamGuest() = default; |
| 19 |
|
| 20 |
bool VulkanStreamGuest::valid() { return true; } |
| 21 |
|
| 22 |
void VulkanStreamGuest::alloc(void** ptrAddr, size_t bytes) { |
| 23 |
if (!bytes) { |
| 24 |
*ptrAddr = nullptr; |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
*ptrAddr = mPool.alloc(bytes); |
| 29 |
} |
| 30 |
|
| 31 |
void VulkanStreamGuest::loadStringInPlace(char** forOutput) { |
| 32 |
size_t len = getBe32(); |
| 33 |
|
| 34 |
alloc((void**)forOutput, len + 1); |
| 35 |
|
| 36 |
memset(*forOutput, 0x0, len + 1); |
| 37 |
|
| 38 |
if (len > 0) read(*forOutput, len); |
| 39 |
} |
| 40 |
|
| 41 |
void VulkanStreamGuest::loadStringArrayInPlace(char*** forOutput) { |
| 42 |
size_t count = getBe32(); |
| 43 |
|
| 44 |
if (!count) { |
| 45 |
*forOutput = nullptr; |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
alloc((void**)forOutput, count * sizeof(char*)); |
| 50 |
|
| 51 |
char** stringsForOutput = *forOutput; |
| 52 |
|
| 53 |
for (size_t i = 0; i < count; i++) { |
| 54 |
loadStringInPlace(stringsForOutput + i); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
void VulkanStreamGuest::loadStringInPlaceWithStreamPtr(char** forOutput, uint8_t** streamPtr) { |
| 59 |
uint32_t len; |
| 60 |
memcpy(&len, *streamPtr, sizeof(uint32_t)); |
| 61 |
*streamPtr += sizeof(uint32_t); |
| 62 |
gfxstream::aemu::Stream::fromBe32((uint8_t*)&len); |
| 63 |
|
| 64 |
alloc((void**)forOutput, len + 1); |
| 65 |
|
| 66 |
memset(*forOutput, 0x0, len + 1); |
| 67 |
|
| 68 |
if (len > 0) { |
| 69 |
memcpy(*forOutput, *streamPtr, len); |
| 70 |
*streamPtr += len; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
void VulkanStreamGuest::loadStringArrayInPlaceWithStreamPtr(char*** forOutput, |
| 75 |
uint8_t** streamPtr) { |
| 76 |
uint32_t count; |
| 77 |
memcpy(&count, *streamPtr, sizeof(uint32_t)); |
| 78 |
*streamPtr += sizeof(uint32_t); |
| 79 |
gfxstream::aemu::Stream::fromBe32((uint8_t*)&count); |
| 80 |
if (!count) { |
| 81 |
*forOutput = nullptr; |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
alloc((void**)forOutput, count * sizeof(char*)); |
| 86 |
|
| 87 |
char** stringsForOutput = *forOutput; |
| 88 |
|
| 89 |
for (size_t i = 0; i < count; i++) { |
| 90 |
loadStringInPlaceWithStreamPtr(stringsForOutput + i, streamPtr); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
ssize_t VulkanStreamGuest::read(void* buffer, size_t size) { |
| 95 |
if (!mStream->readback(buffer, size)) { |
| 96 |
mesa_loge("FATAL: Could not read back %zu bytes", size); |
| 97 |
abort(); |
| 98 |
} |
| 99 |
return size; |
| 100 |
} |
| 101 |
|
| 102 |
ssize_t VulkanStreamGuest::write(const void* buffer, size_t size) { |
| 103 |
uint8_t* streamBuf = (uint8_t*)mStream->alloc(size); |
| 104 |
memcpy(streamBuf, buffer, size); |
| 105 |
return size; |
| 106 |
} |
| 107 |
|
| 108 |
void VulkanStreamGuest::writeLarge(const void* buffer, size_t size) { |
| 109 |
mStream->writeFullyAsync(buffer, size); |
| 110 |
} |
| 111 |
|
| 112 |
void VulkanStreamGuest::clearPool() { mPool.freeAll(); } |
| 113 |
|
| 114 |
void VulkanStreamGuest::setHandleMapping(VulkanHandleMapping* mapping) { |
| 115 |
mCurrentHandleMapping = mapping; |
| 116 |
} |
| 117 |
|
| 118 |
void VulkanStreamGuest::unsetHandleMapping() { mCurrentHandleMapping = &mDefaultHandleMapping; } |
| 119 |
|
| 120 |
VulkanHandleMapping* VulkanStreamGuest::handleMapping() const { return mCurrentHandleMapping; } |
| 121 |
|
| 122 |
void VulkanStreamGuest::flush() { |
| 123 |
MESA_TRACE_SCOPE("VulkanStreamGuest device write"); |
| 124 |
mStream->flush(); |
| 125 |
} |
| 126 |
|
| 127 |
uint32_t VulkanStreamGuest::getFeatureBits() const { return mFeatureBits; } |
| 128 |
|
| 129 |
void VulkanStreamGuest::incStreamRef() { mStream->incRef(); } |
| 130 |
|
| 131 |
bool VulkanStreamGuest::decStreamRef() { return mStream->decRef(); } |
| 132 |
|
| 133 |
uint8_t* VulkanStreamGuest::reserve(size_t size) { return (uint8_t*)mStream->alloc(size); } |
| 134 |
|
| 135 |
VulkanCountingStream::VulkanCountingStream() : VulkanStreamGuest(nullptr) {} |
| 136 |
VulkanCountingStream::~VulkanCountingStream() = default; |
| 137 |
|
| 138 |
ssize_t VulkanCountingStream::read(void*, size_t size) { |
| 139 |
m_read += size; |
| 140 |
return size; |
| 141 |
} |
| 142 |
|
| 143 |
ssize_t VulkanCountingStream::write(const void*, size_t size) { |
| 144 |
m_written += size; |
| 145 |
return size; |
| 146 |
} |
| 147 |
|
| 148 |
void VulkanCountingStream::rewind() { |
| 149 |
m_written = 0; |
| 150 |
m_read = 0; |
| 151 |
} |
| 152 |
|
| 153 |
} // namespace vk |
| 154 |
} // namespace gfxstream |