Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ppapi / tests / power_saver_test_plugin.cc
blob521fafa8a960029542d160834abbbf9192a0ffdd
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
3 // found in the LICENSE file.
5 #include <algorithm>
7 #include "ppapi/cpp/graphics_2d.h"
8 #include "ppapi/cpp/image_data.h"
9 #include "ppapi/cpp/instance.h"
10 #include "ppapi/cpp/module.h"
11 #include "ppapi/tests/test_utils.h"
13 // Windows defines 'PostMessage', so we have to undef it.
14 #ifdef PostMessage
15 #undef PostMessage
16 #endif
18 static void DummyCompletionCallback(void*, int32_t) {
21 // This is a simple C++ Pepper plugin that enables Plugin Power Saver tests.
22 class PowerSaverTestInstance : public pp::Instance {
23 public:
24 explicit PowerSaverTestInstance(PP_Instance instance)
25 : pp::Instance(instance) {}
26 ~PowerSaverTestInstance() override {}
28 bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
29 GetTestingInterface()->SubscribeToPowerSaverNotifications(pp_instance());
30 return true;
33 void HandleMessage(const pp::Var& message_data) override {
34 if (message_data.is_string() &&
35 message_data.AsString() == "getPowerSaverStatus") {
36 GetTestingInterface()->PostPowerSaverStatus(pp_instance());
40 // Broadcast our peripheral status after the initial view data. This is for
41 // tests that await initial plugin creation.
42 void DidChangeView(const pp::View& view) override {
43 view_ = view;
44 device_context_ = pp::Graphics2D(this, view_.GetRect().size(), true);
45 if (!BindGraphics(device_context_))
46 return;
48 // Since we draw a static image, we only need to make a new frame when
49 // the device is initialized or the view size changes.
50 Paint();
53 private:
54 void Paint() {
55 pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL,
56 view_.GetRect().size(), true);
57 if (image.is_null())
58 return;
60 // Draw black and white stripes to present an "interesting" keyframe.
61 for (int y = 0; y < view_.GetRect().size().height(); ++y) {
62 for (int x = 0; x < view_.GetRect().size().width(); ++x) {
63 uint32_t color = x % 2 ? 0xFF0000FF : 0xFFFFFFFF;
64 *image.GetAddr32(pp::Point(x, y)) = color;
68 device_context_.ReplaceContents(&image);
69 device_context_.Flush(
70 pp::CompletionCallback(&DummyCompletionCallback, nullptr));
73 pp::View view_;
74 pp::Graphics2D device_context_;
77 class PowerSaverTestModule : public pp::Module {
78 public:
79 PowerSaverTestModule() : pp::Module() {}
80 virtual ~PowerSaverTestModule() {}
82 virtual pp::Instance* CreateInstance(PP_Instance instance) {
83 return new PowerSaverTestInstance(instance);
87 namespace pp {
89 Module* CreateModule() {
90 return new PowerSaverTestModule();
93 } // namespace pp