OLD | NEW |
1 // Copyright 2014 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 "content/renderer/pepper/pepper_plugin_instance_throttler.h" | 5 #include "content/renderer/pepper/pepper_plugin_instance_metrics.h" |
6 | 6 |
7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
8 #include "base/metrics/sparse_histogram.h" | 8 #include "base/metrics/sparse_histogram.h" |
9 #include "base/time/time.h" | 9 #include "ppapi/shared_impl/ppapi_preferences.h" |
10 #include "content/public/common/content_constants.h" | 10 |
11 #include "content/public/renderer/render_thread.h" | 11 #if defined(OS_WIN) |
12 #include "third_party/WebKit/public/web/WebInputEvent.h" | 12 #include "base/win/windows_version.h" |
13 #include "ui/gfx/color_utils.h" | 13 #endif |
14 #include "url/gurl.h" | 14 |
| 15 #define UMA_HISTOGRAM_ASPECT_RATIO(name, width, height) \ |
| 16 UMA_HISTOGRAM_SPARSE_SLOWLY( \ |
| 17 name, (height) ? ((width)*100) / (height) : kInfiniteRatio); |
15 | 18 |
16 namespace content { | 19 namespace content { |
17 | 20 |
18 namespace { | 21 namespace { |
19 | 22 |
20 static const int kInfiniteRatio = 99999; | |
21 | |
22 #define UMA_HISTOGRAM_ASPECT_RATIO(name, width, height) \ | |
23 UMA_HISTOGRAM_SPARSE_SLOWLY( \ | |
24 name, (height) ? ((width)*100) / (height) : kInfiniteRatio); | |
25 | |
26 // Histogram tracking prevalence of tiny Flash instances. Units in pixels. | 23 // Histogram tracking prevalence of tiny Flash instances. Units in pixels. |
27 enum PluginFlashTinyContentSize { | 24 enum PluginFlashTinyContentSize { |
28 TINY_CONTENT_SIZE_1_1 = 0, | 25 TINY_CONTENT_SIZE_1_1 = 0, |
29 TINY_CONTENT_SIZE_5_5 = 1, | 26 TINY_CONTENT_SIZE_5_5 = 1, |
30 TINY_CONTENT_SIZE_10_10 = 2, | 27 TINY_CONTENT_SIZE_10_10 = 2, |
31 TINY_CONTENT_SIZE_LARGE = 3, | 28 TINY_CONTENT_SIZE_LARGE = 3, |
32 TINY_CONTENT_SIZE_NUM_ITEMS | 29 TINY_CONTENT_SIZE_NUM_ITEMS |
33 }; | 30 }; |
34 | 31 |
| 32 const int kInfiniteRatio = 99999; |
| 33 |
35 const char kFlashClickSizeAspectRatioHistogram[] = | 34 const char kFlashClickSizeAspectRatioHistogram[] = |
36 "Plugin.Flash.ClickSize.AspectRatio"; | 35 "Plugin.Flash.ClickSize.AspectRatio"; |
37 const char kFlashClickSizeHeightHistogram[] = "Plugin.Flash.ClickSize.Height"; | 36 const char kFlashClickSizeHeightHistogram[] = "Plugin.Flash.ClickSize.Height"; |
38 const char kFlashClickSizeWidthHistogram[] = "Plugin.Flash.ClickSize.Width"; | 37 const char kFlashClickSizeWidthHistogram[] = "Plugin.Flash.ClickSize.Width"; |
39 const char kFlashTinyContentSizeHistogram[] = "Plugin.Flash.TinyContentSize"; | 38 const char kFlashTinyContentSizeHistogram[] = "Plugin.Flash.TinyContentSize"; |
40 const char kPowerSaverUnthrottleHistogram[] = "Plugin.PowerSaver.Unthrottle"; | |
41 | 39 |
42 // Record size metrics for all Flash instances. | 40 } // namespace |
| 41 |
43 void RecordFlashSizeMetric(int width, int height) { | 42 void RecordFlashSizeMetric(int width, int height) { |
44 PluginFlashTinyContentSize size = TINY_CONTENT_SIZE_LARGE; | 43 PluginFlashTinyContentSize size = TINY_CONTENT_SIZE_LARGE; |
45 | 44 |
46 if (width <= 1 && height <= 1) | 45 if (width <= 1 && height <= 1) |
47 size = TINY_CONTENT_SIZE_1_1; | 46 size = TINY_CONTENT_SIZE_1_1; |
48 else if (width <= 5 && height <= 5) | 47 else if (width <= 5 && height <= 5) |
49 size = TINY_CONTENT_SIZE_5_5; | 48 size = TINY_CONTENT_SIZE_5_5; |
50 else if (width <= 10 && height <= 10) | 49 else if (width <= 10 && height <= 10) |
51 size = TINY_CONTENT_SIZE_10_10; | 50 size = TINY_CONTENT_SIZE_10_10; |
52 | 51 |
53 UMA_HISTOGRAM_ENUMERATION(kFlashTinyContentSizeHistogram, size, | 52 UMA_HISTOGRAM_ENUMERATION(kFlashTinyContentSizeHistogram, size, |
54 TINY_CONTENT_SIZE_NUM_ITEMS); | 53 TINY_CONTENT_SIZE_NUM_ITEMS); |
55 } | 54 } |
56 | 55 |
57 void RecordUnthrottleMethodMetric( | |
58 PepperPluginInstanceThrottler::PowerSaverUnthrottleMethod method) { | |
59 UMA_HISTOGRAM_ENUMERATION( | |
60 kPowerSaverUnthrottleHistogram, method, | |
61 PepperPluginInstanceThrottler::UNTHROTTLE_METHOD_NUM_ITEMS); | |
62 } | |
63 | |
64 // Records size metrics for Flash instances that are clicked. | |
65 void RecordFlashClickSizeMetric(int width, int height) { | 56 void RecordFlashClickSizeMetric(int width, int height) { |
66 base::HistogramBase* width_histogram = base::LinearHistogram::FactoryGet( | 57 base::HistogramBase* width_histogram = base::LinearHistogram::FactoryGet( |
67 kFlashClickSizeWidthHistogram, | 58 kFlashClickSizeWidthHistogram, |
68 0, // minimum width | 59 0, // minimum width |
69 500, // maximum width | 60 500, // maximum width |
70 100, // number of buckets. | 61 100, // number of buckets. |
71 base::HistogramBase::kUmaTargetedHistogramFlag); | 62 base::HistogramBase::kUmaTargetedHistogramFlag); |
72 width_histogram->Add(width); | 63 width_histogram->Add(width); |
73 | 64 |
74 base::HistogramBase* height_histogram = base::LinearHistogram::FactoryGet( | 65 base::HistogramBase* height_histogram = base::LinearHistogram::FactoryGet( |
75 kFlashClickSizeHeightHistogram, | 66 kFlashClickSizeHeightHistogram, |
76 0, // minimum height | 67 0, // minimum height |
77 400, // maximum height | 68 400, // maximum height |
78 100, // number of buckets. | 69 100, // number of buckets. |
79 base::HistogramBase::kUmaTargetedHistogramFlag); | 70 base::HistogramBase::kUmaTargetedHistogramFlag); |
80 height_histogram->Add(height); | 71 height_histogram->Add(height); |
81 | 72 |
82 UMA_HISTOGRAM_ASPECT_RATIO(kFlashClickSizeAspectRatioHistogram, width, | 73 UMA_HISTOGRAM_ASPECT_RATIO(kFlashClickSizeAspectRatioHistogram, width, |
83 height); | 74 height); |
84 } | 75 } |
85 | 76 |
86 // When we give up waiting for a suitable preview frame, and simply suspend | 77 void SetGPUHistogram(const ppapi::Preferences& prefs, |
87 // the plugin where it's at. In milliseconds. | 78 const std::vector<std::string>& arg_names, |
88 const int kThrottleTimeout = 5000; | 79 const std::vector<std::string>& arg_values) { |
| 80 // Calculate a histogram to let us determine how likely people are to try to |
| 81 // run Stage3D content on machines that have it blacklisted. |
| 82 #if defined(OS_WIN) |
| 83 bool needs_gpu = false; |
| 84 bool is_xp = base::win::GetVersion() <= base::win::VERSION_XP; |
89 | 85 |
90 // Threshold for 'boring' score to accept a frame as good enough to be a | 86 for (size_t i = 0; i < arg_names.size(); i++) { |
91 // representative keyframe. Units are the ratio of all pixels that are within | 87 if (arg_names[i] == "wmode") { |
92 // the most common luma bin. The same threshold is used for history thumbnails. | 88 // In theory content other than Flash could have a "wmode" argument, |
93 const double kAcceptableFrameMaximumBoringness = 0.94; | 89 // but that's pretty unlikely. |
94 | 90 if (arg_values[i] == "direct" || arg_values[i] == "gpu") |
95 const int kMinimumConsecutiveInterestingFrames = 4; | 91 needs_gpu = true; |
96 | 92 break; |
97 } // namespace | |
98 | |
99 PepperPluginInstanceThrottler::PepperPluginInstanceThrottler( | |
100 RenderFrame* frame, | |
101 const blink::WebRect& bounds, | |
102 bool is_flash_plugin, | |
103 const GURL& plugin_url, | |
104 RenderFrame::PluginPowerSaverMode power_saver_mode, | |
105 const base::Closure& throttle_change_callback) | |
106 : bounds_(bounds), | |
107 throttle_change_callback_(throttle_change_callback), | |
108 is_flash_plugin_(is_flash_plugin), | |
109 needs_representative_keyframe_(false), | |
110 consecutive_interesting_frames_(0), | |
111 has_been_clicked_(false), | |
112 power_saver_enabled_(false), | |
113 is_peripheral_content_(power_saver_mode != | |
114 RenderFrame::POWER_SAVER_MODE_ESSENTIAL), | |
115 plugin_throttled_(false), | |
116 weak_factory_(this) { | |
117 if (is_flash_plugin_ && RenderThread::Get()) { | |
118 RenderThread::Get()->RecordAction( | |
119 base::UserMetricsAction("Flash.PluginInstanceCreated")); | |
120 RecordFlashSizeMetric(bounds.width, bounds.height); | |
121 } | |
122 | |
123 power_saver_enabled_ = | |
124 is_flash_plugin_ && | |
125 power_saver_mode == RenderFrame::POWER_SAVER_MODE_PERIPHERAL_THROTTLED; | |
126 | |
127 GURL content_origin = plugin_url.GetOrigin(); | |
128 | |
129 // To collect UMAs, register peripheral content even if power saver disabled. | |
130 if (frame) { | |
131 frame->RegisterPeripheralPlugin( | |
132 content_origin, | |
133 base::Bind(&PepperPluginInstanceThrottler::DisablePowerSaver, | |
134 weak_factory_.GetWeakPtr(), UNTHROTTLE_METHOD_BY_WHITELIST)); | |
135 } | |
136 | |
137 if (power_saver_enabled_) { | |
138 needs_representative_keyframe_ = true; | |
139 base::MessageLoop::current()->PostDelayedTask( | |
140 FROM_HERE, | |
141 base::Bind(&PepperPluginInstanceThrottler::SetPluginThrottled, | |
142 weak_factory_.GetWeakPtr(), true /* throttled */), | |
143 base::TimeDelta::FromMilliseconds(kThrottleTimeout)); | |
144 } | |
145 } | |
146 | |
147 PepperPluginInstanceThrottler::~PepperPluginInstanceThrottler() { | |
148 } | |
149 | |
150 void PepperPluginInstanceThrottler::OnImageFlush(const SkBitmap* bitmap) { | |
151 if (!needs_representative_keyframe_ || !bitmap) | |
152 return; | |
153 | |
154 double boring_score = color_utils::CalculateBoringScore(*bitmap); | |
155 if (boring_score <= kAcceptableFrameMaximumBoringness) | |
156 ++consecutive_interesting_frames_; | |
157 else | |
158 consecutive_interesting_frames_ = 0; | |
159 | |
160 if (consecutive_interesting_frames_ >= kMinimumConsecutiveInterestingFrames) | |
161 SetPluginThrottled(true); | |
162 } | |
163 | |
164 bool PepperPluginInstanceThrottler::ConsumeInputEvent( | |
165 const blink::WebInputEvent& event) { | |
166 // Always allow right-clicks through so users may verify it's a plug-in. | |
167 // TODO(tommycli): We should instead show a custom context menu (probably | |
168 // using PluginPlaceholder) so users aren't confused and try to click the | |
169 // Flash-internal 'Play' menu item. This is a stopgap solution. | |
170 if (event.modifiers & blink::WebInputEvent::Modifiers::RightButtonDown) | |
171 return false; | |
172 | |
173 if (!has_been_clicked_ && is_flash_plugin_ && | |
174 event.type == blink::WebInputEvent::MouseDown && | |
175 (event.modifiers & blink::WebInputEvent::LeftButtonDown)) { | |
176 has_been_clicked_ = true; | |
177 RecordFlashClickSizeMetric(bounds_.width, bounds_.height); | |
178 } | |
179 | |
180 if (is_peripheral_content_ && event.type == blink::WebInputEvent::MouseUp && | |
181 (event.modifiers & blink::WebInputEvent::LeftButtonDown)) { | |
182 is_peripheral_content_ = false; | |
183 power_saver_enabled_ = false; | |
184 needs_representative_keyframe_ = false; | |
185 | |
186 RecordUnthrottleMethodMetric(UNTHROTTLE_METHOD_BY_CLICK); | |
187 | |
188 if (plugin_throttled_) { | |
189 SetPluginThrottled(false /* throttled */); | |
190 return true; | |
191 } | 93 } |
192 } | 94 } |
193 | 95 // 0 : No 3D content and GPU is blacklisted |
194 return plugin_throttled_; | 96 // 1 : No 3D content and GPU is not blacklisted |
195 } | 97 // 2 : 3D content but GPU is blacklisted |
196 | 98 // 3 : 3D content and GPU is not blacklisted |
197 void PepperPluginInstanceThrottler::DisablePowerSaver( | 99 // 4 : No 3D content and GPU is blacklisted on XP |
198 PowerSaverUnthrottleMethod method) { | 100 // 5 : No 3D content and GPU is not blacklisted on XP |
199 if (!is_peripheral_content_) | 101 // 6 : 3D content but GPU is blacklisted on XP |
200 return; | 102 // 7 : 3D content and GPU is not blacklisted on XP |
201 | 103 UMA_HISTOGRAM_ENUMERATION( |
202 is_peripheral_content_ = false; | 104 "Flash.UsesGPU", is_xp * 4 + needs_gpu * 2 + prefs.is_webgl_supported, 8); |
203 power_saver_enabled_ = false; | 105 #endif |
204 SetPluginThrottled(false); | |
205 | |
206 RecordUnthrottleMethodMetric(method); | |
207 } | |
208 | |
209 void PepperPluginInstanceThrottler::SetPluginThrottled(bool throttled) { | |
210 // Do not throttle if we've already disabled power saver. | |
211 if (!power_saver_enabled_ && throttled) | |
212 return; | |
213 | |
214 // Once we change the throttle state, we will never need the snapshot again. | |
215 needs_representative_keyframe_ = false; | |
216 | |
217 plugin_throttled_ = throttled; | |
218 throttle_change_callback_.Run(); | |
219 } | 106 } |
220 | 107 |
221 } // namespace content | 108 } // namespace content |
OLD | NEW |