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