blob: f319649fce506a2f0e49e928332ec114a9fffd42 [file] [log] [blame]
tommycli0dd13012015-04-24 20:27:221// 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.
4
avie029c4132015-12-23 06:45:225#include <stdint.h>
6
tommycli0dd13012015-04-24 20:27:227#include <algorithm>
8
tommycliee0a49b2015-05-05 19:40:499#include "ppapi/cpp/graphics_2d.h"
10#include "ppapi/cpp/image_data.h"
tommycli0dd13012015-04-24 20:27:2211#include "ppapi/cpp/instance.h"
12#include "ppapi/cpp/module.h"
tommycli0dd13012015-04-24 20:27:2213#include "ppapi/tests/test_utils.h"
14
15// Windows defines 'PostMessage', so we have to undef it.
16#ifdef PostMessage
17#undef PostMessage
18#endif
19
tommycli0a9a5fe42015-06-15 18:10:1820static void DummyCompletionCallback(void*, int32_t) {
21}
22
tommycli0dd13012015-04-24 20:27:2223// This is a simple C++ Pepper plugin that enables Plugin Power Saver tests.
24class PowerSaverTestInstance : public pp::Instance {
25 public:
26 explicit PowerSaverTestInstance(PP_Instance instance)
tommycli0a9a5fe42015-06-15 18:10:1827 : pp::Instance(instance) {}
tommycli0dd13012015-04-24 20:27:2228 ~PowerSaverTestInstance() override {}
29
tommycliee0a49b2015-05-05 19:40:4930 bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
31 GetTestingInterface()->SubscribeToPowerSaverNotifications(pp_instance());
32 return true;
33 }
34
tommycli0dd13012015-04-24 20:27:2235 void HandleMessage(const pp::Var& message_data) override {
tommycliee0a49b2015-05-05 19:40:4936 if (message_data.is_string() &&
37 message_data.AsString() == "getPowerSaverStatus") {
38 GetTestingInterface()->PostPowerSaverStatus(pp_instance());
tommycli0dd13012015-04-24 20:27:2239 }
40 }
41
42 // Broadcast our peripheral status after the initial view data. This is for
43 // tests that await initial plugin creation.
44 void DidChangeView(const pp::View& view) override {
tommycliee0a49b2015-05-05 19:40:4945 view_ = view;
46 device_context_ = pp::Graphics2D(this, view_.GetRect().size(), true);
47 if (!BindGraphics(device_context_))
48 return;
49
tommycli0a9a5fe42015-06-15 18:10:1850 // Since we draw a static image, we only need to make a new frame when
51 // the device is initialized or the view size changes.
tommycliee0a49b2015-05-05 19:40:4952 Paint();
tommycli0dd13012015-04-24 20:27:2253 }
54
55 private:
tommycliee0a49b2015-05-05 19:40:4956 void Paint() {
57 pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL,
58 view_.GetRect().size(), true);
59 if (image.is_null())
60 return;
61
tommyclif1959fb92015-10-27 02:41:2862 // Draw blue and green checkerboard pattern to show "interesting" keyframe.
63 const int kSquareSizePixels = 8;
tommycliee0a49b2015-05-05 19:40:4964 for (int y = 0; y < view_.GetRect().size().height(); ++y) {
65 for (int x = 0; x < view_.GetRect().size().width(); ++x) {
tommyclif1959fb92015-10-27 02:41:2866 int x_square = x / kSquareSizePixels;
67 int y_square = y / kSquareSizePixels;
68 uint32_t color = ((x_square + y_square) % 2) ? 0xFF0000FF : 0xFF00FF00;
tommycliee0a49b2015-05-05 19:40:4969 *image.GetAddr32(pp::Point(x, y)) = color;
70 }
71 }
72
73 device_context_.ReplaceContents(&image);
74 device_context_.Flush(
tommycli0a9a5fe42015-06-15 18:10:1875 pp::CompletionCallback(&DummyCompletionCallback, nullptr));
tommycli0dd13012015-04-24 20:27:2276 }
77
tommycliee0a49b2015-05-05 19:40:4978 pp::View view_;
79 pp::Graphics2D device_context_;
tommycli0dd13012015-04-24 20:27:2280};
81
82class PowerSaverTestModule : public pp::Module {
83 public:
84 PowerSaverTestModule() : pp::Module() {}
85 virtual ~PowerSaverTestModule() {}
86
87 virtual pp::Instance* CreateInstance(PP_Instance instance) {
88 return new PowerSaverTestInstance(instance);
89 }
90};
91
92namespace pp {
93
94Module* CreateModule() {
95 return new PowerSaverTestModule();
96}
97
98} // namespace pp