Roll src/third_party/skia 2440fcd:4de8c3a
[chromium-blink-merge.git] / content / renderer / pepper / plugin_instance_throttler_impl.h
blobabecf5db21e9b524806705d054433851f5500324
1 // Copyright 2014 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.
5 #ifndef CONTENT_RENDERER_PEPPER_PLUGIN_INSTANCE_THROTTLER_IMPL_H_
6 #define CONTENT_RENDERER_PEPPER_PLUGIN_INSTANCE_THROTTLER_IMPL_H_
8 #include "base/macros.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/observer_list.h"
11 #include "base/timer/timer.h"
12 #include "content/common/content_export.h"
13 #include "content/public/renderer/plugin_instance_throttler.h"
14 #include "content/renderer/pepper/pepper_webplugin_impl.h"
15 #include "ppapi/shared_impl/ppb_view_shared.h"
16 #include "third_party/skia/include/core/SkBitmap.h"
17 #include "ui/gfx/geometry/size.h"
19 namespace blink {
20 class WebInputEvent;
21 struct WebRect;
24 namespace url {
25 class Origin;
28 namespace content {
30 class PepperWebPluginImpl;
31 class RenderFrameImpl;
33 class CONTENT_EXPORT PluginInstanceThrottlerImpl
34 : public PluginInstanceThrottler {
35 public:
36 PluginInstanceThrottlerImpl();
38 ~PluginInstanceThrottlerImpl() override;
40 // PluginInstanceThrottler implementation:
41 void AddObserver(Observer* observer) override;
42 void RemoveObserver(Observer* observer) override;
43 bool IsThrottled() const override;
44 bool IsHiddenForPlaceholder() const override;
45 void MarkPluginEssential(PowerSaverUnthrottleMethod method) override;
46 void SetHiddenForPlaceholder(bool hidden) override;
47 PepperWebPluginImpl* GetWebPlugin() const override;
48 const gfx::Size& GetSize() const override;
49 void NotifyAudioThrottled() override;
51 void SetWebPlugin(PepperWebPluginImpl* web_plugin);
53 bool needs_representative_keyframe() const {
54 return state_ == THROTTLER_STATE_AWAITING_KEYFRAME;
57 bool power_saver_enabled() const {
58 return state_ != THROTTLER_STATE_MARKED_ESSENTIAL;
61 void Initialize(RenderFrameImpl* frame,
62 const url::Origin& content_origin,
63 const std::string& plugin_module_name,
64 const gfx::Size& unobscured_size);
66 // Called when the plugin flushes it's graphics context. Supplies the
67 // throttler with a candidate to use as the representative keyframe.
68 void OnImageFlush(const SkBitmap* bitmap);
70 // Returns true if |event| was handled and shouldn't be further processed.
71 bool ConsumeInputEvent(const blink::WebInputEvent& event);
73 private:
74 friend class PluginInstanceThrottlerImplTest;
76 enum ThrottlerState {
77 // Plugin has been found to be peripheral, Plugin Power Saver is enabled,
78 // and throttler is awaiting a representative keyframe.
79 THROTTLER_STATE_AWAITING_KEYFRAME,
80 // A representative keyframe has been chosen and the plugin is throttled.
81 THROTTLER_STATE_PLUGIN_THROTTLED,
82 // Plugin instance has been marked essential.
83 THROTTLER_STATE_MARKED_ESSENTIAL,
86 // Maximum number of frames to examine for a suitable keyframe. After that, we
87 // simply suspend the plugin where it's at. Chosen arbitrarily.
88 static const int kMaximumFramesToExamine;
90 void AudioThrottledFrameTimeout();
91 void EngageThrottle();
93 ThrottlerState state_;
95 bool is_hidden_for_placeholder_;
97 PepperWebPluginImpl* web_plugin_;
99 // Holds a reference to the last received frame. This doesn't actually copy
100 // the pixel data, but rather increments the reference count to the pixels.
101 SkBitmap last_received_frame_;
103 // Number of frames we've examined to find a keyframe.
104 int frames_examined_;
106 // Plugin's unobscured dimensions as of initialization.
107 gfx::Size unobscured_size_;
109 // Video plugins with throttled audio often stop generating frames.
110 // This timer is so we don't wait forever for candidate poster frames.
111 bool audio_throttled_;
112 base::DelayTimer<PluginInstanceThrottlerImpl> audio_throttled_frame_timeout_;
114 base::ObserverList<Observer> observer_list_;
116 base::WeakPtrFactory<PluginInstanceThrottlerImpl> weak_factory_;
118 DISALLOW_COPY_AND_ASSIGN(PluginInstanceThrottlerImpl);
122 #endif // CONTENT_RENDERER_PEPPER_PLUGIN_INSTANCE_THROTTLER_IMPL_H_