1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include "HostConnection.h" 20 #include "IOStream.h" 21 22 #include <stdlib.h> 23 24 /* This file implements an IOStream that uses VIRTGPU TRANSFER* ioctls on a 25 * virtio-gpu DRM rendernode device to communicate with a goldfish-pipe 26 * service on the host side. 27 */ 28 29 class VirtioGpuPipeStream : public IOStream { 30 public: 31 typedef enum { ERR_INVALID_SOCKET = -1000 } QemuPipeStreamError; 32 33 explicit VirtioGpuPipeStream(size_t bufsize = 10000); 34 explicit VirtioGpuPipeStream(size_t bufsize, int stream_handle); 35 ~VirtioGpuPipeStream(); 36 int connect(const char* serviceName = 0); 37 static int openRendernode(); 38 uint64_t initProcessPipe(); 39 40 virtual void *allocBuffer(size_t minSize); 41 virtual int commitBuffer(size_t size); 42 virtual const unsigned char *readFully( void *buf, size_t len); 43 virtual const unsigned char *commitBufferAndReadFully( 44 size_t size, void *buf, size_t len); 45 virtual const unsigned char *read( void *buf, size_t *inout_len); 46 valid()47 bool valid() { return m_fd >= 0; } getRendernodeFd()48 int getRendernodeFd() { return m_fd; } 49 int recv(void *buf, size_t len); 50 51 virtual int writeFully(const void *buf, size_t len); 52 53 int getSocket() const; 54 private: 55 // sync. Also resets the write position. 56 void wait(); 57 58 // transfer to/from host ops 59 ssize_t transferToHost(const void* buffer, size_t len); 60 ssize_t transferFromHost(void* buffer, size_t len); 61 62 int m_fd; // rendernode fd 63 int m_fd_owned; // Do we own the fd? We should consider using 64 // modern C++ for this. 65 66 uint32_t m_virtio_rh; // transfer buffer res handle 67 uint32_t m_virtio_bo; // transfer bo handle 68 unsigned char* m_virtio_mapped; // user mapping of bo 69 70 // intermediate buffer 71 size_t m_bufsize; 72 unsigned char *m_buf; 73 size_t m_read; 74 size_t m_readLeft; 75 76 size_t m_writtenPos; 77 }; 78