Dafang-Hacks / Main

138 stars 63 forks source link

Better fixed-width font support #18

Closed puddly closed 6 years ago

puddly commented 6 years ago

The current OSD font is hard-coded to one of two sizes of a proportional font. I've added a proper fixed-width font and moved the font definition preprocessor macros into wrapper classes for each font.

nik0 commented 6 years ago

Merged (I hope) Thanks very much for this work!

nik0 commented 6 years ago

Can you check the compiled binary: https://github.com/EliasKotlyar/Xiaomi-Dafang-Hacks/pull/590 thanks!

puddly commented 6 years ago

Switching between fixed/variable and small/big fonts doesn't work properly because of an issue with the configuration object I missed. It's been fixed now in my branch (https://github.com/puddly/Main/commit/ead5aa04eba3d7fdf611e225416e295c18b392b6).

nik0 commented 6 years ago

I guess there is also another problem: it happens that font is null when calling osd_draw_timestamp it is at process startup (setting from web: position is 400, fixed width=no and size is small I used the code in your branch (puddly@ead5aa0).

puddly commented 6 years ago

Sorry, I did not think to test starting the server with different options. Fixed as of https://github.com/puddly/Main/commit/7e67cfd2f69d582b73ef21ceca39c990fa1bb5cc

The small sans-serif font is still not rendered very well, so I do agree that FreeType will be a massive improvement over the current method of rendering the OSD text.

nik0 commented 6 years ago

The branch you are referring is not the correction :-)

puddly commented 6 years ago

It was fixed in the commit prior to that one (https://github.com/puddly/Main/commit/026b383595961bc67221e520b8eab262faedd6e1), I just accidentally pushed a change intended for a different branch earlier.

nik0 commented 6 years ago

Check this one: https://github.com/EliasKotlyar/Xiaomi-Dafang-Hacks/pull/592

puddly commented 6 years ago

I finished adding support for FreeType so it might just be easier to merge that branch instead, once everything is properly tested this time.

The only minor issue I'm having at the moment is getting everything to compile nicely with the build scripts in the repository. I was able to build and link FreeType2 but only after adding its compilation steps into the compile-libraries.sh script and making some additions to the v4l2rtspserver-master/CMakeLists.txt, but I'm not too familiar with CMake.

nik0 commented 6 years ago

Cool !! Good news can you create a PR it will b easier to merge ... No worries about the libs, it is the same with the others

nik0 commented 6 years ago

I succeeded merging it ! It works like a charm ... I am just trying to make the font name and the font size configurable through setconf. For me it is always taking too much CPU. Will adjust the sleep The goodnews is that the memory is the same

puddly commented 6 years ago

There isn't a whole lot you can do with the sleep call because it runs exactly once per second at the same time (it's set to run at 100ms past the second). Any changes will make the OSD timestamps skip seconds or appear to count unevenly.

I'm guessing the main inefficiency comes from loading and rendering glyphs from the font every time the OSD is displayed, but profiling really should help determine the root cause. Some options:

  1. FreeType has a cache subsystem that might help with this. I'm not sure exactly what it caches, but it's built-in and might work.
  2. We can pre-render all of the ASCII glyphs and store them in a simple bitmap cache.
  3. We switch to using a higher-level library like Cairo, which seems to have its own FreeType caching system and can also render more complex graphics onto the image buffers. It requires FreeType and Pixman but will reduce the complexity of the OSD font rendering code significantly.
nik0 commented 6 years ago

Agree with you assessments. I guess most of the people (like me) doesn't care about the seconds. I see my camera on my phone through 3G or 4G network ... I can make it configurable if you want

nik0 commented 6 years ago

Can you check this: https://github.com/Dafang-Hacks/Main/pull/21

puddly commented 6 years ago

Those changes look good to me.

I realized that I missed a zero in the nanosleep delay, which caused the main update_thread to run every 100ms instead of every second. This was the root cause of the CPU usage issue you saw and has been fixed as of https://github.com/puddly/Main/commit/06ba5cff44e83bc56b2b2fe4d5f6c873be07618f. I don't think the refresh time setting is really necessary now unless there's a real use for it besides performance reasons.

Cairo speeds up things a little bit, even when used inefficiently. Here's my quick attempt at using it to render only the text.

nik0 commented 6 years ago

Yeah, working better ... I did a quick test this morning, will push it this evening What is the gain of Cairo ? Does it worth it?

puddly commented 6 years ago

I've never used FreeType or Cairo for anything besides this so I can't speak authoritatively, but I found Cairo to be much easier to work with than using FreeType directly. It also uses a pixel-manipulation library that contains optimizations for the MIPS instruction set and has font caching, which might be useful.

That being said, it probably makes no difference if the only thing being rendered is a line of text and maybe some rectangles in the future. Rendering everything takes about 0.02 seconds for me, which includes time spent in the magic IMP library.

nik0 commented 6 years ago

Agree, then let's stick on Freetype ...