Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New DebugScreeen implementation (was: "Missing psvDebugScreenClear() function") #50

Closed
windsurfer1122 opened this issue Jul 17, 2019 · 20 comments

Comments

@windsurfer1122
Copy link
Contributor

windsurfer1122 commented Jul 17, 2019

The sample "prx_loader" uses the function psvDebugScreenClear() which is not defined in any header file or library.
Did this function get lost somewhen? Or can it be added to the Vita SDK for general usage?

On the net I found the following code in different projects (mostly in a file called graphics.c):

void psvDebugScreenClear(int bg_color)
{
	gX = gY = 0;
	int i;
	color_t *pixel = (color_t *)getVramDisplayBuffer();
	for(i = 0; i < SCREEN_WIDTH * SCREEN_HEIGHT; i++) {
		pixel->rgba = bg_color;
		pixel++;
	}
}

[1] https://github.com/dots-tb/rePatch-reDux0/blob/master/rePatchAIDs/graphics.c

@Rinnegatamante
Copy link
Member

That's not the only issue with that sample in particular. It refers to an old vitasdk version. The sample itself should be updated.

@windsurfer1122
Copy link
Contributor Author

windsurfer1122 commented Jul 18, 2019

@Rinnegatamante Thanks for the insight.

I investigated further and recognized that psvDebugScreenClear() was transformed to the macro CLEARSCRN. The code of the macro is much slower than the old one for the full screen, where lines and columns do not matter (complete screen vs. rectangular block).

Additionally I wonder why the debug screen code is inside a header file and has several static definitions.

Currently adapting the old psvDebugScreenClear() function, plus splitting debug screen code and defines into separate code and header files.

@windsurfer1122
Copy link
Contributor Author

Adapted psvDebugScreenClear() to current state of debug screen code.
Put debug screen code into a .c file while still keeping the possibility to include it.
Fixed typos, sanitized some macros, prepared macro for font scaling.

All debugScreen*.(h|c) files are still in common folder.
Just have to list ../common/debugScreen.c in add_executable().

For other projects all debugScreen*.c files can also be put into a src folder and all debugScreen*.h files into an inc(lude|) folder.
common_debugScreen_20190718.zip

Next will be to adopt the font scaler to it. Like debug_put_char_32() of https://github.com/TheOfficialFloW/h-encore/blob/master/bootstrap/scr_printf.c

@yne
Copy link
Contributor

yne commented Jul 19, 2019

The debugScreen.h is ANSI compatible. So please stop using all those psvDebugScreenSet{F,B}gColor / psvDebugScreenClear functions and just use escape sequence while calling printf.

See: https://github.com/vitasdk/samples/blob/master/debug_print/src/main.c

@Rinnegatamante Yeah I was probably waiting for some feedback and eventually lost track of time and simply moved to another project.

@windsurfer1122 psvDebugScreen was never meant to be efficient
Sure we could use the DMA to clear the screen, or the GMX to draw the glyphs faster. But that's not the goal of this library.

So you want to switch to a bigger/smaller font ? there's a sample for that.

@windsurfer1122
Copy link
Contributor Author

windsurfer1122 commented Jul 19, 2019

@yne Thanks for the explanation. Never had to deal with CSI sequences back in my C days. Will re-adapt.

@windsurfer1122
Copy link
Contributor Author

windsurfer1122 commented Jul 21, 2019

UPDATE: 2019-07-25. Own bug fixed and added much more for getting closer to the ANSI CSI implementation and for having functionality in own project.

I enhanced and fixed some bugs in the current debugScreen implementation.
Also checked all samples to compile cleanly and correctly use CSI sequences.
Maybe the maintainers can consider accpeting these changes for the Vita SDK.

File: Update in post #50 (comment)

debugScreen

  • Splitted code and shared definitions into .c and .h file, so it can be used in bigger projects with multiple source files.
    C++ projects still need to include the code once(!) into any source file (e.g. main.cpp), for this define DEBUG_SCREEN_CODE_INCLUDE before including the debugScreen header file.
    Added custom header file to avoid people messing up the implementation in their projects.
  • Fixed CSI 0J command. Enhanced sample debugscreen cases to show the issue.
  • Added documentation about CSI commands at top and comments in C functions.
  • Implemented ANSI/VTERM/GREYSCALE colors via lookup table for correct colors.
  • Implemented color state machine to comply better with ECMA-48 / ISO/IEC 6429:1992 and Graphic Rendition Combination Mode (GRCM) "Cumulative".
  • Added CSI 22m. Set standard intensity ("normal" color). Accomplishes CSI 1m and 2m.
  • Added CSI 7m and 27m. Enable/disable FG/BG inversion.
  • Support 16 storage slots for CSI s and u.
  • Enhanced bitmap routines to work also with not byte-aligned glyphs.
    • Enhanced HTML bitmap editor to work also with not byte-aligned glyphs.
  • Draw dummy glyph (dotted line in the middle) in correct font size for not available glyphs.
  • Added function to get a copy of current color state.
  • Added functions to get/set pixel coordinates for custom save storages and custom positioning.
  • Added functions to get/set font.
  • Added function to scale a font by 2.
  • Sanitized macro usage.
  • Fixed typos.
  • Extended sample "debugscreen" to test more cases and demonstrate new functions:
    • used different background colors to better show CSI J and K commands
    • added font scaling
    • added inversion on/off via CSI 7m and CSI 27m
    • show "dark" white on "bright" black
    • added missing glyph
    • show all colors CSI 30-37/40-47/90-97/100-107 with their RGB code
  • Tested nearly all samples, except for network samples.

Samples in C

  • Added "../common/debugScreen.c" in CMakeLists.txt to the project executables:
    • audio
    • ctrl
    • debug_print
    • debugscreen
    • hello_world
    • microphone
    • motion
    • net_http
    • net_http_bsd
    • power
    • rtc
    • socket_ping
    • touch

Samples in C++

  • Defined DEBUG_SCREEN_CODE_INCLUDE in main.cpp
    • net_libcurl
    • soloud

Sample camera

  • Added missing include <psp2/kernel/processmgr.h> for sceKernelExitProcess().

Sample microphone

  • Added missing include <string.h> for memset() and memcpy().
  • Changed direct access to coordX/Y to CSI sequences.

Sample net_http_bsd

  • Changed direct access to font to new functions.

Sample power

  • Added missing include <stdio.h> for FILE, fopen(), fclose().
  • Added missing include <string.h> for memset().

0J bug with old implementation

debugscreen_sample_0J_issue

New debugscreen sample

debugscreen_sample_20190725

@yne
Copy link
Contributor

yne commented Jul 22, 2019

Nice job, it's always nice to see some unification.
What was the issue from having a single self-exclusive .h file ?
The power sample is definitely strange:

  • the fopen shall not succeed to create a new file unless w+
  • the created fd is unused

@windsurfer1122
Copy link
Contributor Author

windsurfer1122 commented Jul 22, 2019

My private project will have 2 or 3 different source files (*.c) and all of them will create some debugScreen output. If the old "debugScreen.h" is included in all 3 files, I will have duplicate code in my .o files and/or linker issues.

Side note:
When checking my own bug I also found out that the color creation for CSI m is a little bit off:

  • CSI 2m is actually "dark" color (faint/decreased intensity) and not "normal" color/intensity
  • CSI 22m is "normal" color/intensity
  • CSI 30-37/40-47/90-97/100-107m do not fit to CSI 1/2m implementation
  • Naming is sometimes wrongly RGB although internally it is BGR.
    Open points:
  • Check official CSI documentation (see [1])
  • CSI 1/2/22m seem to be flags rather than just an immediate color change, e.g. CSI 1;31m should be identical to CSI 31;1m?
  • Check CSI 38/48;5;(n)m implementation

So I'm currently fixing these, also implementing CSI 7m for "invert"/"reverse video" and optimze code on the way (guideline: always code like there is no compiler optimization).
My sample "debugscreen" is becoming heavily colorful :)
Will need some more days due to time constrains.

[1] https://www.ecma-international.org/publications/standards/Ecma-048.htm

@yne
Copy link
Contributor

yne commented Jul 23, 2019

I will have duplicate code in my .o files and/or linker issues.

Have you tried to change psvDebugScreen{Init,Puts,Printf} to static ?
In my tests it resolve the linking issue, and the code is not duplicated either, and it's still easy to include in small project (like all the vita samples).

guideline: always code like there is no compiler optimization

Did you mean

Trying to outsmart a compiler defeats much of the purpose of using one -- Kernighan

?

So I'm currently fixing these, also implementing [...]

Let's hope those features and hand-crafted optimization won't add more bugs and complexity than the features they add

@windsurfer1122
Copy link
Contributor Author

Don't want to outsmart or avoid the optimizer of the compiler (via pragmas, flags, etc.) as I never would assume that I'm smarter than those guys developing compilers.
Just doing calculations when necessary and where appropiate (e.g. either before or inside a loop).

Always try to keep code maintainable/clean, sanitized, straight forward and re-usable.

Just read all available docs for CSI sequences and hope to get it done the next days.

@windsurfer1122
Copy link
Contributor Author

Updated earlier comment/post #50 (comment) with updated implementation. Tested quite a lot and should be a solid implementation. Should be final.
Ready for review.

@windsurfer1122 windsurfer1122 changed the title Missing psvDebugScreenClear() function New DebugScreeen implementation (was: "Missing psvDebugScreenClear() function") Jul 25, 2019
@yne
Copy link
Contributor

yne commented Jul 25, 2019

You shall at least check net_http_bsd sample because it printf the result of http://rate.sx/ and http://wttr.in/ wich rely on those CSI codes.

@windsurfer1122
Copy link
Contributor Author

windsurfer1122 commented Jul 25, 2019

@yne net_http_bsd is looking good.

rate sx
net_http_bsd_wttr in

@yne
Copy link
Contributor

yne commented Jul 26, 2019

@windsurfer1122 nice :)
Any update about the static suggestion ?
If it's working, your PR shall be more simple (no #include or CMake changes)

@windsurfer1122
Copy link
Contributor Author

Hiding internal functions via static is ok, but for shared functions this would be awkward.
The static suggestion would make the code local to the source (and its related binary object), therefore for each source file of a project the same code would be compiled and included into the final binary.
Will check if the current gcc is intelligent enough and recognizes the duplicate binary result. But that will be on sunday.

The only reason I found for including the code as it was before, was for C++, and that could be handled with a simple #define.

The changes to the CPP source with #define and to the CMakeLists.txt are very minor from my point of view. Even for unexperienced C/C++ coders.
Is this still a pain in your stomache?

@yne
Copy link
Contributor

yne commented Jul 26, 2019

Is this still a pain in your stomache?

Nah I'm fine, I was just curious if the static version was also working in your situation.
The .c+.h is the most common practice, (even if It's not my cup of tea anymore, than you Bellard).
I'm okay to have it in c+h style, don't worry :).

@windsurfer1122
Copy link
Contributor Author

windsurfer1122 commented Jul 26, 2019

Compromise:

  • Use #define DEBUG_SCREEN_CODE_INCLUDE in debugScreen_custom.h (gets rid of CMakeLists.txt changes)
  • Leave the #define in C++, as it is mandatory for them.

File: sdk-common-debugscreen-fixes-enhancements_20190726_1459.zip

And thanks for your reviews and discussion.

@windsurfer1122
Copy link
Contributor Author

My first pull request. Hope this is done correctly.
#51

@windsurfer1122
Copy link
Contributor Author

Updated pull request. Corrected header file for C++, so even less changes are needed in the C++ samples to the source code.

@windsurfer1122
Copy link
Contributor Author

Added via ded5780

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants