From a7d170adda90f0ead69330ff49fa1629b868a210 Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Thu, 2 Jul 2020 12:09:49 +0200 Subject: [PATCH] os: add blit/painter.h This utility eases the application of the 'blit' function when using the 'Texture' and 'Surface' types. --- repos/os/include/blit/painter.h | 59 +++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 repos/os/include/blit/painter.h diff --git a/repos/os/include/blit/painter.h b/repos/os/include/blit/painter.h new file mode 100644 index 0000000000..cfbdd3868d --- /dev/null +++ b/repos/os/include/blit/painter.h @@ -0,0 +1,59 @@ +/* + * \brief Functor for blitting textures on a surface + * \author Norman Feske + * \date 2020-07-02 + */ + +/* + * Copyright (C) 2020 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU Affero General Public License version 3. + */ + +#ifndef _INCLUDE__BLIT__PAINTER_H_ +#define _INCLUDE__BLIT__PAINTER_H_ + +#include +#include + + +struct Blit_painter +{ + typedef Genode::Surface_base::Point Point; + typedef Genode::Surface_base::Rect Rect; + + + template + static inline void paint(Genode::Surface &surface, + Genode::Texture const &texture, + Point position) + { + Rect const clipped = Rect::intersect(Rect(position, texture.size()), + surface.clip()); + + if (!clipped.valid()) + return; + + int const src_w = texture.size().w(); + int const dst_w = surface.size().w(); + + /* calculate offset of first texture pixel to copy */ + unsigned long const tex_start_offset = (clipped.y1() - position.y())*src_w + + clipped.x1() - position.x(); + + /* start address of source pixels */ + PT const * const src = texture.pixel() + tex_start_offset; + + /* start address of destination pixels */ + PT * const dst = surface.addr() + clipped.y1()*dst_w + clipped.x1(); + + blit(src, src_w*sizeof(PT), + dst, dst_w*sizeof(PT), + clipped.w()*sizeof(PT), clipped.h()); + + surface.flush_pixels(clipped); + } +}; + +#endif /* _INCLUDE__BLIT__PAINTER_H_ */