1 // Copyright 2025 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 #pragma once 16 17 #include <cstdlib> 18 19 #include "render-utils/IOStream.h" 20 21 namespace gfxstream { 22 namespace vk { 23 24 class TrivialStream : public IOStream { 25 public: TrivialStream()26 TrivialStream() : IOStream(4) {} 27 virtual ~TrivialStream() = default; 28 allocBuffer(size_t minSize)29 void* allocBuffer(size_t minSize) { 30 size_t allocSize = (m_bufsize < minSize ? minSize : m_bufsize); 31 if (!m_buf) { 32 m_buf = (unsigned char*)malloc(allocSize); 33 } else if (m_bufsize < allocSize) { 34 unsigned char* p = (unsigned char*)realloc(m_buf, allocSize); 35 if (p != NULL) { 36 m_buf = p; 37 m_bufsize = allocSize; 38 } else { 39 ERR("realloc (%zu) failed", allocSize); 40 free(m_buf); 41 m_buf = NULL; 42 m_bufsize = 0; 43 } 44 } 45 46 return m_buf; 47 } 48 commitBuffer(size_t size)49 int commitBuffer(size_t size) { 50 if (size == 0) return 0; 51 return writeFully(m_buf, size); 52 } 53 writeFully(const void * buf,size_t len)54 int writeFully(const void* buf, size_t len) { return 0; } 55 readFully(void * buf,size_t len)56 const unsigned char* readFully(void* buf, size_t len) { return NULL; } 57 getDmaForReading(uint64_t guest_paddr)58 virtual void* getDmaForReading(uint64_t guest_paddr) { return nullptr; } unlockDma(uint64_t guest_paddr)59 virtual void unlockDma(uint64_t guest_paddr) {} 60 61 protected: readRaw(void * buf,size_t * inout_len)62 virtual const unsigned char* readRaw(void* buf, size_t* inout_len) { return nullptr; } onSave(android::base::Stream * stream)63 virtual void onSave(android::base::Stream* stream) {} onLoad(android::base::Stream * stream)64 virtual unsigned char* onLoad(android::base::Stream* stream) { return nullptr; } 65 }; 66 67 } // namespace vk 68 } // namespace gfxstream 69