genode/repos/demo/src/server/liquid_framebuffer/services.cc

300 lines
7.7 KiB
C++
Raw Normal View History

2011-12-22 16:19:25 +01:00
/*
* \brief Implementation of Framebuffer and Input services
* \author Norman Feske
* \date 2006-09-22
*/
/*
* Copyright (C) 2006-2017 Genode Labs GmbH
2011-12-22 16:19:25 +01:00
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
2011-12-22 16:19:25 +01:00
*/
/* Genode include */
2011-12-22 16:19:25 +01:00
#include <base/env.h>
#include <base/semaphore.h>
#include <base/rpc_server.h>
#include <framebuffer_session/framebuffer_session.h>
#include <input/root.h>
#include <nitpicker_gfx/texture_painter.h>
#include <os/pixel_rgb565.h>
#include <os/static_root.h>
#include <timer_session/connection.h>
2011-12-22 16:19:25 +01:00
/* local includes */
2011-12-22 16:19:25 +01:00
#include "services.h"
typedef Genode::Texture<Genode::Pixel_rgb565> Texture_rgb565;
class Window_content : public Scout::Element
2011-12-22 16:19:25 +01:00
{
private:
class Content_event_handler : public Scout::Event_handler
2011-12-22 16:19:25 +01:00
{
private:
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
/*
* Noncopyable
*/
Content_event_handler(Content_event_handler const &);
Content_event_handler &operator = (Content_event_handler const &);
Input::Session_component &_input_session;
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
Scout::Point _old_mouse_position { };
Element *_element;
2011-12-22 16:19:25 +01:00
public:
Content_event_handler(Input::Session_component &input_session,
Scout::Element *element)
:
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
_input_session(input_session), _element(element)
{ }
2011-12-22 16:19:25 +01:00
void handle_event(Scout::Event const &ev) override
2011-12-22 16:19:25 +01:00
{
using namespace Scout;
Point mouse_position = ev.mouse_position - _element->abs_position();
2011-12-22 16:19:25 +01:00
auto motion = [&] (Point p) { return Input::Absolute_motion{p.x(), p.y()}; };
2011-12-22 16:19:25 +01:00
if (ev.type == Event::MOTION)
_input_session.submit(motion(mouse_position));
2011-12-22 16:19:25 +01:00
if (ev.type == Event::PRESS)
_input_session.submit(Input::Press{Input::Keycode(ev.code)});
2011-12-22 16:19:25 +01:00
if (ev.type == Event::RELEASE)
_input_session.submit(Input::Release{Input::Keycode(ev.code)});
2011-12-22 16:19:25 +01:00
_old_mouse_position = mouse_position;
2011-12-22 16:19:25 +01:00
}
};
struct Fb_texture
{
Genode::Allocator &alloc;
unsigned w, h;
Genode::Attached_ram_dataspace ds;
Genode::Pixel_rgb565 *pixel;
unsigned char *alpha;
Genode::Texture<Genode::Pixel_rgb565> texture;
Fb_texture(Genode::Ram_allocator &ram, Genode::Region_map &local_rm,
Genode::Allocator &alloc,
unsigned w, unsigned h, bool config_alpha)
:
alloc(alloc), w(w), h(h),
ds(ram, local_rm, w*h*sizeof(Genode::Pixel_rgb565)),
pixel(ds.local_addr<Genode::Pixel_rgb565>()),
alpha((unsigned char *)alloc.alloc(w*h)),
texture(pixel, alpha, Scout::Area(w, h))
{
int alpha_min = config_alpha ? 0 : 255;
/* init alpha channel */
for (unsigned y = 0; y < h; y++)
for (unsigned x = 0; x < w; x++) {
int v = (x * y + (w*h)/4) / w;
v = v + (x + y)/2;
int a = v & 0xff;
if (v & 0x100)
a = 255 - a;
a += (Genode::Dither_matrix::value(x, y) - 127) >> 4;
alpha[y*w + x] = Genode::max(alpha_min, Genode::min(a, 255));
}
}
~Fb_texture()
{
alloc.free(alpha, w*h);
}
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
private:
/*
* Noncopyable
*/
Fb_texture(Fb_texture const &);
Fb_texture &operator = (Fb_texture const &);
};
Genode::Ram_allocator &_ram;
Genode::Region_map &_rm;
Genode::Allocator &_alloc;
bool _config_alpha;
Content_event_handler _ev_handler;
Genode::Reconstructible<Fb_texture> _fb;
/**
* Size of the framebuffer handed out by the next call of 'dataspace'
*/
Scout::Area _next_size;
/**
* Most current designated size of the framebuffer as defined by the
* user.
*
* The '_designated_size' may be updated any time when the user drags
* the resize handle of the window. It is propagated to '_next_size'
* not before the framebuffer client requests the current mode. Once
* the mode information is passed to the client, it is locked until
* the client requests the mode again.
*/
Scout::Area _designated_size;
Follow practices suggested by "Effective C++" The patch adjust the code of the base, base-<kernel>, and os repository. To adapt existing components to fix violations of the best practices suggested by "Effective C++" as reported by the -Weffc++ compiler argument. The changes follow the patterns outlined below: * A class with virtual functions can no longer publicly inherit base classed without a vtable. The inherited object may either be moved to a member variable, or inherited privately. The latter would be used for classes that inherit 'List::Element' or 'Avl_node'. In order to enable the 'List' and 'Avl_tree' to access the meta data, the 'List' must become a friend. * Instead of adding a virtual destructor to abstract base classes, we inherit the new 'Interface' class, which contains a virtual destructor. This way, single-line abstract base classes can stay as compact as they are now. The 'Interface' utility resides in base/include/util/interface.h. * With the new warnings enabled, all member variables must be explicitly initialized. Basic types may be initialized with '='. All other types are initialized with braces '{ ... }' or as class initializers. If basic types and non-basic types appear in a row, it is nice to only use the brace syntax (also for basic types) and align the braces. * If a class contains pointers as members, it must now also provide a copy constructor and assignment operator. In the most cases, one would make them private, effectively disallowing the objects to be copied. Unfortunately, this warning cannot be fixed be inheriting our existing 'Noncopyable' class (the compiler fails to detect that the inheriting class cannot be copied and still gives the error). For now, we have to manually add declarations for both the copy constructor and assignment operator as private class members. Those declarations should be prepended with a comment like this: /* * Noncopyable */ Thread(Thread const &); Thread &operator = (Thread const &); In the future, we should revisit these places and try to replace the pointers with references. In the presence of at least one reference member, the compiler would no longer implicitly generate a copy constructor. So we could remove the manual declaration. Issue #465
2017-12-21 15:42:15 +01:00
Genode::Signal_context_capability _mode_sigh { };
2011-12-22 16:19:25 +01:00
public:
Window_content(Genode::Ram_allocator &ram, Genode::Region_map &rm,
Genode::Allocator &alloc, unsigned fb_w, unsigned fb_h,
Input::Session_component &input_session,
2011-12-22 16:19:25 +01:00
bool config_alpha)
:
_ram(ram), _rm(rm), _alloc(alloc),
_config_alpha(config_alpha),
_ev_handler(input_session, this),
_fb(_ram, _rm, _alloc, fb_w, fb_h, _config_alpha),
_next_size(fb_w, fb_h),
_designated_size(_next_size)
2011-12-22 16:19:25 +01:00
{
_min_size = Scout::Area(_fb->w, _fb->h);
2011-12-22 16:19:25 +01:00
event_handler(&_ev_handler);
}
2011-12-22 16:19:25 +01:00
Genode::Dataspace_capability fb_ds_cap() { return _fb->ds.cap(); }
2011-12-22 16:19:25 +01:00
unsigned fb_w() { return _fb->w; }
unsigned fb_h() { return _fb->h; }
Scout::Area mode_size()
{
_next_size = _designated_size;
return _next_size;
}
2011-12-22 16:19:25 +01:00
void mode_sigh(Genode::Signal_context_capability sigh)
{
_mode_sigh = sigh;
}
2011-12-22 16:19:25 +01:00
void realloc_framebuffer()
{
/* skip reallocation if size has not changed */
if (_next_size.w() == _fb->w && _next_size.h() == _fb->h)
return;
_fb.construct(_ram, _rm, _alloc, _next_size.w(), _next_size.h(), _config_alpha);
2011-12-22 16:19:25 +01:00
}
/**
* Element interface
*/
void draw(Scout::Canvas_base &canvas, Scout::Point abs_position) override
{
canvas.draw_texture(abs_position + _position, _fb->texture);
}
void format_fixed_size(Scout::Area size) override
{
_designated_size = size;
/* notify framebuffer client about mode change */
if (_mode_sigh.valid())
Genode::Signal_transmitter(_mode_sigh).submit();
}
2011-12-22 16:19:25 +01:00
};
static Window_content *_window_content;
Scout::Element *window_content() { return _window_content; }
2011-12-22 16:19:25 +01:00
/***********************************************
** Implementation of the framebuffer service **
***********************************************/
namespace Framebuffer { class Session_component; }
2011-12-22 16:19:25 +01:00
class Framebuffer::Session_component : public Genode::Rpc_object<Session>
{
private:
Timer::Connection _timer;
Window_content &_window_content;
public:
2011-12-22 16:19:25 +01:00
Session_component(Genode::Env &env, Window_content &window_content)
: _timer(env), _window_content(window_content) { }
2011-12-22 16:19:25 +01:00
Genode::Dataspace_capability dataspace() override
{
_window_content.realloc_framebuffer();
return _window_content.fb_ds_cap();
}
Mode mode() const override
{
return Mode(_window_content.mode_size().w(),
_window_content.mode_size().h(), Mode::RGB565);
}
void mode_sigh(Genode::Signal_context_capability sigh) override {
_window_content.mode_sigh(sigh); }
2011-12-22 16:19:25 +01:00
void sync_sigh(Genode::Signal_context_capability sigh) override
{
_timer.sigh(sigh);
_timer.trigger_periodic(10*1000);
}
2011-12-22 16:19:25 +01:00
void refresh(int x, int y, int w, int h) override
{
_window_content.redraw_area(x, y, w, h);
}
};
2011-12-22 16:19:25 +01:00
void init_window_content(Genode::Ram_allocator &ram, Genode::Region_map &rm,
Genode::Allocator &alloc,
Input::Session_component &input_component,
unsigned fb_w, unsigned fb_h, bool config_alpha)
2011-12-22 16:19:25 +01:00
{
static Window_content content(ram, rm, alloc, fb_w, fb_h,
input_component, config_alpha);
2011-12-22 16:19:25 +01:00
_window_content = &content;
}
2011-12-22 16:19:25 +01:00
void init_services(Genode::Env &env, Input::Session_component &input_component)
{
using namespace Genode;
2011-12-22 16:19:25 +01:00
static Framebuffer::Session_component fb_session(env, *_window_content);
static Static_root<Framebuffer::Session> fb_root(env.ep().manage(fb_session));
static Input::Root_component input_root(env.ep().rpc_ep(), input_component);
2011-12-22 16:19:25 +01:00
/*
* Now, the root interfaces are ready to accept requests.
* This is the right time to tell mummy about our services.
*/
env.parent().announce(env.ep().manage(fb_root));
env.parent().announce(env.ep().manage(input_root));
2011-12-22 16:19:25 +01:00
}