MythTV  master
mythpainter_d3d9.cpp
Go to the documentation of this file.
1 // QT headers
2 #include <QApplication>
3 #include <QPainter>
4 #include <QMutex>
5 
6 // Mythdb headers
7 #include "mythlogging.h"
8 
9 // Mythui headers
10 #include "mythpainter_d3d9.h"
11 
12 #define LOC QString("D3D9 Painter: ")
13 
15  MythPainter(), m_render(render)
16 {
17  if (m_render)
18  m_render->IncrRef();
19 }
20 
22 {
23  Teardown();
24 }
25 
26 bool MythD3D9Painter::InitD3D9(QPaintDevice *parent)
27 {
28  if (m_render)
29  return true;
30 
31  QWidget *real_parent = (QWidget*)parent;
32  if (!real_parent)
33  return false;
34 
35  m_render = new MythRenderD3D9();
36  if (!m_render)
37  return false;
38 
39  if (m_render->Create(real_parent->size(), (HWND)real_parent->winId()))
40  return true;
41 
42  Teardown();
43  return false;
44 }
45 
47 {
49  ClearCache();
50  DeleteBitmaps();
51 
52  m_ImageBitmapMap.clear();
53  m_ImageExpireList.clear();
54  m_bitmapDeleteList.clear();
55 
56  if (m_render)
57  {
58  m_render->DecrRef();
59  m_render = nullptr;
60  }
61 }
62 
64 {
65  ClearCache();
66  DeleteBitmaps();
67 }
68 
69 void MythD3D9Painter::Begin(QPaintDevice *parent)
70 {
71  if (!m_render)
72  {
73  if (!InitD3D9(parent))
74  {
75  LOG(VB_GENERAL, LOG_ERR, "Failed to create D3D9 render.");
76  return;
77  }
78  }
79 
80  DeleteBitmaps();
81 
82  if (!m_target)
83  {
84  bool dummy;
85  m_render->Test(dummy); // TODO recreate if necessary
86  }
87  else if (!m_target->SetAsRenderTarget())
88  {
89  LOG(VB_GENERAL, LOG_ERR, "Failed to enable offscreen buffer.");
90  return;
91  }
92 
93  if (m_target)
95  if (m_swap_control)
96  m_render->Begin();
97  MythPainter::Begin(parent);
98 }
99 
101 {
102  if (m_render)
103  {
104  if (m_swap_control)
105  {
106  m_render->End();
107  m_render->Present(nullptr);
108  }
109  if (m_target)
110  m_render->SetRenderTarget(nullptr);
111  }
113 }
114 
116 {
117  LOG(VB_GENERAL, LOG_INFO, LOC + "Clearing D3D9 painter cache.");
118 
119  QMutexLocker locker(&m_bitmapDeleteLock);
120  QMapIterator<MythImage *, D3D9Image*> it(m_ImageBitmapMap);
121  while (it.hasNext())
122  {
123  it.next();
124  m_bitmapDeleteList.push_back(m_ImageBitmapMap[it.key()]);
125  m_ImageExpireList.remove(it.key());
126  }
127  m_ImageBitmapMap.clear();
128 }
129 
131 {
132  QMutexLocker locker(&m_bitmapDeleteLock);
133  while (!m_bitmapDeleteList.empty())
134  {
135  D3D9Image *img = m_bitmapDeleteList.front();
136  m_bitmapDeleteList.pop_front();
137  m_HardwareCacheSize -= img->GetDataSize();
138  delete img;
139  }
140 }
141 
142 void MythD3D9Painter::DrawImage(const QRect &r, MythImage *im,
143  const QRect &src, int alpha)
144 {
145  D3D9Image *image = GetImageFromCache(im);
146  if (image)
147  {
148  image->UpdateVertices(r, src, alpha);
149  image->Draw();
150  }
151 }
152 
153 void MythD3D9Painter::DrawRect(const QRect &area, const QBrush &fillBrush,
154  const QPen &linePen, int alpha)
155 {
156  int style = fillBrush.style();
157  if (style == Qt::SolidPattern || style == Qt::NoBrush)
158  {
159  if (!m_render)
160  return;
161 
162  if (style != Qt::NoBrush)
163  m_render->DrawRect(area, fillBrush.color(), alpha);
164 
165  if (linePen.style() != Qt::NoPen)
166  {
167  int lineWidth = linePen.width();
168  QRect top(QPoint(area.x(), area.y()),
169  QSize(area.width(), lineWidth));
170  QRect bot(QPoint(area.x(), area.y() + area.height() - lineWidth),
171  QSize(area.width(), lineWidth));
172  QRect left(QPoint(area.x(), area.y()),
173  QSize(lineWidth, area.height()));
174  QRect right(QPoint(area.x() + area.width() - lineWidth, area.y()),
175  QSize(lineWidth, area.height()));
176  m_render->DrawRect(top, linePen.color(), alpha);
177  m_render->DrawRect(bot, linePen.color(), alpha);
178  m_render->DrawRect(left, linePen.color(), alpha);
179  m_render->DrawRect(right, linePen.color(), alpha);
180  }
181  return;
182  }
183 
184  MythPainter::DrawRect(area, fillBrush, linePen, alpha);
185 }
186 
188 {
189  if (m_ImageBitmapMap.contains(im))
190  {
191  QMutexLocker locker(&m_bitmapDeleteLock);
192  m_bitmapDeleteList.push_back(m_ImageBitmapMap[im]);
193  m_ImageBitmapMap.remove(im);
194  m_ImageExpireList.remove(im);
195  }
196 }
197 
199 {
200  if (m_ImageBitmapMap.contains(im))
201  {
202  if (!im->IsChanged())
203  {
204  m_ImageExpireList.remove(im);
205  m_ImageExpireList.push_back(im);
206  return m_ImageBitmapMap[im];
207  }
208  else
209  {
211  }
212  }
213 
214  im->SetChanged(false);
215  D3D9Image *newimage = nullptr;
216  if (m_render)
217  newimage = new D3D9Image(m_render,im->size());
218 
219  if (newimage && newimage->IsValid())
220  {
221  CheckFormatImage(im);
222  m_HardwareCacheSize += newimage->GetDataSize();
223  newimage->UpdateImage(im);
224  m_ImageBitmapMap[im] = newimage;
225  m_ImageExpireList.push_back(im);
226 
227  while (m_HardwareCacheSize > m_MaxHardwareCacheSize)
228  {
229  MythImage *expiredIm = m_ImageExpireList.front();
230  m_ImageExpireList.pop_front();
231  DeleteFormatImagePriv(expiredIm);
232  DeleteBitmaps();
233  }
234  }
235  else
236  {
237  LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to create D3D9 UI bitmap.");
238  delete newimage;
239  newimage = nullptr;
240  }
241 
242  return newimage;
243 }
mythpainter_d3d9.h
MythD3D9Painter::End
void End() override
Definition: mythpainter_d3d9.cpp:100
D3D9Image::UpdateVertices
bool UpdateVertices(const QRect &dvr, const QRect &vr, int alpha=255, bool video=false)
Definition: mythrender_d3d9.cpp:115
ReferenceCounter::DecrRef
virtual int DecrRef(void)
Decrements reference count and deletes on 0.
Definition: referencecounter.cpp:125
D3D9Image::SetAsRenderTarget
bool SetAsRenderTarget(void)
Definition: mythrender_d3d9.cpp:90
MythD3D9Painter::FreeResources
void FreeResources(void) override
Definition: mythpainter_d3d9.cpp:63
MythD3D9Painter::m_bitmapDeleteList
std::list< D3D9Image * > m_bitmapDeleteList
Definition: mythpainter_d3d9.h:55
LOC
#define LOC
Definition: mythpainter_d3d9.cpp:12
MythD3D9Painter::DeleteFormatImagePriv
void DeleteFormatImagePriv(MythImage *im) override
Definition: mythpainter_d3d9.cpp:187
MythD3D9Painter::m_bitmapDeleteLock
QMutex m_bitmapDeleteLock
Definition: mythpainter_d3d9.h:56
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
MythD3D9Painter::m_swap_control
bool m_swap_control
Definition: mythpainter_d3d9.h:52
MythRenderD3D9::Create
bool Create(QSize size, HWND window)
Definition: mythrender_d3d9.cpp:248
MythD3D9Painter::GetImageFromCache
D3D9Image * GetImageFromCache(MythImage *im)
Definition: mythpainter_d3d9.cpp:198
MythRenderD3D9::End
bool End(void)
Definition: mythrender_d3d9.cpp:482
MythD3D9Painter::m_ImageExpireList
std::list< MythImage * > m_ImageExpireList
Definition: mythpainter_d3d9.h:54
MythRenderD3D9::ClearBuffer
bool ClearBuffer(void)
Definition: mythrender_d3d9.cpp:449
MythD3D9Painter::Teardown
void Teardown(void) override
Definition: mythpainter_d3d9.cpp:46
mythlogging.h
D3D9Image
Definition: mythrender_d3d9.h:24
MythD3D9Painter::Begin
void Begin(QPaintDevice *parent) override
Definition: mythpainter_d3d9.cpp:69
MythD3D9Painter::m_render
MythRenderD3D9 * m_render
Definition: mythpainter_d3d9.h:50
MythRenderD3D9::DrawRect
void DrawRect(const QRect &rect, const QColor &color, int alpha)
Definition: mythrender_d3d9.cpp:573
MythRenderD3D9
Definition: mythrender_d3d9.h:63
MythRenderD3D9::Present
bool Present(HWND win)
Definition: mythrender_d3d9.cpp:691
MythD3D9Painter::DrawRect
void DrawRect(const QRect &area, const QBrush &fillBrush, const QPen &linePen, int alpha) override
Definition: mythpainter_d3d9.cpp:153
MythImage::SetChanged
virtual void SetChanged(bool change=true)
Definition: mythimage.h:50
D3D9Image::IsValid
bool IsValid(void) const
Definition: mythrender_d3d9.h:29
MythD3D9Painter::DeleteBitmaps
void DeleteBitmaps(void)
Definition: mythpainter_d3d9.cpp:130
MythD3D9Painter::MythD3D9Painter
MythD3D9Painter(MythRenderD3D9 *render=nullptr)
Definition: mythpainter_d3d9.cpp:14
MythD3D9Painter::ClearCache
void ClearCache(void)
Definition: mythpainter_d3d9.cpp:115
MythPainter::End
virtual void End()
Definition: mythpainter.h:53
MythRenderD3D9::SetRenderTarget
bool SetRenderTarget(IDirect3DTexture9 *texture)
Definition: mythrender_d3d9.cpp:715
MythD3D9Painter::~MythD3D9Painter
~MythD3D9Painter()
Definition: mythpainter_d3d9.cpp:21
MythPainter::DrawRect
virtual void DrawRect(const QRect &area, const QBrush &fillBrush, const QPen &linePen, int alpha)
Definition: mythpainter.cpp:157
MythRenderD3D9::Begin
bool Begin(void)
Definition: mythrender_d3d9.cpp:466
D3D9Image::UpdateImage
bool UpdateImage(IDirect3DSurface9 *surface)
Definition: mythrender_d3d9.cpp:97
D3D9Image::Draw
bool Draw(void)
Definition: mythrender_d3d9.cpp:124
MythD3D9Painter::m_ImageBitmapMap
QMap< MythImage *, D3D9Image * > m_ImageBitmapMap
Definition: mythpainter_d3d9.h:53
MythD3D9Painter::InitD3D9
bool InitD3D9(QPaintDevice *parent)
Definition: mythpainter_d3d9.cpp:26
MythPainter
Definition: mythpainter.h:33
MythImage
Definition: mythimage.h:37
MythRenderD3D9::Test
bool Test(bool &reset)
Definition: mythrender_d3d9.cpp:406
D3D9Image::GetDataSize
int GetDataSize(void) const
Definition: mythrender_d3d9.h:31
MythPainter::Begin
virtual void Begin(QPaintDevice *parent)
Definition: mythpainter.h:52
MythD3D9Painter::DrawImage
void DrawImage(const QRect &dest, MythImage *im, const QRect &src, int alpha) override
Definition: mythpainter_d3d9.cpp:142
MythImage::IsChanged
bool IsChanged() const
Definition: mythimage.h:51
MythPainter::Teardown
virtual void Teardown(void)
Definition: mythpainter.cpp:28
MythD3D9Painter::m_target
D3D9Image * m_target
Definition: mythpainter_d3d9.h:51
ReferenceCounter::IncrRef
virtual int IncrRef(void)
Increments reference count.
Definition: referencecounter.cpp:101
MythPainter::CheckFormatImage
void CheckFormatImage(MythImage *im)
Definition: mythpainter.cpp:556