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 "VirtGpu.h"
21 #include "gfxstream/guest/IOStream.h"
22 
23 #include <stdlib.h>
24 
25 /* This file implements an IOStream that uses VIRTGPU TRANSFER* ioctls on a
26  * virtio-gpu DRM rendernode device to communicate with a goldfish-pipe
27  * service on the host side.
28  */
29 
30 class VirtioGpuPipeStream : public gfxstream::guest::IOStream {
31    public:
32     explicit VirtioGpuPipeStream(size_t bufsize = 10000);
33     explicit VirtioGpuPipeStream(size_t bufsize, int stream_handle);
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 
45     bool valid();
46     int getRendernodeFd();
47     int recv(void *buf, size_t len);
48 
49     virtual int writeFully(const void *buf, size_t len);
50 
51 private:
52     // sync. Also resets the write position.
53     void wait();
54 
55     // transfer to/from host ops
56     ssize_t transferToHost(const void* buffer, size_t len);
57     ssize_t transferFromHost(void* buffer, size_t len);
58 
59     int m_fd = -1;
60     std::unique_ptr<VirtGpuDevice> m_device;
61     VirtGpuResourcePtr m_resource;
62     VirtGpuResourceMappingPtr m_resourceMapping;
63     unsigned char* m_virtio_mapped; // user mapping of bo
64 
65     // intermediate buffer
66     size_t m_bufsize;
67     unsigned char *m_buf;
68 
69     size_t m_writtenPos;
70 };
71