Surface.h
Go to the documentation of this file.
1 /****
2  * Surface.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 "Object.h"
25 #include "Buffer.h"
26 
27 namespace Graphics
28 {
29 class Device;
30 
41 class Surface : public AssetTemplate<AssetType::Surface>
42 {
43 public:
46 
47  // Assume that reading requires space for full 24-bit RGB (e.g. ILI9341)
48  static constexpr size_t READ_PIXEL_SIZE{3};
49 
50  enum class Type {
51  Memory,
52  File,
53  Device,
54  Drawing,
55  Blend,
56  };
57 
58  struct Stat {
59  size_t used;
60  size_t available;
61  };
62 
63  using PresentCallback = void (*)(void* param);
64 
69  using ReadCallback = void (*)(ReadBuffer& data, size_t length, void* param);
70 
71  /* Meta */
72 
73  void write(MetaWriter& meta) const override
74  {
75  }
76 
77  /* Surface */
78 
79  virtual Type getType() const = 0;
80  virtual Stat stat() const = 0;
81  virtual Size getSize() const = 0;
82  virtual PixelFormat getPixelFormat() const = 0;
83  virtual bool setAddrWindow(const Rect& rect) = 0;
84  virtual uint8_t* getBuffer(uint16_t minBytes, uint16_t& available) = 0;
85  virtual void commit(uint16_t length) = 0;
86  virtual bool blockFill(const void* data, uint16_t length, uint32_t repeat) = 0;
87  virtual bool writeDataBuffer(SharedBuffer& buffer, size_t offset, uint16_t length) = 0;
88  virtual bool setPixel(PackedColor color, Point pt) = 0;
89  virtual bool writePixels(const void* data, uint16_t length);
90 
91  bool writePixel(PackedColor color)
92  {
93  return writePixels(&color, getBytesPerPixel(getPixelFormat()));
94  }
95 
96  bool writePixel(Color color)
97  {
98  return writePixel(pack(color, getPixelFormat()));
99  }
100 
112  virtual int readDataBuffer(ReadBuffer& buffer, ReadStatus* status = nullptr, ReadCallback callback = nullptr,
113  void* param = nullptr) = 0;
114 
115  virtual int readDataBuffer(ReadStatusBuffer& buffer, ReadCallback callback = nullptr, void* param = nullptr)
116  {
117  return readDataBuffer(buffer, &buffer.status, callback, param);
118  }
119 
132  virtual bool render(const Object& object, const Rect& location, std::unique_ptr<Renderer>& renderer);
133 
144  bool render(const Object& object, const Rect& location);
145 
151  bool execute(std::unique_ptr<Renderer>& renderer)
152  {
153  if(renderer) {
154  if(!renderer->execute(*this)) {
155  return false;
156  }
157  renderer.reset();
158  }
159  return true;
160  }
161 
165  virtual void reset() = 0;
166 
178  virtual bool present(PresentCallback callback = nullptr, void* param = nullptr) = 0;
179 
180  uint16_t width() const
181  {
182  return getSize().w;
183  }
184 
185  uint16_t height() const
186  {
187  return getSize().h;
188  }
189 
190  bool blockFill(PackedColor color, uint32_t repeat)
191  {
192  return blockFill(&color, getBytesPerPixel(getPixelFormat()), repeat);
193  }
194 
195  bool clear()
196  {
197  return fillRect(pack(Color::Black, getPixelFormat()), getSize());
198  }
199 
200  virtual bool fillRect(PackedColor color, const Rect& rect);
201 
205  bool fillSmallRect(const Brush& brush, const Rect& location, const Rect& rect);
206 
210  bool drawHLine(PackedColor color, uint16_t x0, uint16_t x1, uint16_t y, uint16_t w);
211 
215  bool drawVLine(PackedColor color, uint16_t x, uint16_t y0, uint16_t y1, uint16_t w);
216 };
217 
218 } // namespace Graphics
Definition: Asset.h:126
Type
Definition: Asset.h:82
The source of colour for drawing.
Definition: Asset.h:253
Writes object content in readable format for debugging.
Definition: Meta.h:48
A drawable object inherits from this virtual base class.
Definition: Libraries/Graphics/src/include/Graphics/Object.h:97
Shared heap-allocated data buffer.
Definition: Graphics/src/include/Graphics/Buffer.h:36
Interface for a drawing surface.
Definition: Surface.h:42
uint16_t width() const
Definition: Surface.h:180
virtual bool writeDataBuffer(SharedBuffer &buffer, size_t offset, uint16_t length)=0
bool writePixel(PackedColor color)
Definition: Surface.h:91
virtual Size getSize() const =0
bool render(const Object &object, const Rect &location)
Render an object in one cycle.
virtual bool render(const Object &object, const Rect &location, std::unique_ptr< Renderer > &renderer)
Start rendering an object.
void write(MetaWriter &meta) const override
Definition: Surface.h:73
virtual Type getType() const =0
bool execute(std::unique_ptr< Renderer > &renderer)
Execute a renderer.
Definition: Surface.h:151
void(*)(ReadBuffer &data, size_t length, void *param) ReadCallback
Callback for readPixel() operations.
Definition: Surface.h:69
bool clear()
Definition: Surface.h:195
virtual bool writePixels(const void *data, uint16_t length)
bool blockFill(PackedColor color, uint32_t repeat)
Definition: Surface.h:190
bool drawVLine(PackedColor color, uint16_t x, uint16_t y0, uint16_t y1, uint16_t w)
Draw a simple vertical line using a filled rectangle.
virtual int readDataBuffer(ReadStatusBuffer &buffer, ReadCallback callback=nullptr, void *param=nullptr)
Definition: Surface.h:115
virtual void reset()=0
Reset surface ready for more commands.
virtual Stat stat() const =0
static constexpr size_t READ_PIXEL_SIZE
Definition: Surface.h:48
virtual bool present(PresentCallback callback=nullptr, void *param=nullptr)=0
Present surface to display device.
bool drawHLine(PackedColor color, uint16_t x0, uint16_t x1, uint16_t y, uint16_t w)
Draw a simple horizontal line using a filled rectangle.
virtual void commit(uint16_t length)=0
virtual PixelFormat getPixelFormat() const =0
virtual bool setPixel(PackedColor color, Point pt)=0
virtual uint8_t * getBuffer(uint16_t minBytes, uint16_t &available)=0
void(*)(void *param) PresentCallback
Definition: Surface.h:63
bool fillSmallRect(const Brush &brush, const Rect &location, const Rect &rect)
Fill a small rectangle using a non-transparent brush.
bool writePixel(Color color)
Definition: Surface.h:96
virtual int readDataBuffer(ReadBuffer &buffer, ReadStatus *status=nullptr, ReadCallback callback=nullptr, void *param=nullptr)=0
Read some pixels.
virtual bool blockFill(const void *data, uint16_t length, uint32_t repeat)=0
uint16_t height() const
Definition: Surface.h:185
virtual bool fillRect(PackedColor color, const Rect &rect)
virtual bool setAddrWindow(const Rect &rect)=0
Definition: LinkedObjectList.h:90
Definition: Virtual.h:31
PixelBuffer pack(PixelBuffer src, PixelFormat format)
Convert RGB colour into packed format.
PixelFormat
Definition: Colors.h:295
uint8_t getBytesPerPixel(PixelFormat format)
Get number of bytes required to store a pixel in the given format.
Definition: Colors.h:331
Color
Standard colour definitions.
Definition: Colors.h:227
Colour in device pixel format.
Definition: Colors.h:339
Buffer used for reading pixel data from device.
Definition: Graphics/src/include/Graphics/Buffer.h:186
Composite ReadBuffer with status.
Definition: Graphics/src/include/Graphics/Buffer.h:222
ReadStatus status
Definition: Graphics/src/include/Graphics/Buffer.h:224
Stores result of read operation.
Definition: Graphics/src/include/Graphics/Buffer.h:213
Location and size of rectangular area (x, y, w, h)
Definition: Libraries/Graphics/src/include/Graphics/Types.h:287
Size of rectangular area (width x height)
Definition: Libraries/Graphics/src/include/Graphics/Types.h:105
uint16_t w
Definition: Libraries/Graphics/src/include/Graphics/Types.h:106
uint16_t h
Definition: Libraries/Graphics/src/include/Graphics/Types.h:107
Definition: Surface.h:58
size_t available
Definition: Surface.h:60
size_t used
Definition: Surface.h:59