Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: ppapi/tests/power_saver_test_plugin.cc

Issue 1114623002: Plugin Power Saver: Make PPS work well with prerendered pages. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merge Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 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 <algorithm> 5 #include <algorithm>
6 6
7 #include "ppapi/cpp/graphics_2d.h"
8 #include "ppapi/cpp/image_data.h"
7 #include "ppapi/cpp/instance.h" 9 #include "ppapi/cpp/instance.h"
8 #include "ppapi/cpp/module.h" 10 #include "ppapi/cpp/module.h"
9 #include "ppapi/cpp/var.h" 11 #include "ppapi/cpp/var.h"
10 #include "ppapi/cpp/var_dictionary.h" 12 #include "ppapi/cpp/var_dictionary.h"
11 #include "ppapi/tests/test_utils.h" 13 #include "ppapi/tests/test_utils.h"
12 14
13 // Windows defines 'PostMessage', so we have to undef it. 15 // Windows defines 'PostMessage', so we have to undef it.
14 #ifdef PostMessage 16 #ifdef PostMessage
15 #undef PostMessage 17 #undef PostMessage
16 #endif 18 #endif
17 19
18 // This is a simple C++ Pepper plugin that enables Plugin Power Saver tests. 20 // This is a simple C++ Pepper plugin that enables Plugin Power Saver tests.
19 class PowerSaverTestInstance : public pp::Instance { 21 class PowerSaverTestInstance : public pp::Instance {
20 public: 22 public:
21 explicit PowerSaverTestInstance(PP_Instance instance) 23 explicit PowerSaverTestInstance(PP_Instance instance)
22 : pp::Instance(instance), received_first_did_change_view_(false) {} 24 : pp::Instance(instance),
25 received_first_did_change_view_(false),
26 callback_factory_(this) {}
23 ~PowerSaverTestInstance() override {} 27 ~PowerSaverTestInstance() override {}
24 28
25 // For browser tests, responds to: 29 // For browser tests, responds to:
26 // - When postMessage("isPeripheral") is called on the plugin DOM element. 30 // - When postMessage("isPeripheral") is called on the plugin DOM element.
27 // - When the plugin throttler posts a message notifying us that our 31 // - When the plugin throttler posts a message notifying us that our
28 // peripheral status has changed. 32 // peripheral status has changed.
29 void HandleMessage(const pp::Var& message_data) override { 33 void HandleMessage(const pp::Var& message_data) override {
30 if (message_data.is_string()) { 34 if (message_data.is_string()) {
31 if (message_data.AsString() == "getPeripheralStatus") 35 if (message_data.AsString() == "getPowerSaverStatus")
32 BroadcastIsPeripheralStatus("getPeripheralStatusResponse"); 36 BroadcastStatus("getPowerSaverStatusResponse");
33 else if (message_data.AsString() == "peripheralStatusChange") 37 else if (message_data.AsString() == "peripheralStatusChange")
34 BroadcastIsPeripheralStatus("peripheralStatusChange"); 38 BroadcastStatus("peripheralStatusChange");
39 else if (message_data.AsString() == "throttleStatusChange")
40 BroadcastStatus("throttleStatusChange");
35 } 41 }
36 } 42 }
37 43
38 // Broadcast our peripheral status after the initial view data. This is for 44 // Broadcast our peripheral status after the initial view data. This is for
39 // tests that await initial plugin creation. 45 // tests that await initial plugin creation.
40 void DidChangeView(const pp::View& view) override { 46 void DidChangeView(const pp::View& view) override {
41 if (!received_first_did_change_view_) { 47 if (!received_first_did_change_view_) {
42 BroadcastIsPeripheralStatus("initial"); 48 BroadcastStatus("initial");
43 received_first_did_change_view_ = true; 49 received_first_did_change_view_ = true;
44 } 50 }
51
52 view_ = view;
53 device_context_ = pp::Graphics2D(this, view_.GetRect().size(), true);
54 if (!BindGraphics(device_context_))
55 return;
56
57 Paint();
45 } 58 }
46 59
60 void OnFlush(int32_t) { Paint(); }
61
47 private: 62 private:
48 void BroadcastIsPeripheralStatus(const std::string& source) { 63 void Paint() {
tommycli 2015/04/28 18:43:16 The test plugin provides a striped image to the th
64 pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL,
65 view_.GetRect().size(), true);
66 if (image.is_null())
67 return;
68
69 // Draw black and white stripes to present an "interesting" keyframe.
70 for (int y = 0; y < view_.GetRect().size().height(); ++y) {
71 for (int x = 0; x < view_.GetRect().size().width(); ++x) {
72 uint32_t color = x % 2 ? 0xFF0000FF : 0xFFFFFFFF;
73 *image.GetAddr32(pp::Point(x, y)) = color;
74 }
75 }
76
77 device_context_.ReplaceContents(&image);
78 device_context_.Flush(
79 callback_factory_.NewCallback(&PowerSaverTestInstance::OnFlush));
80 }
81
82 void BroadcastStatus(const std::string& source) {
49 pp::VarDictionary message; 83 pp::VarDictionary message;
50 message.Set( 84 message.Set(
51 "isPeripheral", 85 "isPeripheral",
52 pp::Var(PP_ToBool(GetTestingInterface()->IsPeripheral(pp_instance())))); 86 pp::Var(PP_ToBool(GetTestingInterface()->IsPeripheral(pp_instance()))));
87 message.Set(
88 "isThrottled",
89 pp::Var(PP_ToBool(GetTestingInterface()->IsThrottled(pp_instance()))));
53 message.Set("source", pp::Var(source)); 90 message.Set("source", pp::Var(source));
54 PostMessage(message); 91 PostMessage(message);
55 } 92 }
56 93
57 bool received_first_did_change_view_; 94 bool received_first_did_change_view_;
95 pp::View view_;
96 pp::Graphics2D device_context_;
97
98 pp::CompletionCallbackFactory<PowerSaverTestInstance> callback_factory_;
58 }; 99 };
59 100
60 class PowerSaverTestModule : public pp::Module { 101 class PowerSaverTestModule : public pp::Module {
61 public: 102 public:
62 PowerSaverTestModule() : pp::Module() {} 103 PowerSaverTestModule() : pp::Module() {}
63 virtual ~PowerSaverTestModule() {} 104 virtual ~PowerSaverTestModule() {}
64 105
65 virtual pp::Instance* CreateInstance(PP_Instance instance) { 106 virtual pp::Instance* CreateInstance(PP_Instance instance) {
66 return new PowerSaverTestInstance(instance); 107 return new PowerSaverTestInstance(instance);
67 } 108 }
68 }; 109 };
69 110
70 namespace pp { 111 namespace pp {
71 112
72 Module* CreateModule() { 113 Module* CreateModule() {
73 return new PowerSaverTestModule(); 114 return new PowerSaverTestModule();
74 } 115 }
75 116
76 } // namespace pp 117 } // namespace pp
OLDNEW
« ppapi/api/private/ppb_testing_private.idl ('K') | « ppapi/proxy/ppb_testing_proxy.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698