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
|
|
|
|
|
|
|
DispmanxFrameGrabber::DispmanxFrameGrabber(const unsigned width, const unsigned height) :
|
2013-08-21 16:37:00 +02:00
|
|
|
_vc_display(0),
|
|
|
|
_vc_resource(0),
|
2013-10-02 09:46:12 +02:00
|
|
|
_vc_flags(0),
|
2013-08-13 11:10:45 +02:00
|
|
|
_width(width),
|
2016-05-18 11:26:25 +02:00
|
|
|
_height(height),
|
|
|
|
_videoMode(VIDEO_2D),
|
|
|
|
_cropLeft(0),
|
|
|
|
_cropRight(0),
|
|
|
|
_cropTop(0),
|
|
|
|
_cropBottom(0),
|
|
|
|
_captureBuffer(new ColorRgba[0]),
|
|
|
|
_captureBufferSize(0)
|
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-03-23 17:40:34 +01:00
|
|
|
std::cout << "DISPMANXGRABBER INFO: Display opened with resolution: " << vc_info.width << "x" << vc_info.height << std::endl;
|
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;
|
|
|
|
}
|
|
|
|
|
2013-12-21 14:32:30 +01:00
|
|
|
void DispmanxFrameGrabber::setVideoMode(const VideoMode videoMode)
|
|
|
|
{
|
2016-05-18 11:26:25 +02:00
|
|
|
_videoMode = videoMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DispmanxFrameGrabber::setCropping(unsigned cropLeft, unsigned cropRight, unsigned cropTop, unsigned cropBottom)
|
|
|
|
{
|
|
|
|
if (cropLeft + cropRight >= _width || cropTop + cropBottom >= _height)
|
|
|
|
{
|
|
|
|
std::cout
|
|
|
|
<< "DISPMANXGRABBER ERROR: Rejecting invalid crop values"
|
|
|
|
<< " left: " << cropLeft
|
|
|
|
<< " right: " << cropRight
|
|
|
|
<< " top: " << cropTop
|
|
|
|
<< " bottom: " << cropBottom
|
|
|
|
<< std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_cropLeft = cropLeft;
|
|
|
|
_cropRight = cropRight;
|
|
|
|
_cropTop = cropTop;
|
|
|
|
_cropBottom = cropBottom;
|
|
|
|
|
|
|
|
if (cropLeft > 0 || cropRight > 0 || cropTop > 0 || cropBottom > 0)
|
|
|
|
{
|
|
|
|
std::cout
|
|
|
|
<< "DISPMANXGRABBER INFO: Cropping " << _width << "x" << _height << " image"
|
|
|
|
<< " left: " << cropLeft
|
|
|
|
<< " right: " << cropRight
|
|
|
|
<< " top: " << cropTop
|
|
|
|
<< " bottom: " << cropBottom
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DispmanxFrameGrabber::grabFrame(Image<ColorRgba> & image)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
// vc_dispmanx_resource_read_data doesn't seem to work well
|
|
|
|
// with arbitrary positions so we have to handle cropping by ourselves
|
|
|
|
unsigned cropLeft = _cropLeft;
|
|
|
|
unsigned cropRight = _cropRight;
|
|
|
|
unsigned cropTop = _cropTop;
|
|
|
|
unsigned cropBottom = _cropBottom;
|
|
|
|
|
|
|
|
if (_vc_flags & DISPMANX_SNAPSHOT_FILL)
|
|
|
|
{
|
|
|
|
// disable cropping, we are capturing the video overlay window
|
|
|
|
cropLeft = cropRight = cropTop = cropBottom = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned imageWidth = _width - cropLeft - cropRight;
|
|
|
|
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:
|
2016-05-18 11:26:25 +02:00
|
|
|
imageWidth = imageWidth / 2;
|
|
|
|
cropLeft = cropLeft / 2;
|
2013-12-21 14:32:30 +01:00
|
|
|
break;
|
|
|
|
case VIDEO_3DTAB:
|
2016-05-18 11:26:25 +02:00
|
|
|
imageHeight = imageHeight / 2;
|
|
|
|
cropTop = 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
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
std::cout << "DISPMANXGRABBER ERROR: Cannot open display: " << _vc_display << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
std::cout << "DISPMANXGRABBER ERROR: Snapshot failed: " << ret << std::endl;
|
|
|
|
vc_dispmanx_display_close(_vc_display);
|
|
|
|
return;
|
|
|
|
}
|
2013-08-21 16:37:00 +02:00
|
|
|
|
|
|
|
// Read the snapshot into the memory
|
2016-05-18 11:26:25 +02:00
|
|
|
void* imagePtr = image.memptr();
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
std::cout << "DISPMANXGRABBER ERROR: vc_dispmanx_resource_read_data failed: " << ret << std::endl;
|
|
|
|
vc_dispmanx_display_close(_vc_display);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2013-08-13 11:10:45 +02:00
|
|
|
}
|