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