OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/renderer/render_thread_impl.h" | 5 #include "content/renderer/render_thread_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
14 #include "base/memory/discardable_memory.h" | 14 #include "base/memory/discardable_memory.h" |
15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
16 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
18 #include "base/threading/sequenced_worker_pool.h" | 18 #include "base/threading/sequenced_worker_pool.h" |
19 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
20 #include "cc/output/buffer_to_texture_target_map.h" | 20 #include "cc/output/buffer_to_texture_target_map.h" |
21 #include "components/discardable_memory/client/client_discardable_shared_memory_
manager.h" | |
22 #include "components/discardable_memory/service/discardable_shared_memory_manage
r.h" | |
23 #include "content/app/mojo/mojo_init.h" | 21 #include "content/app/mojo/mojo_init.h" |
24 #include "content/child/child_gpu_memory_buffer_manager.h" | 22 #include "content/child/child_gpu_memory_buffer_manager.h" |
25 #include "content/common/in_process_child_thread_params.h" | 23 #include "content/common/in_process_child_thread_params.h" |
26 #include "content/common/resource_messages.h" | 24 #include "content/common/resource_messages.h" |
27 #include "content/common/service_manager/child_connection.h" | 25 #include "content/common/service_manager/child_connection.h" |
28 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
29 #include "content/public/browser/content_browser_client.h" | 27 #include "content/public/browser/content_browser_client.h" |
30 #include "content/public/common/content_client.h" | 28 #include "content/public/common/content_client.h" |
31 #include "content/public/common/content_switches.h" | 29 #include "content/public/common/content_switches.h" |
32 #include "content/public/common/service_manager_connection.h" | 30 #include "content/public/common/service_manager_connection.h" |
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 ::testing::Combine(::testing::Values(kDisableNativeBuffers, | 379 ::testing::Combine(::testing::Values(kDisableNativeBuffers, |
382 kEnableNativeBuffers), | 380 kEnableNativeBuffers), |
383 // These formats are guaranteed to work on all platforms. | 381 // These formats are guaranteed to work on all platforms. |
384 ::testing::Values(gfx::BufferFormat::R_8, | 382 ::testing::Values(gfx::BufferFormat::R_8, |
385 gfx::BufferFormat::BGR_565, | 383 gfx::BufferFormat::BGR_565, |
386 gfx::BufferFormat::RGBA_4444, | 384 gfx::BufferFormat::RGBA_4444, |
387 gfx::BufferFormat::RGBA_8888, | 385 gfx::BufferFormat::RGBA_8888, |
388 gfx::BufferFormat::BGRA_8888, | 386 gfx::BufferFormat::BGRA_8888, |
389 gfx::BufferFormat::YVU_420))); | 387 gfx::BufferFormat::YVU_420))); |
390 | 388 |
391 class RenderThreadImplDiscardableMemoryBrowserTest : public ContentBrowserTest { | |
392 public: | |
393 RenderThreadImplDiscardableMemoryBrowserTest() | |
394 : child_discardable_shared_memory_manager_(nullptr) {} | |
395 | |
396 // Overridden from BrowserTestBase: | |
397 void SetUpCommandLine(base::CommandLine* command_line) override { | |
398 command_line->AppendSwitch(switches::kSingleProcess); | |
399 } | |
400 void SetUpOnMainThread() override { | |
401 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); | |
402 PostTaskToInProcessRendererAndWait(base::Bind( | |
403 &RenderThreadImplDiscardableMemoryBrowserTest::SetUpOnRenderThread, | |
404 base::Unretained(this))); | |
405 } | |
406 | |
407 discardable_memory::ClientDiscardableSharedMemoryManager* | |
408 child_discardable_shared_memory_manager() { | |
409 return child_discardable_shared_memory_manager_; | |
410 } | |
411 | |
412 private: | |
413 void SetUpOnRenderThread() { | |
414 child_discardable_shared_memory_manager_ = | |
415 RenderThreadImpl::current()->GetDiscardableSharedMemoryManagerForTest(); | |
416 } | |
417 | |
418 discardable_memory::ClientDiscardableSharedMemoryManager* | |
419 child_discardable_shared_memory_manager_; | |
420 }; | |
421 | |
422 IN_PROC_BROWSER_TEST_F(RenderThreadImplDiscardableMemoryBrowserTest, | |
423 LockDiscardableMemory) { | |
424 const size_t kSize = 1024 * 1024; // 1MiB. | |
425 | |
426 std::unique_ptr<base::DiscardableMemory> memory = | |
427 child_discardable_shared_memory_manager() | |
428 ->AllocateLockedDiscardableMemory(kSize); | |
429 | |
430 ASSERT_TRUE(memory); | |
431 void* addr = memory->data(); | |
432 ASSERT_NE(nullptr, addr); | |
433 | |
434 memory->Unlock(); | |
435 | |
436 // Purge all unlocked memory. | |
437 discardable_memory::DiscardableSharedMemoryManager::GetInstance() | |
438 ->SetMemoryLimit(0); | |
439 | |
440 // Should fail as memory should have been purged. | |
441 EXPECT_FALSE(memory->Lock()); | |
442 } | |
443 | |
444 IN_PROC_BROWSER_TEST_F(RenderThreadImplDiscardableMemoryBrowserTest, | |
445 DiscardableMemoryAddressSpace) { | |
446 const size_t kLargeSize = 4 * 1024 * 1024; // 4MiB. | |
447 const size_t kNumberOfInstances = 1024 + 1; // >4GiB total. | |
448 | |
449 ScopedVector<base::DiscardableMemory> instances; | |
450 for (size_t i = 0; i < kNumberOfInstances; ++i) { | |
451 std::unique_ptr<base::DiscardableMemory> memory = | |
452 child_discardable_shared_memory_manager() | |
453 ->AllocateLockedDiscardableMemory(kLargeSize); | |
454 ASSERT_TRUE(memory); | |
455 void* addr = memory->data(); | |
456 ASSERT_NE(nullptr, addr); | |
457 memory->Unlock(); | |
458 instances.push_back(std::move(memory)); | |
459 } | |
460 } | |
461 | |
462 IN_PROC_BROWSER_TEST_F(RenderThreadImplDiscardableMemoryBrowserTest, | |
463 ReleaseFreeDiscardableMemory) { | |
464 const size_t kSize = 1024 * 1024; // 1MiB. | |
465 | |
466 std::unique_ptr<base::DiscardableMemory> memory = | |
467 child_discardable_shared_memory_manager() | |
468 ->AllocateLockedDiscardableMemory(kSize); | |
469 | |
470 EXPECT_TRUE(memory); | |
471 memory.reset(); | |
472 | |
473 EXPECT_GE(discardable_memory::DiscardableSharedMemoryManager::GetInstance() | |
474 ->GetBytesAllocated(), | |
475 kSize); | |
476 | |
477 child_discardable_shared_memory_manager()->ReleaseFreeMemory(); | |
478 | |
479 // Busy wait for host memory usage to be reduced. | |
480 base::TimeTicks end = | |
481 base::TimeTicks::Now() + base::TimeDelta::FromSeconds(5); | |
482 while (base::TimeTicks::Now() < end) { | |
483 if (!discardable_memory::DiscardableSharedMemoryManager::GetInstance() | |
484 ->GetBytesAllocated()) | |
485 break; | |
486 } | |
487 | |
488 EXPECT_LT(base::TimeTicks::Now(), end); | |
489 } | |
490 | |
491 } // namespace | 389 } // namespace |
492 } // namespace content | 390 } // namespace content |
OLD | NEW |