Graphics/src/include/Graphics/Buffer.h
Go to the documentation of this file.
1 /****
2  * Object.h
3  *
4  * Copyright 2021 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the Sming-Graphics Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  * @author: May 2021 - mikee47 <mike@sillyhouse.net>
19  *
20  ****/
21 
22 #pragma once
23 
24 #include "Colors.h"
25 #include <Data/LinkedObjectList.h>
26 #include <debug_progmem.h>
27 
28 namespace Graphics
29 {
35 class SharedBuffer
36 {
37 public:
38  class Control
39  {
40  public:
41  Control(size_t bufSize) : data(new uint8_t[bufSize]), size{bufSize}, refCount{1}
42  {
43  // debug_i("Control(%p, %u)", this, size);
44  }
45 
46  ~Control()
47  {
48  // debug_i("~Control(%p, %u)", this, size);
49  delete[] data;
50  }
51 
52  void addRef()
53  {
55  // debug_i("addRef(%p, %u)", this, refCount);
56  }
57 
58  size_t release()
59  {
60  assert(refCount > 0);
61  // debug_i("release(%p, %u)", this, refCount - 1);
62  if(--refCount != 0) {
63  return refCount;
64  }
65  delete this;
66  return 0;
67  }
68 
69  uint8_t* data;
70  size_t size;
71  size_t refCount;
72  };
73 
74  SharedBuffer()
75  {
76  }
77 
78  SharedBuffer(SharedBuffer&& other) = delete;
79 
80  SharedBuffer(const SharedBuffer& other)
81  {
82  *this = other;
83  }
84 
85  SharedBuffer(size_t bufSize)
86  {
87  init(bufSize);
88  }
89 
90  SharedBuffer(SharedBuffer& other) : control(other.control)
91  {
92  addRef();
93  }
94 
96  {
97  release();
98  }
99 
100  SharedBuffer& operator=(const SharedBuffer& other)
101  {
102  if(*this != other) {
103  // debug_i("operator=(%p, %p)", control, other.control);
104  release();
105  control = other.control;
106  addRef();
107  }
108  return *this;
109  }
110 
111  void init(size_t bufSize)
112  {
113  assert(control == nullptr);
114  control = new Control{bufSize};
115  }
116 
117  explicit operator bool() const
118  {
119  return control != nullptr;
120  }
121 
122  uint8_t* get()
123  {
124  return control ? control->data : nullptr;
125  }
126 
127  void addRef()
128  {
129  if(control != nullptr) {
130  control->addRef();
131  }
132  }
133 
134  void release()
135  {
136  if(control == nullptr) {
137  return;
138  }
139  assert(control->refCount != 0);
140  if(control->release() == 0) {
141  // debug_i("SharedBuffer::release(%p)", control);
142  control = nullptr;
143  }
144  }
145 
146  size_t usage_count() const
147  {
148  return control ? control->refCount : 0;
149  }
150 
151  size_t size() const
152  {
153  return control ? control->size : 0;
154  }
155 
156  uint8_t& operator[](size_t offset)
157  {
158  if(control == nullptr) {
159  abort();
160  }
161  return control->data[offset];
162  }
163 
164  bool operator==(const SharedBuffer& other) const
165  {
166  return control == other.control;
167  }
168 
169  bool operator!=(const SharedBuffer& other) const
170  {
171  return !operator==(other);
172  }
173 
175  {
176  return control;
177  }
178 
179 private:
180  Control* control{nullptr};
181 };
182 
186 struct ReadBuffer {
187  SharedBuffer data;
188  uint16 offset{0};
190  uint8_t reserved{0};
191 
192  ReadBuffer()
193  {
194  }
195 
196  ReadBuffer(const ReadBuffer& other) : data(other.data), offset(other.offset), format(other.format)
197  {
198  }
199 
200  ReadBuffer(PixelFormat format, size_t bufSize) : data(bufSize), format(format)
201  {
202  }
203 
204  size_t size() const
205  {
206  return data.size();
207  }
208 };
209 
213 struct ReadStatus {
214  size_t bytesRead{0};
216  bool readComplete{false};
217 };
218 
222 struct ReadStatusBuffer : public ReadBuffer {
225 };
226 
227 } // namespace Graphics
uint8_t * data
Definition: Graphics/src/include/Graphics/Buffer.h:126
ReadBuffer()
Definition: Graphics/src/include/Graphics/Buffer.h:211
ReadStatus status
Definition: Graphics/src/include/Graphics/Buffer.h:243
SharedBuffer data
Buffer to read pixel data.
Definition: Graphics/src/include/Graphics/Buffer.h:206
void init(size_t bufSize)
Definition: Graphics/src/include/Graphics/Buffer.h:149
size_t size() const
Definition: Graphics/src/include/Graphics/Buffer.h:189
uint8_t * get()
Definition: Graphics/src/include/Graphics/Buffer.h:160
bool operator!=(const SharedBuffer &other) const
Definition: Graphics/src/include/Graphics/Buffer.h:207
void addRef()
Definition: Graphics/src/include/Graphics/Buffer.h:109
uint16 offset
Offset from start of buffer to start writing.
Definition: Graphics/src/include/Graphics/Buffer.h:207
size_t size
Definition: Graphics/src/include/Graphics/Buffer.h:127
size_t release()
Definition: Graphics/src/include/Graphics/Buffer.h:115
bool operator==(const SharedBuffer &other) const
Definition: Graphics/src/include/Graphics/Buffer.h:202
Basic interactive button on screen.
Definition: Libraries/Graphics/src/include/Graphics/Control/Control.h:12
size_t refCount
Definition: Graphics/src/include/Graphics/Buffer.h:128
~Control()
Definition: Graphics/src/include/Graphics/Buffer.h:103
uint8_t reserved
Definition: Graphics/src/include/Graphics/Buffer.h:209
Buffer used for reading pixel data from device.
Definition: Graphics/src/include/Graphics/Buffer.h:205
Definition: Virtual.h:30
PixelFormat format
Format of data.
Definition: Graphics/src/include/Graphics/Buffer.h:234
bool readComplete
Definition: Graphics/src/include/Graphics/Buffer.h:235
Definition: Graphics/src/include/Graphics/Buffer.h:76
size_t usage_count() const
Definition: Graphics/src/include/Graphics/Buffer.h:184
size_t bytesRead
On completion, set to actual length of data read.
Definition: Graphics/src/include/Graphics/Buffer.h:233
void addRef()
Definition: Graphics/src/include/Graphics/Buffer.h:165
size_t size() const
Definition: Graphics/src/include/Graphics/Buffer.h:223
SharedBuffer & operator=(const SharedBuffer &other)
Definition: Graphics/src/include/Graphics/Buffer.h:138
Stores result of read operation.
Definition: Graphics/src/include/Graphics/Buffer.h:232
void release()
Definition: Graphics/src/include/Graphics/Buffer.h:172
PixelFormat
Definition: Colors.h:295
Control(size_t bufSize)
Definition: Graphics/src/include/Graphics/Buffer.h:98
Shared heap-allocated data buffer.
Definition: Graphics/src/include/Graphics/Buffer.h:54
uint8_t & operator[](size_t offset)
Definition: Graphics/src/include/Graphics/Buffer.h:194
Control * getControl()
Definition: Graphics/src/include/Graphics/Buffer.h:212
~SharedBuffer()
Definition: Graphics/src/include/Graphics/Buffer.h:133
PixelFormat format
Input: Requested pixel format, specify 'None' to get native format.
Definition: Graphics/src/include/Graphics/Buffer.h:208
SharedBuffer()
Definition: Graphics/src/include/Graphics/Buffer.h:112