• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2011 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 #ifndef __ADDRESS_SPACE_STREAM_H
17 #define __ADDRESS_SPACE_STREAM_H
18 
19 #include "VirtGpu.h"
20 #include "address_space.h"
21 #include "address_space_graphics_types.h"
22 #include "aemu/base/AndroidHealthMonitor.h"
23 #include "gfxstream/guest/IOStream.h"
24 
25 using gfxstream::guest::HealthMonitor;
26 using gfxstream::guest::IOStream;
27 
28 class AddressSpaceStream : public IOStream {
29 public:
30     explicit AddressSpaceStream(
31         address_space_handle_t handle,
32         uint32_t version,
33         struct asg_context context,
34         uint64_t ringOffset,
35         uint64_t writeBufferOffset,
36         struct address_space_ops ops,
37         HealthMonitor<>* healthMonitor);
38     ~AddressSpaceStream();
39 
40     virtual size_t idealAllocSize(size_t len);
41     virtual void *allocBuffer(size_t minSize);
42     virtual int commitBuffer(size_t size);
43     virtual const unsigned char *readFully( void *buf, size_t len);
44     virtual const unsigned char *read( void *buf, size_t *inout_len);
45     virtual int writeFully(const void *buf, size_t len);
46     virtual int writeFullyAsync(const void *buf, size_t len);
47     virtual const unsigned char *commitBufferAndReadFully(size_t size, void *buf, size_t len);
48 
setMapping(VirtGpuBlobMappingPtr mapping)49     void setMapping(VirtGpuBlobMappingPtr mapping) {
50         m_mapping = mapping;
51     }
52 
setResourceId(uint32_t id)53     void setResourceId(uint32_t id) {
54         m_resourceId = id;
55     }
56 
57 private:
58     bool isInError() const;
59     ssize_t speculativeRead(unsigned char* readBuffer, size_t trySize);
60     void notifyAvailable();
61     uint32_t getRelativeBufferPos(uint32_t pos);
62     void advanceWrite();
63     void ensureConsumerFinishing();
64     void ensureType1Finished();
65     void ensureType3Finished();
66     int type1Write(uint32_t offset, size_t size);
67 
68     void backoff();
69     void resetBackoff();
70 
71     VirtGpuBlobMappingPtr m_mapping = nullptr;
72     struct address_space_ops m_ops;
73 
74     unsigned char* m_tmpBuf;
75     size_t m_tmpBufSize;
76     size_t m_tmpBufXferSize;
77     bool m_usingTmpBuf;
78 
79     unsigned char* m_readBuf;
80     size_t m_read;
81     size_t m_readLeft;
82 
83     address_space_handle_t m_handle;
84     uint32_t m_version;
85     struct asg_context m_context;
86 
87     uint64_t m_ringOffset;
88     uint64_t m_writeBufferOffset;
89 
90     uint32_t m_writeBufferSize;
91     uint32_t m_writeBufferMask;
92     unsigned char* m_buf;
93     unsigned char* m_writeStart;
94     uint32_t m_writeStep;
95 
96     uint32_t m_notifs;
97     uint32_t m_written;
98 
99     uint64_t m_backoffIters;
100     uint64_t m_backoffFactor;
101 
102     size_t m_ringStorageSize;
103     uint32_t m_resourceId = 0;
104 
105     HealthMonitor<>* m_healthMonitor;
106 };
107 
108 #endif
109