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     ~VirtioGpuPipeStream();
35     int connect(const char* serviceName = 0);
36     uint64_t initProcessPipe();
37 
38     virtual void *allocBuffer(size_t minSize);
39     virtual int commitBuffer(size_t size);
40     virtual const unsigned char *readFully( void *buf, size_t len);
41     virtual const unsigned char *commitBufferAndReadFully(
42         size_t size, void *buf, size_t len);
43     virtual const unsigned char *read( void *buf, size_t *inout_len);
44 
valid()45     bool valid() { return m_fd >= 0; }
getRendernodeFd()46     int getRendernodeFd() { return m_fd; }
47     int recv(void *buf, size_t len);
48 
49     virtual int writeFully(const void *buf, size_t len);
50 
51     int getSocket() const;
52 private:
53     // sync. Also resets the write position.
54     void wait();
55 
56     // transfer to/from host ops
57     ssize_t transferToHost(const void* buffer, size_t len);
58     ssize_t transferFromHost(void* buffer, size_t len);
59 
60     int m_fd; // rendernode fd
61 
62     uint32_t m_virtio_rh; // transfer buffer res handle
63     uint32_t m_virtio_bo; // transfer bo handle
64     unsigned char* m_virtio_mapped; // user mapping of bo
65 
66     // intermediate buffer
67     size_t m_bufsize;
68     unsigned char *m_buf;
69     size_t m_read;
70     size_t m_readLeft;
71 
72     size_t m_writtenPos;
73 
74     VirtioGpuPipeStream(int sock, size_t bufSize);
75 };
76