OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "ui/surface/d3d9_utils_win.h" |
| 6 |
| 7 #include "base/file_path.h" |
| 8 #include "base/scoped_native_library.h" |
| 9 #include "base/win/scoped_comptr.h" |
| 10 #include "ui/gfx/size.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 const wchar_t kD3D9ModuleName[] = L"d3d9.dll"; |
| 15 const char kCreate3D9DeviceExName[] = "Direct3DCreate9Ex"; |
| 16 typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT sdk_version, |
| 17 IDirect3D9Ex **d3d); |
| 18 } // namespace |
| 19 |
| 20 namespace ui_surface_d3d9_utils { |
| 21 |
| 22 bool LoadD3D9(base::ScopedNativeLibrary* storage) { |
| 23 storage->Reset(base::LoadNativeLibrary(FilePath(kD3D9ModuleName), NULL)); |
| 24 return storage->is_valid(); |
| 25 } |
| 26 |
| 27 bool CreateDevice(const base::ScopedNativeLibrary& d3d_module, |
| 28 D3DDEVTYPE device_type, |
| 29 uint32 presentation_interval, |
| 30 IDirect3DDevice9Ex** device) { |
| 31 |
| 32 Direct3DCreate9ExFunc create_func = reinterpret_cast<Direct3DCreate9ExFunc>( |
| 33 d3d_module.GetFunctionPointer(kCreate3D9DeviceExName)); |
| 34 if (!create_func) |
| 35 return false; |
| 36 |
| 37 base::win::ScopedComPtr<IDirect3D9Ex> d3d; |
| 38 HRESULT hr = create_func(D3D_SDK_VERSION, d3d.Receive()); |
| 39 if (FAILED(hr)) |
| 40 return false; |
| 41 |
| 42 // Any old window will do to create the device. In practice the window to |
| 43 // present to is an argument to IDirect3DDevice9::Present. |
| 44 HWND window = GetShellWindow(); |
| 45 |
| 46 D3DPRESENT_PARAMETERS parameters = { 0 }; |
| 47 parameters.BackBufferWidth = 1; |
| 48 parameters.BackBufferHeight = 1; |
| 49 parameters.BackBufferCount = 1; |
| 50 parameters.BackBufferFormat = D3DFMT_A8R8G8B8; |
| 51 parameters.hDeviceWindow = window; |
| 52 parameters.Windowed = TRUE; |
| 53 parameters.Flags = 0; |
| 54 parameters.PresentationInterval = presentation_interval; |
| 55 parameters.SwapEffect = D3DSWAPEFFECT_COPY; |
| 56 |
| 57 hr = d3d->CreateDeviceEx( |
| 58 D3DADAPTER_DEFAULT, |
| 59 device_type, |
| 60 window, |
| 61 D3DCREATE_FPU_PRESERVE | D3DCREATE_SOFTWARE_VERTEXPROCESSING | |
| 62 D3DCREATE_DISABLE_PSGP_THREADING | D3DCREATE_MULTITHREADED, |
| 63 ¶meters, |
| 64 NULL, |
| 65 device); |
| 66 return SUCCEEDED(hr); |
| 67 } |
| 68 |
| 69 bool OpenSharedTexture(IDirect3DDevice9* device, |
| 70 int64 surface_handle, |
| 71 const gfx::Size& size, |
| 72 IDirect3DTexture9** opened_texture) { |
| 73 HANDLE handle = reinterpret_cast<HANDLE>(surface_handle); |
| 74 HRESULT hr = device->CreateTexture(size.width(), |
| 75 size.height(), |
| 76 1, |
| 77 D3DUSAGE_RENDERTARGET, |
| 78 D3DFMT_A8R8G8B8, |
| 79 D3DPOOL_DEFAULT, |
| 80 opened_texture, |
| 81 &handle); |
| 82 return SUCCEEDED(hr); |
| 83 } |
| 84 |
| 85 bool CreateTemporaryLockableSurface(IDirect3DDevice9* device, |
| 86 const gfx::Size& size, |
| 87 IDirect3DSurface9** surface) { |
| 88 HRESULT hr = device->CreateRenderTarget( |
| 89 size.width(), |
| 90 size.height(), |
| 91 D3DFMT_A8R8G8B8, |
| 92 D3DMULTISAMPLE_NONE, |
| 93 0, |
| 94 TRUE, |
| 95 surface, |
| 96 NULL); |
| 97 return SUCCEEDED(hr); |
| 98 } |
| 99 |
| 100 bool CreateTemporaryRenderTargetTexture(IDirect3DDevice9* device, |
| 101 const gfx::Size& size, |
| 102 IDirect3DTexture9** texture, |
| 103 IDirect3DSurface9** render_target) { |
| 104 HRESULT hr = device->CreateTexture( |
| 105 size.width(), |
| 106 size.height(), |
| 107 1, // Levels |
| 108 D3DUSAGE_RENDERTARGET, |
| 109 D3DFMT_A8R8G8B8, |
| 110 D3DPOOL_DEFAULT, |
| 111 texture, |
| 112 NULL); |
| 113 if (!SUCCEEDED(hr)) |
| 114 return false; |
| 115 hr = (*texture)->GetSurfaceLevel(0, render_target); |
| 116 return SUCCEEDED(hr); |
| 117 } |
| 118 |
| 119 } // namespace ui_surface_d3d9_utils |
OLD | NEW |