tommycli | 0dd1301 | 2015-04-24 20:27:22 | [diff] [blame] | 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. |
| 4 | |
avi | e029c413 | 2015-12-23 06:45:22 | [diff] [blame] | 5 | #include <stdint.h> |
| 6 | |
tommycli | 0dd1301 | 2015-04-24 20:27:22 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
tommycli | ee0a49b | 2015-05-05 19:40:49 | [diff] [blame] | 9 | #include "ppapi/cpp/graphics_2d.h" |
| 10 | #include "ppapi/cpp/image_data.h" |
tommycli | 0dd1301 | 2015-04-24 20:27:22 | [diff] [blame] | 11 | #include "ppapi/cpp/instance.h" |
| 12 | #include "ppapi/cpp/module.h" |
tommycli | 0dd1301 | 2015-04-24 20:27:22 | [diff] [blame] | 13 | #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 | |
tommycli | 0a9a5fe4 | 2015-06-15 18:10:18 | [diff] [blame] | 20 | static void DummyCompletionCallback(void*, int32_t) { |
| 21 | } |
| 22 | |
tommycli | 0dd1301 | 2015-04-24 20:27:22 | [diff] [blame] | 23 | // This is a simple C++ Pepper plugin that enables Plugin Power Saver tests. |
| 24 | class PowerSaverTestInstance : public pp::Instance { |
| 25 | public: |
| 26 | explicit PowerSaverTestInstance(PP_Instance instance) |
tommycli | 0a9a5fe4 | 2015-06-15 18:10:18 | [diff] [blame] | 27 | : pp::Instance(instance) {} |
tommycli | 0dd1301 | 2015-04-24 20:27:22 | [diff] [blame] | 28 | ~PowerSaverTestInstance() override {} |
| 29 | |
tommycli | ee0a49b | 2015-05-05 19:40:49 | [diff] [blame] | 30 | bool Init(uint32_t argc, const char* argn[], const char* argv[]) { |
| 31 | GetTestingInterface()->SubscribeToPowerSaverNotifications(pp_instance()); |
| 32 | return true; |
| 33 | } |
| 34 | |
tommycli | 0dd1301 | 2015-04-24 20:27:22 | [diff] [blame] | 35 | void HandleMessage(const pp::Var& message_data) override { |
tommycli | ee0a49b | 2015-05-05 19:40:49 | [diff] [blame] | 36 | if (message_data.is_string() && |
| 37 | message_data.AsString() == "getPowerSaverStatus") { |
| 38 | GetTestingInterface()->PostPowerSaverStatus(pp_instance()); |
tommycli | 0dd1301 | 2015-04-24 20:27:22 | [diff] [blame] | 39 | } |
| 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 { |
tommycli | ee0a49b | 2015-05-05 19:40:49 | [diff] [blame] | 45 | view_ = view; |
| 46 | device_context_ = pp::Graphics2D(this, view_.GetRect().size(), true); |
| 47 | if (!BindGraphics(device_context_)) |
| 48 | return; |
| 49 | |
tommycli | 0a9a5fe4 | 2015-06-15 18:10:18 | [diff] [blame] | 50 | // 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. |
tommycli | ee0a49b | 2015-05-05 19:40:49 | [diff] [blame] | 52 | Paint(); |
tommycli | 0dd1301 | 2015-04-24 20:27:22 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | private: |
tommycli | ee0a49b | 2015-05-05 19:40:49 | [diff] [blame] | 56 | void Paint() { |
| 57 | pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, |
| 58 | view_.GetRect().size(), true); |
| 59 | if (image.is_null()) |
| 60 | return; |
| 61 | |
tommycli | f1959fb9 | 2015-10-27 02:41:28 | [diff] [blame] | 62 | // Draw blue and green checkerboard pattern to show "interesting" keyframe. |
| 63 | const int kSquareSizePixels = 8; |
tommycli | ee0a49b | 2015-05-05 19:40:49 | [diff] [blame] | 64 | for (int y = 0; y < view_.GetRect().size().height(); ++y) { |
| 65 | for (int x = 0; x < view_.GetRect().size().width(); ++x) { |
tommycli | f1959fb9 | 2015-10-27 02:41:28 | [diff] [blame] | 66 | int x_square = x / kSquareSizePixels; |
| 67 | int y_square = y / kSquareSizePixels; |
| 68 | uint32_t color = ((x_square + y_square) % 2) ? 0xFF0000FF : 0xFF00FF00; |
tommycli | ee0a49b | 2015-05-05 19:40:49 | [diff] [blame] | 69 | *image.GetAddr32(pp::Point(x, y)) = color; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | device_context_.ReplaceContents(&image); |
| 74 | device_context_.Flush( |
tommycli | 0a9a5fe4 | 2015-06-15 18:10:18 | [diff] [blame] | 75 | pp::CompletionCallback(&DummyCompletionCallback, nullptr)); |
tommycli | 0dd1301 | 2015-04-24 20:27:22 | [diff] [blame] | 76 | } |
| 77 | |
tommycli | ee0a49b | 2015-05-05 19:40:49 | [diff] [blame] | 78 | pp::View view_; |
| 79 | pp::Graphics2D device_context_; |
tommycli | 0dd1301 | 2015-04-24 20:27:22 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | class 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 | |
| 92 | namespace pp { |
| 93 | |
| 94 | Module* CreateModule() { |
| 95 | return new PowerSaverTestModule(); |
| 96 | } |
| 97 | |
| 98 | } // namespace pp |