keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 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 | |
| 27 | IOStream(size_t bufSize) { |
| 28 | m_buf = NULL; |
| 29 | m_bufsize = bufSize; |
| 30 | m_free = 0; |
| 31 | } |
| 32 | |
| 33 | virtual void *allocBuffer(size_t minSize) = 0; |
| 34 | virtual int commitBuffer(size_t size) = 0; |
| 35 | virtual const unsigned char *readFully( void *buf, size_t len) = 0; |
| 36 | virtual const unsigned char *read( void *buf, size_t *inout_len) = 0; |
| 37 | virtual int writeFully(const void* buf, size_t len) = 0; |
| 38 | |
| 39 | virtual ~IOStream() { |
| 40 | |
| 41 | // NOTE: m_buf is 'owned' by the child class thus we expect it to be released by it |
| 42 | } |
| 43 | |
Alistair Strachan | 694745b | 2018-03-30 18:11:39 -0700 | [diff] [blame] | 44 | virtual unsigned char *alloc(size_t len) { |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 45 | |
| 46 | if (m_buf && len > m_free) { |
| 47 | if (flush() < 0) { |
| 48 | ERR("Failed to flush in alloc\n"); |
| 49 | return NULL; // we failed to flush so something is wrong |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if (!m_buf || len > m_bufsize) { |
| 54 | int allocLen = m_bufsize < len ? len : m_bufsize; |
| 55 | m_buf = (unsigned char *)allocBuffer(allocLen); |
| 56 | if (!m_buf) { |
| 57 | ERR("Alloc (%u bytes) failed\n", allocLen); |
| 58 | return NULL; |
| 59 | } |
| 60 | m_bufsize = m_free = allocLen; |
| 61 | } |
| 62 | |
| 63 | unsigned char *ptr; |
| 64 | |
| 65 | ptr = m_buf + (m_bufsize - m_free); |
| 66 | m_free -= len; |
| 67 | |
| 68 | return ptr; |
| 69 | } |
| 70 | |
Alistair Strachan | 694745b | 2018-03-30 18:11:39 -0700 | [diff] [blame] | 71 | virtual int flush() { |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 72 | |
| 73 | if (!m_buf || m_free == m_bufsize) return 0; |
| 74 | |
| 75 | int stat = commitBuffer(m_bufsize - m_free); |
| 76 | m_buf = NULL; |
| 77 | m_free = 0; |
| 78 | return stat; |
| 79 | } |
| 80 | |
| 81 | const unsigned char *readback(void *buf, size_t len) { |
| 82 | flush(); |
| 83 | return readFully(buf, len); |
| 84 | } |
| 85 | |
Lingfeng Yang | 22dc42d | 2018-05-29 10:11:38 -0700 | [diff] [blame] | 86 | void readbackPixels(void* context, int width, int height, unsigned int format, unsigned int type, void* pixels); |
| 87 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 88 | |
| 89 | private: |
| 90 | unsigned char *m_buf; |
| 91 | size_t m_bufsize; |
| 92 | size_t m_free; |
| 93 | }; |
| 94 | |
| 95 | // |
| 96 | // When a client opens a connection to the renderer, it should |
| 97 | // send unsigned int value indicating the "clientFlags". |
| 98 | // The following are the bitmask of the clientFlags. |
| 99 | // currently only one bit is used which flags the server |
| 100 | // it should exit. |
| 101 | // |
| 102 | #define IOSTREAM_CLIENT_EXIT_SERVER 1 |
| 103 | |
| 104 | #endif |