blob: 824b510717f1a79211844b8231774e5b353727c1 [file] [log] [blame]
keunyoungb85b2752013-03-08 12:28:03 -08001/*
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 Yangfa7efc32020-08-27 09:10:42 -070020#include <stdint.h>
keunyoungb85b2752013-03-08 12:28:03 -080021#include <stdio.h>
22
23#include "ErrorLog.h"
24
25class IOStream {
26public:
27
28 IOStream(size_t bufSize) {
Lingfeng Yang667b9e72019-10-12 17:02:03 -070029 m_iostreamBuf = NULL;
Lingfeng Yang2aae3152021-01-22 17:44:56 -080030 m_bufsizeOrig = bufSize;
keunyoungb85b2752013-03-08 12:28:03 -080031 m_bufsize = bufSize;
32 m_free = 0;
Lingfeng Yangfa7efc32020-08-27 09:10:42 -070033 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;
keunyoungb85b2752013-03-08 12:28:03 -080046 }
47
Lingfeng Yang667b9e72019-10-12 17:02:03 -070048 virtual size_t idealAllocSize(size_t len) {
49 return m_bufsize < len ? len : m_bufsize;
50 }
51
keunyoungb85b2752013-03-08 12:28:03 -080052 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 Reveman8bc32552019-06-28 10:32:41 -040055 virtual const unsigned char *commitBufferAndReadFully(size_t size, void *buf, size_t len) = 0;
keunyoungb85b2752013-03-08 12:28:03 -080056 virtual const unsigned char *read( void *buf, size_t *inout_len) = 0;
57 virtual int writeFully(const void* buf, size_t len) = 0;
Lingfeng Yang2aae3152021-01-22 17:44:56 -080058 virtual int writeFullyAsync(const void* buf, size_t len) {
59 return writeFully(buf, len);
60 }
keunyoungb85b2752013-03-08 12:28:03 -080061
62 virtual ~IOStream() {
63
Lingfeng Yang667b9e72019-10-12 17:02:03 -070064 // NOTE: m_iostreamBuf is 'owned' by the child class thus we expect it to be released by it
keunyoungb85b2752013-03-08 12:28:03 -080065 }
66
Alistair Strachan694745b2018-03-30 18:11:39 -070067 virtual unsigned char *alloc(size_t len) {
keunyoungb85b2752013-03-08 12:28:03 -080068
Lingfeng Yang667b9e72019-10-12 17:02:03 -070069 if (m_iostreamBuf && len > m_free) {
keunyoungb85b2752013-03-08 12:28:03 -080070 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 Yang667b9e72019-10-12 17:02:03 -070076 if (!m_iostreamBuf || len > m_bufsize) {
77 int allocLen = this->idealAllocSize(len);
78 m_iostreamBuf = (unsigned char *)allocBuffer(allocLen);
79 if (!m_iostreamBuf) {
keunyoungb85b2752013-03-08 12:28:03 -080080 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 Yang667b9e72019-10-12 17:02:03 -070088 ptr = m_iostreamBuf + (m_bufsize - m_free);
keunyoungb85b2752013-03-08 12:28:03 -080089 m_free -= len;
90
91 return ptr;
92 }
93
Alistair Strachan694745b2018-03-30 18:11:39 -070094 virtual int flush() {
keunyoungb85b2752013-03-08 12:28:03 -080095
Lingfeng Yang667b9e72019-10-12 17:02:03 -070096 if (!m_iostreamBuf || m_free == m_bufsize) return 0;
keunyoungb85b2752013-03-08 12:28:03 -080097
98 int stat = commitBuffer(m_bufsize - m_free);
Lingfeng Yang667b9e72019-10-12 17:02:03 -070099 m_iostreamBuf = NULL;
keunyoungb85b2752013-03-08 12:28:03 -0800100 m_free = 0;
101 return stat;
102 }
103
104 const unsigned char *readback(void *buf, size_t len) {
Lingfeng Yang667b9e72019-10-12 17:02:03 -0700105 if (m_iostreamBuf && m_free != m_bufsize) {
David Reveman8bc32552019-06-28 10:32:41 -0400106 size_t size = m_bufsize - m_free;
Lingfeng Yang667b9e72019-10-12 17:02:03 -0700107 m_iostreamBuf = NULL;
David Reveman8bc32552019-06-28 10:32:41 -0400108 m_free = 0;
109 return commitBufferAndReadFully(size, buf, len);
110 }
keunyoungb85b2752013-03-08 12:28:03 -0800111 return readFully(buf, len);
112 }
113
Roman Kiryanovdba946b2020-05-11 13:38:21 -0700114 // 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 Yang22dc42d2018-05-29 10:11:38 -0700117 void readbackPixels(void* context, int width, int height, unsigned int format, unsigned int type, void* pixels);
Lingfeng Yang50995652020-06-11 15:07:42 -0700118 void uploadPixels(void* context, int width, int height, int depth, unsigned int format, unsigned int type, const void* pixels);
Lingfeng Yang22dc42d2018-05-29 10:11:38 -0700119
keunyoungb85b2752013-03-08 12:28:03 -0800120
Lingfeng Yang2aae3152021-01-22 17:44:56 -0800121protected:
122 void rewind() {
123 m_iostreamBuf = NULL;
124 m_bufsize = m_bufsizeOrig;
125 m_free = 0;
126 }
127
keunyoungb85b2752013-03-08 12:28:03 -0800128private:
Lingfeng Yang667b9e72019-10-12 17:02:03 -0700129 unsigned char *m_iostreamBuf;
Lingfeng Yang2aae3152021-01-22 17:44:56 -0800130 size_t m_bufsizeOrig;
keunyoungb85b2752013-03-08 12:28:03 -0800131 size_t m_bufsize;
132 size_t m_free;
Lingfeng Yangfa7efc32020-08-27 09:10:42 -0700133 uint32_t m_refcount;
keunyoungb85b2752013-03-08 12:28:03 -0800134};
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