2016-09-16 09:39:28 -04:00
|
|
|
/*
|
2019-08-03 08:34:29 -04:00
|
|
|
* Copyright (C) 2016-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.
|
|
|
|
*/
|
2016-09-16 09:39:28 -04:00
|
|
|
|
2016-10-13 17:11:38 -04:00
|
|
|
#include <vector>
|
|
|
|
|
2016-09-16 09:39:28 -04:00
|
|
|
#include <cairomm/region.h>
|
|
|
|
#include <cairomm/surface.h>
|
|
|
|
#include <cairomm/context.h>
|
|
|
|
|
2016-09-16 14:17:24 -04:00
|
|
|
#include "pbd/compose.h"
|
2016-10-13 17:11:38 -04:00
|
|
|
#include "pbd/error.h"
|
2016-09-16 14:17:24 -04:00
|
|
|
|
|
|
|
#include "ardour/debug.h"
|
|
|
|
|
2016-09-16 09:39:28 -04:00
|
|
|
#include "canvas.h"
|
|
|
|
#include "layout.h"
|
|
|
|
#include "push2.h"
|
|
|
|
|
2018-09-21 13:05:39 -04:00
|
|
|
#include "pbd/i18n.h"
|
|
|
|
|
2016-09-28 17:24:14 -04:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#define Rect ArdourCanvas::Rect
|
|
|
|
#endif
|
|
|
|
|
2016-09-16 09:39:28 -04:00
|
|
|
using namespace ArdourCanvas;
|
|
|
|
using namespace ArdourSurface;
|
2016-09-16 14:17:24 -04:00
|
|
|
using namespace PBD;
|
2016-09-16 09:39:28 -04:00
|
|
|
|
|
|
|
const int Push2Canvas::pixels_per_row = 1024;
|
|
|
|
|
|
|
|
Push2Canvas::Push2Canvas (Push2& pr, int c, int r)
|
|
|
|
: p2 (pr)
|
|
|
|
, _cols (c)
|
|
|
|
, _rows (r)
|
2017-09-18 12:39:17 -04:00
|
|
|
, sample_buffer (Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _cols, _rows))
|
2016-09-16 09:39:28 -04:00
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
context = Cairo::Context::create (sample_buffer);
|
2016-09-16 09:39:28 -04:00
|
|
|
expose_region = Cairo::Region::create ();
|
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
device_sample_buffer = new uint16_t[pixel_area()];
|
|
|
|
memset (device_sample_buffer, 0, sizeof (uint16_t) * pixel_area());
|
2016-09-16 09:39:28 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
sample_header[0] = 0xef;
|
|
|
|
sample_header[1] = 0xcd;
|
|
|
|
sample_header[2] = 0xab;
|
|
|
|
sample_header[3] = 0x89;
|
2016-09-16 09:39:28 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
memset (&sample_header[4], 0, 12);
|
2016-09-16 09:39:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Push2Canvas::~Push2Canvas ()
|
|
|
|
{
|
2017-09-18 12:39:17 -04:00
|
|
|
delete [] device_sample_buffer;
|
|
|
|
device_sample_buffer = 0;
|
2016-09-16 09:39:28 -04:00
|
|
|
}
|
|
|
|
|
2018-11-19 11:32:11 -05:00
|
|
|
void
|
|
|
|
Push2Canvas::queue_resize ()
|
|
|
|
{
|
|
|
|
/* nothing to do here, for now */
|
|
|
|
}
|
|
|
|
|
2016-09-16 09:39:28 -04:00
|
|
|
bool
|
|
|
|
Push2Canvas::vblank ()
|
|
|
|
{
|
2018-11-19 11:32:11 -05:00
|
|
|
if (_root.resize_queued()) {
|
|
|
|
_root.layout ();
|
|
|
|
}
|
|
|
|
|
2016-09-16 09:39:28 -04:00
|
|
|
/* re-render dirty areas, if any */
|
|
|
|
|
|
|
|
if (expose ()) {
|
2017-09-18 12:39:17 -04:00
|
|
|
/* something rendered, update device_sample_buffer */
|
|
|
|
blit_to_device_sample_buffer ();
|
2016-09-27 15:58:40 -04:00
|
|
|
|
2016-09-27 16:42:36 -04:00
|
|
|
#undef RENDER_LAYOUTS
|
|
|
|
#ifdef RENDER_LAYOUTS
|
2016-09-27 15:58:40 -04:00
|
|
|
if (p2.current_layout()) {
|
|
|
|
std::string s = p2.current_layout()->name();
|
|
|
|
s += ".png";
|
2017-09-18 12:39:17 -04:00
|
|
|
sample_buffer->write_to_png (s);
|
2016-09-27 15:58:40 -04:00
|
|
|
}
|
|
|
|
#endif
|
2016-09-16 09:39:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int transferred = 0;
|
|
|
|
const int timeout_msecs = 1000;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/* transfer to device */
|
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
if ((err = libusb_bulk_transfer (p2.usb_handle(), 0x01, sample_header, sizeof (sample_header), &transferred, timeout_msecs))) {
|
2016-09-16 09:39:28 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
if ((err = libusb_bulk_transfer (p2.usb_handle(), 0x01, (uint8_t*) device_sample_buffer, 2 * pixel_area (), &transferred, timeout_msecs))) {
|
2016-09-16 09:39:28 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Push2Canvas::request_redraw ()
|
|
|
|
{
|
|
|
|
request_redraw (Rect (0, 0, _cols, _rows));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Push2Canvas::request_redraw (Rect const & r)
|
|
|
|
{
|
|
|
|
Cairo::RectangleInt cr;
|
|
|
|
|
2016-09-16 14:17:24 -04:00
|
|
|
cr.x = r.x0;
|
|
|
|
cr.y = r.y0;
|
2016-09-16 09:39:28 -04:00
|
|
|
cr.width = r.width();
|
2016-09-19 17:11:22 -04:00
|
|
|
cr.height = r.height();
|
2016-09-16 09:39:28 -04:00
|
|
|
|
2016-09-21 16:25:44 -04:00
|
|
|
// DEBUG_TRACE (DEBUG::Push2, string_compose ("invalidate rect %1\n", r));
|
2016-09-16 14:17:24 -04:00
|
|
|
|
2016-09-16 09:39:28 -04:00
|
|
|
expose_region->do_union (cr);
|
|
|
|
|
|
|
|
/* next vblank will redraw */
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Push2Canvas::expose ()
|
|
|
|
{
|
|
|
|
if (expose_region->empty()) {
|
|
|
|
return false; /* nothing drawn */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set up clipping */
|
|
|
|
|
|
|
|
const int nrects = expose_region->get_num_rectangles ();
|
|
|
|
|
2016-09-21 16:25:44 -04:00
|
|
|
//DEBUG_TRACE (DEBUG::Push2, string_compose ("expose with %1 rects\n", nrects));
|
2016-09-16 14:17:24 -04:00
|
|
|
|
2016-09-16 09:39:28 -04:00
|
|
|
for (int n = 0; n < nrects; ++n) {
|
|
|
|
Cairo::RectangleInt r = expose_region->get_rectangle (n);
|
|
|
|
context->rectangle (r.x, r.y, r.width, r.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
context->clip ();
|
|
|
|
|
|
|
|
Push2Layout* layout = p2.current_layout();
|
|
|
|
|
|
|
|
if (layout) {
|
2016-09-21 16:25:44 -04:00
|
|
|
/* all layouts cover (at least) the full size of the video
|
|
|
|
display, so we do not need to check if the layout intersects
|
|
|
|
the bounding box of the full expose region.
|
|
|
|
*/
|
2016-09-16 09:39:28 -04:00
|
|
|
Cairo::RectangleInt r = expose_region->get_extents();
|
2016-09-16 14:17:24 -04:00
|
|
|
Rect rr (r.x, r.y, r.x + r.width, r.y + r.height);
|
2016-09-21 16:25:44 -04:00
|
|
|
//DEBUG_TRACE (DEBUG::Push2, string_compose ("render layout with %1\n", rr));
|
2016-09-16 09:39:28 -04:00
|
|
|
layout->render (Rect (r.x, r.y, r.x + r.width, r.y + r.height), context);
|
|
|
|
}
|
|
|
|
|
|
|
|
context->reset_clip ();
|
|
|
|
|
2016-09-16 14:17:24 -04:00
|
|
|
/* why is there no "reset()" method for Cairo::Region? */
|
|
|
|
|
|
|
|
expose_region = Cairo::Region::create ();
|
|
|
|
|
2016-09-16 09:39:28 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
/** render host-side sample buffer (a Cairo ImageSurface) to the current
|
|
|
|
* device-side sample buffer. The device sample buffer will be pushed to the
|
2016-09-16 09:39:28 -04:00
|
|
|
* device on the next call to vblank()
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2017-09-18 12:39:17 -04:00
|
|
|
Push2Canvas::blit_to_device_sample_buffer ()
|
2016-09-16 09:39:28 -04:00
|
|
|
{
|
|
|
|
/* ensure that all drawing has been done before we fetch pixel data */
|
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
sample_buffer->flush ();
|
2016-09-16 09:39:28 -04:00
|
|
|
|
|
|
|
const int stride = 3840; /* bytes per row for Cairo::FORMAT_ARGB32 */
|
2017-09-18 12:39:17 -04:00
|
|
|
const uint8_t* data = sample_buffer->get_data ();
|
2016-09-16 09:39:28 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
/* fill sample buffer (320kB) */
|
2016-09-16 09:39:28 -04:00
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
uint16_t* fb = (uint16_t*) device_sample_buffer;
|
2016-09-16 09:39:28 -04:00
|
|
|
|
|
|
|
for (int row = 0; row < _rows; ++row) {
|
|
|
|
|
|
|
|
const uint8_t* dp = data + row * stride;
|
|
|
|
|
|
|
|
for (int col = 0; col < _cols; ++col) {
|
|
|
|
|
|
|
|
/* fetch r, g, b (range 0..255). Ignore alpha */
|
|
|
|
|
|
|
|
const int r = (*((const uint32_t*)dp) >> 16) & 0xff;
|
|
|
|
const int g = (*((const uint32_t*)dp) >> 8) & 0xff;
|
|
|
|
const int b = *((const uint32_t*)dp) & 0xff;
|
|
|
|
|
|
|
|
/* convert to 5 bits, 6 bits, 5 bits, respectively */
|
|
|
|
/* generate 16 bit BGB565 value */
|
|
|
|
|
|
|
|
*fb++ = (r >> 3) | ((g & 0xfc) << 3) | ((b & 0xf8) << 8);
|
|
|
|
|
|
|
|
/* the push2 docs state that we should xor the pixel
|
|
|
|
* data. Doing so doesn't work correctly, and not doing
|
|
|
|
* so seems to work fine (colors roughly match intended
|
|
|
|
* values).
|
|
|
|
*/
|
|
|
|
|
|
|
|
dp += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* skip 128 bytes to next line. This is filler, used to avoid line borders occuring in the middle of 512
|
|
|
|
byte USB buffers
|
|
|
|
*/
|
|
|
|
|
|
|
|
fb += 64; /* 128 bytes = 64 int16_t */
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-09-16 14:17:24 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
Push2Canvas::request_size (Duple)
|
|
|
|
{
|
|
|
|
/* fixed size canvas */
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect
|
|
|
|
Push2Canvas::visible_area () const
|
|
|
|
{
|
|
|
|
/* may need to get more sophisticated once we do scrolling */
|
|
|
|
return Rect (0, 0, 960, 160);
|
|
|
|
}
|
2016-10-13 17:11:38 -04:00
|
|
|
|
|
|
|
Glib::RefPtr<Pango::Context>
|
|
|
|
Push2Canvas::get_pango_context ()
|
|
|
|
{
|
|
|
|
if (!pango_context) {
|
2019-12-09 17:38:10 -05:00
|
|
|
PangoFontMap* map = pango_cairo_font_map_new ();
|
2016-10-13 17:11:38 -04:00
|
|
|
if (!map) {
|
|
|
|
error << _("Default Cairo font map is null!") << endmsg;
|
|
|
|
return Glib::RefPtr<Pango::Context> ();
|
|
|
|
}
|
|
|
|
|
|
|
|
PangoContext* context = pango_font_map_create_context (map);
|
2019-12-09 17:38:10 -05:00
|
|
|
pango_cairo_context_set_resolution (context, 96);
|
2016-10-13 17:11:38 -04:00
|
|
|
|
|
|
|
if (!context) {
|
|
|
|
error << _("cannot create new PangoContext from cairo font map") << endmsg;
|
|
|
|
return Glib::RefPtr<Pango::Context> ();
|
|
|
|
}
|
|
|
|
|
|
|
|
pango_context = Glib::wrap (context);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pango_context;
|
|
|
|
}
|