OLD | NEW |
(Empty) | |
| 1 // Copyright 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 "stdafx.h" |
| 6 #include "win8/metro_driver/direct3d_helper.h" |
| 7 #include "win8/metro_driver/winrt_utils.h" |
| 8 |
| 9 #include "base/logging.h" |
| 10 |
| 11 #include <windows.graphics.display.h> |
| 12 |
| 13 namespace { |
| 14 |
| 15 void CheckIfFailed(HRESULT hr) { |
| 16 DCHECK(!FAILED(hr)); |
| 17 DVLOG(ERROR) << "Direct3D call failed, hr = " << hr; |
| 18 } |
| 19 |
| 20 float GetLogicalDpi() { |
| 21 mswr::ComPtr<wingfx::Display::IDisplayPropertiesStatics> display_properties; |
| 22 CheckIfFailed(winrt_utils::CreateActivationFactory( |
| 23 RuntimeClass_Windows_Graphics_Display_DisplayProperties, |
| 24 display_properties.GetAddressOf())); |
| 25 float dpi = 0.0; |
| 26 CheckIfFailed(display_properties->get_LogicalDpi(&dpi)); |
| 27 return dpi; |
| 28 } |
| 29 |
| 30 float ConvertDipsToPixels(float dips) { |
| 31 static const float dips_per_inch = 96.f; |
| 32 float logical_dpi = GetLogicalDpi(); |
| 33 return floor(dips * logical_dpi / dips_per_inch + 0.5f); |
| 34 } |
| 35 |
| 36 } |
| 37 |
| 38 namespace metro_driver { |
| 39 |
| 40 Direct3DHelper::Direct3DHelper() { |
| 41 } |
| 42 |
| 43 Direct3DHelper::~Direct3DHelper() { |
| 44 } |
| 45 |
| 46 void Direct3DHelper::Initialize(winui::Core::ICoreWindow* window) { |
| 47 window_ = window; |
| 48 CreateDeviceResources(); |
| 49 CreateWindowSizeDependentResources(); |
| 50 } |
| 51 |
| 52 // TODO(scottmg): Need to handle resize messages and recreation. |
| 53 |
| 54 void Direct3DHelper::CreateDeviceResources() { |
| 55 unsigned int creation_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; |
| 56 D3D_FEATURE_LEVEL feature_levels[] = { |
| 57 D3D_FEATURE_LEVEL_11_1, |
| 58 D3D_FEATURE_LEVEL_11_0, |
| 59 D3D_FEATURE_LEVEL_10_1, |
| 60 D3D_FEATURE_LEVEL_10_0, |
| 61 D3D_FEATURE_LEVEL_9_3, |
| 62 D3D_FEATURE_LEVEL_9_2, |
| 63 D3D_FEATURE_LEVEL_9_1, |
| 64 }; |
| 65 |
| 66 mswr::ComPtr<ID3D11Device> device; |
| 67 mswr::ComPtr<ID3D11DeviceContext> context; |
| 68 CheckIfFailed( |
| 69 D3D11CreateDevice( |
| 70 nullptr, |
| 71 D3D_DRIVER_TYPE_HARDWARE, |
| 72 nullptr, |
| 73 creation_flags, |
| 74 feature_levels, |
| 75 ARRAYSIZE(feature_levels), |
| 76 D3D11_SDK_VERSION, |
| 77 &device, |
| 78 &feature_level_, |
| 79 &context)); |
| 80 CheckIfFailed(device.As(&d3d_device_)); |
| 81 CheckIfFailed(context.As(&d3d_context_)); |
| 82 } |
| 83 |
| 84 void Direct3DHelper::CreateWindowSizeDependentResources() { |
| 85 CheckIfFailed(window_->get_Bounds(&window_bounds_)); |
| 86 float window_width = ConvertDipsToPixels(window_bounds_.Width); |
| 87 float window_height = ConvertDipsToPixels(window_bounds_.Height); |
| 88 |
| 89 // TODO(scottmg): Orientation. |
| 90 |
| 91 if (swap_chain_ != nullptr) { |
| 92 // TODO(scottmg): Resize if it already exists. |
| 93 NOTIMPLEMENTED(); |
| 94 } else { |
| 95 DXGI_SWAP_CHAIN_DESC1 swap_chain_desc = { 0 }; |
| 96 swap_chain_desc.Width = window_width; |
| 97 swap_chain_desc.Height = window_height; |
| 98 swap_chain_desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; |
| 99 swap_chain_desc.Stereo = false; |
| 100 swap_chain_desc.SampleDesc.Count = 1; |
| 101 swap_chain_desc.SampleDesc.Quality = 0; |
| 102 swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; |
| 103 swap_chain_desc.BufferCount = 2; // TODO(scottmg): Probably 1 is fine. |
| 104 swap_chain_desc.Scaling = DXGI_SCALING_NONE; |
| 105 swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; |
| 106 swap_chain_desc.Flags = 0; |
| 107 |
| 108 mswr::ComPtr<IDXGIDevice1> dxgi_device; |
| 109 CheckIfFailed(d3d_device_.As(&dxgi_device)); |
| 110 |
| 111 mswr::ComPtr<IDXGIAdapter> dxgi_adapter; |
| 112 CheckIfFailed(dxgi_device->GetAdapter(&dxgi_adapter)); |
| 113 |
| 114 mswr::ComPtr<IDXGIFactory2> dxgi_factory; |
| 115 CheckIfFailed(dxgi_adapter->GetParent( |
| 116 __uuidof(IDXGIFactory2), &dxgi_factory)); |
| 117 |
| 118 CheckIfFailed(dxgi_factory->CreateSwapChainForCoreWindow( |
| 119 d3d_device_.Get(), |
| 120 reinterpret_cast<IUnknown*>(window_), |
| 121 &swap_chain_desc, |
| 122 nullptr, |
| 123 &swap_chain_)); |
| 124 } |
| 125 } |
| 126 |
| 127 } // namespace metro_driver |
OLD | NEW |