Highlight keys when clicking or tapping
This merge request should be considered a draft. It creates a stop-gap solution for #12 (closed) by inverting the background / foreground color of keys upon clicking and tapping.
I would like to use this as a basis for a discussion about the approach taken. My C++ is less than poor and my experience with SDL ... well, what experience?
Implementation-wise, I changed the keyboard textures to have streaming access mode (which is supposedly optimized for frequent changes). When clicking or tapping a key, its area is recreated inside the surface (taking the selection state into account) and then copied into the texture.
Open questions on my end:
Is the general approach of updating the surface, then copying it into the texture advisable here? Reading into some of the SDL docs, it seems that textures are optimized for display so maybe we should rather perform all operations on the textures directly (if that's possible by any chance)?Is it really necessary to convert the surface into the window's pixel format before every transfer into the texture? It seems like I could also directly create the surface in the window's pixel format from the start but I wasn't sure what side effects that might have.I'm currentlymemcpy
ing the whole pixel array into the texture on any touch / mouse down & up event. That's probably not very efficient. Is there an easy way to copy just the key rect?
Update
Answering part of my questions myself after having done more research.
It appears that texture's are stored in VRAM on the GPU whereas surfaces are stored in RAM which should in principle make textures a lot more performant than surfaces. The key drawing seems to be possible on the texture directly, e.g. there is SDL_RenderFillRect
which could be used on the texture instead of SDL_FillRect
on the surface. The only thing that appears to require surfaces as an intermediary step is the text rendering. To speed that up, we could cache the textures created for each character to be able to quickly draw them upon selection.
Drawing into the texture directly would eliminate having to constantly map all of the surface's pixels into the window format as well as having to manually copy pixels over from the surface into the texture.
Update 2
This fixes #12 (closed) and adds both key highlighting as well as character previews. I'm using the approach suggested by @MartijnBraam in the comments now. All highlighted keys are drawn into a separate texture. Upon rendering of the keyboard, both the normal and highlighted texture are combined to create the highlighting / preview effect.
I'd welcome feedback on the styling. Also, my C++ is still ugly so an opinionated review would be appreciated.
Some screenshots:
Merge request reports
Activity
added 1 commit
- ed903510 - Highlight keys when clicking or tapping
By Johannes Marbach on 2020-11-28T19:50:24
mentioned in issue #96 (closed)
By Johannes Marbach on 2020-11-29T19:19:24
added 6 commits
-
ed903510...fb8dd4bc - 5 commits from branch
postmarketOS:master
- 9e70c5be - Highlight keys when clicking or tapping
By Johannes Marbach on 2020-12-06T12:43:01
-
ed903510...fb8dd4bc - 5 commits from branch
- Resolved by Administrator
Seems great. I wonder if it would be possible to just draw the whole keyboard twice at the start, once normal and once with inverted colors and have that in 2 textures. Then when a key is held it can pick out the correct region of the inverted keyboard and draw that over the normal keyboard.
It would also be quite easy to draw the inverted keyboard with a small vertical offset so thee highlighted keys are slightly above the one pressed, like offset with half a key's height.
It would solve both the cache part and the memcpy part in one go and since all pixel data is in VRAM it should be very efficient on gpu accelerated hardware.
By Martijn Braam on 2020-12-09T18:51:03
Edited by Administrator
- Resolved by Administrator
I'm also pretty sure the color conversions for the surface formats is because the drawing code in SDL supports less pixel formats than what the framebuffers and window managers support, especially the weird formats with R and B swapped on some phones.
By Martijn Braam on 2020-12-11T19:42:03
Edited by Ghost User
added 3 commits
-
9e70c5be...f595754f - 2 commits from branch
postmarketOS:master
- 51623584 - Highlight keys and show preview when clicking or tapping
By Johannes Marbach on 2020-12-11T19:30:45
-
9e70c5be...f595754f - 2 commits from branch
added 1 commit
- 5e7072f8 - Highlight keys and show preview when clicking or tapping
By Johannes Marbach on 2020-12-11T19:33:47