OLD | NEW |
(Empty) | |
| 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 |
| 5 #include <sstream> |
| 6 #include <utility> |
| 7 |
| 8 #include "ppapi/cpp/completion_callback.h" |
| 9 #include "ppapi/cpp/graphics_2d.h" |
| 10 #include "ppapi/cpp/image_data.h" |
| 11 #include "ppapi/cpp/input_event.h" |
| 12 #include "ppapi/cpp/instance.h" |
| 13 #include "ppapi/cpp/module.h" |
| 14 |
| 15 static void DummyCompletionCallback(void*, int32_t) {} |
| 16 |
| 17 // This is a simple C++ Pepper plugin for Blink layout tests. |
| 18 class BlinkTestInstance : public pp::Instance { |
| 19 public: |
| 20 explicit BlinkTestInstance(PP_Instance instance) |
| 21 : pp::Instance(instance), first_paint_(true) {} |
| 22 ~BlinkTestInstance() override {} |
| 23 |
| 24 bool Init(uint32_t argc, const char* argn[], const char* argv[]) { |
| 25 return RequestFilteringInputEvents(PP_INPUTEVENT_CLASS_MOUSE | |
| 26 PP_INPUTEVENT_CLASS_KEYBOARD) == PP_OK; |
| 27 } |
| 28 |
| 29 void DidChangeView(const pp::View& view) override { |
| 30 view_ = view; |
| 31 device_context_ = pp::Graphics2D(this, view_.GetRect().size(), true); |
| 32 if (!BindGraphics(device_context_)) |
| 33 return; |
| 34 |
| 35 // Since we draw a static image, we only need to make a new frame when |
| 36 // the device is initialized or the view size changes. |
| 37 Paint(); |
| 38 } |
| 39 |
| 40 void DidChangeFocus(bool has_focus) override { |
| 41 LogMessage("DidChangeFocus(", has_focus, ")"); |
| 42 } |
| 43 |
| 44 bool HandleInputEvent(const pp::InputEvent& event) override { |
| 45 switch (event.GetType()) { |
| 46 case PP_INPUTEVENT_TYPE_MOUSEDOWN: |
| 47 LogMouseEvent("Down", event); |
| 48 break; |
| 49 case PP_INPUTEVENT_TYPE_MOUSEUP: |
| 50 LogMouseEvent("Up", event); |
| 51 break; |
| 52 case PP_INPUTEVENT_TYPE_KEYDOWN: |
| 53 LogKeyboardEvent("Down", event); |
| 54 break; |
| 55 case PP_INPUTEVENT_TYPE_KEYUP: |
| 56 LogKeyboardEvent("Up", event); |
| 57 break; |
| 58 case PP_INPUTEVENT_TYPE_MOUSEMOVE: |
| 59 case PP_INPUTEVENT_TYPE_MOUSEENTER: |
| 60 case PP_INPUTEVENT_TYPE_MOUSELEAVE: |
| 61 case PP_INPUTEVENT_TYPE_RAWKEYDOWN: |
| 62 case PP_INPUTEVENT_TYPE_CHAR: |
| 63 // Just swallow these events without any logging. |
| 64 return true; |
| 65 default: |
| 66 LogMessage("Unexpected input event with type = ", event.GetType()); |
| 67 return false; |
| 68 } |
| 69 return true; |
| 70 } |
| 71 |
| 72 private: |
| 73 void Paint() { |
| 74 pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, |
| 75 view_.GetRect().size(), true); |
| 76 if (image.is_null()) |
| 77 return; |
| 78 |
| 79 // Draw blue and green checkerboard pattern to show "interesting" keyframe. |
| 80 const int kSquareSizePixels = 8; |
| 81 for (int y = 0; y < view_.GetRect().size().height(); ++y) { |
| 82 for (int x = 0; x < view_.GetRect().size().width(); ++x) { |
| 83 int x_square = x / kSquareSizePixels; |
| 84 int y_square = y / kSquareSizePixels; |
| 85 uint32_t color = ((x_square + y_square) % 2) ? 0xFF0000FF : 0xFF00FF00; |
| 86 *image.GetAddr32(pp::Point(x, y)) = color; |
| 87 } |
| 88 } |
| 89 |
| 90 device_context_.ReplaceContents(&image); |
| 91 device_context_.Flush( |
| 92 pp::CompletionCallback(&DummyCompletionCallback, nullptr)); |
| 93 |
| 94 if (first_paint_) { |
| 95 first_paint_ = false; |
| 96 PostMessage(pp::Var("loaded")); |
| 97 } else { |
| 98 PostMessage(pp::Var("painted")); |
| 99 } |
| 100 } |
| 101 |
| 102 void LogMouseEvent(const std::string& type, const pp::InputEvent& event) { |
| 103 pp::MouseInputEvent mouse_event(event); |
| 104 pp::Point mouse_position = mouse_event.GetPosition(); |
| 105 LogMessage("Mouse", type, " at (", mouse_position.x(), ",", |
| 106 mouse_position.y(), ")"); |
| 107 } |
| 108 |
| 109 void LogKeyboardEvent(const std::string& type, const pp::InputEvent& event) { |
| 110 pp::KeyboardInputEvent keyboard_event(event); |
| 111 LogMessage("Key", type, " '", keyboard_event.GetCode().AsString(), "'"); |
| 112 } |
| 113 |
| 114 // Template magic to cover the lack of base::StringPrintf. |
| 115 template <typename... Args> |
| 116 void LogMessage(Args&&... args) { |
| 117 std::ostringstream ss; |
| 118 ss << std::boolalpha; |
| 119 LogMessageHelper(&ss, std::forward<Args>(args)...); |
| 120 } |
| 121 |
| 122 template <typename Arg, typename... Args> |
| 123 void LogMessageHelper(std::ostringstream* os, Arg&& arg, Args&&... args) { |
| 124 *os << arg; |
| 125 LogMessageHelper(os, std::forward<Args>(args)...); |
| 126 } |
| 127 |
| 128 template <typename Arg> |
| 129 void LogMessageHelper(std::ostringstream* os, Arg&& arg) { |
| 130 *os << arg; |
| 131 LogToConsoleWithSource(PP_LOGLEVEL_LOG, pp::Var("Blink Test Plugin"), |
| 132 pp::Var(os->str())); |
| 133 } |
| 134 |
| 135 bool first_paint_; |
| 136 pp::View view_; |
| 137 pp::Graphics2D device_context_; |
| 138 }; |
| 139 |
| 140 class BlinkTestModule : public pp::Module { |
| 141 public: |
| 142 BlinkTestModule() {} |
| 143 virtual ~BlinkTestModule() {} |
| 144 |
| 145 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| 146 return new BlinkTestInstance(instance); |
| 147 } |
| 148 }; |
| 149 |
| 150 namespace pp { |
| 151 |
| 152 Module* CreateModule() { |
| 153 return new BlinkTestModule(); |
| 154 } |
| 155 |
| 156 } // namespace pp |
OLD | NEW |