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.
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.
18 // This is a simple C++ Pepper plugin that enables Plugin Power Saver tests.
19 class PowerSaverTestInstance
: public pp::Instance
{
21 explicit PowerSaverTestInstance(PP_Instance instance
)
22 : pp::Instance(instance
), callback_factory_(this) {}
23 ~PowerSaverTestInstance() override
{}
25 bool Init(uint32_t argc
, const char* argn
[], const char* argv
[]) {
26 GetTestingInterface()->SubscribeToPowerSaverNotifications(pp_instance());
30 void HandleMessage(const pp::Var
& message_data
) override
{
31 if (message_data
.is_string() &&
32 message_data
.AsString() == "getPowerSaverStatus") {
33 GetTestingInterface()->PostPowerSaverStatus(pp_instance());
37 // Broadcast our peripheral status after the initial view data. This is for
38 // tests that await initial plugin creation.
39 void DidChangeView(const pp::View
& view
) override
{
41 device_context_
= pp::Graphics2D(this, view_
.GetRect().size(), true);
42 if (!BindGraphics(device_context_
))
48 void OnFlush(int32_t) { Paint(); }
52 pp::ImageData
image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL
,
53 view_
.GetRect().size(), true);
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
;
65 device_context_
.ReplaceContents(&image
);
66 device_context_
.Flush(
67 callback_factory_
.NewCallback(&PowerSaverTestInstance::OnFlush
));
71 pp::Graphics2D device_context_
;
73 pp::CompletionCallbackFactory
<PowerSaverTestInstance
> callback_factory_
;
76 class PowerSaverTestModule
: public pp::Module
{
78 PowerSaverTestModule() : pp::Module() {}
79 virtual ~PowerSaverTestModule() {}
81 virtual pp::Instance
* CreateInstance(PP_Instance instance
) {
82 return new PowerSaverTestInstance(instance
);
88 Module
* CreateModule() {
89 return new PowerSaverTestModule();