1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/surface/d3d9_utils_win.h"
7 #include "base/debug/trace_event.h"
8 #include "base/files/file_path.h"
9 #include "base/scoped_native_library.h"
10 #include "base/win/scoped_comptr.h"
11 #include "ui/gfx/size.h"
15 const wchar_t kD3D9ModuleName
[] = L
"d3d9.dll";
16 const char kCreate3D9DeviceExName
[] = "Direct3DCreate9Ex";
17 typedef HRESULT (WINAPI
*Direct3DCreate9ExFunc
)(UINT sdk_version
,
21 namespace ui_surface_d3d9_utils
{
23 bool LoadD3D9(base::ScopedNativeLibrary
* storage
) {
25 base::LoadNativeLibrary(base::FilePath(kD3D9ModuleName
), NULL
));
26 return storage
->is_valid();
29 bool CreateDevice(const base::ScopedNativeLibrary
& d3d_module
,
30 D3DDEVTYPE device_type
,
31 uint32 presentation_interval
,
32 IDirect3DDevice9Ex
** device
) {
34 Direct3DCreate9ExFunc create_func
= reinterpret_cast<Direct3DCreate9ExFunc
>(
35 d3d_module
.GetFunctionPointer(kCreate3D9DeviceExName
));
39 base::win::ScopedComPtr
<IDirect3D9Ex
> d3d
;
40 HRESULT hr
= create_func(D3D_SDK_VERSION
, d3d
.Receive());
44 // Any old window will do to create the device. In practice the window to
45 // present to is an argument to IDirect3DDevice9::Present.
46 HWND window
= GetDesktopWindow();
48 D3DPRESENT_PARAMETERS parameters
= { 0 };
49 parameters
.BackBufferWidth
= 1;
50 parameters
.BackBufferHeight
= 1;
51 parameters
.BackBufferCount
= 1;
52 parameters
.BackBufferFormat
= D3DFMT_A8R8G8B8
;
53 parameters
.hDeviceWindow
= window
;
54 parameters
.Windowed
= TRUE
;
56 parameters
.PresentationInterval
= presentation_interval
;
57 parameters
.SwapEffect
= D3DSWAPEFFECT_COPY
;
59 hr
= d3d
->CreateDeviceEx(
63 D3DCREATE_FPU_PRESERVE
| D3DCREATE_SOFTWARE_VERTEXPROCESSING
|
64 D3DCREATE_DISABLE_PSGP_THREADING
| D3DCREATE_MULTITHREADED
,
71 bool OpenSharedTexture(IDirect3DDevice9
* device
,
73 const gfx::Size
& size
,
74 IDirect3DTexture9
** opened_texture
) {
75 TRACE_EVENT0("gpu", "OpenSharedTexture");
76 HANDLE handle
= reinterpret_cast<HANDLE
>(surface_handle
);
77 HRESULT hr
= device
->CreateTexture(size
.width(),
80 D3DUSAGE_RENDERTARGET
,
88 bool CreateOrReuseLockableSurface(
89 IDirect3DDevice9
* device
,
90 const gfx::Size
& size
,
91 base::win::ScopedComPtr
<IDirect3DSurface9
>* surface
) {
92 if (!*surface
|| GetSize(*surface
) != size
) {
93 TRACE_EVENT0("gpu", "CreateRenderTarget");
95 HRESULT hr
= device
->CreateRenderTarget(
110 bool CreateOrReuseRenderTargetTexture(
111 IDirect3DDevice9
* device
,
112 const gfx::Size
& size
,
113 base::win::ScopedComPtr
<IDirect3DTexture9
>* texture
,
114 IDirect3DSurface9
** render_target
) {
115 if (!*texture
|| GetSize(*texture
) != size
) {
116 TRACE_EVENT0("gpu", "CreateTexture");
118 HRESULT hr
= device
->CreateTexture(
122 D3DUSAGE_RENDERTARGET
,
130 HRESULT hr
= (*texture
)->GetSurfaceLevel(0, render_target
);
131 return SUCCEEDED(hr
);
134 gfx::Size
GetSize(IDirect3DSurface9
* surface
) {
135 D3DSURFACE_DESC surface_description
;
136 HRESULT hr
= surface
->GetDesc(&surface_description
);
138 return gfx::Size(0, 0);
139 return gfx::Size(surface_description
.Width
, surface_description
.Height
);
142 gfx::Size
GetSize(IDirect3DTexture9
* texture
) {
143 D3DSURFACE_DESC surface_description
;
144 HRESULT hr
= texture
->GetLevelDesc(0, &surface_description
);
146 return gfx::Size(0, 0);
147 return gfx::Size(surface_description
.Width
, surface_description
.Height
);
150 } // namespace ui_surface_d3d9_utils