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> |
Lingfeng Yang | fa7efc3 | 2020-08-27 09:10:42 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | |
| 23 | #include "ErrorLog.h" |
| 24 | |
| 25 | class IOStream { |
| 26 | public: |
| 27 | |
| 28 | IOStream(size_t bufSize) { |
Lingfeng Yang | 667b9e7 | 2019-10-12 17:02:03 -0700 | [diff] [blame] | 29 | m_iostreamBuf = NULL; |
Lingfeng Yang | 2aae315 | 2021-01-22 17:44:56 -0800 | [diff] [blame] | 30 | m_bufsizeOrig = bufSize; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 31 | m_bufsize = bufSize; |
| 32 | m_free = 0; |
Lingfeng Yang | fa7efc3 | 2020-08-27 09:10:42 -0700 | [diff] [blame] | 33 | m_refcount = 1; |
| 34 | } |
| 35 | |
| 36 | void incRef() { |
| 37 | __atomic_add_fetch(&m_refcount, 1, __ATOMIC_SEQ_CST); |
| 38 | } |
| 39 | |
| 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; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Lingfeng Yang | 667b9e7 | 2019-10-12 17:02:03 -0700 | [diff] [blame] | 48 | virtual size_t idealAllocSize(size_t len) { |
| 49 | return m_bufsize < len ? len : m_bufsize; |
| 50 | } |
| 51 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 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; |
David Reveman | 8bc3255 | 2019-06-28 10:32:41 -0400 | [diff] [blame] | 55 | virtual const unsigned char *commitBufferAndReadFully(size_t size, void *buf, size_t len) = 0; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 56 | virtual const unsigned char *read( void *buf, size_t *inout_len) = 0; |
| 57 | virtual int writeFully(const void* buf, size_t len) = 0; |
Lingfeng Yang | 2aae315 | 2021-01-22 17:44:56 -0800 | [diff] [blame] | 58 | virtual int writeFullyAsync(const void* buf, size_t len) { |
| 59 | return writeFully(buf, len); |
| 60 | } |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 61 | |
| 62 | virtual ~IOStream() { |
| 63 | |
Lingfeng Yang | 667b9e7 | 2019-10-12 17:02:03 -0700 | [diff] [blame] | 64 | // NOTE: m_iostreamBuf is 'owned' by the child class thus we expect it to be released by it |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Alistair Strachan | 694745b | 2018-03-30 18:11:39 -0700 | [diff] [blame] | 67 | virtual unsigned char *alloc(size_t len) { |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 68 | |
Lingfeng Yang | 667b9e7 | 2019-10-12 17:02:03 -0700 | [diff] [blame] | 69 | if (m_iostreamBuf && len > m_free) { |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 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 | |
Lingfeng Yang | 667b9e7 | 2019-10-12 17:02:03 -0700 | [diff] [blame] | 76 | if (!m_iostreamBuf || len > m_bufsize) { |
| 77 | int allocLen = this->idealAllocSize(len); |
| 78 | m_iostreamBuf = (unsigned char *)allocBuffer(allocLen); |
| 79 | if (!m_iostreamBuf) { |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 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 | |
Lingfeng Yang | 667b9e7 | 2019-10-12 17:02:03 -0700 | [diff] [blame] | 88 | ptr = m_iostreamBuf + (m_bufsize - m_free); |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 89 | m_free -= len; |
| 90 | |
| 91 | return ptr; |
| 92 | } |
| 93 | |
Alistair Strachan | 694745b | 2018-03-30 18:11:39 -0700 | [diff] [blame] | 94 | virtual int flush() { |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 95 | |
Lingfeng Yang | 667b9e7 | 2019-10-12 17:02:03 -0700 | [diff] [blame] | 96 | if (!m_iostreamBuf || m_free == m_bufsize) return 0; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 97 | |
| 98 | int stat = commitBuffer(m_bufsize - m_free); |
Lingfeng Yang | 667b9e7 | 2019-10-12 17:02:03 -0700 | [diff] [blame] | 99 | m_iostreamBuf = NULL; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 100 | m_free = 0; |
| 101 | return stat; |
| 102 | } |
| 103 | |
| 104 | const unsigned char *readback(void *buf, size_t len) { |
Lingfeng Yang | 667b9e7 | 2019-10-12 17:02:03 -0700 | [diff] [blame] | 105 | if (m_iostreamBuf && m_free != m_bufsize) { |
David Reveman | 8bc3255 | 2019-06-28 10:32:41 -0400 | [diff] [blame] | 106 | size_t size = m_bufsize - m_free; |
Lingfeng Yang | 667b9e7 | 2019-10-12 17:02:03 -0700 | [diff] [blame] | 107 | m_iostreamBuf = NULL; |
David Reveman | 8bc3255 | 2019-06-28 10:32:41 -0400 | [diff] [blame] | 108 | m_free = 0; |
| 109 | return commitBufferAndReadFully(size, buf, len); |
| 110 | } |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 111 | return readFully(buf, len); |
| 112 | } |
| 113 | |
Roman Kiryanov | dba946b | 2020-05-11 13:38:21 -0700 | [diff] [blame] | 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). |
Lingfeng Yang | 22dc42d | 2018-05-29 10:11:38 -0700 | [diff] [blame] | 117 | void readbackPixels(void* context, int width, int height, unsigned int format, unsigned int type, void* pixels); |
Lingfeng Yang | 5099565 | 2020-06-11 15:07:42 -0700 | [diff] [blame] | 118 | void uploadPixels(void* context, int width, int height, int depth, unsigned int format, unsigned int type, const void* pixels); |
Lingfeng Yang | 22dc42d | 2018-05-29 10:11:38 -0700 | [diff] [blame] | 119 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 120 | |
Lingfeng Yang | 2aae315 | 2021-01-22 17:44:56 -0800 | [diff] [blame] | 121 | protected: |
| 122 | void rewind() { |
| 123 | m_iostreamBuf = NULL; |
| 124 | m_bufsize = m_bufsizeOrig; |
| 125 | m_free = 0; |
| 126 | } |
| 127 | |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 128 | private: |
Lingfeng Yang | 667b9e7 | 2019-10-12 17:02:03 -0700 | [diff] [blame] | 129 | unsigned char *m_iostreamBuf; |
Lingfeng Yang | 2aae315 | 2021-01-22 17:44:56 -0800 | [diff] [blame] | 130 | size_t m_bufsizeOrig; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 131 | size_t m_bufsize; |
| 132 | size_t m_free; |
Lingfeng Yang | fa7efc3 | 2020-08-27 09:10:42 -0700 | [diff] [blame] | 133 | uint32_t m_refcount; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 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 |