FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
D3d10AsyncDrawService.cpp
1 /**********************************************************\
2 Original Author: Gil Gonen
3 
4 Created: Jan 28, 2013
5 License: Dual license model; choose one of two:
6 New BSD License
7 http://www.opensource.org/licenses/bsd-license.php
8 - or -
9 GNU Lesser General Public License, version 2.1
10 http://www.gnu.org/licenses/lgpl-2.1.html
11 
12 Copyright 2013 Gil Gonen and the Firebreath development team
13 \**********************************************************/
14 
15 #include "logging.h"
16 #include "D3d10AsyncDrawService.h"
17 #include "D3d10DeviceContext.h"
18 #include "BrowserHost.h"
19 #include "FactoryBase.h"
20 
21 using namespace FB;
22 
23 
24 D3d10AsyncDrawService::D3d10AsyncDrawService(BrowserHostPtr host)
25  : m_weakHost(host)
26  , m_dc(getFactoryInstance()->createD3d10DeviceContext())
27  , m_width(0)
28  , m_height(0)
29  , m_dimsChanged(false)
30 {
31  assert(m_dc); // the factory shouldn't fail
32 }
33 
34 D3d10AsyncDrawService::~D3d10AsyncDrawService()
35 {
36  if (ID3D10Device1* dev = device()) {
37  dev->ClearState();
38  }
39 }
40 
41 void D3d10AsyncDrawService::resized(uint32_t width, uint32_t height)
42 {
43  m_dimsChanged = (m_width != width) || (m_height != height);
44 
45  uint32_t was = m_width * m_height;
46  uint32_t now = width * height;
47 
48  m_width = width; m_height = height;
49 
50  if (was == 0 && now != 0) {
51  present(true);
52  }
53 }
54 
55 ID3D10Device1* D3d10AsyncDrawService::device() const
56 {
57  return m_dc ? m_dc->device() : 0;
58 }
59 
60 void D3d10AsyncDrawService::render(RenderCallback cb)
61 {
62  ID3D10Device1* dev = device();
63  if (!dev)
64  return;
65 
66  boost::unique_lock<boost::mutex> lock(m_mut);
67  while (!m_pBackBuffer)
68  {
69  m_cond.wait(lock);
70  }
71 
72  CComPtr<ID3D10RenderTargetView> rtView;
73  HRESULT hr = dev->CreateRenderTargetView(m_pBackBuffer, NULL, &rtView);
74  if (SUCCEEDED(hr)) {
75  D3D10_TEXTURE2D_DESC desc;
76  m_pBackBuffer->GetDesc(&desc);
77 
78  cb(dev, rtView, desc.Width, desc.Height);
79 
80  BrowserHostPtr host(m_weakHost.lock());
81  if (host) {
82  if (host->isMainThread()) {
83  present(false);
84  } else {
85  host->ScheduleOnMainThread(this->shared_from_this(), boost::bind(&D3d10AsyncDrawService::present, this, false));
86  }
87  }
88  }
89 }
90 
91 
boost::shared_ptr< FB::BrowserHost > BrowserHostPtr
Defines an alias representing a BrowserHost shared_ptr (you should never use a BrowserHost* directly)...
Definition: APITypes.h:117