34
34
#include "shared-module/sharpdisplay/SharpMemoryFramebuffer.h"
35
35
36
36
#include "supervisor/memory.h"
37
- #include "supervisor/shared/safe_mode.h"
38
37
39
38
#define SHARPMEM_BIT_WRITECMD_LSB (0x80)
40
39
#define SHARPMEM_BIT_VCOM_LSB (0x40)
41
40
42
- static void * hybrid_alloc (size_t sz ) {
43
- supervisor_allocation * allocation = allocate_memory (align32_size (sz ), false, false);
44
- if (allocation ) {
45
- memset (allocation -> ptr , 0 , sz );
46
- return allocation -> ptr ;
47
- }
48
- if (gc_alloc_possible ()) {
49
- return m_malloc (sz , true);
50
- }
51
- reset_into_safe_mode (MEM_MANAGE );
52
- return NULL ; // unreached
53
- }
54
-
55
- static inline void hybrid_free (void * ptr_in ) {
56
- supervisor_allocation * allocation = allocation_from_ptr (ptr_in );
57
-
58
- if (allocation ) {
59
- free_memory (allocation );
60
- }
61
- }
62
-
63
41
STATIC uint8_t bitrev (uint8_t n ) {
64
42
uint8_t r = 0 ;
65
43
for (int i = 0 ;i < 8 ;i ++ ) r |= ((n >>i ) & 1 )<<(7 - i );
@@ -102,17 +80,22 @@ void common_hal_sharpdisplay_framebuffer_reset(sharpdisplay_framebuffer_obj_t *s
102
80
}
103
81
104
82
void common_hal_sharpdisplay_framebuffer_reconstruct (sharpdisplay_framebuffer_obj_t * self ) {
105
- if (! allocation_from_ptr ( self -> bufinfo . buf )) {
106
- self -> bufinfo .buf = NULL ;
107
- }
83
+ // Look up the allocation by the old pointer and get the new pointer from it.
84
+ supervisor_allocation * alloc = allocation_from_ptr ( self -> bufinfo .buf ) ;
85
+ self -> bufinfo . buf = alloc ? alloc -> ptr : NULL ;
108
86
}
109
87
110
88
void common_hal_sharpdisplay_framebuffer_get_bufinfo (sharpdisplay_framebuffer_obj_t * self , mp_buffer_info_t * bufinfo ) {
111
89
if (!self -> bufinfo .buf ) {
112
90
int row_stride = common_hal_sharpdisplay_framebuffer_get_row_stride (self );
113
91
int height = common_hal_sharpdisplay_framebuffer_get_height (self );
114
92
self -> bufinfo .len = row_stride * height + 2 ;
115
- self -> bufinfo .buf = hybrid_alloc (self -> bufinfo .len );
93
+ supervisor_allocation * alloc = allocate_memory (align32_size (self -> bufinfo .len ), false, true);
94
+ if (alloc == NULL ) {
95
+ m_malloc_fail (self -> bufinfo .len );
96
+ }
97
+ self -> bufinfo .buf = alloc -> ptr ;
98
+ memset (alloc -> ptr , 0 , self -> bufinfo .len );
116
99
117
100
uint8_t * data = self -> bufinfo .buf ;
118
101
* data ++ = SHARPMEM_BIT_WRITECMD_LSB ;
@@ -123,7 +106,9 @@ void common_hal_sharpdisplay_framebuffer_get_bufinfo(sharpdisplay_framebuffer_ob
123
106
}
124
107
self -> full_refresh = true;
125
108
}
126
- * bufinfo = self -> bufinfo ;
109
+ if (bufinfo ) {
110
+ * bufinfo = self -> bufinfo ;
111
+ }
127
112
}
128
113
129
114
void common_hal_sharpdisplay_framebuffer_deinit (sharpdisplay_framebuffer_obj_t * self ) {
@@ -137,7 +122,7 @@ void common_hal_sharpdisplay_framebuffer_deinit(sharpdisplay_framebuffer_obj_t *
137
122
138
123
common_hal_reset_pin (self -> chip_select .pin );
139
124
140
- hybrid_free ( self -> bufinfo .buf );
125
+ free_memory ( allocation_from_ptr ( self -> bufinfo .buf ) );
141
126
142
127
memset (self , 0 , sizeof (* self ));
143
128
}
@@ -154,19 +139,7 @@ void common_hal_sharpdisplay_framebuffer_construct(sharpdisplay_framebuffer_obj_
154
139
self -> height = height ;
155
140
self -> baudrate = baudrate ;
156
141
157
- int row_stride = common_hal_sharpdisplay_framebuffer_get_row_stride (self );
158
- self -> bufinfo .len = row_stride * height + 2 ;
159
- // re-use a supervisor allocation if possible
160
- self -> bufinfo .buf = hybrid_alloc (self -> bufinfo .len );
161
-
162
- uint8_t * data = self -> bufinfo .buf ;
163
- * data ++ = SHARPMEM_BIT_WRITECMD_LSB ;
164
-
165
- for (int y = 0 ; y < self -> height ; y ++ ) {
166
- * data = bitrev (y + 1 );
167
- data += row_stride ;
168
- }
169
- self -> full_refresh = true;
142
+ common_hal_sharpdisplay_framebuffer_get_bufinfo (self , NULL );
170
143
}
171
144
172
145
void common_hal_sharpdisplay_framebuffer_swapbuffers (sharpdisplay_framebuffer_obj_t * self , uint8_t * dirty_row_bitmask ) {
@@ -271,7 +244,5 @@ const framebuffer_p_t sharpdisplay_framebuffer_proto = {
271
244
};
272
245
273
246
void common_hal_sharpdisplay_framebuffer_collect_ptrs (sharpdisplay_framebuffer_obj_t * self ) {
274
- gc_collect_ptr (self -> framebuffer );
275
247
gc_collect_ptr (self -> bus );
276
- gc_collect_ptr (self -> bufinfo .buf );
277
248
}
0 commit comments