From c2e4a7977349f5b4b69cc39829c50512a9503f15 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Fri, 3 Feb 2017 13:01:00 -0800 Subject: fb: add fragment bounds checking helper Simple x,y coordinate check --- src/fb.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/fb.h b/src/fb.h index 13f6bcd..64f657b 100644 --- a/src/fb.h +++ b/src/fb.h @@ -34,4 +34,13 @@ void fb_get_put_pages_count(fb_t *fb, unsigned *count); fb_t * fb_new(int drm_fd, uint32_t crtc_id, uint32_t *connectors, int n_connectors, drmModeModeInfoPtr mode, int n_pages); void fb_fragment_divide(fb_fragment_t *fragment, unsigned n_fragments, fb_fragment_t fragments[]); +static inline int fb_fragment_contains(fb_fragment_t *fragment, int x, int y) +{ + if (x < fragment->x || x >= fragment->x + fragment->width || + y < fragment->y || y >= fragment->y + fragment->height) + return 0; + + return 1; +} + #endif -- cgit v1.2.3 From 91e5956932d17de17c0c75be20c834cf7a6c6ed9 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Fri, 3 Feb 2017 13:06:26 -0800 Subject: rototiller: extricate draw_pixel() bounds checking If a z-buffer is added these checks will need to be done independent from and prior to drawing. Also it's silly to makergb() pixels which can't be drawn. --- src/modules/sparkler/draw.h | 8 +------- src/modules/sparkler/rocket.c | 7 ++++++- src/modules/sparkler/simple.c | 7 ++++++- src/modules/sparkler/spark.c | 8 +++++++- src/modules/sparkler/xplode.c | 12 ++++++++---- 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/modules/sparkler/draw.h b/src/modules/sparkler/draw.h index 5010374..b988319 100644 --- a/src/modules/sparkler/draw.h +++ b/src/modules/sparkler/draw.h @@ -15,18 +15,12 @@ static inline uint32_t makergb(uint32_t r, uint32_t g, uint32_t b, float intensi return (((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff)); } -static inline int draw_pixel(fb_fragment_t *f, int x, int y, uint32_t pixel) +static inline void draw_pixel(fb_fragment_t *f, int x, int y, uint32_t pixel) { uint32_t *pixels = f->buf; - if (y < 0 || y >= f->height || x < 0 || x >= f->width) { - return 0; - } - /* FIXME this assumes stride is aligned to 4 */ pixels[(y * (f->width + (f->stride >> 2))) + x] = pixel; - - return 1; } #endif diff --git a/src/modules/sparkler/rocket.c b/src/modules/sparkler/rocket.c index 6b9dc5e..2088cc7 100644 --- a/src/modules/sparkler/rocket.c +++ b/src/modules/sparkler/rocket.c @@ -1,6 +1,7 @@ #include #include "draw.h" +#include "fb.h" #include "particle.h" #include "particles.h" @@ -122,10 +123,14 @@ static void rocket_draw(particles_t *particles, particle_t *p, int x, int y, fb_ { rocket_ctxt_t *ctxt = p->ctxt; - if (!draw_pixel(f, x, y, 0xff0000)) { + if (!fb_fragment_contains(f, x, y)) { /* kill off parts that wander off screen */ ctxt->longevity = 0; + + return; } + + draw_pixel(f, x, y, 0xff0000); } diff --git a/src/modules/sparkler/simple.c b/src/modules/sparkler/simple.c index e453e46..74edc24 100644 --- a/src/modules/sparkler/simple.c +++ b/src/modules/sparkler/simple.c @@ -1,6 +1,7 @@ #include #include "draw.h" +#include "fb.h" #include "particle.h" #include "particles.h" @@ -97,10 +98,14 @@ static void simple_draw(particles_t *particles, particle_t *p, int x, int y, fb_ { simple_ctxt_t *ctxt = p->ctxt; - if (!draw_pixel(f, x, y, makergb(0xff, 0xff, 0xff, ((float)ctxt->longevity / ctxt->lifetime)))) { + if (!fb_fragment_contains(f, x, y)) { /* immediately kill off stars that wander off screen */ ctxt->longevity = 0; + + return; } + + draw_pixel(f, x, y, makergb(0xff, 0xff, 0xff, ((float)ctxt->longevity / ctxt->lifetime))); } diff --git a/src/modules/sparkler/spark.c b/src/modules/sparkler/spark.c index ea68ac2..786be78 100644 --- a/src/modules/sparkler/spark.c +++ b/src/modules/sparkler/spark.c @@ -1,6 +1,7 @@ #include #include "draw.h" +#include "fb.h" #include "particle.h" #include "particles.h" @@ -47,10 +48,15 @@ static void spark_draw(particles_t *particles, particle_t *p, int x, int y, fb_f { spark_ctxt_t *ctxt = p->ctxt; - if (!draw_pixel(f, x, y, makergb(0xff, 0xa0, 0x20, ((float)ctxt->longevity / ctxt->lifetime)))) { + if (!fb_fragment_contains(f, x, y)) { /* offscreen */ ctxt->longevity = 0; + + return; } + + draw_pixel(f, x, y, makergb(0xff, 0xa0, 0x20, ((float)ctxt->longevity / ctxt->lifetime))); + } diff --git a/src/modules/sparkler/xplode.c b/src/modules/sparkler/xplode.c index 24a436e..5f99f7b 100644 --- a/src/modules/sparkler/xplode.c +++ b/src/modules/sparkler/xplode.c @@ -1,6 +1,7 @@ #include #include "draw.h" +#include "fb.h" #include "particle.h" #include "particles.h" @@ -60,16 +61,19 @@ static void xplode_draw(particles_t *particles, particle_t *p, int x, int y, fb_ xplode_ctxt_t *ctxt = p->ctxt; uint32_t color; + if (!fb_fragment_contains(f, x, y)) { + /* offscreen */ + ctxt->longevity = 0; + return; + } + if (ctxt->longevity == ctxt->lifetime) { color = makergb(0xff, 0xff, 0xa0, 1.0); } else { color = makergb(0xff, 0xff, 0x00, ((float)ctxt->longevity / ctxt->lifetime)); } - if (!draw_pixel(f, x, y, color)) { - /* offscreen */ - ctxt->longevity = 0; - } + draw_pixel(f, x, y, color); } -- cgit v1.2.3 From 1a330772b827364471dbe02b417c14fb42838c2d Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Fri, 3 Feb 2017 15:23:50 -0800 Subject: fb: add pixel drawing helpers Modules seem to be duplicating this, just pull it into fb.h since we're already dependent on the fb.h abstractions in the modules. Slippery slope! --- src/fb.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/fb.h b/src/fb.h index 64f657b..b990ae6 100644 --- a/src/fb.h +++ b/src/fb.h @@ -34,6 +34,8 @@ void fb_get_put_pages_count(fb_t *fb, unsigned *count); fb_t * fb_new(int drm_fd, uint32_t crtc_id, uint32_t *connectors, int n_connectors, drmModeModeInfoPtr mode, int n_pages); void fb_fragment_divide(fb_fragment_t *fragment, unsigned n_fragments, fb_fragment_t fragments[]); + +/* checks if a coordinate is contained within a fragment */ static inline int fb_fragment_contains(fb_fragment_t *fragment, int x, int y) { if (x < fragment->x || x >= fragment->x + fragment->width || @@ -43,4 +45,26 @@ static inline int fb_fragment_contains(fb_fragment_t *fragment, int x, int y) return 1; } + +/* puts a pixel into the fragment, no bounds checking is performed. */ +static inline void fb_fragment_put_pixel_unchecked(fb_fragment_t *fragment, int x, int y, uint32_t pixel) +{ + uint32_t *pixels = fragment->buf; + + /* FIXME this assumes stride is aligned to 4 */ + pixels[(y * (fragment->width + (fragment->stride >> 2))) + x] = pixel; +} + + +/* puts a pixel into the fragment, bounds checking is performed with a draw performed return status */ +static inline int fb_fragment_put_pixel_checked(fb_fragment_t *fragment, int x, int y, uint32_t pixel) +{ + if (!fb_fragment_contains(fragment, x, y)) + return 0; + + fb_fragment_put_pixel_unchecked(fragment, x, y, pixel); + + return 1; +} + #endif -- cgit v1.2.3 From 90fde2df2d8c59b0b885e6e02b5e89dbe966a5e9 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Fri, 3 Feb 2017 15:43:51 -0800 Subject: sparkler: use fb.h put_pixel helpers discards draw_pixel(), introduces helpers.h and a convenience function for bounds checking and oob extermination. Move makergb to helpers.h, draw.h gets removed in a later commit. --- src/modules/sparkler/draw.h | 9 --------- src/modules/sparkler/helpers.h | 36 ++++++++++++++++++++++++++++++++++++ src/modules/sparkler/rocket.c | 9 +++------ src/modules/sparkler/simple.c | 9 +++------ src/modules/sparkler/spark.c | 10 +++------- src/modules/sparkler/xplode.c | 9 +++------ 6 files changed, 48 insertions(+), 34 deletions(-) create mode 100644 src/modules/sparkler/helpers.h diff --git a/src/modules/sparkler/draw.h b/src/modules/sparkler/draw.h index b988319..10701e0 100644 --- a/src/modules/sparkler/draw.h +++ b/src/modules/sparkler/draw.h @@ -5,15 +5,6 @@ #include "fb.h" -/* helper for scaling rgb colors and packing them into an pixel */ -static inline uint32_t makergb(uint32_t r, uint32_t g, uint32_t b, float intensity) -{ - r = (((float)intensity) * r); - g = (((float)intensity) * g); - b = (((float)intensity) * b); - - return (((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff)); -} static inline void draw_pixel(fb_fragment_t *f, int x, int y, uint32_t pixel) { diff --git a/src/modules/sparkler/helpers.h b/src/modules/sparkler/helpers.h new file mode 100644 index 0000000..e263d36 --- /dev/null +++ b/src/modules/sparkler/helpers.h @@ -0,0 +1,36 @@ +#ifndef _HELPERS_H +#define _HELPERS_H + +#include + +#include "fb.h" +#include "particle.h" +#include "particles.h" + + +/* helper for scaling rgb colors and packing them into an pixel */ +static inline uint32_t makergb(uint32_t r, uint32_t g, uint32_t b, float intensity) +{ + r = (((float)intensity) * r); + g = (((float)intensity) * g); + b = (((float)intensity) * b); + + return (((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff)); +} + + +/* return if the particle should be drawn, and set *longevity to 0 if out of bounds */ +static inline int should_draw_expire_if_oob(particles_t *particles, particle_t *p, int x, int y, fb_fragment_t *f, int *longevity) +{ + if (!fb_fragment_contains(f, x, y)) { + /* offscreen */ + if (longevity) + *longevity = 0; + + return 0; + } + + return 1; +} + +#endif diff --git a/src/modules/sparkler/rocket.c b/src/modules/sparkler/rocket.c index 2088cc7..ae628d3 100644 --- a/src/modules/sparkler/rocket.c +++ b/src/modules/sparkler/rocket.c @@ -1,7 +1,7 @@ #include -#include "draw.h" #include "fb.h" +#include "helpers.h" #include "particle.h" #include "particles.h" @@ -123,14 +123,11 @@ static void rocket_draw(particles_t *particles, particle_t *p, int x, int y, fb_ { rocket_ctxt_t *ctxt = p->ctxt; - if (!fb_fragment_contains(f, x, y)) { + if (!should_draw_expire_if_oob(particles, p, x, y, f, &ctxt->longevity)) /* kill off parts that wander off screen */ - ctxt->longevity = 0; - return; - } - draw_pixel(f, x, y, 0xff0000); + fb_fragment_put_pixel_unchecked(f, x, y, 0xff0000); } diff --git a/src/modules/sparkler/simple.c b/src/modules/sparkler/simple.c index 74edc24..5524282 100644 --- a/src/modules/sparkler/simple.c +++ b/src/modules/sparkler/simple.c @@ -1,7 +1,7 @@ #include -#include "draw.h" #include "fb.h" +#include "helpers.h" #include "particle.h" #include "particles.h" @@ -98,14 +98,11 @@ static void simple_draw(particles_t *particles, particle_t *p, int x, int y, fb_ { simple_ctxt_t *ctxt = p->ctxt; - if (!fb_fragment_contains(f, x, y)) { + if (!should_draw_expire_if_oob(particles, p, x, y, f, &ctxt->longevity)) /* immediately kill off stars that wander off screen */ - ctxt->longevity = 0; - return; - } - draw_pixel(f, x, y, makergb(0xff, 0xff, 0xff, ((float)ctxt->longevity / ctxt->lifetime))); + fb_fragment_put_pixel_unchecked(f, x, y, makergb(0xff, 0xff, 0xff, ((float)ctxt->longevity / ctxt->lifetime))); } diff --git a/src/modules/sparkler/spark.c b/src/modules/sparkler/spark.c index 786be78..aa449c6 100644 --- a/src/modules/sparkler/spark.c +++ b/src/modules/sparkler/spark.c @@ -1,7 +1,7 @@ #include -#include "draw.h" #include "fb.h" +#include "helpers.h" #include "particle.h" #include "particles.h" @@ -48,15 +48,11 @@ static void spark_draw(particles_t *particles, particle_t *p, int x, int y, fb_f { spark_ctxt_t *ctxt = p->ctxt; - if (!fb_fragment_contains(f, x, y)) { + if (!should_draw_expire_if_oob(particles, p, x, y, f, &ctxt->longevity)) /* offscreen */ - ctxt->longevity = 0; - return; - } - - draw_pixel(f, x, y, makergb(0xff, 0xa0, 0x20, ((float)ctxt->longevity / ctxt->lifetime))); + fb_fragment_put_pixel_unchecked(f, x, y, makergb(0xff, 0xa0, 0x20, ((float)ctxt->longevity / ctxt->lifetime))); } diff --git a/src/modules/sparkler/xplode.c b/src/modules/sparkler/xplode.c index 5f99f7b..3e3beb4 100644 --- a/src/modules/sparkler/xplode.c +++ b/src/modules/sparkler/xplode.c @@ -1,7 +1,7 @@ #include -#include "draw.h" #include "fb.h" +#include "helpers.h" #include "particle.h" #include "particles.h" @@ -61,11 +61,8 @@ static void xplode_draw(particles_t *particles, particle_t *p, int x, int y, fb_ xplode_ctxt_t *ctxt = p->ctxt; uint32_t color; - if (!fb_fragment_contains(f, x, y)) { - /* offscreen */ - ctxt->longevity = 0; + if (!should_draw_expire_if_oob(particles, p, x, y, f, &ctxt->longevity)) return; - } if (ctxt->longevity == ctxt->lifetime) { color = makergb(0xff, 0xff, 0xa0, 1.0); @@ -73,7 +70,7 @@ static void xplode_draw(particles_t *particles, particle_t *p, int x, int y, fb_ color = makergb(0xff, 0xff, 0x00, ((float)ctxt->longevity / ctxt->lifetime)); } - draw_pixel(f, x, y, color); + fb_fragment_put_pixel_unchecked(f, x, y, color); } -- cgit v1.2.3 From f21492a1e8314e27ef307bc4b2629468d40e6c72 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Fri, 3 Feb 2017 15:46:08 -0800 Subject: sparkler: drop draw.h --- src/modules/sparkler/draw.h | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 src/modules/sparkler/draw.h diff --git a/src/modules/sparkler/draw.h b/src/modules/sparkler/draw.h deleted file mode 100644 index 10701e0..0000000 --- a/src/modules/sparkler/draw.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef _DRAW_H -#define _DRAW_H - -#include - -#include "fb.h" - - -static inline void draw_pixel(fb_fragment_t *f, int x, int y, uint32_t pixel) -{ - uint32_t *pixels = f->buf; - - /* FIXME this assumes stride is aligned to 4 */ - pixels[(y * (f->width + (f->stride >> 2))) + x] = pixel; -} - -#endif -- cgit v1.2.3 From a7c44b6146e306b13b63b8bececa6b7bc8ed59fb Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Fri, 3 Feb 2017 16:08:01 -0800 Subject: stars: use fb_fragment_put_pixel_unchecked() drop draw_pixel() duplication --- src/modules/stars/draw.h | 16 ---------------- src/modules/stars/stars.c | 2 +- 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/src/modules/stars/draw.h b/src/modules/stars/draw.h index 5010374..0b68c00 100644 --- a/src/modules/stars/draw.h +++ b/src/modules/stars/draw.h @@ -3,8 +3,6 @@ #include -#include "fb.h" - /* helper for scaling rgb colors and packing them into an pixel */ static inline uint32_t makergb(uint32_t r, uint32_t g, uint32_t b, float intensity) { @@ -15,18 +13,4 @@ static inline uint32_t makergb(uint32_t r, uint32_t g, uint32_t b, float intensi return (((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff)); } -static inline int draw_pixel(fb_fragment_t *f, int x, int y, uint32_t pixel) -{ - uint32_t *pixels = f->buf; - - if (y < 0 || y >= f->height || x < 0 || x >= f->width) { - return 0; - } - - /* FIXME this assumes stride is aligned to 4 */ - pixels[(y * (f->width + (f->stride >> 2))) + x] = pixel; - - return 1; -} - #endif diff --git a/src/modules/stars/stars.c b/src/modules/stars/stars.c index e009714..0f6c2d5 100644 --- a/src/modules/stars/stars.c +++ b/src/modules/stars/stars.c @@ -43,7 +43,7 @@ static void stars(fb_fragment_t *fragment) for (;;) { int ret = process_point( u, &rp ); if (ret==0) break; - if (ret==1) draw_pixel(fragment, rp.x+(width/2), rp.y+(height/2), + if (ret==1) fb_fragment_put_pixel_unchecked(fragment, rp.x+(width/2), rp.y+(height/2), makergb(0xFF, 0xFF, 0xFF, (float)rp.opacity/OPACITY_MAX) ); } -- cgit v1.2.3 From e0619c5dfa3bc90c2c241f8e330bf4a76a5a003d Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Fri, 3 Feb 2017 16:12:23 -0800 Subject: fb: add fb_fragment_zero() helper Currently just a memset wrapper... but maybe could get noop'd if the flipper thread started supporting pre-zeroing pages in its thread, just need a zeroed state in the fb page. --- src/fb.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/fb.h b/src/fb.h index b990ae6..e8d818f 100644 --- a/src/fb.h +++ b/src/fb.h @@ -2,6 +2,7 @@ #define _FB_H #include +#include #include #include /* for drmModeModeInfoPtr */ @@ -67,4 +68,11 @@ static inline int fb_fragment_put_pixel_checked(fb_fragment_t *fragment, int x, return 1; } + +/* zero a fragment */ +static inline void fb_fragment_zero(fb_fragment_t *fragment) +{ + memset(fragment->buf, 0, ((fragment->width << 2) + fragment->stride) * fragment->height); +} + #endif -- cgit v1.2.3 From b35ba71b6de341f7b87beb3bc078600f3d191612 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Fri, 3 Feb 2017 16:14:04 -0800 Subject: *: use fb_fragment_zero() instead of memset() --- src/modules/sparkler/sparkler.c | 2 +- src/modules/stars/stars.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/sparkler/sparkler.c b/src/modules/sparkler/sparkler.c index 0bb0fcf..e2ff76e 100644 --- a/src/modules/sparkler/sparkler.c +++ b/src/modules/sparkler/sparkler.c @@ -35,7 +35,7 @@ static void sparkler(fb_fragment_t *fragment) initialized = 1; } - memset(buf, 0, ((fragment->width << 2) + fragment->stride) * fragment->height); + fb_fragment_zero(fragment); particles_age(particles); particles_draw(particles, fragment); diff --git a/src/modules/stars/stars.c b/src/modules/stars/stars.c index 0f6c2d5..2ff9b99 100644 --- a/src/modules/stars/stars.c +++ b/src/modules/stars/stars.c @@ -37,7 +37,7 @@ static void stars(fb_fragment_t *fragment) } // draw space (or blank the frame, if you prefer) - memset(fragment->buf, 0, ((fragment->width << 2) + fragment->stride) * fragment->height); + fb_fragment_zero(fragment); // draw stars for (;;) { -- cgit v1.2.3