roo_display
API Documentation for roo_display
Loading...
Searching...
No Matches
front_to_back_writer.h
Go to the documentation of this file.
1#pragma once
2
8
9namespace roo_display {
10
11/// Writer that ensures front-most pixels are written first.
12///
13/// Uses an offscreen mask to avoid overwriting already-drawn pixels.
15 public:
16 /// Construct a front-to-back writer for a given bounds rectangle.
17 ///
18 /// The caller must guarantee bounds are within the output area and no writes
19 /// go out of bounds.
21 : color_format_(output.getColorFormat()),
22 offscreen_(bounds, color::Transparent),
23 mask_(offscreen_.buffer(), bounds),
24 mask_filter_(output, &mask_) {}
25
27 BlendingMode mode) override {
28 mask_filter_.setAddress(x0, y0, x1, y1, mode);
29 int16_t dx = mask_.bounds().xMin();
30 int16_t dy = mask_.bounds().yMin();
31 offscreen_.output().setAddress(x0 - dx, y0 - dy, x1 - dx, y1 - dy, mode);
32 }
33
34 void write(Color* color, uint32_t pixel_count) override {
35 mask_filter_.write(color, pixel_count);
36 offscreen_.output().fill(color::Black, pixel_count);
37 }
38
39 void fill(Color color, uint32_t pixel_count) override {
40 mask_filter_.fill(color, pixel_count);
41 offscreen_.output().fill(color::Black, pixel_count);
42 }
43
44 void writeRects(BlendingMode mode, Color* color, int16_t* x0, int16_t* y0,
45 int16_t* x1, int16_t* y1, uint16_t count) override {
46 for (int i = 0; i < count; i++) {
47 Box clipped =
48 Box::Intersect(offscreen_.extents(), Box(x0[i], y0[i], x1[i], y1[i]));
49 mask_filter_.fillRect(mode, x0[i], y0[i], x1[i], y1[i], color[i]);
50 if (!clipped.empty()) {
51 offscreen_.output().fillRect(
52 mode,
53 clipped.translate(-offscreen_.extents().xMin(),
54 -offscreen_.extents().yMin()),
56 }
57 }
58 }
59
60 void fillRects(BlendingMode mode, Color color, int16_t* x0, int16_t* y0,
61 int16_t* x1, int16_t* y1, uint16_t count) override {
62 for (int i = 0; i < count; i++) {
63 Box clipped =
64 Box::Intersect(offscreen_.extents(), Box(x0[i], y0[i], x1[i], y1[i]));
65 mask_filter_.fillRect(mode, x0[i], y0[i], x1[i], y1[i], color);
66 if (!clipped.empty()) {
67 offscreen_.output().fillRect(
68 mode,
69 clipped.translate(-offscreen_.extents().xMin(),
70 -offscreen_.extents().yMin()),
72 }
73 }
74 }
75
76 void writePixels(BlendingMode mode, Color* color, int16_t* x, int16_t* y,
77 uint16_t pixel_count) override {
78 for (int i = 0; i < pixel_count; ++i) {
79 int16_t cx = x[i];
80 int16_t cy = y[i];
81 mask_filter_.fillPixels(mode, color[i], &x[i], &y[i], 1);
82 if (offscreen_.extents().contains(cx, cy)) {
83 cx -= offscreen_.extents().xMin();
84 cy -= offscreen_.extents().yMin();
85 offscreen_.output().fillPixels(mode, color::Black, &cx, &cy, 1);
86 }
87 }
88 // // TODO: remove duplicates.
89 // int16_t x_copy[pixel_count];
90 // std::copy(x, x + pixel_count, x_copy);
91 // int16_t y_copy[pixel_count];
92 // std::copy(y, y + pixel_count, y_copy);
93 // mask_filter_.writePixels(mode, color, x, y, pixel_count);
94 // // offscreen_.output().fillPixels(mode, color::Black, x_copy, y_copy,
95 // // pixel_count);
96 }
97
98 void fillPixels(BlendingMode mode, Color color, int16_t* x, int16_t* y,
99 uint16_t pixel_count) override {
100 for (int i = 0; i < pixel_count; ++i) {
101 int16_t cx = x[i];
102 int16_t cy = y[i];
103 mask_filter_.fillPixels(mode, color, &x[i], &y[i], 1);
104 if (offscreen_.extents().contains(cx, cy)) {
105 cx -= offscreen_.extents().xMin();
106 cy -= offscreen_.extents().yMin();
107 offscreen_.output().fillPixels(mode, color::Black, &cx, &cy, 1);
108 }
109 }
110
111 // int16_t x_copy[pixel_count];
112 // std::copy(x, x + pixel_count, x_copy);
113 // int16_t y_copy[pixel_count];
114 // std::copy(y, y + pixel_count, y_copy);
115 // mask_filter_.fillPixels(mode, color, x, y, pixel_count);
116 // // offscreen_.output().fillPixels(mode, color::Black, x_copy, y_copy,
117 // // pixel_count);
118 }
119
120 const ColorFormat& getColorFormat() const override { return color_format_; }
121
122 private:
123 const ColorFormat& color_format_;
124 BitMaskOffscreen offscreen_;
125 ClipMask mask_;
126 ClipMaskFilter mask_filter_;
127};
128
129} // namespace roo_display
Axis-aligned integer rectangle.
Definition box.h:12
int16_t xMin() const
Minimum x (inclusive).
Definition box.h:65
bool empty() const
Return whether the box is empty.
Definition box.h:62
Box translate(int16_t x_offset, int16_t y_offset) const
Return a translated copy of this box.
Definition box.h:127
bool contains(int16_t x, int16_t y) const
Return whether the point (x, y) lies within the box.
Definition box.h:86
static Box Intersect(const Box &a, const Box &b)
Return the intersection of two boxes (may be empty).
Definition box.h:25
int16_t yMin() const
Minimum y (inclusive).
Definition box.h:68
Filtering device that applies a clip mask.
Definition clip_mask.h:103
void setAddress(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, BlendingMode mode) override
Set a rectangular window filled by subsequent calls to write().
Definition clip_mask.h:116
void write(Color *color, uint32_t pixel_count) override
Write pixels into the current address window.
Definition clip_mask.h:124
void fill(Color color, uint32_t pixel_count) override
Write pixel_count copies of the same color into the current address window.
Definition clip_mask.h:140
void fillPixels(BlendingMode mode, Color color, int16_t *x, int16_t *y, uint16_t pixel_count) override
Draw the specified pixels using the same color. Invalidates the address window.
Definition clip_mask.h:188
Binary clip mask using a packed bit buffer.
Definition clip_mask.h:17
const Box & bounds() const
Return mask bounds.
Definition clip_mask.h:36
ARGB8888 color stored as a 32-bit unsigned integer.
Definition color.h:16
The abstraction for drawing to a display.
Definition device.h:15
void fillRect(BlendingMode blending_mode, const Box &rect, Color color)
Fill a single rectangle. Invalidates the address window.
Definition device.h:135
virtual void fill(Color color, uint32_t pixel_count)
Write pixel_count copies of the same color into the current address window.
Definition device.cpp:7
Writer that ensures front-most pixels are written first.
const ColorFormat & getColorFormat() const override
Return the native color format used by this device for direct drawing.
void writeRects(BlendingMode mode, Color *color, int16_t *x0, int16_t *y0, int16_t *x1, int16_t *y1, uint16_t count) override
Draw the specified rectangles (per-rectangle colors). Invalidates the address window.
void write(Color *color, uint32_t pixel_count) override
Write pixels into the current address window.
void fill(Color color, uint32_t pixel_count) override
Write pixel_count copies of the same color into the current address window.
void setAddress(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, BlendingMode mode) override
Set a rectangular window filled by subsequent calls to write().
void fillPixels(BlendingMode mode, Color color, int16_t *x, int16_t *y, uint16_t pixel_count) override
Draw the specified pixels using the same color. Invalidates the address window.
void fillRects(BlendingMode mode, Color color, int16_t *x0, int16_t *y0, int16_t *x1, int16_t *y1, uint16_t count) override
Draw the specified rectangles using the same color. Invalidates the address window.
void writePixels(BlendingMode mode, Color *color, int16_t *x, int16_t *y, uint16_t pixel_count) override
Draw the specified pixels (per-pixel colors). Invalidates the address window.
FrontToBackWriter(DisplayOutput &output, Box bounds)
Construct a front-to-back writer for a given bounds rectangle.
void setAddress(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, BlendingMode mode) override
Set a rectangular window filled by subsequent calls to write().
Definition offscreen.h:1381
void fillPixels(BlendingMode mode, Color color, int16_t *x, int16_t *y, uint16_t pixel_count) override
Draw the specified pixels using the same color. Invalidates the address window.
Definition offscreen.h:1524
Box extents() const override
Return the bounding box encompassing all pixels that need to be drawn.
Definition offscreen.h:592
const OffscreenDevice< ColorMode, pixel_order, byte_order, pixels_per_byte, storage_type > & output() const
Definition offscreen.h:610
static constexpr auto Black
Definition named.h:184
Defines 140 opaque HTML named colors.
BlendingMode
Porter-Duff style blending modes.
Definition blending.h:17