1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/viz/service/display_embedder/software_output_device_win_swapchain.h"
#include <windows.h>
#include <dcomp.h>
#include <dxgi1_2.h>
#include <wrl/client.h>
#include "base/win/scoped_gdi_object.h"
#include "base/win/windows_types.h"
#include "components/viz/service/display_embedder/output_device_backing.h"
#include "components/viz/service/display_embedder/test_support/dcomp_mocks.h"
#include "media/base/win/d3d11_mocks.h"
#include "media/base/win/test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "ui/gfx/geometry/size.h"
namespace viz {
namespace {
constexpr gfx::Size kTestSize(1024, 768);
class MockOutputDeviceBacking : public OutputDeviceBacking {
public:
MockOutputDeviceBacking() = default;
~MockOutputDeviceBacking() override = default;
MOCK_METHOD(Microsoft::WRL::ComPtr<ID3D11Texture2D>,
GetOrCreateStagingTexture,
(),
(override));
MOCK_METHOD(HRESULT,
GetOrCreateDXObjects,
(Microsoft::WRL::ComPtr<ID3D11Device> * d3d11_device,
Microsoft::WRL::ComPtr<IDXGIFactory2>* dxgi_factory,
Microsoft::WRL::ComPtr<IDCompositionDevice>* dcomp_device),
(override));
};
class TestSoftwareOutputDeviceWinSwapChain
: public SoftwareOutputDeviceWinSwapChain {
public:
TestSoftwareOutputDeviceWinSwapChain(HWND hwnd,
HWND& child_hwnd,
OutputDeviceBacking* output_backing)
: SoftwareOutputDeviceWinSwapChain(hwnd, child_hwnd, output_backing) {}
// Override to avoid actual window operations.
bool UpdateWindowSize(const gfx::Size& viewport_pixel_size) override {
return resize_result_;
}
void set_resize_result(bool result) { resize_result_ = result; }
private:
bool resize_result_ = true;
};
class TestWindowClass {
public:
TestWindowClass() : window_(nullptr), child_window_(nullptr) {
// Create a temporary window for testing
window_ = CreateWindowEx(0, L"STATIC", L"Test Window", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, nullptr,
nullptr, GetModuleHandle(nullptr), nullptr);
}
~TestWindowClass() { DestroyWindow(window_); }
HWND window() const { return window_; }
HWND child_window() const { return child_window_; }
void set_child_window(HWND hwnd) { child_window_ = hwnd; }
private:
HWND window_;
HWND child_window_;
};
class SoftwareOutputDeviceWinSwapChainTest : public testing::Test {
protected:
void SetUp() override {
mock_output_backing_ =
std::make_unique<testing::NiceMock<MockOutputDeviceBacking>>();
mock_d3d11_device_ =
media::MakeComPtr<testing::NiceMock<media::D3D11DeviceMock>>();
mock_dxgi_factory_ =
media::MakeComPtr<testing::NiceMock<media::DXGIFactory2Mock>>();
mock_dcomp_device_ =
media::MakeComPtr<testing::NiceMock<media::DCompositionDeviceMock>>();
mock_dxgi_swapchain_ =
media::MakeComPtr<testing::NiceMock<media::DXGISwapChain1Mock>>();
mock_d3d11_device_context_ =
media::MakeComPtr<testing::NiceMock<media::D3D11DeviceContextMock>>();
mock_dcomp_visual_ =
media::MakeComPtr<testing::NiceMock<media::DCompositionVisualMock>>();
mock_dcomp_target_ =
media::MakeComPtr<testing::NiceMock<media::DCompositionTargetMock>>();
// Allow leak for the mock DCompositionDevice since it is prevented from
// being reset in the event of unexpected gpu process exit to allow for a
// final commit. Also allow leak for the mock DXGISwapChain because the
// DCompositionDevice maintains a reference to it via the root visual.
testing::Mock::AllowLeak(mock_dcomp_device_.Get());
testing::Mock::AllowLeak(mock_dxgi_swapchain_.Get());
ON_CALL(*mock_output_backing_, GetOrCreateDXObjects)
.WillByDefault(
[this](Microsoft::WRL::ComPtr<ID3D11Device>* d3d11_device,
Microsoft::WRL::ComPtr<IDXGIFactory2>* dxgi_factory,
Microsoft::WRL::ComPtr<IDCompositionDevice>* dcomp_device) {
*d3d11_device = mock_d3d11_device_;
*dxgi_factory = mock_dxgi_factory_;
*dcomp_device = mock_dcomp_device_;
return S_OK;
});
HWND child_hwnd = nullptr;
device_ = std::make_unique<TestSoftwareOutputDeviceWinSwapChain>(
window_class_.window(), child_hwnd, mock_output_backing_.get());
}
void TearDown() override {
testing::Mock::VerifyAndClearExpectations(mock_dxgi_swapchain_.Get());
}
void SetSwapChainExpectations() {
ON_CALL(*mock_dcomp_device_.Get(), CreateTargetForHwnd)
.WillByDefault(
media::SetComPointeeAndReturnOk<2>(mock_dcomp_target_.Get()));
ON_CALL(*mock_dcomp_device_.Get(), CreateVisual)
.WillByDefault(
media::SetComPointeeAndReturnOk<0>(mock_dcomp_visual_.Get()));
ON_CALL(*mock_d3d11_device_.Get(), GetImmediateContext)
.WillByDefault(
media::SetComPointee<0>(mock_d3d11_device_context_.Get()));
ON_CALL(*mock_dxgi_factory_.Get(), CreateSwapChainForComposition)
.WillByDefault(
media::SetComPointeeAndReturnOk<3>(mock_dxgi_swapchain_.Get()));
// Device should start with no D3D resources.
EXPECT_FALSE(device_->HasSwapChainForTesting());
EXPECT_FALSE(device_->HasDeviceContextForTesting());
EXPECT_CALL(*mock_output_backing_, GetOrCreateDXObjects).Times(1);
EXPECT_EQ(device_->GetViewportPixelSize(), gfx::Size(0, 0));
}
void DoResize(gfx::Size size) {
device_->Resize(size, 1.f);
EXPECT_EQ(device_->GetViewportPixelSize(), size);
EXPECT_TRUE(device_->HasSwapChainForTesting());
EXPECT_TRUE(device_->HasDeviceContextForTesting());
}
void DoBeginPaint(gfx::Size size) {
Microsoft::WRL::ComPtr<media::D3D11Texture2DMock>
mock_d3d11_staging_texture =
media::MakeComPtr<testing::NiceMock<media::D3D11Texture2DMock>>();
EXPECT_CALL(*mock_output_backing_, GetOrCreateStagingTexture)
.WillOnce(testing::Return(mock_d3d11_staging_texture));
auto desc = D3D11_TEXTURE2D_DESC{
.Width = static_cast<UINT>(size.width()),
.Height = static_cast<UINT>(size.height()),
.Format = DXGI_FORMAT_B8G8R8A8_UNORM,
.SampleDesc = {.Count = 1, .Quality = 0},
.Usage = D3D11_USAGE_STAGING,
.BindFlags = 0,
.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE};
EXPECT_CALL(*mock_d3d11_staging_texture.Get(), GetDesc)
.WillOnce(
[desc](D3D11_TEXTURE2D_DESC* descriptor) { *descriptor = desc; });
EXPECT_CALL(*mock_d3d11_device_context_.Get(), Map)
.WillOnce(testing::Return(S_OK));
SkCanvas* canvas = device_->BeginPaintDelegated();
auto image_info = canvas->imageInfo();
EXPECT_EQ(image_info.width(), size.width());
EXPECT_EQ(image_info.height(), size.height());
}
TestWindowClass window_class_;
std::unique_ptr<testing::NiceMock<MockOutputDeviceBacking>>
mock_output_backing_;
std::unique_ptr<TestSoftwareOutputDeviceWinSwapChain> device_;
Microsoft::WRL::ComPtr<media::DXGIFactory2Mock> mock_dxgi_factory_;
Microsoft::WRL::ComPtr<media::D3D11DeviceMock> mock_d3d11_device_;
Microsoft::WRL::ComPtr<media::DCompositionDeviceMock> mock_dcomp_device_;
Microsoft::WRL::ComPtr<IDCompositionTarget> mock_dcomp_target_;
Microsoft::WRL::ComPtr<media::D3D11DeviceContextMock>
mock_d3d11_device_context_;
Microsoft::WRL::ComPtr<IDCompositionVisual> mock_dcomp_visual_;
Microsoft::WRL::ComPtr<media::DXGISwapChain1Mock> mock_dxgi_swapchain_;
};
} // namespace
// Test that ResizeDelegated creates D3D resources when none exist.
TEST_F(SoftwareOutputDeviceWinSwapChainTest,
ResizeDelegatedCreatesD3DResources) {
SetSwapChainExpectations();
DoResize(kTestSize);
}
// Test that ResizeDelegated handles SetWindowPos failure.
TEST_F(SoftwareOutputDeviceWinSwapChainTest, SetWindowPosFailure) {
// Set the resize to fail
device_->set_resize_result(false);
EXPECT_EQ(device_->GetViewportPixelSize(), gfx::Size(0, 0));
device_->Resize(kTestSize, 1.f);
EXPECT_EQ(device_->GetViewportPixelSize(), gfx::Size(0, 0));
}
// Test that ResizeDelegated fails without process crash if CreateTargetForHwnd
// fails due to E_INVALIDARG. Such a scenario can occur if the window is deleted
// before the resize is complete.
TEST_F(SoftwareOutputDeviceWinSwapChainTest, CreateTargetForHwndInvalidArg) {
EXPECT_CALL(*mock_dcomp_device_.Get(), CreateTargetForHwnd)
.WillOnce(testing::Return(E_INVALIDARG));
EXPECT_EQ(device_->GetViewportPixelSize(), gfx::Size(0, 0));
device_->Resize(kTestSize, 1.f);
EXPECT_EQ(device_->GetViewportPixelSize(), gfx::Size(0, 0));
}
// Test the entire paint pipeline.
TEST_F(SoftwareOutputDeviceWinSwapChainTest, Paint) {
SetSwapChainExpectations();
// Ensure present gets called in EndPaint.
ON_CALL(*mock_dxgi_swapchain_.Get(), Present1)
.WillByDefault(testing::Return(S_OK));
DoResize(kTestSize);
DoBeginPaint(kTestSize);
device_->EndPaintDelegated(
gfx::Rect(0, 0, kTestSize.width(), kTestSize.height()));
}
// Test that Map fails without process crash if the device is removed.
TEST_F(SoftwareOutputDeviceWinSwapChainTest, FailedMapDueToDeviceRemoved) {
SetSwapChainExpectations();
EXPECT_CALL(*mock_d3d11_device_context_.Get(), Map)
.WillOnce(testing::Return(DXGI_ERROR_DEVICE_REMOVED));
EXPECT_CALL(*mock_d3d11_device_.Get(), GetDeviceRemovedReason)
.WillOnce(testing::Return(DXGI_ERROR_DEVICE_REMOVED));
DoResize(kTestSize);
Microsoft::WRL::ComPtr<media::D3D11Texture2DMock> mock_d3d11_staging_texture =
media::MakeComPtr<testing::NiceMock<media::D3D11Texture2DMock>>();
EXPECT_CALL(*mock_output_backing_, GetOrCreateStagingTexture)
.WillOnce(testing::Return(mock_d3d11_staging_texture));
// There should be no GPU process termination.
SkCanvas* canvas = device_->BeginPaintDelegated();
EXPECT_FALSE(!!canvas);
}
TEST_F(SoftwareOutputDeviceWinSwapChainTest, FailedPresentDueToDeviceRemoved) {
SetSwapChainExpectations();
EXPECT_CALL(*mock_dxgi_swapchain_.Get(), Present1)
.WillOnce(testing::Return(DXGI_ERROR_DEVICE_REMOVED));
EXPECT_CALL(*mock_d3d11_device_.Get(), GetDeviceRemovedReason)
.WillOnce(testing::Return(DXGI_ERROR_DEVICE_REMOVED));
DoResize(kTestSize);
DoBeginPaint(kTestSize);
// There should be no GPU process termination.
device_->EndPaintDelegated(
gfx::Rect(0, 0, kTestSize.width(), kTestSize.height()));
}
using SoftwareOutputDeviceWinSwapChainDeathTest =
SoftwareOutputDeviceWinSwapChainTest;
// Ensure the GPU process is terminated if ResizeBuffers fails.
TEST_F(SoftwareOutputDeviceWinSwapChainDeathTest, ResizeBuffersFailure) {
SetSwapChainExpectations();
ON_CALL(*mock_dxgi_swapchain_.Get(),
ResizeBuffers(/*BufferCount=*/2, /*Width=*/1180, /*Height=*/620,
/*NewFormat=*/testing::_, /*SwapChainFlags=*/0))
.WillByDefault(testing::Return(E_FAIL));
// Do first resize and ensure it is successful. We don't expect ResizeBuffers
// to be called here as it's the first Resize and the SwapChain will be
// initialized in the scall.
DoResize(kTestSize);
// SwapChain should be initialized, resulting in a ResizeBuffers call; which
// should fail triggering a crash.
EXPECT_DEATH(device_->Resize(gfx::Size(1180, 620), 1.f), "");
}
// Test that ResizeDelegated results in a process crash if
// CreateSwapChainForComposition fails.
TEST_F(SoftwareOutputDeviceWinSwapChainDeathTest,
CreateSwapSchainForCompositionFailure) {
ON_CALL(*mock_dcomp_device_.Get(), CreateTargetForHwnd)
.WillByDefault(
media::SetComPointeeAndReturnOk<2>(mock_dcomp_target_.Get()));
ON_CALL(*mock_dcomp_device_.Get(), CreateVisual)
.WillByDefault(
media::SetComPointeeAndReturnOk<0>(mock_dcomp_visual_.Get()));
ON_CALL(*mock_dxgi_factory_.Get(), CreateSwapChainForComposition)
.WillByDefault(testing::Return(E_OUTOFMEMORY));
EXPECT_DEATH(device_->Resize(kTestSize, 1.f), "");
}
} // namespace viz
|