forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGDIRenderer.cpp
304 lines (276 loc) · 9.71 KB
/
GDIRenderer.cpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/******************************************************************************
QtAV: Multimedia framework based on Qt and FFmpeg
Copyright (C) 2012-2016 Wang Bin <[email protected]>
* This file is part of QtAV
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include "QtAV/VideoRenderer.h"
#include "QtAV/private/VideoRenderer_p.h"
#include <QWidget>
#include <windows.h> //GetDC()
#include <gdiplus.h>
#include <QResizeEvent>
#include "QtAV/private/factory.h"
#define USE_GRAPHICS 0
//http://msdn.microsoft.com/en-us/library/ms927613.aspx
//#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;
namespace QtAV {
class GDIRendererPrivate;
class GDIRenderer : public QWidget, public VideoRenderer
{
Q_OBJECT
DPTR_DECLARE_PRIVATE(GDIRenderer)
public:
GDIRenderer(QWidget* parent = 0, Qt::WindowFlags f = 0); //offscreen?
VideoRendererId id() const Q_DECL_OVERRIDE;
bool isSupported(VideoFormat::PixelFormat pixfmt) const Q_DECL_OVERRIDE;
/* WA_PaintOnScreen: To render outside of Qt's paint system, e.g. If you require
* native painting primitives, you need to reimplement QWidget::paintEngine() to
* return 0 and set this flag
*/
QPaintEngine* paintEngine() const Q_DECL_OVERRIDE;
/*http://lists.trolltech.com/qt4-preview-feedback/2005-04/thread00609-0.html
* true: paintEngine.getDC(), double buffer is enabled by defalut.
* false: GetDC(winId()), no double buffer, should reimplement paintEngine()
*/
QWidget* widget() Q_DECL_OVERRIDE { return this; }
protected:
bool receiveFrame(const VideoFrame& frame) Q_DECL_OVERRIDE;
void drawBackground() Q_DECL_OVERRIDE;
void drawFrame() Q_DECL_OVERRIDE;
/*usually you don't need to reimplement paintEvent, just drawXXX() is ok. unless you want do all
*things yourself totally*/
void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE;
void resizeEvent(QResizeEvent *) Q_DECL_OVERRIDE;
//stay on top will change parent, hide then show(windows). we need GetDC() again
void showEvent(QShowEvent *) Q_DECL_OVERRIDE;
};
typedef GDIRenderer VideoRendererGDI;
extern VideoRendererId VideoRendererId_GDI;
#if 0
FACTORY_REGISTER_ID_AUTO(VideoRenderer, GDI, "GDI")
#else
void RegisterVideoRendererGDI_Man()
{
VideoRenderer::Register<GDIRenderer>(VideoRendererId_GDI, "GDI");
}
#endif
VideoRendererId GDIRenderer::id() const
{
return VideoRendererId_GDI;
}
class GDIRendererPrivate : public VideoRendererPrivate
{
public:
DPTR_DECLARE_PUBLIC(GDIRenderer)
GDIRendererPrivate():
VideoRendererPrivate()
, support_bitblt(true)
, gdiplus_token(0)
, device_context(0)
#if USE_GRAPHICS
, graphics(0)
#endif //USE_GRAPHICS
{
GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(&gdiplus_token, &gdiplusStartupInput, NULL);
}
~GDIRendererPrivate() {
if (device_context) {
DPTR_P(GDIRenderer);
ReleaseDC((HWND)p.winId(), device_context); /*Q5: must cast WID to HWND*/
#if !USE_GRAPHICS
DeleteDC(off_dc);
#endif //USE_GRAPHICS
device_context = 0;
}
GdiplusShutdown(gdiplus_token);
}
void prepare() {
update_background = true;
DPTR_P(GDIRenderer);
device_context = GetDC((HWND)p.winId()); /*Q5: must cast WID to HWND*/
#if USE_GRAPHICS
if (graphics) {
delete graphics;
}
graphics = new Graphics(device_context);
#endif //USE_GRAPHICS
//TODO: check bitblt support
int ret = GetDeviceCaps(device_context, RC_BITBLT);
qDebug("bitblt=%d", ret);
//TODO: wingapi? vlc
#if 0
BITMAPINFOHEADER bih;
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biSizeImage = 0;
bih.biPlanes = 1;
bih.biCompression = BI_RGB; //vlc: 16bpp=>BI_RGB, 15bpp=>BI_BITFIELDS
bih.biBitCount = 32;
bih.biWidth = src_width;
bih.biHeight = src_height;
bih.biClrImportant = 0;
bih.biClrUsed = 0;
bih.biXPelsPerMeter = 0;
bih.biYPelsPerMeter = 0;
off_bitmap = CreateDIBSection(device_context,
, (BITMAPINFO*)&bih
, DIB_RGB_COLORS
, &p_pic_buffer, NULL, 0);
#endif //0
#if !USE_GRAPHICS
off_dc = CreateCompatibleDC(device_context);
#endif //USE_GRAPHICS
}
void setupQuality() {
//http://www.codeproject.com/Articles/9184/Custom-AntiAliasing-with-GDI
//http://msdn.microsoft.com/en-us/library/windows/desktop/ms533836%28v=vs.85%29.aspx
/*
*Graphics.DrawImage, Graphics.InterpolationMode
* bitblit?
*/
#if USE_GRAPHICS
if (!graphics)
return;
switch (quality) {
case VideoRenderer::QualityBest:
graphics->SetInterpolationMode(InterpolationModeHighQualityBicubic);
break;
case VideoRenderer::QualityFastest:
graphics->SetInterpolationMode(InterpolationModeNearestNeighbor);
break;
default:
graphics->SetInterpolationMode(InterpolationModeDefault);
break;
}
#endif //USE_GRAPHICS
}
bool support_bitblt;
ULONG_PTR gdiplus_token;
/*
* GetDC(winID()): will flick
* QPainter.paintEngine()->getDC() in paintEvent: doc says it's for internal use
*/
HDC device_context;
/* Our offscreen bitmap and its framebuffer */
#if USE_GRAPHICS
Graphics *graphics;
#else
HDC off_dc;
HBITMAP off_bitmap;
#endif //USE_GRAPHICS
};
GDIRenderer::GDIRenderer(QWidget *parent, Qt::WindowFlags f):
QWidget(parent, f),VideoRenderer(*new GDIRendererPrivate())
{
DPTR_INIT_PRIVATE(GDIRenderer);
setAcceptDrops(true);
setFocusPolicy(Qt::StrongFocus);
/* To rapidly update custom widgets that constantly paint over their entire areas with
* opaque content, e.g., video streaming widgets, it is better to set the widget's
* Qt::WA_OpaquePaintEvent, avoiding any unnecessary overhead associated with repainting the
* widget's background
*/
setAttribute(Qt::WA_OpaquePaintEvent);
//setAttribute(Qt::WA_NoSystemBackground);
setAutoFillBackground(false);
//setDCFromPainter(false); //changeEvent will be called on show()
setAttribute(Qt::WA_PaintOnScreen, true);
}
bool GDIRenderer::isSupported(VideoFormat::PixelFormat pixfmt) const
{
return VideoFormat::isRGB(pixfmt);
}
QPaintEngine* GDIRenderer::paintEngine() const
{
return 0;
}
bool GDIRenderer::receiveFrame(const VideoFrame& frame)
{
DPTR_D(GDIRenderer);
if (frame.constBits(0))
d.video_frame = frame;
else
d.video_frame = frame.to(frame.pixelFormat());
updateUi();
return true;
}
void GDIRenderer::drawBackground()
{
const QRegion bgRegion(backgroundRegion());
if (bgRegion.isEmpty())
return;
const QColor bc(backgroundColor());
DPTR_D(GDIRenderer);
//HDC hdc = d.device_context;
Graphics g(d.device_context);
SolidBrush brush(Color(bc.alpha(), bc.red(), bc.green(), bc.blue())); //argb
const QVector<QRect> bg(bgRegion.rects());
foreach (const QRect& r, bg) {
g.FillRectangle(&brush, r.x(), r.y(), r.width(), r.height());
}
}
void GDIRenderer::drawFrame()
{
DPTR_D(GDIRenderer);
/* http://msdn.microsoft.com/en-us/library/windows/desktop/ms533829%28v=vs.85%29.aspx
* Improving Performance by Avoiding Automatic Scaling
* TODO: How about QPainter?
*/
//steps to use BitBlt: http://bbs.csdn.net/topics/60183502
Bitmap bitmap(d.video_frame.width(), d.video_frame.height(), d.video_frame.bytesPerLine(0)
, PixelFormat32bppRGB, (BYTE*)d.video_frame.constBits(0));
#if USE_GRAPHICS
if (d.graphics)
d.graphics->DrawImage(&bitmap, d.out_rect.x(), d.out_rect.y(), d.out_rect.width(), d.out_rect.height());
#else
if (FAILED(bitmap.GetHBITMAP(Color(), &d.off_bitmap))) {
qWarning("Failed GetHBITMAP");
return;
}
HDC hdc = d.device_context;
HBITMAP hbmp_old = (HBITMAP)SelectObject(d.off_dc, d.off_bitmap);
QRect roi = realROI();
// && image.size() != size()
//assume that the image data is already scaled to out_size(NOT renderer size!)
StretchBlt(hdc
, d.out_rect.left(), d.out_rect.top()
, d.out_rect.width(), d.out_rect.height()
, d.off_dc
, roi.x(), roi.y()
, roi.width(), roi.height()
, SRCCOPY);
SelectObject(d.off_dc, hbmp_old);
DeleteObject(d.off_bitmap); //avoid mem leak
#endif
//end paint
}
void GDIRenderer::paintEvent(QPaintEvent *)
{
handlePaintEvent();
}
void GDIRenderer::resizeEvent(QResizeEvent *e)
{
d_func().update_background = true;
resizeRenderer(e->size());
update();
}
void GDIRenderer::showEvent(QShowEvent *)
{
DPTR_D(GDIRenderer);
d.update_background = true;
d_func().prepare();
}
} //namespace QtAV
#include "GDIRenderer.moc"