Cleanup a couple of consts left un-renamed.
[chromium-blink-merge.git] / win8 / metro_driver / direct3d_helper.cc
blobd0c30cf71219f1d024854e1c25f7f3b4b22291ab
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.
5 #include "stdafx.h"
6 #include "win8/metro_driver/direct3d_helper.h"
7 #include "win8/metro_driver/winrt_utils.h"
9 #include "base/logging.h"
10 #include "base/win/windows_version.h"
12 #include <corewindow.h>
13 #include <windows.applicationmodel.core.h>
14 #include <windows.graphics.display.h>
16 namespace {
18 void CheckIfFailed(HRESULT hr) {
19 DCHECK(!FAILED(hr));
20 if (FAILED(hr))
21 DVLOG(0) << "Direct3D call failed, hr = " << hr;
24 float GetLogicalDpi() {
25 mswr::ComPtr<wingfx::Display::IDisplayPropertiesStatics> display_properties;
26 CheckIfFailed(winrt_utils::CreateActivationFactory(
27 RuntimeClass_Windows_Graphics_Display_DisplayProperties,
28 display_properties.GetAddressOf()));
29 float dpi = 0.0;
30 CheckIfFailed(display_properties->get_LogicalDpi(&dpi));
31 return dpi;
34 float ConvertDipsToPixels(float dips) {
35 static const float dips_per_inch = 96.f;
36 float logical_dpi = GetLogicalDpi();
37 return floor(dips * logical_dpi / dips_per_inch + 0.5f);
42 namespace metro_driver {
44 Direct3DHelper::Direct3DHelper() {
47 Direct3DHelper::~Direct3DHelper() {
50 void Direct3DHelper::Initialize(winui::Core::ICoreWindow* window) {
51 window_ = window;
52 CreateDeviceResources();
53 CreateWindowSizeDependentResources();
56 // TODO(scottmg): Need to handle resize messages and recreation.
58 void Direct3DHelper::CreateDeviceResources() {
59 unsigned int creation_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
60 D3D_FEATURE_LEVEL feature_levels[] = {
61 D3D_FEATURE_LEVEL_11_1,
62 D3D_FEATURE_LEVEL_11_0,
63 D3D_FEATURE_LEVEL_10_1,
64 D3D_FEATURE_LEVEL_10_0,
65 D3D_FEATURE_LEVEL_9_3,
66 D3D_FEATURE_LEVEL_9_2,
67 D3D_FEATURE_LEVEL_9_1,
70 mswr::ComPtr<ID3D11Device> device;
71 mswr::ComPtr<ID3D11DeviceContext> context;
72 CheckIfFailed(
73 D3D11CreateDevice(
74 nullptr,
75 D3D_DRIVER_TYPE_HARDWARE,
76 nullptr,
77 creation_flags,
78 feature_levels,
79 ARRAYSIZE(feature_levels),
80 D3D11_SDK_VERSION,
81 &device,
82 &feature_level_,
83 &context));
84 CheckIfFailed(device.As(&d3d_device_));
85 CheckIfFailed(context.As(&d3d_context_));
88 void Direct3DHelper::CreateWindowSizeDependentResources() {
89 float window_width = 0;
90 float window_height = 0;
92 if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
93 // Windows 8 returns in DIPs.
94 CheckIfFailed(window_->get_Bounds(&window_bounds_));
95 window_width = ConvertDipsToPixels(window_width);
96 window_height = ConvertDipsToPixels(window_height);
99 // TODO(scottmg): Orientation.
101 if (swap_chain_ != nullptr) {
102 // TODO(scottmg): Resize if it already exists.
103 NOTIMPLEMENTED();
104 } else {
105 DXGI_SWAP_CHAIN_DESC1 swap_chain_desc = { 0 };
106 swap_chain_desc.Width = window_width;
107 swap_chain_desc.Height = window_height;
108 swap_chain_desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
109 swap_chain_desc.Stereo = false;
110 swap_chain_desc.SampleDesc.Count = 1;
111 swap_chain_desc.SampleDesc.Quality = 0;
112 swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
113 swap_chain_desc.BufferCount = 2; // TODO(scottmg): Probably 1 is fine.
114 swap_chain_desc.Scaling = DXGI_SCALING_NONE;
115 swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
116 swap_chain_desc.Flags = 0;
118 mswr::ComPtr<IDXGIDevice1> dxgi_device;
119 CheckIfFailed(d3d_device_.As(&dxgi_device));
121 mswr::ComPtr<IDXGIAdapter> dxgi_adapter;
122 CheckIfFailed(dxgi_device->GetAdapter(&dxgi_adapter));
124 mswr::ComPtr<IDXGIFactory2> dxgi_factory;
125 CheckIfFailed(dxgi_adapter->GetParent(
126 __uuidof(IDXGIFactory2), &dxgi_factory));
128 if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
129 // On Win8 we need the CoreWindow interface to create the Swapchain.
130 CheckIfFailed(dxgi_factory->CreateSwapChainForCoreWindow(
131 d3d_device_.Get(),
132 reinterpret_cast<IUnknown*>(window_),
133 &swap_chain_desc,
134 nullptr,
135 &swap_chain_));
136 } else {
137 // On Win7 we need the raw HWND to create the Swapchain.
138 mswr::ComPtr<ICoreWindowInterop> interop;
139 CheckIfFailed(window_->QueryInterface(interop.GetAddressOf()));
140 HWND window = NULL;
141 interop->get_WindowHandle(&window);
143 swap_chain_desc.Scaling = DXGI_SCALING_STRETCH;
144 swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
146 CheckIfFailed(dxgi_factory->CreateSwapChainForHwnd(
147 d3d_device_.Get(),
148 window,
149 &swap_chain_desc,
150 nullptr,
151 nullptr,
152 &swap_chain_));
157 } // namespace metro_driver