2013-04-15 22:10:18 -04:00
|
|
|
/*
|
2019-08-02 22:40:09 -04:00
|
|
|
* Copyright (C) 2013-2015 Robin Gareus <robin@gareus.org>
|
|
|
|
* Copyright (C) 2013-2017 Paul Davis <paul@linuxaudiosystems.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2013-04-15 22:10:18 -04:00
|
|
|
|
2013-04-15 21:40:15 -04:00
|
|
|
#include "canvas/image.h"
|
|
|
|
|
|
|
|
#include "gtkmm2ext/gui_thread.h"
|
|
|
|
|
|
|
|
using namespace ArdourCanvas;
|
|
|
|
|
2014-06-12 14:53:44 -04:00
|
|
|
Image::Image (Canvas* canvas, Cairo::Format fmt, int width, int height)
|
|
|
|
: Item (canvas)
|
|
|
|
, _format (fmt)
|
|
|
|
, _width (width)
|
|
|
|
, _height (height)
|
|
|
|
, _need_render (false)
|
|
|
|
{
|
|
|
|
DataReady.connect (data_connections, MISSING_INVALIDATOR, boost::bind (&Image::accept_data, this), gui_context());
|
|
|
|
}
|
|
|
|
|
2014-06-21 11:43:42 -04:00
|
|
|
Image::Image (Item* parent, Cairo::Format fmt, int width, int height)
|
|
|
|
: Item (parent)
|
2013-04-15 21:40:15 -04:00
|
|
|
, _format (fmt)
|
|
|
|
, _width (width)
|
|
|
|
, _height (height)
|
|
|
|
, _need_render (false)
|
|
|
|
{
|
|
|
|
DataReady.connect (data_connections, MISSING_INVALIDATOR, boost::bind (&Image::accept_data, this), gui_context());
|
|
|
|
}
|
|
|
|
|
2015-10-04 14:51:05 -04:00
|
|
|
void
|
2013-04-15 21:40:15 -04:00
|
|
|
Image::render (Rect const& area, Cairo::RefPtr<Cairo::Context> context) const
|
|
|
|
{
|
2013-04-16 14:20:04 -04:00
|
|
|
if (_need_render && _pending) {
|
2013-04-17 14:26:09 -04:00
|
|
|
_surface = Cairo::ImageSurface::create (_pending->data,
|
2013-04-16 14:20:04 -04:00
|
|
|
_pending->format,
|
|
|
|
_pending->width,
|
|
|
|
_pending->height,
|
|
|
|
_pending->stride);
|
|
|
|
_current = _pending;
|
2013-04-15 21:40:15 -04:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2013-06-18 08:23:06 -04:00
|
|
|
Rect self = item_to_window (Rect (0, 0, _width, _height));
|
2017-01-19 14:54:24 -05:00
|
|
|
Rect draw = self.intersection (area);
|
2013-04-15 21:40:15 -04:00
|
|
|
|
2013-06-18 08:23:06 -04:00
|
|
|
if (_surface && draw) {
|
2013-06-22 14:59:43 -04:00
|
|
|
context->set_source (_surface, self.x0, self.y0);
|
2017-01-19 14:54:24 -05:00
|
|
|
context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
|
2013-04-16 14:20:04 -04:00
|
|
|
context->fill ();
|
|
|
|
}
|
2013-04-15 21:40:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Image::compute_bounding_box () const
|
|
|
|
{
|
2017-01-19 14:54:24 -05:00
|
|
|
_bounding_box = Rect (0, 0, _width, _height);
|
2022-04-27 00:02:22 -04:00
|
|
|
set_bbox_clean ();
|
2013-04-15 21:40:15 -04:00
|
|
|
}
|
|
|
|
|
2023-02-16 18:33:28 -05:00
|
|
|
std::shared_ptr<Image::Data>
|
2013-04-17 14:26:09 -04:00
|
|
|
Image::get_image (bool allocate_data)
|
2013-04-15 21:40:15 -04:00
|
|
|
{
|
2013-04-15 22:34:36 -04:00
|
|
|
/* can be called by any thread */
|
|
|
|
|
2013-04-15 21:40:15 -04:00
|
|
|
int stride = Cairo::ImageSurface::format_stride_for_width (_format, _width);
|
2013-04-17 14:26:09 -04:00
|
|
|
if (allocate_data) {
|
2023-02-16 18:33:28 -05:00
|
|
|
std::shared_ptr<Data> d (new Data (new uint8_t[stride*_height], _width, _height, stride, _format));
|
2013-04-17 14:26:09 -04:00
|
|
|
return d;
|
|
|
|
} else {
|
2023-02-16 18:33:28 -05:00
|
|
|
std::shared_ptr<Data> d (new Data (NULL, _width, _height, stride, _format));
|
2013-04-17 14:26:09 -04:00
|
|
|
return d;
|
|
|
|
}
|
2013-04-15 21:40:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-02-16 18:33:28 -05:00
|
|
|
Image::put_image (std::shared_ptr<Data> d)
|
2013-04-15 21:40:15 -04:00
|
|
|
{
|
2013-04-15 22:34:36 -04:00
|
|
|
/* can be called by any thread */
|
|
|
|
|
2013-04-15 21:40:15 -04:00
|
|
|
_pending = d;
|
|
|
|
DataReady (); /* EMIT SIGNAL */
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-10-04 14:51:05 -04:00
|
|
|
Image::accept_data ()
|
2013-04-15 21:40:15 -04:00
|
|
|
{
|
|
|
|
/* must be executed in gui thread */
|
2013-04-15 22:34:36 -04:00
|
|
|
|
|
|
|
begin_change ();
|
2013-04-15 21:40:15 -04:00
|
|
|
_need_render = true;
|
2013-04-15 22:34:36 -04:00
|
|
|
end_change (); // notify canvas that we need redrawing
|
2015-10-05 10:17:49 -04:00
|
|
|
}
|
2013-04-15 21:40:15 -04:00
|
|
|
|