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 void HandleMessage(const pp::Var& message_data) override {} | |
raymes
2015/12/01 23:43:42
nit: can you remove this line?
dcheng
2015/12/01 23:51:17
Done.
| |
73 | |
74 private: | |
75 void Paint() { | |
76 pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, | |
77 view_.GetRect().size(), true); | |
78 if (image.is_null()) | |
79 return; | |
80 | |
81 // Draw blue and green checkerboard pattern to show "interesting" keyframe. | |
82 const int kSquareSizePixels = 8; | |
83 for (int y = 0; y < view_.GetRect().size().height(); ++y) { | |
84 for (int x = 0; x < view_.GetRect().size().width(); ++x) { | |
85 int x_square = x / kSquareSizePixels; | |
86 int y_square = y / kSquareSizePixels; | |
87 uint32_t color = ((x_square + y_square) % 2) ? 0xFF0000FF : 0xFF00FF00; | |
88 *image.GetAddr32(pp::Point(x, y)) = color; | |
89 } | |
90 } | |
91 | |
92 device_context_.ReplaceContents(&image); | |
93 device_context_.Flush( | |
94 pp::CompletionCallback(&DummyCompletionCallback, nullptr)); | |
95 | |
96 if (first_paint_) { | |
97 first_paint_ = false; | |
98 PostMessage(pp::Var("loaded")); | |
99 } else { | |
100 PostMessage(pp::Var("painted")); | |
101 } | |
102 } | |
103 | |
104 void LogMouseEvent(const std::string& type, const pp::InputEvent& event) { | |
105 pp::MouseInputEvent mouse_event(event); | |
106 pp::Point mouse_position = mouse_event.GetPosition(); | |
107 LogMessage("Mouse", type, " at (", mouse_position.x(), ",", | |
108 mouse_position.y(), ")"); | |
109 } | |
110 | |
111 void LogKeyboardEvent(const std::string& type, const pp::InputEvent& event) { | |
112 pp::KeyboardInputEvent keyboard_event(event); | |
113 LogMessage("Key", type, " '", keyboard_event.GetCode().AsString(), "'"); | |
114 } | |
115 | |
116 // Template magic to cover the lack of base::StringPrintf. | |
117 template <typename... Args> | |
118 void LogMessage(Args&&... args) { | |
119 std::ostringstream ss; | |
120 ss << std::boolalpha; | |
121 LogMessageHelper(&ss, std::forward<Args>(args)...); | |
122 } | |
123 | |
124 template <typename Arg, typename... Args> | |
125 void LogMessageHelper(std::ostringstream* os, Arg&& arg, Args&&... args) { | |
126 *os << arg; | |
127 LogMessageHelper(os, std::forward<Args>(args)...); | |
128 } | |
129 | |
130 template <typename Arg> | |
131 void LogMessageHelper(std::ostringstream* os, Arg&& arg) { | |
132 *os << arg; | |
133 LogToConsoleWithSource(PP_LOGLEVEL_LOG, pp::Var("Blink Test Plugin"), | |
134 pp::Var(os->str())); | |
135 } | |
136 | |
137 bool first_paint_; | |
138 pp::View view_; | |
139 pp::Graphics2D device_context_; | |
140 }; | |
141 | |
142 class BlinkTestModule : public pp::Module { | |
143 public: | |
144 BlinkTestModule() {} | |
145 virtual ~BlinkTestModule() {} | |
146 | |
147 virtual pp::Instance* CreateInstance(PP_Instance instance) { | |
148 return new BlinkTestInstance(instance); | |
149 } | |
150 }; | |
151 | |
152 namespace pp { | |
153 | |
154 Module* CreateModule() { | |
155 return new BlinkTestModule(); | |
156 } | |
157 | |
158 } // namespace pp | |
OLD | NEW |