Preferences/Config changes for image-surface settings
[ardour.git] / gtk2_ardour / plugin_display.cc
1 /*
2  * Copyright (C) 2017 Johannes Mueller <github@johannes-mueller.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <gtkmm/container.h>
20
21 #include "gtkmm2ext/colors.h"
22 #include "gtkmm2ext/gtk_ui.h"
23 #include "gtkmm2ext/gui_thread.h"
24 #include "gtkmm2ext/utils.h"
25
26 #include "ui_config.h"
27
28 #include "plugin_display.h"
29
30
31
32 PluginDisplay::PluginDisplay (boost::shared_ptr<ARDOUR::Plugin> p, uint32_t max_height)
33         : _plug (p)
34         , _surf (0)
35         , _max_height (max_height)
36         , _cur_height (1)
37         , _scroll (false)
38 {
39         add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
40         _plug->DropReferences.connect (_death_connection, invalidator (*this), boost::bind (&PluginDisplay::plugin_going_away, this), gui_context());
41         _plug->QueueDraw.connect (_qdraw_connection, invalidator (*this),
42                         boost::bind (&Gtk::Widget::queue_draw, this), gui_context ());
43 }
44
45 PluginDisplay::~PluginDisplay ()
46 {
47         if (_surf) {
48                 cairo_surface_destroy (_surf);
49         }
50 }
51
52 bool
53 PluginDisplay::on_button_press_event (GdkEventButton *ev)
54 {
55         return false;
56 }
57
58 bool
59 PluginDisplay::on_button_release_event (GdkEventButton *ev)
60 {
61         return false;
62 }
63
64 void
65 PluginDisplay::on_size_request (Gtk::Requisition* req)
66 {
67         req->width = 300;
68         req->height = _cur_height;
69 }
70
71
72 void
73 PluginDisplay::update_height_alloc (uint32_t height)
74 {
75         uint32_t shm = std::min (_max_height, height);
76
77         if (shm != _cur_height) {
78                 _cur_height = shm;
79                 queue_resize ();
80         }
81 }
82
83 uint32_t
84 PluginDisplay::render_inline (cairo_t* cr, uint32_t width)
85 {
86         ARDOUR::Plugin::Display_Image_Surface* dis = _plug->render_inline_display (width, _max_height);
87         if (!dis) {
88                 return 0;
89         }
90
91         /* allocate a local image-surface,
92          * We cannot re-use the data via cairo_image_surface_create_for_data(),
93          * since pixman keeps a reference to it.
94          * we'd need to hand over the data and ha cairo_surface_destroy to free it.
95          * it might be possible to work around via cairo_surface_set_user_data().
96          */
97         if (!_surf
98                         || dis->width !=  cairo_image_surface_get_width (_surf)
99                         || dis->height !=  cairo_image_surface_get_height (_surf)
100                  ) {
101                 if (_surf) {
102                         cairo_surface_destroy (_surf);
103                 }
104                 _surf = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, dis->width, dis->height);
105         }
106
107         if (cairo_image_surface_get_stride (_surf) == dis->stride) {
108                 memcpy (cairo_image_surface_get_data (_surf), dis->data, dis->stride * dis->height);
109         } else {
110                 unsigned char *src = dis->data;
111                 unsigned char *dst = cairo_image_surface_get_data (_surf);
112                 const int dst_stride =  cairo_image_surface_get_stride (_surf);
113                 for (int y = 0; y < dis->height; ++y) {
114                         memcpy (dst, src, dis->width * 4 /*ARGB32*/);
115                         src += dis->stride;
116                         dst += dst_stride;
117                 }
118         }
119
120         cairo_surface_flush(_surf);
121         cairo_surface_mark_dirty(_surf);
122         const double xc = floor ((width - dis->width) * .5);
123         cairo_set_source_surface(cr, _surf, xc, 0);
124         cairo_paint (cr);
125
126         return dis->height;
127 }
128
129 bool
130 PluginDisplay::on_expose_event (GdkEventExpose* ev)
131 {
132         Gtk::Allocation a = get_allocation();
133         double const width = a.get_width();
134         double const height = a.get_height();
135
136         cairo_t* cr = gdk_cairo_create (get_window()->gobj());
137         cairo_rectangle (cr, ev->area.x, ev->area.y, ev->area.width, ev->area.height);
138         cairo_clip (cr);
139
140         Gdk::Color const bg = get_style()->get_bg (Gtk::STATE_NORMAL);
141         cairo_set_source_rgb (cr, bg.get_red_p (), bg.get_green_p (), bg.get_blue_p ());
142         cairo_rectangle (cr, 0, 0, width, height);
143         cairo_fill (cr);
144
145         cairo_save (cr);
146         cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
147         display_frame(cr, width, height);
148         cairo_clip (cr);
149         cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
150
151         uint32_t ht = render_inline (cr, width);
152         cairo_restore (cr);
153
154         if (ht == 0) {
155                 hide ();
156                 if (_cur_height != 1) {
157                         _cur_height = 1;
158                         queue_resize ();
159                 }
160                 cairo_destroy (cr);
161                 return true;
162         } else {
163                 update_height_alloc (ht);
164         }
165
166         bool failed = false;
167         std::string name = get_name();
168         Gtkmm2ext::Color fill_color = UIConfiguration::instance().color (string_compose ("%1: fill active", name), &failed);
169
170         display_frame(cr, width, height);
171         cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
172         cairo_set_line_width(cr, 1.0);
173         if (failed) {
174                 cairo_set_source_rgba (cr, .75, .75, .75, 1.0);
175         } else {
176                 Gtkmm2ext::set_source_rgb_a (cr, fill_color, 1.0);
177         }
178         cairo_stroke (cr);
179
180         cairo_destroy(cr);
181         return true;
182 }
183
184 void
185 PluginDisplay::display_frame (cairo_t* cr, double w, double h)
186 {
187         cairo_rectangle (cr, 0.0, 0.0, w, h);
188 }