• 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 __IO_STREAM_H__
17 #define __IO_STREAM_H__
18 
19 #include <stdlib.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 
23 #include "ErrorLog.h"
24 
25 namespace gfxstream {
26 namespace guest {
27 
28 class IOStream {
29 public:
30 
IOStream(size_t bufSize)31     IOStream(size_t bufSize) {
32         m_iostreamBuf = NULL;
33         m_bufsizeOrig = bufSize;
34         m_bufsize = bufSize;
35         m_free = 0;
36         m_refcount = 1;
37     }
38 
incRef()39     void incRef() {
40         __atomic_add_fetch(&m_refcount, 1, __ATOMIC_SEQ_CST);
41     }
42 
decRef()43     bool decRef() {
44         if (0 == __atomic_sub_fetch(&m_refcount, 1, __ATOMIC_SEQ_CST)) {
45             delete this;
46             return true;
47         }
48         return false;
49     }
50 
idealAllocSize(size_t len)51     virtual size_t idealAllocSize(size_t len) {
52         return m_bufsize < len ? len : m_bufsize;
53     }
54 
55     virtual void *allocBuffer(size_t minSize) = 0;
56     virtual int commitBuffer(size_t size) = 0;
57     virtual const unsigned char *readFully( void *buf, size_t len) = 0;
58     virtual const unsigned char *commitBufferAndReadFully(size_t size, void *buf, size_t len) = 0;
59     virtual const unsigned char *read( void *buf, size_t *inout_len) = 0;
60     virtual int writeFully(const void* buf, size_t len) = 0;
writeFullyAsync(const void * buf,size_t len)61     virtual int writeFullyAsync(const void* buf, size_t len) {
62         return writeFully(buf, len);
63     }
64 
~IOStream()65     virtual ~IOStream() {
66 
67         // NOTE: m_iostreamBuf is 'owned' by the child class thus we expect it to be released by it
68     }
69 
alloc(size_t len)70     virtual unsigned char *alloc(size_t len) {
71 
72         if (m_iostreamBuf && len > m_free) {
73             if (flush() < 0) {
74                 ERR("Failed to flush in alloc\n");
75                 return NULL; // we failed to flush so something is wrong
76             }
77         }
78 
79         if (!m_iostreamBuf || len > m_bufsize) {
80             size_t allocLen = this->idealAllocSize(len);
81             m_iostreamBuf = (unsigned char *)allocBuffer(allocLen);
82             if (!m_iostreamBuf) {
83                 ERR("Alloc (%zu bytes) failed\n", allocLen);
84                 return NULL;
85             }
86             m_bufsize = m_free = allocLen;
87         }
88 
89         unsigned char *ptr;
90 
91         ptr = m_iostreamBuf + (m_bufsize - m_free);
92         m_free -= len;
93 
94         return ptr;
95     }
96 
flush()97     virtual int flush() {
98 
99         if (!m_iostreamBuf || m_free == m_bufsize) return 0;
100 
101         int stat = commitBuffer(m_bufsize - m_free);
102         m_iostreamBuf = NULL;
103         m_free = 0;
104         return stat;
105     }
106 
readback(void * buf,size_t len)107     const unsigned char *readback(void *buf, size_t len) {
108         if (m_iostreamBuf && m_free != m_bufsize) {
109             size_t size = m_bufsize - m_free;
110             m_iostreamBuf = NULL;
111             m_free = 0;
112             return commitBufferAndReadFully(size, buf, len);
113         }
114         return readFully(buf, len);
115     }
116 
117     // These two methods are defined and used in GLESv2_enc. Any reference
118     // outside of GLESv2_enc will produce a link error. This is intentional
119     // (technical debt).
120     void readbackPixels(void* context, int width, int height, unsigned int format, unsigned int type, void* pixels);
121     void uploadPixels(void* context, int width, int height, int depth, unsigned int format, unsigned int type, const void* pixels);
122 
123 
124 protected:
rewind()125     void rewind() {
126         m_iostreamBuf = NULL;
127         m_bufsize = m_bufsizeOrig;
128         m_free = 0;
129     }
130 
131 private:
132     unsigned char *m_iostreamBuf;
133     size_t m_bufsizeOrig;
134     size_t m_bufsize;
135     size_t m_free;
136     uint32_t m_refcount;
137 };
138 
139 }  // namespace guest
140 }  // namespace gfxstream
141 
142 //
143 // When a client opens a connection to the renderer, it should
144 // send unsigned int value indicating the "clientFlags".
145 // The following are the bitmask of the clientFlags.
146 // currently only one bit is used which flags the server
147 // it should exit.
148 //
149 #define IOSTREAM_CLIENT_EXIT_SERVER      1
150 
151 #endif
152