OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/callback.h" |
| 6 #include "base/command_line.h" |
| 7 #include "base/path_service.h" |
| 8 #include "content/public/browser/gpu_data_manager.h" |
| 9 #include "content/public/browser/gpu_data_manager_observer.h" |
| 10 #include "content/public/browser/web_contents.h" |
| 11 #include "content/public/common/content_paths.h" |
| 12 #include "content/public/common/content_switches.h" |
| 13 #include "content/public/test/browser_test_utils.h" |
| 14 #include "content/public/test/test_utils.h" |
| 15 #include "content/shell/shell.h" |
| 16 #include "content/test/content_browser_test.h" |
| 17 #include "content/test/content_browser_test_utils.h" |
| 18 #include "gpu/command_buffer/service/gpu_switches.h" |
| 19 #include "net/base/net_util.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 // Observer to report GPU memory usage when requested. |
| 24 class GpuMemoryBytesAllocatedObserver |
| 25 : public content::GpuDataManagerObserver { |
| 26 public: |
| 27 GpuMemoryBytesAllocatedObserver() |
| 28 : bytes_allocated_(0) { |
| 29 } |
| 30 |
| 31 virtual ~GpuMemoryBytesAllocatedObserver() { |
| 32 } |
| 33 |
| 34 virtual void OnGpuInfoUpdate() OVERRIDE {} |
| 35 |
| 36 virtual void OnVideoMemoryUsageStatsUpdate( |
| 37 const content::GPUVideoMemoryUsageStats& video_memory_usage_stats) |
| 38 OVERRIDE { |
| 39 bytes_allocated_ = video_memory_usage_stats.bytes_allocated; |
| 40 message_loop_runner_->Quit(); |
| 41 } |
| 42 |
| 43 size_t GetBytesAllocated() { |
| 44 message_loop_runner_ = new content::MessageLoopRunner; |
| 45 content::GpuDataManager::GetInstance()->AddObserver(this); |
| 46 content::GpuDataManager::GetInstance()-> |
| 47 RequestVideoMemoryUsageStatsUpdate(); |
| 48 message_loop_runner_->Run(); |
| 49 content::GpuDataManager::GetInstance()->RemoveObserver(this); |
| 50 message_loop_runner_ = NULL; |
| 51 return bytes_allocated_; |
| 52 } |
| 53 |
| 54 private: |
| 55 size_t bytes_allocated_; |
| 56 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
| 57 }; |
| 58 |
| 59 class GpuMemoryTest : public content::ContentBrowserTest { |
| 60 public: |
| 61 GpuMemoryTest() |
| 62 : allow_tests_to_run_(false), |
| 63 has_used_first_shell_(false) { |
| 64 } |
| 65 virtual ~GpuMemoryTest() { |
| 66 } |
| 67 |
| 68 virtual void SetUpInProcessBrowserTestFixture() { |
| 69 FilePath test_dir; |
| 70 ASSERT_TRUE(PathService::Get(content::DIR_TEST_DATA, &test_dir)); |
| 71 gpu_test_dir_ = test_dir.AppendASCII("gpu"); |
| 72 } |
| 73 |
| 74 virtual void SetUpCommandLine(CommandLine* command_line) { |
| 75 command_line->AppendSwitch(switches::kEnableLogging); |
| 76 command_line->AppendSwitch(switches::kForceCompositingMode); |
| 77 // TODO: Use switches::kForceGpuMemAvailableMb to fix the memory limit |
| 78 // (this is failing some bots (probably because of differing meanings of |
| 79 // GPU_EXPORT) |
| 80 // Only run this on GPU bots for now. These tests should work with |
| 81 // any GPU process, but may be slow. |
| 82 if (command_line->HasSwitch(switches::kUseGpuInTests)) { |
| 83 allow_tests_to_run_ = true; |
| 84 } |
| 85 // Don't enable these tests on Android just yet (they use lots of memory and |
| 86 // may not be stable). |
| 87 #if defined(OS_ANDROID) |
| 88 allow_tests_to_run_ = false; |
| 89 #endif |
| 90 } |
| 91 |
| 92 enum PageType { |
| 93 PAGE_CSS3D, |
| 94 PAGE_WEBGL, |
| 95 }; |
| 96 |
| 97 // Load a page and consume a specified amount of GPU memory. |
| 98 void LoadPage(content::Shell* shell_to_load, |
| 99 PageType page_type, |
| 100 size_t mb_to_use) { |
| 101 FilePath url; |
| 102 switch (page_type) { |
| 103 case PAGE_CSS3D: |
| 104 url = gpu_test_dir_.AppendASCII("mem_css3d.html"); |
| 105 break; |
| 106 case PAGE_WEBGL: |
| 107 url = gpu_test_dir_.AppendASCII("mem_webgl.html"); |
| 108 break; |
| 109 } |
| 110 |
| 111 content::NavigateToURL(shell_to_load, net::FilePathToFileURL(url)); |
| 112 std::ostringstream js_call; |
| 113 js_call << "useGpuMemory("; |
| 114 js_call << mb_to_use; |
| 115 js_call << ");"; |
| 116 content::DOMMessageQueue message_queue; |
| 117 std::string message; |
| 118 ASSERT_TRUE(content::ExecuteScript( |
| 119 shell_to_load->web_contents(), js_call.str())); |
| 120 ASSERT_TRUE(message_queue.WaitForMessage(&message)); |
| 121 EXPECT_EQ("\"DONE_USE_GPU_MEMORY\"", message); |
| 122 } |
| 123 |
| 124 // Create a new tab. |
| 125 content::Shell* CreateNewTab() { |
| 126 // The ContentBrowserTest will create one shell by default, use that one |
| 127 // first so that we don't confuse the memory manager into thinking there |
| 128 // are more windows than there are. |
| 129 content::Shell* new_shell = |
| 130 has_used_first_shell_ ? CreateBrowser() : shell(); |
| 131 has_used_first_shell_ = true; |
| 132 visible_shells_.insert(new_shell); |
| 133 return new_shell; |
| 134 } |
| 135 |
| 136 void SetTabBackgrounded(content::Shell* shell_to_background) { |
| 137 ASSERT_TRUE( |
| 138 visible_shells_.find(shell_to_background) != visible_shells_.end()); |
| 139 visible_shells_.erase(shell_to_background); |
| 140 shell_to_background->web_contents()->WasHidden(); |
| 141 } |
| 142 |
| 143 size_t GetMemoryUsageMbytes() { |
| 144 // TODO: This should wait until all effects of memory management complete. |
| 145 // We will need to wait until all |
| 146 // 1. pending commits from the main thread to the impl thread in the |
| 147 // compositor complete (for visible compositors). |
| 148 // 2. allocations that the renderer's impl thread will make due to the |
| 149 // compositor and WebGL are completed. |
| 150 // 3. pending GpuMemoryManager::Manage() calls to manage are made. |
| 151 // 4. renderers' OnMemoryAllocationChanged callbacks in response to |
| 152 // manager are made. |
| 153 // Each step in this sequence can cause trigger the next (as a 1-2-3-4-1 |
| 154 // cycle), so we will need to pump this cycle until it stabilizes. |
| 155 GpuMemoryBytesAllocatedObserver observer; |
| 156 observer.GetBytesAllocated(); |
| 157 return observer.GetBytesAllocated() / 1048576; |
| 158 } |
| 159 |
| 160 bool AllowTestsToRun() const { |
| 161 return allow_tests_to_run_; |
| 162 } |
| 163 |
| 164 private: |
| 165 bool allow_tests_to_run_; |
| 166 std::set<content::Shell*> visible_shells_; |
| 167 bool has_used_first_shell_; |
| 168 FilePath gpu_test_dir_; |
| 169 }; |
| 170 |
| 171 } // namespace |
OLD | NEW |