When Retrier succeeds, record errors it encountered.
[chromium-blink-merge.git] / ui / surface / d3d9_utils_win.cc
blob67df0fea3e3380fa0eda9570a57049a6b10e9fba
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"
13 namespace {
15 const wchar_t kD3D9ModuleName[] = L"d3d9.dll";
16 const char kCreate3D9DeviceExName[] = "Direct3DCreate9Ex";
17 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT sdk_version,
18 IDirect3D9Ex **d3d);
19 } // namespace
21 namespace ui_surface_d3d9_utils {
23 bool LoadD3D9(base::ScopedNativeLibrary* storage) {
24 storage->Reset(
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));
36 if (!create_func)
37 return false;
39 base::win::ScopedComPtr<IDirect3D9Ex> d3d;
40 HRESULT hr = create_func(D3D_SDK_VERSION, d3d.Receive());
41 if (FAILED(hr))
42 return false;
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;
55 parameters.Flags = 0;
56 parameters.PresentationInterval = presentation_interval;
57 parameters.SwapEffect = D3DSWAPEFFECT_COPY;
59 hr = d3d->CreateDeviceEx(
60 D3DADAPTER_DEFAULT,
61 device_type,
62 window,
63 D3DCREATE_FPU_PRESERVE | D3DCREATE_SOFTWARE_VERTEXPROCESSING |
64 D3DCREATE_DISABLE_PSGP_THREADING | D3DCREATE_MULTITHREADED,
65 &parameters,
66 NULL,
67 device);
68 return SUCCEEDED(hr);
71 bool OpenSharedTexture(IDirect3DDevice9* device,
72 int64 surface_handle,
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(),
78 size.height(),
80 D3DUSAGE_RENDERTARGET,
81 D3DFMT_A8R8G8B8,
82 D3DPOOL_DEFAULT,
83 opened_texture,
84 &handle);
85 return SUCCEEDED(hr);
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");
94 surface->Release();
95 HRESULT hr = device->CreateRenderTarget(
96 size.width(),
97 size.height(),
98 D3DFMT_A8R8G8B8,
99 D3DMULTISAMPLE_NONE,
101 TRUE,
102 surface->Receive(),
103 NULL);
104 if (FAILED(hr))
105 return false;
107 return true;
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");
117 texture->Release();
118 HRESULT hr = device->CreateTexture(
119 size.width(),
120 size.height(),
121 1, // Levels
122 D3DUSAGE_RENDERTARGET,
123 D3DFMT_A8R8G8B8,
124 D3DPOOL_DEFAULT,
125 texture->Receive(),
126 NULL);
127 if (!SUCCEEDED(hr))
128 return false;
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);
137 if (FAILED(hr))
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);
145 if (FAILED(hr))
146 return gfx::Size(0, 0);
147 return gfx::Size(surface_description.Width, surface_description.Height);
150 } // namespace ui_surface_d3d9_utils