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 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
{
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());
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
{
44 device_context_
= pp::Graphics2D(this, view_
.GetRect().size(), true);
45 if (!BindGraphics(device_context_
))
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.
55 pp::ImageData
image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL
,
56 view_
.GetRect().size(), true);
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));
74 pp::Graphics2D device_context_
;
77 class PowerSaverTestModule
: public pp::Module
{
79 PowerSaverTestModule() : pp::Module() {}
80 virtual ~PowerSaverTestModule() {}
82 virtual pp::Instance
* CreateInstance(PP_Instance instance
) {
83 return new PowerSaverTestInstance(instance
);
89 Module
* CreateModule() {
90 return new PowerSaverTestModule();