1 // Copyright 2017 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 #ifndef DEVICE_VR_WINDOWS_D3D11_TEXTURE_HELPER_H
6 #define DEVICE_VR_WINDOWS_D3D11_TEXTURE_HELPER_H
7 
8 #include <D3D11_1.h>
9 #include <DXGI1_4.h>
10 #include <wrl.h>
11 
12 #include "base/win/scoped_handle.h"
13 #include "mojo/public/cpp/system/platform_handle.h"
14 #include "ui/gfx/geometry/rect_f.h"
15 
16 namespace device {
17 
18 class D3D11TextureHelper {
19  public:
20   D3D11TextureHelper();
21   ~D3D11TextureHelper();
22 
23   void Reset();
24 
25   bool EnsureInitialized();
26   bool SetAdapterIndex(int32_t index);
27   bool SetAdapterLUID(const LUID& luid);
SetUseBGRA(bool bgra)28   void SetUseBGRA(bool bgra) { bgra_ = bgra; }
29 
30   void CleanupNoSubmit();
31   void SetSourceAndOverlayVisible(bool source_visible, bool overlay_visible);
32 
33   bool CompositeToBackBuffer();
34   bool SetSourceTexture(base::win::ScopedHandle texture_handle,
35                         gfx::RectF left,
36                         gfx::RectF right);
37   bool SetOverlayTexture(base::win::ScopedHandle texture_handle,
38                          gfx::RectF left,
39                          gfx::RectF right);
40 
41   void AllocateBackBuffer();
42   const Microsoft::WRL::ComPtr<ID3D11Texture2D>& GetBackbuffer();
43   void DiscardView();
44 
45   bool UpdateBackbufferSizes();
BackBufferLeft()46   gfx::RectF BackBufferLeft() { return target_left_; }
BackBufferRight()47   gfx::RectF BackBufferRight() { return target_right_; }
OverrideViewports(gfx::RectF left,gfx::RectF right)48   void OverrideViewports(gfx::RectF left, gfx::RectF right) {
49     target_left_ = left;
50     target_right_ = right;
51     force_viewport_ = true;
52   }
BackBufferSize()53   gfx::Size BackBufferSize() { return target_size_; }
54   void SetBackbuffer(Microsoft::WRL::ComPtr<ID3D11Texture2D> back_buffer);
55   Microsoft::WRL::ComPtr<ID3D11Device> GetDevice();
56 
SetDefaultSize(gfx::Size size)57   void SetDefaultSize(gfx::Size size) { default_size_ = size; }
58 
59  private:
60   struct LayerData {
61     LayerData();
62     ~LayerData();
63 
64     Microsoft::WRL::ComPtr<ID3D11Texture2D> source_texture_;
65     Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> shader_resource_;
66     Microsoft::WRL::ComPtr<ID3D11SamplerState> sampler_;
67     Microsoft::WRL::ComPtr<IDXGIKeyedMutex> keyed_mutex_;
68     gfx::RectF left_;   // 0 to 1 in each direction
69     gfx::RectF right_;  // 0 to 1 in each direction
70     bool submitted_this_frame_ = false;
71   };
72 
73   bool EnsureOverlayBlendState();
74   bool EnsureContentBlendState();
75   bool EnsureRenderTargetView();
76   bool EnsureShaders();
77   bool EnsureInputLayout();
78   bool EnsureVertexBuffer();
79   bool UpdateVertexBuffer(LayerData& layer);
80   bool EnsureSampler(LayerData& layer);
81   Microsoft::WRL::ComPtr<IDXGIAdapter> GetAdapter();
82   bool CompositeLayer(LayerData& layer);
83   bool BindTarget();
84   void CleanupLayerData(LayerData& layer);
85 
86   struct RenderState {
87     RenderState();
88     ~RenderState();
89 
90     Microsoft::WRL::ComPtr<ID3D11Device1> d3d11_device_;
91     Microsoft::WRL::ComPtr<ID3D11DeviceContext> d3d11_device_context_;
92 
93     Microsoft::WRL::ComPtr<ID3D11RenderTargetView> render_target_view_;
94     Microsoft::WRL::ComPtr<ID3D11PixelShader> flip_pixel_shader_;
95     Microsoft::WRL::ComPtr<ID3D11VertexShader> vertex_shader_;
96     Microsoft::WRL::ComPtr<ID3D11GeometryShader> geometry_shader_;
97 
98     Microsoft::WRL::ComPtr<ID3D11InputLayout> input_layout_;
99     Microsoft::WRL::ComPtr<ID3D11Buffer> vertex_buffer_;
100     Microsoft::WRL::ComPtr<ID3D11Texture2D> target_texture_;
101 
102     Microsoft::WRL::ComPtr<ID3D11BlendState> content_blend_state_;
103     Microsoft::WRL::ComPtr<ID3D11BlendState> overlay_blend_state_;
104     Microsoft::WRL::ComPtr<ID3D11BlendState> current_blend_state_;
105 
106     LayerData source_;
107     LayerData overlay_;
108   };
109 
110   bool overlay_visible_ = true;
111   bool source_visible_ = true;
112 
113   bool bgra_ = false;
114   bool force_viewport_ = false;
115 
116   gfx::RectF target_left_;   // 0 to 1 in each direction
117   gfx::RectF target_right_;  // 0 to 1 in each direction
118   gfx::Size target_size_;
119 
120   gfx::Size default_size_;
121 
122   RenderState render_state_;
123   int32_t adapter_index_ = -1;
124   LUID adapter_luid_ = {};
125 };
126 }  // namespace device
127 
128 #endif  // DEVICE_VR_WINDOWS_D3D11_TEXTURE_HELPER_H
129