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