Mir
render_surfaces.cpp: A simple program using the mir library.

Table of Contents

render_surfaces shows the use of mir to render some moving surfaces

main()

The main() function uses a RenderSurfacesServerConfiguration to initialize and run mir.

RenderSurfacesServer render_surfaces{argc, argv};
render_surfaces.run();
return render_surfaces.exited_normally() ? EXIT_SUCCESS : EXIT_FAILURE;

RenderSurfacesServerConfiguration

The configuration stubs out client connectivity and input.

class RenderSurfacesDisplayBufferCompositor : public mc::DisplayBufferCompositor
{
public:
RenderSurfacesDisplayBufferCompositor(
std::unique_ptr<DisplayBufferCompositor> db_compositor,
std::vector<Moveable>& moveables)
: db_compositor{std::move(db_compositor)},
moveables(moveables),
frames{0}
{
}
void composite(mc::SceneElementSequence&& scene_sequence) override
{
while (!created) std::this_thread::yield();
stop_watch.stop();
if (stop_watch.elapsed_seconds_since_last_restart() >= 1)
{
std::cout << "FPS: " << frames << " Frame Time: " << 1.0 / frames << std::endl;
frames = 0;
stop_watch.restart();
}
db_compositor->composite(std::move(scene_sequence));
for (auto& m : moveables)
m.step();
frames++;
}
private:
std::unique_ptr<DisplayBufferCompositor> const db_compositor;
StopWatch stop_watch;
std::vector<Moveable>& moveables;
uint32_t frames;
};

it also provides a bespoke display buffer compositor

// Decorate the DefaultDisplayBufferCompositor in order to move surfaces.
wrap_display_buffer_compositor_factory([this](std::shared_ptr<mc::DisplayBufferCompositorFactory> const& wrapped)
{
return std::make_shared<RenderSurfacesDisplayBufferCompositorFactory>(
wrapped,
moveables);
});

Utility classes

For smooth animation we need to track time and move surfaces accordingly

StopWatch

// tracks elapsed time - for animation.
class StopWatch
{
public:
StopWatch() : start(std::chrono::high_resolution_clock::now()),
last(start),
now(last)
{
}
void stop()
{
now = std::chrono::high_resolution_clock::now();
}
double elapsed_seconds_since_start()
{
auto elapsed = now - start;
float elapsed_sec = std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count() / 1000000.0f;
return elapsed_sec;
}
double elapsed_seconds_since_last_restart()
{
auto elapsed = now - last;
float elapsed_sec = std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count() / 1000000.0f;
return elapsed_sec;
}
void restart()
{
std::swap(last, now);
}
private:
std::chrono::high_resolution_clock::time_point start;
std::chrono::high_resolution_clock::time_point last;
std::chrono::high_resolution_clock::time_point now;
};

Moveable

// Adapter to support movement of surfaces.
class Moveable
{
public:
Moveable() {}
Moveable(std::shared_ptr<ms::Surface> const& s, const geom::Size& display_size,
float dx, float dy, const glm::vec3& rotation_axis, float alpha_offset)
: surface(s), display_size(display_size),
x(s->top_left().x.as_int()),
y(s->top_left().y.as_int()),
w(s->size().width.as_int()),
h(s->size().height.as_int()),
dx{dx},
dy{dy},
rotation_axis(rotation_axis),
alpha_offset{alpha_offset}
{
}
void step()
{
stop_watch.stop();
float elapsed_sec = stop_watch.elapsed_seconds_since_last_restart();
float total_elapsed_sec = stop_watch.elapsed_seconds_since_start();
stop_watch.restart();
bool should_update = true;
float new_x = x + elapsed_sec * dx;
float new_y = y + elapsed_sec * dy;
if (new_x < 0.0 || new_x + w > display_size.width.as_uint32_t())
{
dx = -dx;
should_update = false;
}
if (new_y < 0.0 || new_y + h > display_size.height.as_uint32_t())
{
dy = -dy;
should_update = false;
}
if (should_update)
{
surface->move_to({new_x, new_y});
x = new_x;
y = new_y;
}
glm::mat4 trans = glm::rotate(glm::mat4(1.0f),
glm::radians(total_elapsed_sec * 120.0f),
rotation_axis);
surface->set_transformation(trans);
float const alpha_amplitude = (1.0f - min_alpha) / 2.0f;
surface->set_alpha(min_alpha + alpha_amplitude +
alpha_amplitude *
sin(alpha_offset + 2 * M_PI * total_elapsed_sec /
3.0));
}
private:
std::shared_ptr<ms::Surface> surface;
geom::Size display_size;
float x;
float y;
float w;
float h;
float dx;
float dy;
StopWatch stop_watch;
glm::vec3 rotation_axis;
float alpha_offset;
};

Copyright © 2012-2016 Canonical Ltd.
Generated on Mon Jun 5 11:05:03 UTC 2017