Replace explicit image-surface with cairo pattern/group

For MacOS/X this is equivalent, rendering happens using a
CGBitmapContext + image-surface. Windows and Linux needs profiling
for respective equivalent surfaces.
This commit is contained in:
Robin Gareus 2019-12-26 23:55:44 +01:00
parent c3ab63a2ea
commit 2edbda2526
No known key found for this signature in database
GPG key ID: A090BCE02CF57F04
4 changed files with 40 additions and 73 deletions

View file

@ -59,14 +59,20 @@ 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 #if (defined USE_CAIRO_IMAGE_SURFACE || defined __APPLE__)
_use_image_surface = true; _use_intermediate_surface = true;
#else #else
_use_image_surface = NULL != getenv("ARDOUR_IMAGE_SURFACE"); _use_intermediate_surface = NULL != getenv("ARDOUR_IMAGE_SURFACE");
#endif #endif
set_epoch (); set_epoch ();
} }
void
Canvas::use_intermediate_surface (bool yn)
{
_use_intermediate_surface = yn;
}
void void
Canvas::scroll_to (Coord x, Coord y) Canvas::scroll_to (Coord x, Coord y)
{ {
@ -840,11 +846,6 @@ 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);
if (_use_image_surface) {
/* 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());
}
#ifdef __APPLE__ #ifdef __APPLE__
if (_nsglview) { if (_nsglview) {
@ -879,21 +880,16 @@ GtkCanvas::on_expose_event (GdkEventExpose* ev)
const int64_t start = g_get_monotonic_time (); const int64_t start = g_get_monotonic_time ();
#endif #endif
Cairo::RefPtr<Cairo::Context> draw_context; Cairo::RefPtr<Cairo::Context> draw_context = get_window()->create_cairo_context ();
if (_use_image_surface) {
if (!canvas_image) {
canvas_image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, get_width(), get_height());
}
draw_context = Cairo::Context::create (canvas_image);
} else {
draw_context = get_window()->create_cairo_context ();
}
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();
#ifdef __APPLE__ /* (this comment applies to macOS, but is other platforms
/* group calls cairo_quartz_surface_create() which * also benefit from using CPU-rendering on a image-surface
* with a final bitblt).
*
* group calls cairo_quartz_surface_create() which
* effectively uses a CGBitmapContext + image-surface * effectively uses a CGBitmapContext + image-surface
* *
* This avoids expensive argb32_image_mark_image() during drawing. * This avoids expensive argb32_image_mark_image() during drawing.
@ -905,8 +901,9 @@ GtkCanvas::on_expose_event (GdkEventExpose* ev)
* *
* Fixing this for good likely involves changes to GdkQuartzWindow, GdkQuartzView * Fixing this for good likely involves changes to GdkQuartzWindow, GdkQuartzView
*/ */
draw_context->push_group (); if (_use_intermediate_surface) {
#endif draw_context->push_group ();
}
/* draw background color */ /* draw background color */
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);
@ -930,20 +927,9 @@ GtkCanvas::on_expose_event (GdkEventExpose* ev)
g_free (rects); g_free (rects);
} }
#ifdef __APPLE__ if (_use_intermediate_surface) {
draw_context->pop_group_to_source (); draw_context->pop_group_to_source ();
draw_context->paint (); draw_context->paint ();
#endif
if (_use_image_surface) {
canvas_image->flush ();
/* 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->clip ();
window_context->set_source (canvas_image, 0, 0);
window_context->set_operator (Cairo::OPERATOR_SOURCE);
window_context->paint ();
} }
#ifdef CANVAS_PROFILE #ifdef CANVAS_PROFILE

View file

@ -173,6 +173,11 @@ public:
virtual Glib::RefPtr<Pango::Context> get_pango_context() = 0; virtual Glib::RefPtr<Pango::Context> get_pango_context() = 0;
/** Redirect drawing to an intermediate (image) surface.
* see also https://www.cairographics.org/manual/cairo-cairo-t.html#cairo-push-group
*/
void use_intermediate_surface (bool yn = true);
protected: protected:
Root _root; Root _root;
Gtkmm2ext::Color _bg_color; Gtkmm2ext::Color _bg_color;
@ -187,7 +192,7 @@ protected:
std::list<ScrollGroup*> scrollers; std::list<ScrollGroup*> scrollers;
bool _use_image_surface; bool _use_intermediate_surface;
}; };
/** A canvas which renders onto a GTK EventBox */ /** A canvas which renders onto a GTK EventBox */
@ -264,8 +269,6 @@ private:
void item_shown_or_hidden (Item *); void item_shown_or_hidden (Item *);
bool send_leave_event (Item const *, double, double) const; bool send_leave_event (Item const *, double, double) const;
Cairo::RefPtr<Cairo::Surface> canvas_image;
/** Item currently chosen for event delivery based on pointer position */ /** Item currently chosen for event delivery based on pointer position */
Item * _current_item; Item * _current_item;
/** Item pending as _current_item */ /** Item pending as _current_item */

View file

@ -58,9 +58,9 @@ CairoWidget::CairoWidget ()
{ {
_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 #ifdef USE_CAIRO_IMAGE_SURFACE
_use_image_surface = true; _use_intermediate_surface = true;
#else #else
_use_image_surface = NULL != getenv("ARDOUR_IMAGE_SURFACE"); _use_intermediate_surface = NULL != getenv("ARDOUR_IMAGE_SURFACE");
#endif #endif
} }
@ -82,8 +82,7 @@ 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; _use_intermediate_surface = false;
image_surface.clear ();
} }
void void
@ -98,13 +97,9 @@ CairoWidget::use_nsglview ()
} }
void void
CairoWidget::use_image_surface (bool yn) CairoWidget::use_intermediate_surface (bool yn)
{ {
if (_use_image_surface == yn) { _use_intermediate_surface = yn;
return;
}
image_surface.clear ();
_use_image_surface = yn;
} }
int int
@ -163,14 +158,9 @@ CairoWidget::on_expose_event (GdkEventExpose *ev)
return true; return true;
} }
#endif #endif
Cairo::RefPtr<Cairo::Context> cr; Cairo::RefPtr<Cairo::Context> cr = get_window()->create_cairo_context ();
if (_use_image_surface) { if (_use_intermediate_surface) {
if (!image_surface) { cr->push_group ();
image_surface = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, get_width(), get_height());
}
cr = Cairo::Context::create (image_surface);
} else {
cr = get_window()->create_cairo_context ();
} }
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);
@ -198,15 +188,9 @@ CairoWidget::on_expose_event (GdkEventExpose *ev)
render (cr, &expose_area); render (cr, &expose_area);
if (_use_image_surface) { if (_use_intermediate_surface) {
image_surface->flush(); cr->pop_group_to_source ();
/* now blit our private surface back to the GDK one */ cr->paint ();
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->clip ();
window_context->set_source (image_surface, 0, 0);
window_context->set_operator (Cairo::OPERATOR_SOURCE);
window_context->paint ();
} }
return true; return true;
@ -261,11 +245,6 @@ CairoWidget::on_size_allocate (Gtk::Allocation& alloc)
memcpy (&_allocation, &alloc, sizeof(Gtk::Allocation)); memcpy (&_allocation, &alloc, sizeof(Gtk::Allocation));
} }
if (_use_image_surface) {
image_surface.clear ();
image_surface = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, alloc.get_width(), alloc.get_height());
}
if (_canvas_widget) { if (_canvas_widget) {
return; return;
} }

View file

@ -40,7 +40,7 @@ public:
void set_canvas_widget (); void set_canvas_widget ();
void use_nsglview (); void use_nsglview ();
void use_image_surface (bool yn = true); void use_intermediate_surface (bool yn = true);
/* swizzle Gtk::Widget methods for Canvas::Widget */ /* swizzle Gtk::Widget methods for Canvas::Widget */
void queue_draw (); void queue_draw ();
@ -143,13 +143,12 @@ 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;
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; bool _use_intermediate_surface;
Gdk::Rectangle _allocation; Gdk::Rectangle _allocation;
}; };