2013-08-13 11:10:45 +02:00
|
|
|
|
2013-11-11 10:00:37 +01:00
|
|
|
// STL includes
|
|
|
|
#include <cassert>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
// Local includes
|
2016-02-24 14:34:19 +01:00
|
|
|
#include "grabber/DispmanxFrameGrabber.h"
|
2013-08-13 11:10:45 +02:00
|
|
|
|
2017-08-04 23:08:15 +02:00
|
|
|
DispmanxFrameGrabber::DispmanxFrameGrabber(const unsigned width, const unsigned height)
|
|
|
|
: Grabber("DISPMANXGRABBER", width, height)
|
|
|
|
, _vc_display(0)
|
|
|
|
, _vc_resource(0)
|
|
|
|
, _vc_flags(0)
|
|
|
|
, _captureBuffer(new ColorRgba[0])
|
|
|
|
, _captureBufferSize(0)
|
2017-08-12 07:55:32 +02:00
|
|
|
, _image_rgba(width, height)
|
2013-08-13 11:10:45 +02:00
|
|
|
{
|
2017-08-12 07:55:32 +02:00
|
|
|
_useImageResampler = false;
|
|
|
|
|
2013-08-13 11:10:45 +02:00
|
|
|
// Initiase BCM
|
|
|
|
bcm_host_init();
|
|
|
|
|
2013-08-21 16:37:00 +02:00
|
|
|
{
|
|
|
|
// Check if the display can be opened and display the current resolution
|
|
|
|
// Open the connection to the display
|
|
|
|
_vc_display = vc_dispmanx_display_open(0);
|
|
|
|
assert(_vc_display > 0);
|
|
|
|
|
|
|
|
// Obtain the display information
|
|
|
|
DISPMANX_MODEINFO_T vc_info;
|
|
|
|
int result = vc_dispmanx_display_get_info(_vc_display, &vc_info);
|
2013-08-21 17:23:32 +02:00
|
|
|
// Keep compiler happy in 'release' mode
|
|
|
|
(void)result;
|
2013-08-21 16:37:00 +02:00
|
|
|
assert(result == 0);
|
2016-07-12 00:53:48 +02:00
|
|
|
Info(_log, "Display opened with resolution: %dx%d", vc_info.width, vc_info.height);
|
2013-08-21 16:37:00 +02:00
|
|
|
|
|
|
|
// Close the displaye
|
|
|
|
vc_dispmanx_display_close(_vc_display);
|
|
|
|
}
|
2013-08-13 11:10:45 +02:00
|
|
|
|
|
|
|
// Create the resources for capturing image
|
2013-08-21 16:37:00 +02:00
|
|
|
uint32_t vc_nativeImageHandle;
|
|
|
|
_vc_resource = vc_dispmanx_resource_create(
|
2013-11-11 10:00:37 +01:00
|
|
|
VC_IMAGE_RGBA32,
|
2013-08-13 11:10:45 +02:00
|
|
|
width,
|
|
|
|
height,
|
2013-08-21 16:37:00 +02:00
|
|
|
&vc_nativeImageHandle);
|
|
|
|
assert(_vc_resource);
|
2013-08-13 11:10:45 +02:00
|
|
|
|
|
|
|
// Define the capture rectangle with the same size
|
|
|
|
vc_dispmanx_rect_set(&_rectangle, 0, 0, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
DispmanxFrameGrabber::~DispmanxFrameGrabber()
|
|
|
|
{
|
2016-05-18 11:26:25 +02:00
|
|
|
delete[] _captureBuffer;
|
|
|
|
|
2013-08-13 11:10:45 +02:00
|
|
|
// Clean up resources
|
2013-08-21 16:37:00 +02:00
|
|
|
vc_dispmanx_resource_delete(_vc_resource);
|
2013-08-13 11:10:45 +02:00
|
|
|
|
|
|
|
// De-init BCM
|
|
|
|
bcm_host_deinit();
|
|
|
|
}
|
|
|
|
|
2013-10-02 09:46:12 +02:00
|
|
|
void DispmanxFrameGrabber::setFlags(const int vc_flags)
|
|
|
|
{
|
|
|
|
_vc_flags = vc_flags;
|
|
|
|
}
|
|
|
|
|
2017-08-12 07:55:32 +02:00
|
|
|
int DispmanxFrameGrabber::grabFrame(Image<ColorRgb> & image)
|
2016-05-18 11:26:25 +02:00
|
|
|
{
|
2017-09-01 08:50:37 +02:00
|
|
|
if (!_enabled) return 0;
|
|
|
|
|
2016-05-18 11:26:25 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
// vc_dispmanx_resource_read_data doesn't seem to work well
|
|
|
|
// with arbitrary positions so we have to handle cropping by ourselves
|
2017-08-12 07:55:32 +02:00
|
|
|
unsigned cropLeft = _cropLeft;
|
|
|
|
unsigned cropRight = _cropRight;
|
|
|
|
unsigned cropTop = _cropTop;
|
2016-05-18 11:26:25 +02:00
|
|
|
unsigned cropBottom = _cropBottom;
|
|
|
|
|
|
|
|
if (_vc_flags & DISPMANX_SNAPSHOT_FILL)
|
|
|
|
{
|
|
|
|
// disable cropping, we are capturing the video overlay window
|
|
|
|
cropLeft = cropRight = cropTop = cropBottom = 0;
|
|
|
|
}
|
|
|
|
|
2017-08-12 07:55:32 +02:00
|
|
|
unsigned imageWidth = _width - cropLeft - cropRight;
|
2016-05-18 11:26:25 +02:00
|
|
|
unsigned imageHeight = _height - cropTop - cropBottom;
|
|
|
|
|
|
|
|
// calculate final image dimensions and adjust top/left cropping in 3D modes
|
|
|
|
switch (_videoMode)
|
|
|
|
{
|
2013-12-21 14:32:30 +01:00
|
|
|
case VIDEO_3DSBS:
|
2017-08-12 07:55:32 +02:00
|
|
|
imageWidth /= 2;
|
|
|
|
cropLeft /= 2;
|
2013-12-21 14:32:30 +01:00
|
|
|
break;
|
|
|
|
case VIDEO_3DTAB:
|
2017-08-12 07:55:32 +02:00
|
|
|
imageHeight /= 2;
|
|
|
|
cropTop /= 2;
|
2013-12-21 14:32:30 +01:00
|
|
|
break;
|
|
|
|
case VIDEO_2D:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// resize the given image if needed
|
2016-05-18 11:26:25 +02:00
|
|
|
if (image.width() != imageWidth || image.height() != imageHeight)
|
2013-12-21 14:32:30 +01:00
|
|
|
{
|
2016-05-18 11:26:25 +02:00
|
|
|
image.resize(imageWidth, imageHeight);
|
2013-12-21 14:32:30 +01:00
|
|
|
}
|
2013-08-13 11:10:45 +02:00
|
|
|
|
2017-08-12 07:55:32 +02:00
|
|
|
if (_image_rgba.width() != imageWidth || _image_rgba.height() != imageHeight)
|
|
|
|
{
|
|
|
|
_image_rgba.resize(imageWidth, imageHeight);
|
|
|
|
}
|
|
|
|
|
2013-08-21 16:37:00 +02:00
|
|
|
// Open the connection to the display
|
|
|
|
_vc_display = vc_dispmanx_display_open(0);
|
2016-05-18 11:26:25 +02:00
|
|
|
if (_vc_display < 0)
|
|
|
|
{
|
2016-07-12 00:53:48 +02:00
|
|
|
Error(_log, "Cannot open display: %d", _vc_display);
|
2017-08-12 07:55:32 +02:00
|
|
|
return -1;
|
2016-05-18 11:26:25 +02:00
|
|
|
}
|
2013-08-21 16:37:00 +02:00
|
|
|
|
|
|
|
// Create the snapshot (incl down-scaling)
|
2016-05-18 11:26:25 +02:00
|
|
|
ret = vc_dispmanx_snapshot(_vc_display, _vc_resource, (DISPMANX_TRANSFORM_T) _vc_flags);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-07-12 00:53:48 +02:00
|
|
|
Error(_log, "Snapshot failed: %d", ret);
|
2016-05-18 11:26:25 +02:00
|
|
|
vc_dispmanx_display_close(_vc_display);
|
2017-08-12 07:55:32 +02:00
|
|
|
return ret;
|
2016-05-18 11:26:25 +02:00
|
|
|
}
|
2013-08-21 16:37:00 +02:00
|
|
|
|
|
|
|
// Read the snapshot into the memory
|
2017-08-12 07:55:32 +02:00
|
|
|
void* imagePtr = _image_rgba.memptr();
|
2016-05-18 11:26:25 +02:00
|
|
|
void* capturePtr = imagePtr;
|
|
|
|
|
|
|
|
unsigned imagePitch = imageWidth * sizeof(ColorRgba);
|
|
|
|
|
|
|
|
// dispmanx seems to require the pitch to be a multiple of 64
|
|
|
|
unsigned capturePitch = (_rectangle.width * sizeof(ColorRgba) + 63) & (~63);
|
|
|
|
|
|
|
|
// grab to temp buffer if image pitch isn't valid or if we are cropping
|
|
|
|
if (imagePitch != capturePitch
|
|
|
|
|| (unsigned)_rectangle.width != imageWidth
|
|
|
|
|| (unsigned)_rectangle.height != imageHeight)
|
|
|
|
{
|
|
|
|
// check if we need to resize the capture buffer
|
|
|
|
unsigned captureSize = capturePitch * _rectangle.height / sizeof(ColorRgba);
|
|
|
|
if (_captureBufferSize != captureSize)
|
|
|
|
{
|
|
|
|
delete[] _captureBuffer;
|
|
|
|
_captureBuffer = new ColorRgba[captureSize];
|
|
|
|
_captureBufferSize = captureSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
capturePtr = &_captureBuffer[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = vc_dispmanx_resource_read_data(_vc_resource, &_rectangle, capturePtr, capturePitch);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-07-12 00:53:48 +02:00
|
|
|
Error(_log, "vc_dispmanx_resource_read_data failed: %d", ret);
|
2016-05-18 11:26:25 +02:00
|
|
|
vc_dispmanx_display_close(_vc_display);
|
2017-08-12 07:55:32 +02:00
|
|
|
return ret;
|
2016-05-18 11:26:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// copy capture data to image if we captured to temp buffer
|
|
|
|
if (imagePtr != capturePtr)
|
|
|
|
{
|
|
|
|
// adjust source pointer to top/left cropping
|
|
|
|
uint8_t* src_ptr = (uint8_t*) capturePtr
|
|
|
|
+ cropLeft * sizeof(ColorRgba)
|
|
|
|
+ cropTop * capturePitch;
|
|
|
|
|
|
|
|
for (unsigned y = 0; y < imageHeight; y++)
|
|
|
|
{
|
|
|
|
memcpy((uint8_t*)imagePtr + y * imagePitch,
|
|
|
|
src_ptr + y * capturePitch,
|
|
|
|
imagePitch);
|
|
|
|
}
|
|
|
|
}
|
2013-08-13 11:10:45 +02:00
|
|
|
|
2013-08-21 16:37:00 +02:00
|
|
|
// Close the displaye
|
|
|
|
vc_dispmanx_display_close(_vc_display);
|
2017-08-12 07:55:32 +02:00
|
|
|
|
|
|
|
// image to output image
|
|
|
|
_image_rgba.toRgb(image);
|
|
|
|
|
|
|
|
return 0;
|
2013-08-13 11:10:45 +02:00
|
|
|
}
|