forked from swissmicros/SDKdemo
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathblitter.h
2215 lines (1940 loc) · 72.3 KB
/
blitter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef BLITTER_H
#define BLITTER_H
// ****************************************************************************
// blitter.h DB48X project
// ****************************************************************************
//
// File Description:
//
// Low-level graphic routines (blitter) for the DB48X project
//
// These routines are designed to be highly-optimizable while being able
// to deal with 1, 4 and 16 bits per pixel as found on various calculators
// To achieve that objective, the code is parameterized at compile-time,
// so it makes relatively heavy use of templates and inlining.
//
//
//
// ****************************************************************************
// (C) 2022 Christophe de Dinechin <[email protected]>
// This software is licensed under the terms outlined in LICENSE.txt
// ****************************************************************************
// This file is part of DB48X.
//
// DB48X is free software: you can redistribute it and/or modify
// it under the terms outlined in the LICENSE.txt file
//
// DB48X is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// ****************************************************************************
//
// In the code, BPP stands for "Bits per pixels", and BPW for "Bits per word"
// Pixel buffer words are assumed to be 32-bit as on most calculators today.
//
#include "font.h"
#include "types.h"
#include "utf8.h"
#include "recorder.h"
RECORDER_DECLARE(debug);
#define PACKED __attribute__((packed))
struct blitter
// ----------------------------------------------------------------------------
// The graphics class managing bitmap operations
// ----------------------------------------------------------------------------
{
// ========================================================================
//
// Types and constants
//
// ========================================================================
// Basic types
typedef int32_t coord;
typedef uint32_t size;
typedef size_t offset;
typedef uint32_t pixword;
typedef uint16_t palette_index;
typedef uint64_t pattern_bits;
enum
{
BPW = 8 * sizeof(pixword) // Bits per pixword
};
enum mode
// ------------------------------------------------------------------------
// Graphics mode (including bits per pixel info)
// ------------------------------------------------------------------------
{
MONOCHROME, // Monochrome bitmap, e.g. fonts
MONOCHROME_REVERSE, // Monochrome bitmap, reverse X axis (DM42)
GRAY_4BPP, // Gray, 4 bits per pixel (HP50G and related)
RGB_16BPP, // RGB16 (HP Prime)
};
// ========================================================================
//
// Color representation
//
// ========================================================================
// Colors have a generic RGB-based interface, even on monochrome systems
// like the DM42, or on grayscale systems like the HP50G.
template <mode Mode>
union color
// ------------------------------------------------------------------------
// The generic color representation
// ------------------------------------------------------------------------
{
// The constructor takes red/green/blue values
color(uint8_t red, uint8_t green, uint8_t blue);
// The constructor from a bit pattern
color(pixword bits);
// Return the components
uint8_t red();
uint8_t green();
uint8_t blue();
enum
{
BPP = 1
};
};
// ========================================================================
//
// Pattern representation
//
// ========================================================================
// A pattern is a NxN set of pixels on screen, corresponding to a fixed
// number of bits. This is used to simulate gray scales on monochrome
// machines like the DM42, but can also create visual effects on grayscale
// or color systems.
// Patterns are presently always stored as a 64-bit value for efficient
// processing during drawing. For 1BPP, patterns represent 8x8 pixel,
// for 4BPP they represent 4x4 pixels, and for 16BPP 2x2 pixels.
// This means it is always possible to create two-color and four-color
// patterns in a way that is independent of the target system.
// A pattern can be built from RGB values, which will build a pattern
// with the corresponding pixel density on a monochrome system.
template <mode Mode>
union pattern
// ------------------------------------------------------------------------
// Pattern representation for fills
// ------------------------------------------------------------------------
{
uint64_t bits;
enum
{
SIZE = 1
}; // Size of the pattern
enum : uint64_t
{
SOLID = 1
}; // Multiplier for solids
enum
{
BPP = color<Mode>::BPP
}; // Bit per pixels for pattern
using color = blitter::color<Mode>;
public:
// Build a solid pattern from a single color
pattern(color c);
// Build a checkered pattern for a given RGB level
pattern(uint8_t red, uint8_t green, uint8_t blue);
// Build a checkerboard from 2 or 4 colors in an array
template <uint N>
pattern(color colors[N]);
// Build a pattern with two or four alternating colors
pattern(color a, color b);
pattern(color a, color b, color c, color d);
// Pattern from bits
pattern(uint64_t bits = ~0ULL): bits(bits) {}
// Some pre-defined shades of gray
static const pattern black = pattern(0, 0, 0);
static const pattern gray10 = pattern(32, 32, 32);
static const pattern gray25 = pattern(64, 64, 64);
static const pattern gray50 = pattern(128, 128, 128);
static const pattern gray75 = pattern(192, 192, 192);
static const pattern gray90 = pattern(224, 224, 224);
static const pattern white = pattern(255, 255, 255);
static const pattern invert = pattern();
};
// ========================================================================
//
// Points and rectangles
//
// ========================================================================
struct point
// ------------------------------------------------------------------------
// A point holds a pair of coordinates
// ------------------------------------------------------------------------
{
point(coord x = 0, coord y = 0) : x(x), y(y)
{
}
point(const point &o) = default;
coord x, y;
};
struct rect
// ------------------------------------------------------------------------
// A rectangle is a point with a width and a height
// ------------------------------------------------------------------------
{
rect(coord x1 = 0, coord y1 = 0, coord x2 = -1, coord y2 = -1)
: x1(x1),
y1(y1),
x2(x2),
y2(y2)
{
}
rect(size w, size h) : rect(0, 0, w - 1, h - 1)
{
}
rect(const rect &o) = default;
rect &operator=(const rect &o) = default;
rect &operator&=(const rect &o)
// --------------------------------------------------------------------
// Intersection of two rectangles
// --------------------------------------------------------------------
{
if (x1 < o.x1)
x1 = o.x1;
if (x2 > o.x2)
x2 = o.x2;
if (y1 < o.y1)
y1 = o.y1;
if (y2 > o.y2)
y2 = o.y2;
return *this;
}
rect &operator|=(const rect &o)
// --------------------------------------------------------------------
// Union of two rectangles
// --------------------------------------------------------------------
{
if (x1 > o.x1)
x1 = o.x1;
if (x2 < o.x2)
x2 = o.x2;
if (y1 > o.y1)
y1 = o.y1;
if (y2 < o.y2)
y2 = o.y2;
return *this;
}
friend rect operator&(const rect &a, const rect &b)
// --------------------------------------------------------------------
// Intersection of two rectangle
// --------------------------------------------------------------------
{
rect r(a);
r &= b;
return r;
}
friend rect operator|(const rect &a, const rect &b)
// --------------------------------------------------------------------
// Union of two rectangle
// --------------------------------------------------------------------
{
rect r(a);
r |= b;
return r;
}
void inset(size dw, size dh)
// --------------------------------------------------------------------
// Inset the rectangle by the given amount
// --------------------------------------------------------------------
{
x1 += dw;
y1 += dh;
x2 -= dw;
y2 -= dh;
}
void inset(size d)
// --------------------------------------------------------------------
// Inset the rectangle by the given amount
// --------------------------------------------------------------------
{
inset(d, d);
}
void offset(coord dx, coord dy)
// --------------------------------------------------------------------
// Offset a rectangle by the given amounts
// --------------------------------------------------------------------
{
x1 += dx;
x2 += dx;
y1 += dy;
y2 += dy;
}
bool empty() const
// --------------------------------------------------------------------
// Check if a rectangle is empty
// --------------------------------------------------------------------
{
return x1 > x2 || y1 > y2;
}
size width() const
// --------------------------------------------------------------------
// Return the width of a rectangle
// --------------------------------------------------------------------
{
return x2 - x1 + 1;
}
size height() const
// --------------------------------------------------------------------
// Return the height of a rectangle
// --------------------------------------------------------------------
{
return y2 - y1 + 1;
}
bool contains(point p) const
// --------------------------------------------------------------------
// Return true if point is inside the rectangle
// --------------------------------------------------------------------
{
return p.x >= x1 && p.x <= x2 && p.y >= y1 && p.y <= y2;
}
public:
coord x1, y1, x2, y2;
};
// =========================================================================
//
// Core blitting routine
//
// =========================================================================
enum clipping
// ------------------------------------------------------------------------
// Hints to help the compiler drop useless code
// ------------------------------------------------------------------------
{
CLIP_NONE = 0,
CLIP_SRC = 1,
CLIP_DST = 2,
CLIP_ALL = 3,
SKIP_SOURCE = 4,
SKIP_COLOR = 8,
OVERLAP = 16, // Set if src and dest overlap
FILL_QUICK = SKIP_SOURCE,
FILL_SAFE = SKIP_SOURCE | CLIP_DST,
COPY = CLIP_ALL | SKIP_COLOR,
DRAW = CLIP_ALL,
};
// blitop: a blitting operation
typedef pixword (*blitop)(pixword dst, pixword src, pixword arg);
template <clipping Clip, typename Dst, typename Src, mode CMode>
static void blit(Dst &dst,
const Src &src,
const rect &drect,
const point &spos,
blitop op,
pattern<CMode> colors);
// ========================================================================
//
// Surface: a bitmap for graphic operations
//
// ========================================================================
template <mode Mode>
struct surface
// -------------------------------------------------------------------------
// Structure representing a surface on screen
// -------------------------------------------------------------------------
{
surface(pixword *p, size w, size h, size scanline)
: pixels(p),
w(w),
h(h),
scanline(scanline),
drawable(w, h)
{
}
surface(pixword *p, size w, size h) : surface(p, w, h, w)
{
}
surface(const surface &o) = default;
void horizontal_adjust(coord UNUSED &x1, coord UNUSED &x2) const
{
}
void vertical_adjust(coord UNUSED &x1, coord UNUSED &x2) const
{
}
static bool horizontal_swap() { return false; }
static bool vertical_swap() { return false; }
static const mode blitmode = Mode;
// Operations used by the blitting routine
using color = blitter::color<Mode>;
using pattern = blitter::pattern<Mode>;
enum
{
BPP = color::BPP
};
public:
void clip(const rect &r)
// --------------------------------------------------------------------
// Limit drawing to the given rectangle
// --------------------------------------------------------------------
{
drawable = r;
drawable &= rect(w, h);
}
void clip(coord x1, coord y1, coord x2, coord y2)
// --------------------------------------------------------------------
// Clip an area given in coordinates
// --------------------------------------------------------------------
{
clip(rect(x1, y1, x2, y2));
}
const rect &clip() const
// --------------------------------------------------------------------
// Return the clipping area
// --------------------------------------------------------------------
{
return drawable;
}
rect area() const
// --------------------------------------------------------------------
// Return total drawing area
// --------------------------------------------------------------------
{
return rect(w, h);
}
size width() const
// --------------------------------------------------------------------
// Total drawing width
// --------------------------------------------------------------------
{
return w;
}
size height() const
// --------------------------------------------------------------------
// Total drawing height
// --------------------------------------------------------------------
{
return h;
}
color pixel_color(coord x, coord y) const
// --------------------------------------------------------------------
// Return the color for the given pixel
// --------------------------------------------------------------------
{
if (drawable.contains(point(x,y)))
{
horizontal_adjust(x,x);
vertical_adjust(y,y);
offset po = pixel_offset(x, y);
pixword *pa = pixel_address(po);
offset ps = pixel_shift(po);
pixword pw = *pa;
pixword pc = (pw >> ps) & ~(~0U << BPP);
return color(pc);
}
return color(0);
}
template <clipping Clip = FILL_SAFE>
void fill(const rect &r, pattern colors = pattern::black)
// --------------------------------------------------------------------
// Fill a rectangle
// --------------------------------------------------------------------
{
blit<Clip>(*this, *this, r, point(), blitop_set, colors);
}
template <clipping Clip = FILL_SAFE>
void fill(coord x1,
coord y1,
coord x2,
coord y2,
pattern colors = pattern::black)
// --------------------------------------------------------------------
// Fill a rectangle with a color pattern
// --------------------------------------------------------------------
{
fill<Clip>(rect(x1, y1, x2, y2), colors);
}
template <clipping Clip = FILL_SAFE>
void fill(pattern colors = pattern::black)
// --------------------------------------------------------------------
// Fill the entire area with the chosen color
// --------------------------------------------------------------------
{
fill<Clip>(drawable, colors);
}
template <clipping Clip = FILL_SAFE>
void invert(const rect &r, pattern colors = pattern::invert)
// --------------------------------------------------------------------
// Invert a rectangle
// --------------------------------------------------------------------
{
blit<Clip>(*this, *this, r, point(), blitop_xor, colors);
}
template <clipping Clip = FILL_SAFE>
void invert(coord x1,
coord y1,
coord x2,
coord y2,
pattern colors = pattern::invert)
// --------------------------------------------------------------------
// Invert a rectangle with a color pattern
// --------------------------------------------------------------------
{
invert<Clip>(rect(x1, y1, x2, y2), colors);
}
template <clipping Clip = FILL_SAFE>
void invert(pattern colors = pattern::invert)
// --------------------------------------------------------------------
// Invert the entire area with the chosen color
// --------------------------------------------------------------------
{
invert<Clip>(drawable, colors);
}
template <clipping Clip = COPY, mode SMode>
void copy(surface<SMode> &src,
const rect &r,
const point &spos = point(0, 0))
// --------------------------------------------------------------------
// Copy a rectangular area from the source
// --------------------------------------------------------------------
{
blit<Clip>(*this, src, r, spos, blitop_source, pattern());
}
template <clipping Clip = COPY, mode SMode>
void copy(surface<SMode> &src,
const point &pos = point(0, 0))
// --------------------------------------------------------------------
// Copy a rectangular area from the source
// --------------------------------------------------------------------
{
return copy(src, pos.x, pos.y, pattern());
}
template <clipping Clip = COPY, mode SMode>
void copy(surface<SMode> &src,
coord x,
coord y)
// --------------------------------------------------------------------
// Copy a rectangular area from the source
// --------------------------------------------------------------------
{
rect dest(x, y, x + src.w - 1, y + src.h - 1);
blit<Clip>(*this, src, dest, point(), blitop_source, pattern());
}
template <clipping Clip = DRAW, mode SMode>
void draw(surface<SMode> &src,
const point &pos = point(0, 0),
pattern color = pattern::black,
blitop op = blitop_draw)
// --------------------------------------------------------------------
// Draw a rectangular area from the source
// --------------------------------------------------------------------
{
return draw(src, pos.x, pos.y, color, op);
}
template <clipping Clip = DRAW, mode SMode>
void draw(surface<SMode> &src,
coord x,
coord y,
pattern color = pattern::black,
blitop op = blitop_draw)
// --------------------------------------------------------------------
// Draw a rectangular area from the source
// --------------------------------------------------------------------
{
rect dest(x, y, x + src.w - 1, y + src.h - 1);
blit<Clip>(*this, src, dest, point(), op, color);
}
template <clipping Clip = DRAW, mode SMode>
void draw_background(surface<SMode> &src,
const point &pos = point(0, 0),
pattern color = pattern::black,
blitop op = blitop_background)
// --------------------------------------------------------------------
// Draw a rectangular area from the source backgroundo
// --------------------------------------------------------------------
{
draw(src, pos.x, pos.y, color, op);
}
template <clipping Clip = DRAW, mode SMode>
void draw_background(surface<SMode> &src,
coord x,
coord y,
pattern color = pattern::white,
blitop op = blitop_background)
// --------------------------------------------------------------------
// Draw a rectangular area from the source background
// --------------------------------------------------------------------
{
draw(src, x, y, color, op);
}
template <clipping Clip = CLIP_DST>
coord glyph(coord x,
coord y,
unicode codepoint,
const font *f,
pattern colors = pattern::black,
blitop op = blitop_draw);
// --------------------------------------------------------------------
// Draw a glyph with the given operation and colors
// --------------------------------------------------------------------
template <clipping Clip = CLIP_DST>
coord glyph(coord x,
coord y,
unicode codepoint,
const font *f,
pattern fg,
pattern bg);
// --------------------------------------------------------------------
// Draw a glyph with a foreground and background
// --------------------------------------------------------------------
template <clipping Clip = CLIP_DST>
coord text(coord x,
coord y,
utf8 text,
const font *f,
pattern colors = pattern::black,
blitop op = blitop_draw);
// --------------------------------------------------------------------
// Draw a text with the given operation and colors
// --------------------------------------------------------------------
template <clipping Clip = CLIP_DST>
coord text(coord x,
coord y,
utf8 text,
const font *f,
pattern fg,
pattern bg);
// --------------------------------------------------------------------
// Draw a text with a foreground and background
// --------------------------------------------------------------------
template <clipping Clip = CLIP_DST>
coord text(coord x,
coord y,
utf8 text,
size_t len,
const font *f,
pattern colors = pattern::black,
blitop op = blitop_draw);
// --------------------------------------------------------------------
// Draw a text with the given operation and colors
// --------------------------------------------------------------------
template <clipping Clip = CLIP_DST>
coord text(coord x,
coord y,
utf8 text,
size_t len,
const font *f,
pattern fg,
pattern bg);
// --------------------------------------------------------------------
// Draw a text with a foreground and background
// --------------------------------------------------------------------
template <clipping Clip = FILL_SAFE>
void line(coord x1,
coord y1,
coord x2,
coord y2,
size width,
pattern fg);
// --------------------------------------------------------------------
// Draw a line between the given coordinates
// --------------------------------------------------------------------
template <clipping Clip = FILL_SAFE>
void ellipse(coord x1,
coord y1,
coord x2,
coord y2,
size width,
pattern fg);
// --------------------------------------------------------------------
// Draw an ellipse with the given coordinates
// --------------------------------------------------------------------
template <clipping Clip = FILL_SAFE>
void circle(coord x, coord y, size r, size width, pattern fg)
// --------------------------------------------------------------------
// Draw a circle with the given coordinates
// --------------------------------------------------------------------
{
ellipse(x - r / 2,
y - r / 2,
x + (r + 1) / 2,
y + (r + 1) / 2,
width,
fg);
}
template <clipping Clip = FILL_SAFE>
void rectangle(coord x1, coord y1,
coord x2, coord y2,
size width, pattern fg)
// --------------------------------------------------------------------
// Draw a rectangle
// --------------------------------------------------------------------
{
rounded_rectangle(x1, y1, x2, y2, 0, width, fg);
}
template <clipping Clip = FILL_SAFE>
void rounded_rectangle(coord x1, coord y1,
coord x2, coord y2,
size r, size width, pattern fg);
// --------------------------------------------------------------------
// Draw a rounded rectangle between the given coordinates
// --------------------------------------------------------------------
protected:
offset pixel_offset(coord x, coord y) const
// ---------------------------------------------------------------------
// Offset in bits in a given surface for the given coordinates
// ---------------------------------------------------------------------
{
return ((offset) scanline * y + x) * (offset) BPP;
}
offset pixel_shift(offset bitoffset) const
// ---------------------------------------------------------------------
// Shift in bits in the word for the given coordinates
// ---------------------------------------------------------------------
{
return bitoffset % BPW;
}
pixword *pixel_address(offset bitoffset) const
// ---------------------------------------------------------------------
// Get the address of a word representing the pixel in a surface
// ---------------------------------------------------------------------
{
return pixels + bitoffset / BPW;
}
protected:
friend struct blitter;
pixword *pixels; // Word-aligned address of surface buffer
size w; // Pixel width of buffer
size h; // Pixel height of buffer
size scanline; // Scanline for the buffer (can be > width)
rect drawable; // Draw area (clipping outside)
};
protected:
// ========================================================================
//
// Helper routines
//
// ========================================================================
static inline pixword shl(pixword value, unsigned shift)
// -------------------------------------------------------------------------
// Shift right guaranteeing a zero for a large shift (even on x86)
// -------------------------------------------------------------------------
{
return shift < BPW ? value << shift : 0;
}
static inline pixword shr(pixword value, unsigned shift)
// -------------------------------------------------------------------------
// Shift right guaranteeing a zero for a large shift (even on x86)
// -------------------------------------------------------------------------
{
return shift < BPW ? value >> shift : 0;
}
static inline pixword shlc(pixword value, unsigned shift)
// -------------------------------------------------------------------------
// Shift x1 by the complement of the given shift
// -------------------------------------------------------------------------
{
return shl(value, BPW - shift);
}
static pixword shrc(pixword value, unsigned shift)
// -------------------------------------------------------------------------
// Shift x2 by the complement of the given shift
// -------------------------------------------------------------------------
{
return shr(value, BPW - shift);
}
template <typename Int>
static inline uint64_t rotate(Int bits, uint shift)
// --------------------------------------------------------------------
// Rotate a 64-bit pattern by the given amount
// --------------------------------------------------------------------
{
enum
{
BIT_SIZE = sizeof(bits) * 8
};
shift %= BIT_SIZE;
bits = ((bits >> shift) | (bits << (BIT_SIZE - shift)));
return bits;
}
static inline pixword bitswap(pixword bits)
// ------------------------------------------------------------------------
// Flip left and right in the input bits
// ------------------------------------------------------------------------
{
pixword result = 0;
for (uint bit = 0; bit < 32; bit++)
if ((bits >> bit) & 1)
result |= 1U << bit;
return result;
}
template <mode Dst, mode Src>
static pixword convert(pixword data);
// ------------------------------------------------------------------------
// Convert data from Src mode to Dst mode (converts the low bits)
// ------------------------------------------------------------------------
// =========================================================================
//
// Operators for blit
//
// =========================================================================
public:
static pixword blitop_set(pixword UNUSED dst,
pixword UNUSED src,
pixword arg)
// -------------------------------------------------------------------------
// This simply sets the color passed in arg
// -------------------------------------------------------------------------
{
return arg;
}
static pixword blitop_source(pixword UNUSED dst,
pixword src,
pixword UNUSED arg)
// -------------------------------------------------------------------------
// This simly sets the color from the source
// -------------------------------------------------------------------------
{
return src;
}
static pixword blitop_xor(pixword dst, pixword src, pixword arg)
// -------------------------------------------------------------------------
// Perform a 'xor' graphical operation (can also be used for inverting)
// -------------------------------------------------------------------------
{
dst ^= src ^ arg;
return dst;
}
static pixword blitop_and(pixword dst, pixword src, pixword arg)
// -------------------------------------------------------------------------
// Perform the 'and' operation
// -------------------------------------------------------------------------
{
dst &= src ^ arg;
return dst;
}
static pixword blitop_or(pixword dst, pixword src, pixword arg)
// -------------------------------------------------------------------------
// Perform a 'or' graphical operation
// -------------------------------------------------------------------------
{
dst |= src ^ arg;
return dst;
}
static pixword blitop_nop(pixword dst, pixword UNUSED s, pixword UNUSED a)
// ------------------------------------------------------------------------
// No graphical operation
// ------------------------------------------------------------------------
{
return dst;
}
static pixword blitop_draw(pixword dst, pixword src, pixword arg)
// ------------------------------------------------------------------------
// Colorize based on source
// ------------------------------------------------------------------------
{
return (dst & src) | (arg & ~src);
}
static pixword blitop_background(pixword dst, pixword src, pixword arg)
// ------------------------------------------------------------------------
// Colorize based on source
// ------------------------------------------------------------------------
{
return (dst & ~src) | (arg & src);
}
};
// ============================================================================
//
// Color template specializations for various BPP
//
// ============================================================================
template <>
union blitter::color<blitter::mode::MONOCHROME>
// ------------------------------------------------------------------------
// Color representation (1-bit, e.g. DM42)
// ------------------------------------------------------------------------
{
color(uint8_t red, uint8_t green, uint8_t blue)
: value((red + green + green + blue) / 4 >= 128)
{ }
color(pixword pix): value(pix != 0) {}
uint8_t red()
{
return value * 255;
}
uint8_t green()
{
return value * 255;
}
uint8_t blue()
{
return value * 255;
}