OLD | NEW |
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" | |
10 #include "ppapi/cpp/var_dictionary.h" | |
11 #include "ppapi/tests/test_utils.h" | 11 #include "ppapi/tests/test_utils.h" |
12 | 12 |
13 // Windows defines 'PostMessage', so we have to undef it. | 13 // Windows defines 'PostMessage', so we have to undef it. |
14 #ifdef PostMessage | 14 #ifdef PostMessage |
15 #undef PostMessage | 15 #undef PostMessage |
16 #endif | 16 #endif |
17 | 17 |
18 // This is a simple C++ Pepper plugin that enables Plugin Power Saver tests. | 18 // This is a simple C++ Pepper plugin that enables Plugin Power Saver tests. |
19 class PowerSaverTestInstance : public pp::Instance { | 19 class PowerSaverTestInstance : public pp::Instance { |
20 public: | 20 public: |
21 explicit PowerSaverTestInstance(PP_Instance instance) | 21 explicit PowerSaverTestInstance(PP_Instance instance) |
22 : pp::Instance(instance), received_first_did_change_view_(false) {} | 22 : pp::Instance(instance), callback_factory_(this) {} |
23 ~PowerSaverTestInstance() override {} | 23 ~PowerSaverTestInstance() override {} |
24 | 24 |
25 // For browser tests, responds to: | 25 bool Init(uint32_t argc, const char* argn[], const char* argv[]) { |
26 // - When postMessage("isPeripheral") is called on the plugin DOM element. | 26 GetTestingInterface()->SubscribeToPowerSaverNotifications(pp_instance()); |
27 // - When the plugin throttler posts a message notifying us that our | 27 return true; |
28 // peripheral status has changed. | 28 } |
| 29 |
29 void HandleMessage(const pp::Var& message_data) override { | 30 void HandleMessage(const pp::Var& message_data) override { |
30 if (message_data.is_string()) { | 31 if (message_data.is_string() && |
31 if (message_data.AsString() == "getPeripheralStatus") | 32 message_data.AsString() == "getPowerSaverStatus") { |
32 BroadcastIsPeripheralStatus("getPeripheralStatusResponse"); | 33 GetTestingInterface()->PostPowerSaverStatus(pp_instance()); |
33 else if (message_data.AsString() == "peripheralStatusChange") | |
34 BroadcastIsPeripheralStatus("peripheralStatusChange"); | |
35 } | 34 } |
36 } | 35 } |
37 | 36 |
38 // Broadcast our peripheral status after the initial view data. This is for | 37 // Broadcast our peripheral status after the initial view data. This is for |
39 // tests that await initial plugin creation. | 38 // tests that await initial plugin creation. |
40 void DidChangeView(const pp::View& view) override { | 39 void DidChangeView(const pp::View& view) override { |
41 if (!received_first_did_change_view_) { | 40 view_ = view; |
42 BroadcastIsPeripheralStatus("initial"); | 41 device_context_ = pp::Graphics2D(this, view_.GetRect().size(), true); |
43 received_first_did_change_view_ = true; | 42 if (!BindGraphics(device_context_)) |
44 } | 43 return; |
| 44 |
| 45 Paint(); |
45 } | 46 } |
46 | 47 |
| 48 void OnFlush(int32_t) { Paint(); } |
| 49 |
47 private: | 50 private: |
48 void BroadcastIsPeripheralStatus(const std::string& source) { | 51 void Paint() { |
49 pp::VarDictionary message; | 52 pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, |
50 message.Set( | 53 view_.GetRect().size(), true); |
51 "isPeripheral", | 54 if (image.is_null()) |
52 pp::Var(PP_ToBool(GetTestingInterface()->IsPeripheral(pp_instance())))); | 55 return; |
53 message.Set("source", pp::Var(source)); | 56 |
54 PostMessage(message); | 57 // Draw black and white stripes to present an "interesting" keyframe. |
| 58 for (int y = 0; y < view_.GetRect().size().height(); ++y) { |
| 59 for (int x = 0; x < view_.GetRect().size().width(); ++x) { |
| 60 uint32_t color = x % 2 ? 0xFF0000FF : 0xFFFFFFFF; |
| 61 *image.GetAddr32(pp::Point(x, y)) = color; |
| 62 } |
| 63 } |
| 64 |
| 65 device_context_.ReplaceContents(&image); |
| 66 device_context_.Flush( |
| 67 callback_factory_.NewCallback(&PowerSaverTestInstance::OnFlush)); |
55 } | 68 } |
56 | 69 |
57 bool received_first_did_change_view_; | 70 pp::View view_; |
| 71 pp::Graphics2D device_context_; |
| 72 |
| 73 pp::CompletionCallbackFactory<PowerSaverTestInstance> callback_factory_; |
58 }; | 74 }; |
59 | 75 |
60 class PowerSaverTestModule : public pp::Module { | 76 class PowerSaverTestModule : public pp::Module { |
61 public: | 77 public: |
62 PowerSaverTestModule() : pp::Module() {} | 78 PowerSaverTestModule() : pp::Module() {} |
63 virtual ~PowerSaverTestModule() {} | 79 virtual ~PowerSaverTestModule() {} |
64 | 80 |
65 virtual pp::Instance* CreateInstance(PP_Instance instance) { | 81 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
66 return new PowerSaverTestInstance(instance); | 82 return new PowerSaverTestInstance(instance); |
67 } | 83 } |
68 }; | 84 }; |
69 | 85 |
70 namespace pp { | 86 namespace pp { |
71 | 87 |
72 Module* CreateModule() { | 88 Module* CreateModule() { |
73 return new PowerSaverTestModule(); | 89 return new PowerSaverTestModule(); |
74 } | 90 } |
75 | 91 |
76 } // namespace pp | 92 } // namespace pp |
OLD | NEW |