Skip to contentSkip to conversation

fbuffer: Improve the speed of the cursor drawing

PATCH In review Thomas Huth 3 participants 8 messages Jul 28, 2015 – Jul 29, 2015

lore
Share & tools
lore
#0 lore raw

cover fbuffer: Improve the speed of the cursor drawing

Greg Kurz recently already did a great work by introducing the invert-region helpers to speed up the drawing of the cursor. This made the grub2 command prompt and editor mode usable. However, the editor mode of grub2 is still sluggish - because grub2 always redraws the whole line (with cursor enabled!) when you enter a character, and since cursor and character drawing is still not super-fast in SLOF, this sums up to a noticable delay.

So I tried to speed up the drawing a little bit and came up with the following patches. I've used a little "benchmark" in Forth to see how the patches improve the drawing:

1 buffer: etest-buf
: etest
erase-screen
41 etest-buf c!
1b emit 5b emit [char] H emit
cursor-on
milliseconds
20 0 do 3a 0 do etest-buf 1 terminal-write drop loop cr loop
milliseconds
swap -
." Duration: " .d ." ms" cr
;

Without my patches, the etest function takes 1.8 s to draw. With the first patch applied, the function take ca. 1.2 s. With both patches applied, the function only takes ca. 0.8 s !

The first patch changes invert-region to use a bigger access width if possible. And the second patch simply decreases the size of the cursor from a full-character size rectangle to an underscore-like cursor instead... not sure whether this is acceptable, but at least I think it looks as good as the original cursor.

Apart from my patches, I think grub2 should also disable the cursor when redrawing a whole line in the editor. However, I do not have much clue about the grub2 sources yet ... is anybody familiar with this and could give some pointers?

Thomas Huth (2):
fbuffer: Improve invert-region helper
fbuffer: Use a smaller cursor

 board-js2x/slof/helper.fs | 13 ++++++++-----
 board-qemu/slof/helper.fs | 14 ++++++++++----
 slof/fs/fbuffer.fs        |  5 +++--
 3 files changed, 21 insertions(+), 11 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/2 fbuffer: Improve invert-region helper

The introduction of invert-region already speeded up the cursor drawing very much. But there is still space for improvement:
So far invert-region is accessing the memory only byte by byte, but with some additional logic that checks the alignment of the address and the length of the area, we can also make this function to access the memory with half-word, word or long-word accesses. With this additional logic, invert-region-x is also no longer necessary and thus can be removed.

---
 board-js2x/slof/helper.fs | 13 ++++++++-----
 board-qemu/slof/helper.fs | 14 ++++++++++----
 slof/fs/fbuffer.fs        |  2 +-
 3 files changed, 19 insertions(+), 10 deletions(-)
board-js2x/slof/helper.fs +8 -5
index 6030330..5941315 100644
@@ -28,9 +28,12 @@
 ;
 
 : invert-region ( addr len -- )
-   0 ?DO dup dup rb@ -1 xor swap rb! 1+ LOOP drop
-;
-
-: invert-region-x ( addr len -- )
-   /x / 0 ?DO dup dup rx@ -1 xor swap rx! xa1+ LOOP drop
+   2dup or 7 and CASE
+      0 OF 3 rshift 0 ?DO dup dup rx@ -1 xor swap rx! xa1+ LOOP ENDOF
+      2 OF 1 rshift 0 ?DO dup dup rw@ -1 xor swap rw! wa1+ LOOP ENDOF
+      4 OF 2 rshift 0 ?DO dup dup rl@ -1 xor swap rl! la1+ LOOP ENDOF
+      6 OF 1 rshift 0 ?DO dup dup rw@ -1 xor swap rw! wa1+ LOOP ENDOF
+      dup OF 0 ?DO dup dup rb@ -1 xor swap rb! 1+ LOOP ENDOF
+   ENDCASE
+   drop
 ;
board-qemu/slof/helper.fs +10 -4
index c807bc6..6613782 100644
@@ -33,10 +33,16 @@
   swap -
 ;
 
-: invert-region ( addr len -- )
-   over swap 0 swap 1 hv-logical-memop drop
+: invert-region-cs ( addr len cellsize -- )
+   >r over swap r@ rshift r> swap 1 hv-logical-memop drop
 ;
 
-: invert-region-x ( addr len -- )
-   over swap /x / 3 swap 1 hv-logical-memop drop
+: invert-region ( addr len -- )
+   2dup or 7 and CASE
+      0 OF 3 invert-region-cs ENDOF
+      2 OF 1 invert-region-cs ENDOF
+      4 OF 2 invert-region-cs ENDOF
+      6 OF 1 invert-region-cs ENDOF
+      dup OF 0 invert-region-cs ENDOF
+   ENDCASE
 ;
slof/fs/fbuffer.fs +1 -1
index fcdd2fa..0128c07 100644
@@ -170,7 +170,7 @@ CREATE bitmap-buffer 400 4 * allot
 ;
 
 : fb8-invert-screen ( -- )
-	frame-buffer-adr screen-height screen-width * screen-depth * invert-region-x
+	frame-buffer-adr screen-height screen-width * screen-depth * invert-region
 ;
 
 : fb8-blink-screen ( -- ) fb8-invert-screen fb8-invert-screen ;
Signature · 1 line
1.8.3.1
#2 lore raw

Re: [SLOF PATCH 1/2] fbuffer: Improve invert-region helper

Quoted 5 lines · Thomas Huth

: invert-region ( addr len -- )

board-js2x/slof/helper.fs +0 -4
-   0 ?DO dup dup rb@ -1 xor swap rb! 1+ LOOP drop
-;
-
-: invert-region-x ( addr len -- )

board-js2x/slof/helper.fs line 39

Quoted 10 lines
board-js2x/slof/helper.fs +8 -1
-   /x / 0 ?DO dup dup rx@ -1 xor swap rx! xa1+ LOOP drop
+   2dup or 7 and CASE
+      0 OF 3 rshift 0 ?DO dup dup rx@ -1 xor swap rx! xa1+ LOOP ENDOF
+      2 OF 1 rshift 0 ?DO dup dup rw@ -1 xor swap rw! wa1+ LOOP ENDOF
+      4 OF 2 rshift 0 ?DO dup dup rl@ -1 xor swap rl! la1+ LOOP ENDOF
+      6 OF 1 rshift 0 ?DO dup dup rw@ -1 xor swap rw! wa1+ LOOP ENDOF
+      dup OF 0 ?DO dup dup rb@ -1 xor swap rb! 1+ LOOP ENDOF
+   ENDCASE
+   drop
 ;

Can you access device memory as 64 bits for all supported devices?

You can get a bigger speedup by writing some of the core blitting functions in C, btw.

A small simplification:

2dup or 7 and CASE
0 OF 3 rshift 0 ?DO dup dup rx@ -1 xor swap rx! xa1+ LOOP ENDOF 4 OF 2 rshift 0 ?DO dup dup rl@ -1 xor swap rl! la1+ LOOP ENDOF 3 and
2 OF 1 rshift 0 ?DO dup dup rw@ -1 xor swap rw! wa1+ LOOP ENDOF dup OF 0 ?DO dup dup rb@ -1 xor swap rb! 1+ LOOP ENDOF
ENDCASE

If this code is often called unaligned, it makes more sense to special- case the begin and end probably.

Segher

#3 lore raw

Re: [SLOF PATCH 1/2] fbuffer: Improve invert-region helper

Hi Segher,

Quoted 17 lines · Segher Boessenkool
Quoted 5 lines · Thomas Huth

: invert-region ( addr len -- )

board-js2x/slof/helper.fs +0 -4
-   0 ?DO dup dup rb@ -1 xor swap rb! 1+ LOOP drop
-;
-
-: invert-region-x ( addr len -- )

board-js2x/slof/helper.fs line 39

Quoted 10 lines
board-js2x/slof/helper.fs +8 -1
-   /x / 0 ?DO dup dup rx@ -1 xor swap rx! xa1+ LOOP drop
+   2dup or 7 and CASE
+      0 OF 3 rshift 0 ?DO dup dup rx@ -1 xor swap rx! xa1+ LOOP ENDOF
+      2 OF 1 rshift 0 ?DO dup dup rw@ -1 xor swap rw! wa1+ LOOP ENDOF
+      4 OF 2 rshift 0 ?DO dup dup rl@ -1 xor swap rl! la1+ LOOP ENDOF
+      6 OF 1 rshift 0 ?DO dup dup rw@ -1 xor swap rw! wa1+ LOOP ENDOF
+      dup OF 0 ?DO dup dup rb@ -1 xor swap rb! 1+ LOOP ENDOF
+   ENDCASE
+   drop
 ;
Quoted 1 line

Can you access device memory as 64 bits for all supported devices?

Yes, should be fine since 64 bit access was already used in the original code, see fb8-invert-screen in
https://github.com/aik/SLOF/commit/99c534ecc7a8566bd9ca6346915d9ac1bfacae1e

Quoted 2 lines

You can get a bigger speedup by writing some of the core blitting functions in C, btw.

Well, the above code is for js2x only ... so this is likely not worth the effort anymore. The code for qemu-spapr calls into a hypercall already, so this is already accelerated.

Quoted 9 lines

A small simplification:

2dup or 7 and CASE
0 OF 3 rshift 0 ?DO dup dup rx@ -1 xor swap rx! xa1+ LOOP ENDOF 4 OF 2 rshift 0 ?DO dup dup rl@ -1 xor swap rl! la1+ LOOP ENDOF 3 and
2 OF 1 rshift 0 ?DO dup dup rw@ -1 xor swap rw! wa1+ LOOP ENDOF dup OF 0 ?DO dup dup rb@ -1 xor swap rb! 1+ LOOP ENDOF
ENDCASE

Ok, nice idea, makes sense! I'll include it in v2 (after waiting a little bit to see if there's other feedback)

Quoted 2 lines

If this code is often called unaligned, it makes more sense to special- case the begin and end probably.

It's only used for drawing the cursor, so it always should be aligned.

Thomas

#4 lore raw

PATCH 2/2 fbuffer: Use a smaller cursor

In reply to Thomas Huth

Drawing the cursor in the frame buffer memory is a very, very slow operation. So let's simply switch to a "underscore" cursor instead of the full block cursor to save some precious cycles.

---
 slof/fs/fbuffer.fs | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
slof/fs/fbuffer.fs +2 -1
index 0128c07..542c431 100644
@@ -98,7 +98,8 @@ CREATE bitmap-buffer 400 4 * allot
 
 : fb8-toggle-cursor ( -- )
 	line# fb8-line2addr column# fb8-columns2bytes +
-	char-height 0 ?DO
+	char-height 3 - screen-width screen-depth * * +
+	3 0 ?DO
 		dup char-width screen-depth * invert-region
 		screen-width screen-depth * +
 	LOOP drop
Signature · 1 line
1.8.3.1
#5 lore raw

Re: [SLOF PATCH 2/2] fbuffer: Use a smaller cursor

Quoted 14 lines · Thomas Huth

Drawing the cursor in the frame buffer memory is a very, very slow operation. So let's simply switch to a "underscore" cursor instead of the full block cursor to save some precious cycles.

---
  slof/fs/fbuffer.fs | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
slof/fs/fbuffer.fs +0 -0
index 0128c07..542c431 100644

slof/fs/fbuffer.fs CREATE bitmap-buffer 400 4 * allot · line 102

Quoted 6 lines
slof/fs/fbuffer.fs +2 -1
@@ -98,7 +98,8 @@ CREATE bitmap-buffer 400 4 * allot
 
  : fb8-toggle-cursor ( -- )
  	line# fb8-line2addr column# fb8-columns2bytes +
-	char-height 0 ?DO
+	char-height 3 - screen-width screen-depth * * +
+	3 0 ?DO

Why not just:

- char-height 0 ?DO
+ 1 0 ?DO

? What is this magic with screen-width about?

Quoted 4 lines
slof/fs/fbuffer.fs +0 -0
  		dup char-width screen-depth * invert-region
  		screen-width screen-depth * +
  	LOOP drop
Signature · 1 line
Alexey
#6 lore raw

Re: [SLOF] [SLOF PATCH 2/2] fbuffer: Use a smaller cursor

Quoted 11 lines · Alexey Kardashevskiy

slof/fs/fbuffer.fs CREATE bitmap-buffer 400 4 * allot · line 102

Quoted 5 lines

: fb8-toggle-cursor ( -- )

slof/fs/fbuffer.fs +2 -1
 	line# fb8-line2addr column# fb8-columns2bytes +
-	char-height 0 ?DO
+	char-height 3 - screen-width screen-depth * * +
+	3 0 ?DO

Why not just:

(quoted diff) +1 -1
-	char-height 0 ?DO
+	1 0 ?DO
Quoted 1 line

? What is this magic with screen-width about?

Thomas' patch draws the cursor as the bottom three lines of a character cell; your suggestion would draw it as the top one line. But indeed it could be

char-height dup 3 - ?DO ...

Segher

#7 lore raw

Re: [SLOF] [SLOF PATCH 2/2] fbuffer: Use a smaller cursor

Quoted 9 lines · Segher Boessenkool
Quoted 8 lines · Alexey Kardashevskiy

slof/fs/fbuffer.fs CREATE bitmap-buffer 400 4 * allot · line 102

Quoted 5 lines

: fb8-toggle-cursor ( -- )

slof/fs/fbuffer.fs +2 -1
 	line# fb8-line2addr column# fb8-columns2bytes +
-	char-height 0 ?DO
+	char-height 3 - screen-width screen-depth * * +
+	3 0 ?DO

Why not just:

Quoted 7 lines
Quoted 4 lines
(quoted diff) +1 -1
-	char-height 0 ?DO
+	1 0 ?DO

? What is this magic with screen-width about?

Thomas' patch draws the cursor as the bottom three lines of a character cell; your suggestion would draw it as the top one line.

Right.

Quoted 3 lines

But indeed it could be

char-height dup 3 - ?DO ...

Since the loop body expects the framebuffer address on the stack, the loop boundaries are just used to count the iterations ... so "3 0" sounds like the easiest and most readable way to specify this IMHO.

Thomas