• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_EXECBUFFER ioctls on a
25  * virtio-gpu DRM rendernode device to communicate with the host.
26  */
27 
28 struct VirtioGpuCmd;
29 
30 class CrosGralloc : public Gralloc
31 {
32     friend class VirtioGpuStream;
33 
34 public:
35     virtual uint32_t getHostHandle(native_handle_t const* handle);
36     virtual int getFormat(native_handle_t const* handle);
37 
38 private:
setFd(int fd)39     inline void setFd(int fd) { m_fd = fd; }
40     int m_fd = -1;
41 };
42 
43 class VirtioGpuProcessPipe : public ProcessPipe
44 {
45 public:
46     virtual bool processPipeInit(renderControl_encoder_context_t *rcEnc);
47 };
48 
49 class VirtioGpuStream : public IOStream
50 {
51 public:
52     explicit VirtioGpuStream(size_t bufSize);
53     ~VirtioGpuStream();
54 
55     int connect();
getGralloc()56     Gralloc *getGralloc() { return &m_gralloc; }
getProcessPipe()57     ProcessPipe *getProcessPipe() { return &m_processPipe; }
58 
59     // override IOStream so we can see non-rounded allocation sizes
alloc(size_t len)60     virtual unsigned char *alloc(size_t len)
61     {
62         return static_cast<unsigned char *>(allocBuffer(len));
63     }
64 
65     // override IOStream so we can model the caller's writes
66     virtual int flush();
67 
68     virtual void *allocBuffer(size_t minSize);
69     virtual int writeFully(const void *buf, size_t len);
70     virtual const unsigned char *readFully(void *buf, size_t len);
71     virtual int commitBuffer(size_t size);
read(void * buf,size_t * inout_len)72     virtual const unsigned char *read(void *buf, size_t *inout_len) final
73     {
74         return readFully(buf, *inout_len);
75     }
76 
valid()77     bool valid()
78     {
79         return m_fd >= 0 && m_cmdResp_bo > 0 && m_cmdResp;
80     }
81 
82 private:
83     // rendernode fd
84     int m_fd;
85 
86     // command memory buffer
87     size_t m_bufSize;
88     unsigned char *m_buf;
89 
90     // response buffer res handle
91     uint32_t m_cmdResp_rh;
92 
93     // response buffer ttm buffer object
94     uint32_t m_cmdResp_bo;
95 
96     // user mapping of response buffer object
97     VirtioGpuCmd *m_cmdResp;
98 
99     // byte offset to read cursor for last response
100     size_t m_cmdRespPos;
101 
102     // byte offset to command being assembled
103     size_t m_cmdPos;
104 
105     // byte offset to flush cursor
106     size_t m_flushPos;
107 
108     // byte counter of allocs since last command boundary
109     size_t m_allocSize;
110 
111     // bytes of an alloc flushed through flush() API
112     size_t m_allocFlushSize;
113 
114     // CrOS gralloc interface
115     CrosGralloc m_gralloc;
116 
117     // Fake process pipe implementation
118     VirtioGpuProcessPipe m_processPipe;
119 
120     // commits all commands, resets buffer offsets
121     int commitAll();
122 };
123