OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "gpu/ipc/service/direct_composition_surface_win.h" | 5 #include "gpu/ipc/service/direct_composition_surface_win.h" |
6 #include "base/memory/ref_counted_memory.h" | 6 #include "base/memory/ref_counted_memory.h" |
7 #include "base/memory/weak_ptr.h" | 7 #include "base/memory/weak_ptr.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 desc.Width = size.width(); | 106 desc.Width = size.width(); |
107 desc.Height = size.height(); | 107 desc.Height = size.height(); |
108 desc.MipLevels = 1; | 108 desc.MipLevels = 1; |
109 desc.ArraySize = 1; | 109 desc.ArraySize = 1; |
110 desc.Format = DXGI_FORMAT_NV12; | 110 desc.Format = DXGI_FORMAT_NV12; |
111 desc.Usage = D3D11_USAGE_DEFAULT; | 111 desc.Usage = D3D11_USAGE_DEFAULT; |
112 desc.SampleDesc.Count = 1; | 112 desc.SampleDesc.Count = 1; |
113 desc.BindFlags = 0; | 113 desc.BindFlags = 0; |
114 | 114 |
115 std::vector<char> image_data(size.width() * size.height() * 3 / 2); | 115 std::vector<char> image_data(size.width() * size.height() * 3 / 2); |
116 // Y, U, and V should all be Oxff. Output color should be pink. | 116 // Y, U, and V should all be 160. Output color should be pink. |
117 memset(&image_data[0], 0xff, size.width() * size.height() * 3 / 2); | 117 memset(&image_data[0], 160, size.width() * size.height() * 3 / 2); |
118 | 118 |
119 D3D11_SUBRESOURCE_DATA data = {}; | 119 D3D11_SUBRESOURCE_DATA data = {}; |
120 data.pSysMem = (const void*)&image_data[0]; | 120 data.pSysMem = (const void*)&image_data[0]; |
121 data.SysMemPitch = size.width(); | 121 data.SysMemPitch = size.width(); |
122 | 122 |
123 base::win::ScopedComPtr<ID3D11Texture2D> texture; | 123 base::win::ScopedComPtr<ID3D11Texture2D> texture; |
124 HRESULT hr = | 124 HRESULT hr = |
125 d3d11_device->CreateTexture2D(&desc, &data, texture.GetAddressOf()); | 125 d3d11_device->CreateTexture2D(&desc, &data, texture.GetAddressOf()); |
126 CHECK(SUCCEEDED(hr)); | 126 CHECK(SUCCEEDED(hr)); |
127 return texture; | 127 return texture; |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 base::win::ScopedComPtr<ID3D11Texture2D> texture = | 309 base::win::ScopedComPtr<ID3D11Texture2D> texture = |
310 CreateNV12Texture(d3d11_device, texture_size); | 310 CreateNV12Texture(d3d11_device, texture_size); |
311 | 311 |
312 scoped_refptr<gl::GLImageDXGI> image_dxgi( | 312 scoped_refptr<gl::GLImageDXGI> image_dxgi( |
313 new gl::GLImageDXGI(texture_size, nullptr)); | 313 new gl::GLImageDXGI(texture_size, nullptr)); |
314 image_dxgi->SetTexture(texture, 0); | 314 image_dxgi->SetTexture(texture, 0); |
315 | 315 |
316 ui::DCRendererLayerParams params( | 316 ui::DCRendererLayerParams params( |
317 false, gfx::Rect(), 1, gfx::Transform(), | 317 false, gfx::Rect(), 1, gfx::Transform(), |
318 std::vector<scoped_refptr<gl::GLImage>>{image_dxgi}, | 318 std::vector<scoped_refptr<gl::GLImage>>{image_dxgi}, |
319 gfx::RectF(gfx::Rect(texture_size)), gfx::Rect(window_size), 0, 0, 1.0, | 319 gfx::RectF(gfx::Rect(texture_size)), gfx::Rect(window_size), 0, 0, 1.0, 0, |
320 0); | 320 gfx::ColorSpace::CreateREC709()); |
321 surface->ScheduleDCLayer(params); | 321 surface->ScheduleDCLayer(params); |
322 | 322 |
323 base::win::ScopedComPtr<IDXGISwapChain1> swap_chain = | 323 base::win::ScopedComPtr<IDXGISwapChain1> swap_chain = |
324 surface->GetLayerSwapChainForTesting(1); | 324 surface->GetLayerSwapChainForTesting(1); |
325 ASSERT_FALSE(swap_chain); | 325 ASSERT_FALSE(swap_chain); |
326 | 326 |
327 EXPECT_EQ(gfx::SwapResult::SWAP_ACK, surface->SwapBuffers()); | 327 EXPECT_EQ(gfx::SwapResult::SWAP_ACK, surface->SwapBuffers()); |
328 | 328 |
329 swap_chain = surface->GetLayerSwapChainForTesting(1); | 329 swap_chain = surface->GetLayerSwapChainForTesting(1); |
330 ASSERT_TRUE(swap_chain); | 330 ASSERT_TRUE(swap_chain); |
(...skipping 14 matching lines...) Expand all Loading... |
345 | 345 |
346 // It's the same image, so it should have the same swapchain. | 346 // It's the same image, so it should have the same swapchain. |
347 EXPECT_TRUE(SUCCEEDED(swap_chain->GetLastPresentCount(&last_present_count))); | 347 EXPECT_TRUE(SUCCEEDED(swap_chain->GetLastPresentCount(&last_present_count))); |
348 EXPECT_EQ(2u, last_present_count); | 348 EXPECT_EQ(2u, last_present_count); |
349 | 349 |
350 // The size of the swapchain changed, so it should be recreated. | 350 // The size of the swapchain changed, so it should be recreated. |
351 ui::DCRendererLayerParams params2( | 351 ui::DCRendererLayerParams params2( |
352 false, gfx::Rect(), 1, gfx::Transform(), | 352 false, gfx::Rect(), 1, gfx::Transform(), |
353 std::vector<scoped_refptr<gl::GLImage>>{image_dxgi}, | 353 std::vector<scoped_refptr<gl::GLImage>>{image_dxgi}, |
354 gfx::RectF(gfx::Rect(texture_size)), gfx::Rect(0, 0, 25, 25), 0, 0, 1.0, | 354 gfx::RectF(gfx::Rect(texture_size)), gfx::Rect(0, 0, 25, 25), 0, 0, 1.0, |
355 0); | 355 0, gfx::ColorSpace::CreateREC709()); |
356 surface->ScheduleDCLayer(params2); | 356 surface->ScheduleDCLayer(params2); |
357 | 357 |
358 EXPECT_EQ(gfx::SwapResult::SWAP_ACK, surface->SwapBuffers()); | 358 EXPECT_EQ(gfx::SwapResult::SWAP_ACK, surface->SwapBuffers()); |
359 | 359 |
360 base::win::ScopedComPtr<IDXGISwapChain1> swap_chain3 = | 360 base::win::ScopedComPtr<IDXGISwapChain1> swap_chain3 = |
361 surface->GetLayerSwapChainForTesting(1); | 361 surface->GetLayerSwapChainForTesting(1); |
362 EXPECT_NE(swap_chain2, swap_chain3); | 362 EXPECT_NE(swap_chain2, swap_chain3); |
363 | 363 |
364 context = nullptr; | 364 context = nullptr; |
365 DestroySurface(std::move(surface)); | 365 DestroySurface(std::move(surface)); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 bool AreColorsSimilar(int a, int b) { | 460 bool AreColorsSimilar(int a, int b) { |
461 // The precise colors may differ depending on the video processor, so allow | 461 // The precise colors may differ depending on the video processor, so allow |
462 // a margin for error. | 462 // a margin for error. |
463 const int kMargin = 10; | 463 const int kMargin = 10; |
464 return abs(SkColorGetA(a) - SkColorGetA(b)) < kMargin && | 464 return abs(SkColorGetA(a) - SkColorGetA(b)) < kMargin && |
465 abs(SkColorGetR(a) - SkColorGetR(b)) < kMargin && | 465 abs(SkColorGetR(a) - SkColorGetR(b)) < kMargin && |
466 abs(SkColorGetG(a) - SkColorGetG(b)) < kMargin && | 466 abs(SkColorGetG(a) - SkColorGetG(b)) < kMargin && |
467 abs(SkColorGetB(a) - SkColorGetB(b)) < kMargin; | 467 abs(SkColorGetB(a) - SkColorGetB(b)) < kMargin; |
468 } | 468 } |
469 | 469 |
470 TEST_F(DirectCompositionPixelTest, VideoSwapchain) { | 470 class DirectCompositionVideoPixelTest : public DirectCompositionPixelTest { |
471 if (!CheckIfDCSupported()) | 471 protected: |
472 return; | 472 void TestVideo(const gfx::ColorSpace& color_space, SkColor expected_color) { |
473 InitializeSurface(); | 473 if (!CheckIfDCSupported()) |
474 surface_->SetEnableDCLayers(true); | 474 return; |
475 gfx::Size window_size(100, 100); | 475 InitializeSurface(); |
| 476 surface_->SetEnableDCLayers(true); |
| 477 gfx::Size window_size(100, 100); |
476 | 478 |
477 scoped_refptr<gl::GLContext> context = gl::init::CreateGLContext( | 479 scoped_refptr<gl::GLContext> context = gl::init::CreateGLContext( |
478 nullptr, surface_.get(), gl::GLContextAttribs()); | 480 nullptr, surface_.get(), gl::GLContextAttribs()); |
479 EXPECT_TRUE(surface_->Resize(window_size, 1.0, true)); | 481 EXPECT_TRUE(surface_->Resize(window_size, 1.0, true)); |
480 | 482 |
481 base::win::ScopedComPtr<ID3D11Device> d3d11_device = | 483 base::win::ScopedComPtr<ID3D11Device> d3d11_device = |
482 gl::QueryD3D11DeviceObjectFromANGLE(); | 484 gl::QueryD3D11DeviceObjectFromANGLE(); |
483 | 485 |
484 gfx::Size texture_size(50, 50); | 486 gfx::Size texture_size(50, 50); |
485 base::win::ScopedComPtr<ID3D11Texture2D> texture = | 487 base::win::ScopedComPtr<ID3D11Texture2D> texture = |
486 CreateNV12Texture(d3d11_device, texture_size); | 488 CreateNV12Texture(d3d11_device, texture_size); |
487 | 489 |
488 scoped_refptr<gl::GLImageDXGI> image_dxgi( | 490 scoped_refptr<gl::GLImageDXGI> image_dxgi( |
489 new gl::GLImageDXGI(texture_size, nullptr)); | 491 new gl::GLImageDXGI(texture_size, nullptr)); |
490 image_dxgi->SetTexture(texture, 0); | 492 image_dxgi->SetTexture(texture, 0); |
491 | 493 |
492 ui::DCRendererLayerParams params( | 494 ui::DCRendererLayerParams params( |
493 false, gfx::Rect(), 1, gfx::Transform(), | 495 false, gfx::Rect(), 1, gfx::Transform(), |
494 std::vector<scoped_refptr<gl::GLImage>>{image_dxgi}, | 496 std::vector<scoped_refptr<gl::GLImage>>{image_dxgi}, |
495 gfx::RectF(gfx::Rect(texture_size)), gfx::Rect(window_size), 0, 0, 1.0, | 497 gfx::RectF(gfx::Rect(texture_size)), gfx::Rect(window_size), 0, 0, 1.0, |
496 0); | 498 0, color_space); |
497 surface_->ScheduleDCLayer(params); | 499 surface_->ScheduleDCLayer(params); |
498 | 500 |
499 EXPECT_EQ(gfx::SwapResult::SWAP_ACK, surface_->SwapBuffers()); | 501 EXPECT_EQ(gfx::SwapResult::SWAP_ACK, surface_->SwapBuffers()); |
500 Sleep(1000); | 502 Sleep(1000); |
501 | 503 |
502 SkColor expected_color = SkColorSetRGB(0xff, 0xb7, 0xff); | 504 SkColor actual_color = |
503 SkColor actual_color = | 505 ReadBackWindowPixel(window_.hwnd(), gfx::Point(75, 75)); |
504 ReadBackWindowPixel(window_.hwnd(), gfx::Point(75, 75)); | 506 EXPECT_TRUE(AreColorsSimilar(expected_color, actual_color)) |
505 EXPECT_TRUE(AreColorsSimilar(expected_color, actual_color)) | 507 << std::hex << "Expected " << expected_color << " Actual " |
506 << std::hex << "Expected " << expected_color << " Actual " | 508 << actual_color; |
507 << actual_color; | |
508 | 509 |
509 context = nullptr; | 510 context = nullptr; |
510 DestroySurface(std::move(surface_)); | 511 DestroySurface(std::move(surface_)); |
| 512 } |
| 513 }; |
| 514 |
| 515 TEST_F(DirectCompositionVideoPixelTest, BT601) { |
| 516 TestVideo(gfx::ColorSpace::CreateREC601(), SkColorSetRGB(0xdb, 0x81, 0xe8)); |
| 517 } |
| 518 |
| 519 TEST_F(DirectCompositionVideoPixelTest, BT709) { |
| 520 TestVideo(gfx::ColorSpace::CreateREC709(), SkColorSetRGB(0xe1, 0x90, 0xeb)); |
| 521 } |
| 522 |
| 523 TEST_F(DirectCompositionVideoPixelTest, SRGB) { |
| 524 // SRGB doesn't make sense on an NV12 input, but don't crash. |
| 525 TestVideo(gfx::ColorSpace::CreateSRGB(), SkColorSetRGB(0xd7, 0x89, 0xe0)); |
| 526 } |
| 527 |
| 528 TEST_F(DirectCompositionVideoPixelTest, SCRGBLinear) { |
| 529 // SCRGB doesn't make sense on an NV12 input, but don't crash. |
| 530 TestVideo(gfx::ColorSpace::CreateSCRGBLinear(), |
| 531 SkColorSetRGB(0xd7, 0x89, 0xe0)); |
| 532 } |
| 533 TEST_F(DirectCompositionVideoPixelTest, InvalidColorSpace) { |
| 534 // Invalid color space should be treated as BT.709 |
| 535 TestVideo(gfx::ColorSpace(), SkColorSetRGB(0xe1, 0x90, 0xeb)); |
511 } | 536 } |
512 | 537 |
513 TEST_F(DirectCompositionPixelTest, SoftwareVideoSwapchain) { | 538 TEST_F(DirectCompositionPixelTest, SoftwareVideoSwapchain) { |
514 if (!CheckIfDCSupported()) | 539 if (!CheckIfDCSupported()) |
515 return; | 540 return; |
516 InitializeSurface(); | 541 InitializeSurface(); |
517 surface_->SetEnableDCLayers(true); | 542 surface_->SetEnableDCLayers(true); |
518 gfx::Size window_size(100, 100); | 543 gfx::Size window_size(100, 100); |
519 | 544 |
520 scoped_refptr<gl::GLContext> context = gl::init::CreateGLContext( | 545 scoped_refptr<gl::GLContext> context = gl::init::CreateGLContext( |
(...skipping 17 matching lines...) Expand all Loading... |
538 y_image->Initialize(new base::RefCountedBytes(y_data), | 563 y_image->Initialize(new base::RefCountedBytes(y_data), |
539 gfx::BufferFormat::R_8); | 564 gfx::BufferFormat::R_8); |
540 scoped_refptr<gl::GLImageRefCountedMemory> uv_image( | 565 scoped_refptr<gl::GLImageRefCountedMemory> uv_image( |
541 new gl::GLImageRefCountedMemory(uv_size, GL_BGRA_EXT)); | 566 new gl::GLImageRefCountedMemory(uv_size, GL_BGRA_EXT)); |
542 uv_image->Initialize(new base::RefCountedBytes(uv_data), | 567 uv_image->Initialize(new base::RefCountedBytes(uv_data), |
543 gfx::BufferFormat::RG_88); | 568 gfx::BufferFormat::RG_88); |
544 | 569 |
545 ui::DCRendererLayerParams params( | 570 ui::DCRendererLayerParams params( |
546 false, gfx::Rect(), 1, gfx::Transform(), | 571 false, gfx::Rect(), 1, gfx::Transform(), |
547 std::vector<scoped_refptr<gl::GLImage>>{y_image, uv_image}, | 572 std::vector<scoped_refptr<gl::GLImage>>{y_image, uv_image}, |
548 gfx::RectF(gfx::Rect(y_size)), gfx::Rect(window_size), 0, 0, 1.0, 0); | 573 gfx::RectF(gfx::Rect(y_size)), gfx::Rect(window_size), 0, 0, 1.0, 0, |
| 574 gfx::ColorSpace::CreateREC709()); |
549 surface_->ScheduleDCLayer(params); | 575 surface_->ScheduleDCLayer(params); |
550 | 576 |
551 EXPECT_EQ(gfx::SwapResult::SWAP_ACK, surface_->SwapBuffers()); | 577 EXPECT_EQ(gfx::SwapResult::SWAP_ACK, surface_->SwapBuffers()); |
552 Sleep(1000); | 578 Sleep(1000); |
553 | 579 |
554 SkColor expected_color = SkColorSetRGB(0xff, 0xb7, 0xff); | 580 SkColor expected_color = SkColorSetRGB(0xff, 0xb7, 0xff); |
555 SkColor actual_color = | 581 SkColor actual_color = |
556 ReadBackWindowPixel(window_.hwnd(), gfx::Point(75, 75)); | 582 ReadBackWindowPixel(window_.hwnd(), gfx::Point(75, 75)); |
557 EXPECT_TRUE(AreColorsSimilar(expected_color, actual_color)) | 583 EXPECT_TRUE(AreColorsSimilar(expected_color, actual_color)) |
558 << std::hex << "Expected " << expected_color << " Actual " | 584 << std::hex << "Expected " << expected_color << " Actual " |
559 << actual_color; | 585 << actual_color; |
560 | 586 |
561 context = nullptr; | 587 context = nullptr; |
562 DestroySurface(std::move(surface_)); | 588 DestroySurface(std::move(surface_)); |
563 } | 589 } |
| 590 |
564 } // namespace | 591 } // namespace |
565 } // namespace gpu | 592 } // namespace gpu |
OLD | NEW |