File: glOffscreenTarget.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 239,888 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 95
file content (153 lines) | stat: -rw-r--r-- 4,157 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//

#include <BALL/VIEW/RENDERING/glOffscreenTarget.h>

#include <QtWidgets/QApplication>
#include <QtGui/QImage>
#include <QtGui/QPaintEvent>
#include <QtGui/QPainter>

#include <QtOpenGL/QGLPixelBuffer>

namespace BALL
{
	namespace VIEW
	{
		GLOffscreenTarget::GLOffscreenTarget(GLRenderWindow* share_from, const String& filename)
			: RenderWindow(),
				QPaintDevice(),
				filename_(filename),
				share_from_(share_from),
				pixel_buffer_(),
				use_pixel_buffer_(QGLPixelBuffer::hasOpenGLPbuffers()),
				current_image_(share_from->width(), share_from->height(), QImage::Format_ARGB32)
		{
		}

		void GLOffscreenTarget::prepareRendering()
		{
			if (use_pixel_buffer_ && pixel_buffer_)
				pixel_buffer_->makeCurrent();
			else if (share_from_)
				share_from_->makeCurrent();
		}

		bool GLOffscreenTarget::resize(const unsigned int width, const unsigned int height)
		{
			RenderWindow::resize(width, height);

			if (use_pixel_buffer_)
			{
				pixel_buffer_ = boost::shared_ptr<QGLPixelBuffer>(new QGLPixelBuffer(width, height, share_from_->format(), share_from_));
			}
			return true;
		}

		void GLOffscreenTarget::tryUsePixelBuffer(bool use_pbo)
		{
			use_pixel_buffer_ = QGLPixelBuffer::hasOpenGLPbuffers() && use_pbo;
		}

		QImage GLOffscreenTarget::getImage()
		{
			if (use_pixel_buffer_ && pixel_buffer_)
				return pixel_buffer_->toImage();
			else if (share_from_)
				return share_from_->grabFrameBuffer();

			return QImage();
		}

		void GLOffscreenTarget::prepareUpscaling(Size final_width, Size final_height)
		{
			if (use_pixel_buffer_ && pixel_buffer_)
				current_image_ = QImage(final_width, final_height, pixel_buffer_->toImage().format());
			else if (share_from_)
				current_image_ = QImage(final_width, final_height, share_from_->grabFrameBuffer().format());
			QPainter::setRedirected(this, &current_image_);
		}

		void GLOffscreenTarget::updateImageTile(Size x_lower, Size y_lower, Size x_upper, Size y_upper)
		{
			QImage current_screen;

			if (use_pixel_buffer_ && pixel_buffer_)
				current_screen = pixel_buffer_->toImage();
			else if (share_from_)
				current_screen = share_from_->grabFrameBuffer();

			if (!current_screen.isNull())
			{
				// compute the coordinates of the rectangle in the final image (remembering
				// that Qt sets y to 0 on the *upper* boundary of the image!)
				QPoint  top_left(x_lower, current_image_.height() - (y_upper+1));
				QPoint bot_right(x_upper, current_image_.height() - (y_lower+1));

				QPainter p(&current_image_);
				p.drawImage(QRect(top_left, bot_right), current_screen, current_screen.rect());
				p.end();
			}
		}
		
		void GLOffscreenTarget::refresh()
		{
			if (!current_image_.isNull())
				current_image_.save(filename_.c_str(), "PNG");
		}

		QPaintEngine* GLOffscreenTarget::paintEngine() const
		{
			return current_image_.paintEngine();
		}

		int GLOffscreenTarget::metric(PaintDeviceMetric m) const
		{
			// Unfortunately, metric() is protected in PaintDevice, so we
			// cannot simply redirect the call to current_image_.
			// Hence, some tedious conversions have to be made here

			int result = 0;

			switch (m)
			{
				case QPaintDevice::PdmWidth:  
					result = current_image_.width(); 
					break;
				case QPaintDevice::PdmHeight: 
					result = current_image_.height();
					break;
				case QPaintDevice::PdmWidthMM:
				  result = current_image_.widthMM(); 
					break;
				case QPaintDevice::PdmHeightMM: 
					result = current_image_.heightMM();
					break;
				case QPaintDevice::PdmNumColors: 
					result = current_image_.colorCount();
					break;
				case QPaintDevice::PdmDepth: 
					result = current_image_.depth();
					break;
				case QPaintDevice::PdmDpiX: 
					result = current_image_.logicalDpiX();
					break;
				case QPaintDevice::PdmDpiY: 
					result = current_image_.logicalDpiY();
					break;
				case QPaintDevice::PdmPhysicalDpiX: 
					result = current_image_.physicalDpiX();
					break;
				case QPaintDevice::PdmPhysicalDpiY: 
					result = current_image_.physicalDpiY();
					break;
				default:
					result = 0;
			}

			return result;
		}

	}
}