| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
| 7 #include "base/memory/discardable_memory.h" | 7 #include "base/memory/discardable_memory.h" |
| 8 #include "base/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
| 9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
| 10 #include "content/child/child_discardable_shared_memory_manager.h" | 10 #include "content/child/child_discardable_shared_memory_manager.h" |
| 11 #include "content/child/child_gpu_memory_buffer_manager.h" | |
| 12 #include "content/child/child_thread_impl.h" | 11 #include "content/child/child_thread_impl.h" |
| 13 #include "content/common/gpu/client/gpu_memory_buffer_impl.h" | |
| 14 #include "content/common/host_discardable_shared_memory_manager.h" | 12 #include "content/common/host_discardable_shared_memory_manager.h" |
| 15 #include "content/public/common/content_switches.h" | 13 #include "content/public/common/content_switches.h" |
| 16 #include "content/public/test/content_browser_test.h" | 14 #include "content/public/test/content_browser_test.h" |
| 17 #include "content/public/test/content_browser_test_utils.h" | 15 #include "content/public/test/content_browser_test_utils.h" |
| 18 #include "content/shell/browser/shell.h" | 16 #include "content/shell/browser/shell.h" |
| 19 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 20 | 18 |
| 21 namespace content { | 19 namespace content { |
| 22 namespace { | |
| 23 | 20 |
| 24 class ChildThreadImplBrowserTest : public ContentBrowserTest { | 21 class ChildThreadImplBrowserTest : public ContentBrowserTest { |
| 25 public: | 22 public: |
| 26 ChildThreadImplBrowserTest() | 23 ChildThreadImplBrowserTest() |
| 27 : child_gpu_memory_buffer_manager_(nullptr), | 24 : child_discardable_shared_memory_manager_(nullptr) {} |
| 28 child_discardable_shared_memory_manager_(nullptr) {} | |
| 29 | 25 |
| 30 // Overridden from BrowserTestBase: | 26 // Overridden from BrowserTestBase: |
| 31 void SetUpCommandLine(base::CommandLine* command_line) override { | 27 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 32 command_line->AppendSwitch(switches::kSingleProcess); | 28 command_line->AppendSwitch(switches::kSingleProcess); |
| 33 } | 29 } |
| 34 void SetUpOnMainThread() override { | 30 void SetUpOnMainThread() override { |
| 35 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); | 31 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); |
| 36 PostTaskToInProcessRendererAndWait( | 32 PostTaskToInProcessRendererAndWait( |
| 37 base::Bind(&ChildThreadImplBrowserTest::SetUpOnChildThread, this)); | 33 base::Bind(&ChildThreadImplBrowserTest::SetUpOnChildThread, this)); |
| 38 } | 34 } |
| 39 | 35 |
| 40 ChildGpuMemoryBufferManager* child_gpu_memory_buffer_manager() { | |
| 41 return child_gpu_memory_buffer_manager_; | |
| 42 } | |
| 43 | |
| 44 ChildDiscardableSharedMemoryManager* | 36 ChildDiscardableSharedMemoryManager* |
| 45 child_discardable_shared_memory_manager() { | 37 child_discardable_shared_memory_manager() { |
| 46 return child_discardable_shared_memory_manager_; | 38 return child_discardable_shared_memory_manager_; |
| 47 } | 39 } |
| 48 | 40 |
| 49 private: | 41 private: |
| 50 void SetUpOnChildThread() { | 42 void SetUpOnChildThread() { |
| 51 child_gpu_memory_buffer_manager_ = | |
| 52 ChildThreadImpl::current()->gpu_memory_buffer_manager(); | |
| 53 child_discardable_shared_memory_manager_ = | 43 child_discardable_shared_memory_manager_ = |
| 54 ChildThreadImpl::current()->discardable_shared_memory_manager(); | 44 ChildThreadImpl::current()->discardable_shared_memory_manager(); |
| 55 } | 45 } |
| 56 | 46 |
| 57 ChildGpuMemoryBufferManager* child_gpu_memory_buffer_manager_; | |
| 58 ChildDiscardableSharedMemoryManager* child_discardable_shared_memory_manager_; | 47 ChildDiscardableSharedMemoryManager* child_discardable_shared_memory_manager_; |
| 59 }; | 48 }; |
| 60 | 49 |
| 61 IN_PROC_BROWSER_TEST_F(ChildThreadImplBrowserTest, | 50 IN_PROC_BROWSER_TEST_F(ChildThreadImplBrowserTest, |
| 62 DISABLED_LockDiscardableMemory) { | 51 DISABLED_LockDiscardableMemory) { |
| 63 const size_t kSize = 1024 * 1024; // 1MiB. | 52 const size_t kSize = 1024 * 1024; // 1MiB. |
| 64 | 53 |
| 65 scoped_ptr<base::DiscardableMemory> memory = | 54 scoped_ptr<base::DiscardableMemory> memory = |
| 66 child_discardable_shared_memory_manager() | 55 child_discardable_shared_memory_manager() |
| 67 ->AllocateLockedDiscardableMemory(kSize); | 56 ->AllocateLockedDiscardableMemory(kSize); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 base::TimeTicks end = | 106 base::TimeTicks end = |
| 118 base::TimeTicks::Now() + base::TimeDelta::FromSeconds(5); | 107 base::TimeTicks::Now() + base::TimeDelta::FromSeconds(5); |
| 119 while (base::TimeTicks::Now() < end) { | 108 while (base::TimeTicks::Now() < end) { |
| 120 if (!HostDiscardableSharedMemoryManager::current()->GetBytesAllocated()) | 109 if (!HostDiscardableSharedMemoryManager::current()->GetBytesAllocated()) |
| 121 break; | 110 break; |
| 122 } | 111 } |
| 123 | 112 |
| 124 EXPECT_LT(base::TimeTicks::Now(), end); | 113 EXPECT_LT(base::TimeTicks::Now(), end); |
| 125 } | 114 } |
| 126 | 115 |
| 127 enum NativeBufferFlag { kDisableNativeBuffers, kEnableNativeBuffers }; | 116 } // content |
| 128 | |
| 129 class ChildThreadImplGpuMemoryBufferBrowserTest | |
| 130 : public ChildThreadImplBrowserTest, | |
| 131 public testing::WithParamInterface< | |
| 132 ::testing::tuple<NativeBufferFlag, gfx::GpuMemoryBuffer::Format>> { | |
| 133 public: | |
| 134 // Overridden from BrowserTestBase: | |
| 135 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 136 ChildThreadImplBrowserTest::SetUpCommandLine(command_line); | |
| 137 NativeBufferFlag native_buffer_flag = ::testing::get<0>(GetParam()); | |
| 138 switch (native_buffer_flag) { | |
| 139 case kEnableNativeBuffers: | |
| 140 command_line->AppendSwitch(switches::kEnableNativeGpuMemoryBuffers); | |
| 141 break; | |
| 142 case kDisableNativeBuffers: | |
| 143 break; | |
| 144 } | |
| 145 } | |
| 146 }; | |
| 147 | |
| 148 IN_PROC_BROWSER_TEST_P(ChildThreadImplGpuMemoryBufferBrowserTest, Map) { | |
| 149 gfx::GpuMemoryBuffer::Format format = ::testing::get<1>(GetParam()); | |
| 150 gfx::Size buffer_size(4, 4); | |
| 151 | |
| 152 scoped_ptr<gfx::GpuMemoryBuffer> buffer = | |
| 153 child_gpu_memory_buffer_manager()->AllocateGpuMemoryBuffer( | |
| 154 buffer_size, format, gfx::GpuMemoryBuffer::MAP); | |
| 155 ASSERT_TRUE(buffer); | |
| 156 EXPECT_EQ(format, buffer->GetFormat()); | |
| 157 | |
| 158 size_t num_planes = | |
| 159 GpuMemoryBufferImpl::NumberOfPlanesForGpuMemoryBufferFormat(format); | |
| 160 | |
| 161 // Map buffer planes. | |
| 162 scoped_ptr<void* []> planes(new void* [num_planes]); | |
| 163 bool rv = buffer->Map(planes.get()); | |
| 164 ASSERT_TRUE(rv); | |
| 165 EXPECT_TRUE(buffer->IsMapped()); | |
| 166 | |
| 167 // Get strides. | |
| 168 scoped_ptr<int[]> strides(new int[num_planes]); | |
| 169 buffer->GetStride(strides.get()); | |
| 170 | |
| 171 // Write to buffer and check result. | |
| 172 for (size_t plane = 0; plane < num_planes; ++plane) { | |
| 173 size_t row_size_in_bytes = 0; | |
| 174 EXPECT_TRUE(GpuMemoryBufferImpl::RowSizeInBytes(buffer_size.width(), format, | |
| 175 plane, &row_size_in_bytes)); | |
| 176 | |
| 177 scoped_ptr<char[]> data(new char[row_size_in_bytes]); | |
| 178 memset(data.get(), 0x2a + plane, row_size_in_bytes); | |
| 179 size_t height = buffer_size.height() / | |
| 180 GpuMemoryBufferImpl::SubsamplingFactor(format, plane); | |
| 181 for (size_t y = 0; y < height; ++y) { | |
| 182 // Copy |data| to row |y| of |plane| and verify result. | |
| 183 memcpy(static_cast<char*>(planes[plane]) + y * strides[plane], data.get(), | |
| 184 row_size_in_bytes); | |
| 185 EXPECT_EQ(memcmp(static_cast<char*>(planes[plane]) + y * strides[plane], | |
| 186 data.get(), row_size_in_bytes), | |
| 187 0); | |
| 188 } | |
| 189 } | |
| 190 | |
| 191 buffer->Unmap(); | |
| 192 EXPECT_FALSE(buffer->IsMapped()); | |
| 193 } | |
| 194 | |
| 195 INSTANTIATE_TEST_CASE_P( | |
| 196 ChildThreadImplGpuMemoryBufferBrowserTests, | |
| 197 ChildThreadImplGpuMemoryBufferBrowserTest, | |
| 198 ::testing::Combine(::testing::Values(kDisableNativeBuffers, | |
| 199 kEnableNativeBuffers), | |
| 200 // These formats are guaranteed to work on all platforms. | |
| 201 ::testing::Values(gfx::GpuMemoryBuffer::R_8, | |
| 202 gfx::GpuMemoryBuffer::RGBA_4444, | |
| 203 gfx::GpuMemoryBuffer::RGBA_8888, | |
| 204 gfx::GpuMemoryBuffer::BGRA_8888, | |
| 205 gfx::GpuMemoryBuffer::YUV_420))); | |
| 206 | |
| 207 } // namespace | |
| 208 } // namespace content | |
| OLD | NEW |