Allow for per-widget image-surface backing

This is an intermediate commit, before replacing image surfaces with
cairo pattern groups.

The eventual goal is to reduce flickering and/or use
CPU + bitblt for specific widgets instead of cairo
graphics-cards accel.

This also removes excessive calls to getenv() for every rendering
operation.
This commit is contained in:
Robin Gareus 2019-12-26 18:19:21 +01:00
parent 7b1a875f9c
commit c3ab63a2ea
No known key found for this signature in database
GPG key ID: A090BCE02CF57F04
4 changed files with 52 additions and 78 deletions

View file

@ -22,10 +22,6 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/ */
#if !defined USE_CAIRO_IMAGE_SURFACE && !defined NDEBUG
#define OPTIONAL_CAIRO_IMAGE_SURFACE
#endif
/** @file canvas/canvas.cc /** @file canvas/canvas.cc
* @brief Implementation of the main canvas classes. * @brief Implementation of the main canvas classes.
*/ */
@ -63,6 +59,11 @@ Canvas::Canvas ()
, _bg_color (Gtkmm2ext::rgba_to_color (0, 1.0, 0.0, 1.0)) , _bg_color (Gtkmm2ext::rgba_to_color (0, 1.0, 0.0, 1.0))
, _last_render_start_timestamp(0) , _last_render_start_timestamp(0)
{ {
#ifdef USE_CAIRO_IMAGE_SURFACE
_use_image_surface = true;
#else
_use_image_surface = NULL != getenv("ARDOUR_IMAGE_SURFACE");
#endif
set_epoch (); set_epoch ();
} }
@ -839,18 +840,11 @@ void
GtkCanvas::on_size_allocate (Gtk::Allocation& a) GtkCanvas::on_size_allocate (Gtk::Allocation& a)
{ {
EventBox::on_size_allocate (a); EventBox::on_size_allocate (a);
#ifdef OPTIONAL_CAIRO_IMAGE_SURFACE if (_use_image_surface) {
if (getenv("ARDOUR_IMAGE_SURFACE")) { /* allocate an image surface as large as the canvas itself */
#endif canvas_image.clear ();
#if defined USE_CAIRO_IMAGE_SURFACE || defined OPTIONAL_CAIRO_IMAGE_SURFACE canvas_image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, a.get_width(), a.get_height());
/* allocate an image surface as large as the canvas itself */
canvas_image.clear ();
canvas_image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, a.get_width(), a.get_height());
#endif
#ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
} }
#endif
#ifdef __APPLE__ #ifdef __APPLE__
if (_nsglview) { if (_nsglview) {
@ -885,27 +879,15 @@ GtkCanvas::on_expose_event (GdkEventExpose* ev)
const int64_t start = g_get_monotonic_time (); const int64_t start = g_get_monotonic_time ();
#endif #endif
#ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
Cairo::RefPtr<Cairo::Context> draw_context; Cairo::RefPtr<Cairo::Context> draw_context;
Cairo::RefPtr<Cairo::Context> window_context; if (_use_image_surface) {
if (getenv("ARDOUR_IMAGE_SURFACE")) {
if (!canvas_image) { if (!canvas_image) {
canvas_image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, get_width(), get_height()); canvas_image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, get_width(), get_height());
} }
draw_context = Cairo::Context::create (canvas_image); draw_context = Cairo::Context::create (canvas_image);
window_context = get_window()->create_cairo_context ();
} else { } else {
draw_context = get_window()->create_cairo_context (); draw_context = get_window()->create_cairo_context ();
} }
#elif defined USE_CAIRO_IMAGE_SURFACE
if (!canvas_image) {
canvas_image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, get_width(), get_height());
}
Cairo::RefPtr<Cairo::Context> draw_context = Cairo::Context::create (canvas_image);
Cairo::RefPtr<Cairo::Context> window_context = get_window()->create_cairo_context ();
#else
Cairo::RefPtr<Cairo::Context> draw_context = get_window()->create_cairo_context ();
#endif
draw_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height); draw_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
draw_context->clip(); draw_context->clip();
@ -953,21 +935,16 @@ GtkCanvas::on_expose_event (GdkEventExpose* ev)
draw_context->paint (); draw_context->paint ();
#endif #endif
#ifdef OPTIONAL_CAIRO_IMAGE_SURFACE if (_use_image_surface) {
if (getenv("ARDOUR_IMAGE_SURFACE")) { canvas_image->flush ();
#endif
#if defined USE_CAIRO_IMAGE_SURFACE || defined OPTIONAL_CAIRO_IMAGE_SURFACE
/* now blit our private surface back to the GDK one */ /* now blit our private surface back to the GDK one */
Cairo::RefPtr<Cairo::Context> window_context = get_window()->create_cairo_context ();
window_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height); window_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
window_context->clip (); window_context->clip ();
window_context->set_source (canvas_image, 0, 0); window_context->set_source (canvas_image, 0, 0);
window_context->set_operator (Cairo::OPERATOR_SOURCE); window_context->set_operator (Cairo::OPERATOR_SOURCE);
window_context->paint (); window_context->paint ();
#endif
#ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
} }
#endif
#ifdef CANVAS_PROFILE #ifdef CANVAS_PROFILE
const int64_t end = g_get_monotonic_time (); const int64_t end = g_get_monotonic_time ();

View file

@ -186,6 +186,8 @@ protected:
virtual void pick_current_item (Duple const &, int state) = 0; virtual void pick_current_item (Duple const &, int state) = 0;
std::list<ScrollGroup*> scrollers; std::list<ScrollGroup*> scrollers;
bool _use_image_surface;
}; };
/** A canvas which renders onto a GTK EventBox */ /** A canvas which renders onto a GTK EventBox */

View file

@ -17,9 +17,6 @@
* with this program; if not, write to the Free Software Foundation, Inc., * with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/ */
#if !defined USE_CAIRO_IMAGE_SURFACE && !defined NDEBUG
#define OPTIONAL_CAIRO_IMAGE_SURFACE
#endif
#include "gtkmm2ext/cairo_widget.h" #include "gtkmm2ext/cairo_widget.h"
#include "gtkmm2ext/gui_thread.h" #include "gtkmm2ext/gui_thread.h"
@ -60,6 +57,11 @@ CairoWidget::CairoWidget ()
, _nsglview (0) , _nsglview (0)
{ {
_name_proxy.connect (sigc::mem_fun (*this, &CairoWidget::on_name_changed)); _name_proxy.connect (sigc::mem_fun (*this, &CairoWidget::on_name_changed));
#ifdef USE_CAIRO_IMAGE_SURFACE
_use_image_surface = true;
#else
_use_image_surface = NULL != getenv("ARDOUR_IMAGE_SURFACE");
#endif
} }
CairoWidget::~CairoWidget () CairoWidget::~CairoWidget ()
@ -67,7 +69,9 @@ CairoWidget::~CairoWidget ()
if (_canvas_widget) { if (_canvas_widget) {
gtk_widget_set_realized (GTK_WIDGET(gobj()), false); gtk_widget_set_realized (GTK_WIDGET(gobj()), false);
} }
if (_parent_style_change) _parent_style_change.disconnect(); if (_parent_style_change) {
_parent_style_change.disconnect();
}
} }
void void
@ -78,6 +82,8 @@ CairoWidget::set_canvas_widget ()
ensure_style (); ensure_style ();
gtk_widget_set_realized (GTK_WIDGET(gobj()), true); gtk_widget_set_realized (GTK_WIDGET(gobj()), true);
_canvas_widget = true; _canvas_widget = true;
_use_image_surface = false;
image_surface.clear ();
} }
void void
@ -91,6 +97,16 @@ CairoWidget::use_nsglview ()
#endif #endif
} }
void
CairoWidget::use_image_surface (bool yn)
{
if (_use_image_surface == yn) {
return;
}
image_surface.clear ();
_use_image_surface = yn;
}
int int
CairoWidget::get_width () const CairoWidget::get_width () const
{ {
@ -147,9 +163,8 @@ CairoWidget::on_expose_event (GdkEventExpose *ev)
return true; return true;
} }
#endif #endif
#ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
Cairo::RefPtr<Cairo::Context> cr; Cairo::RefPtr<Cairo::Context> cr;
if (getenv("ARDOUR_IMAGE_SURFACE")) { if (_use_image_surface) {
if (!image_surface) { if (!image_surface) {
image_surface = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, get_width(), get_height()); image_surface = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, get_width(), get_height());
} }
@ -157,16 +172,6 @@ CairoWidget::on_expose_event (GdkEventExpose *ev)
} else { } else {
cr = get_window()->create_cairo_context (); cr = get_window()->create_cairo_context ();
} }
#elif defined USE_CAIRO_IMAGE_SURFACE
if (!image_surface) {
image_surface = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, get_width(), get_height());
}
Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create (image_surface);
#else
Cairo::RefPtr<Cairo::Context> cr = get_window()->create_cairo_context ();
#endif
cr->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height); cr->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
@ -193,24 +198,16 @@ CairoWidget::on_expose_event (GdkEventExpose *ev)
render (cr, &expose_area); render (cr, &expose_area);
#ifdef OPTIONAL_CAIRO_IMAGE_SURFACE if (_use_image_surface) {
if (getenv("ARDOUR_IMAGE_SURFACE")) { image_surface->flush();
#endif /* now blit our private surface back to the GDK one */
#if defined USE_CAIRO_IMAGE_SURFACE || defined OPTIONAL_CAIRO_IMAGE_SURFACE Cairo::RefPtr<Cairo::Context> window_context = get_window()->create_cairo_context ();
image_surface->flush(); window_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
/* now blit our private surface back to the GDK one */ window_context->clip ();
window_context->set_source (image_surface, 0, 0);
Cairo::RefPtr<Cairo::Context> cairo_context = get_window()->create_cairo_context (); window_context->set_operator (Cairo::OPERATOR_SOURCE);
window_context->paint ();
cairo_context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
cairo_context->clip ();
cairo_context->set_source (image_surface, 0, 0);
cairo_context->set_operator (Cairo::OPERATOR_SOURCE);
cairo_context->paint ();
#endif
#ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
} }
#endif
return true; return true;
} }
@ -264,19 +261,15 @@ CairoWidget::on_size_allocate (Gtk::Allocation& alloc)
memcpy (&_allocation, &alloc, sizeof(Gtk::Allocation)); memcpy (&_allocation, &alloc, sizeof(Gtk::Allocation));
} }
#ifdef OPTIONAL_CAIRO_IMAGE_SURFACE if (_use_image_surface) {
if (getenv("ARDOUR_IMAGE_SURFACE")) { image_surface.clear ();
#endif image_surface = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, alloc.get_width(), alloc.get_height());
#if defined USE_CAIRO_IMAGE_SURFACE || defined OPTIONAL_CAIRO_IMAGE_SURFACE
image_surface = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, alloc.get_width(), alloc.get_height());
#endif
#ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
} }
#endif
if (_canvas_widget) { if (_canvas_widget) {
return; return;
} }
#ifdef __APPLE__ #ifdef __APPLE__
if (_nsglview) { if (_nsglview) {
gint xx, yy; gint xx, yy;

View file

@ -40,6 +40,7 @@ public:
void set_canvas_widget (); void set_canvas_widget ();
void use_nsglview (); void use_nsglview ();
void use_image_surface (bool yn = true);
/* swizzle Gtk::Widget methods for Canvas::Widget */ /* swizzle Gtk::Widget methods for Canvas::Widget */
void queue_draw (); void queue_draw ();
@ -141,13 +142,14 @@ protected:
static sigc::slot<void,Gtk::Widget*> focus_handler; static sigc::slot<void,Gtk::Widget*> focus_handler;
private: private:
Cairo::RefPtr<Cairo::Surface> image_surface; Cairo::RefPtr<Cairo::Surface> image_surface;
Glib::SignalProxyProperty _name_proxy; Glib::SignalProxyProperty _name_proxy;
sigc::connection _parent_style_change; sigc::connection _parent_style_change;
Widget * _current_parent; Widget * _current_parent;
bool _canvas_widget; bool _canvas_widget;
void* _nsglview; void* _nsglview;
bool _use_image_surface;
Gdk::Rectangle _allocation; Gdk::Rectangle _allocation;
}; };