1 // Copyright (c) 2011 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/c/pp_input_event.h"
8 #include "ppapi/cpp/graphics_2d.h"
9 #include "ppapi/cpp/image_data.h"
10 #include "ppapi/cpp/input_event.h"
11 #include "ppapi/cpp/instance.h"
12 #include "ppapi/cpp/module.h"
13 #include "ppapi/cpp/size.h"
14 #include "ppapi/cpp/view.h"
15 #include "ppapi/utility/graphics/paint_manager.h"
17 // Number of pixels to each side of the center of the square that we draw.
18 static const int kSquareRadius
= 2;
20 // We identify our square by the center point. This computes the rect for the
21 // square given that point.
22 pp::Rect
SquareForPoint(int x
, int y
) {
23 return PP_MakeRectFromXYWH(x
- kSquareRadius
, y
- kSquareRadius
,
24 kSquareRadius
* 2 + 1, kSquareRadius
* 2 + 1);
27 static void FillRect(pp::ImageData
* image
,
28 int left
, int top
, int width
, int height
,
30 for (int y
= std::max(0, top
);
31 y
< std::min(image
->size().height() - 1, top
+ height
);
33 for (int x
= std::max(0, left
);
34 x
< std::min(image
->size().width() - 1, left
+ width
);
36 *image
->GetAddr32(pp::Point(x
, y
)) = color
;
40 class MyInstance
: public pp::Instance
, public pp::PaintManager::Client
{
42 MyInstance(PP_Instance instance
)
43 : pp::Instance(instance
),
47 paint_manager_
.Initialize(this, this, false);
48 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE
);
51 virtual bool HandleInputEvent(const pp::InputEvent
& event
) {
52 switch (event
.GetType()) {
53 case PP_INPUTEVENT_TYPE_MOUSEDOWN
: {
54 pp::MouseInputEvent
mouse_event(event
);
55 // Update the square on a mouse down.
56 if (mouse_event
.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_LEFT
) {
57 UpdateSquare(static_cast<int>(mouse_event
.GetPosition().x()),
58 static_cast<int>(mouse_event
.GetPosition().y()));
62 case PP_INPUTEVENT_TYPE_MOUSEMOVE
: {
63 pp::MouseInputEvent
mouse_event(event
);
64 // Update the square on a drag.
65 if (mouse_event
.GetButton() == PP_INPUTEVENT_MOUSEBUTTON_LEFT
) {
66 UpdateSquare(static_cast<int>(mouse_event
.GetPosition().x()),
67 static_cast<int>(mouse_event
.GetPosition().y()));
76 virtual void DidChangeView(const pp::View
& view
) {
77 paint_manager_
.SetSize(view
.GetRect().size());
80 // PaintManager::Client implementation.
81 virtual bool OnPaint(pp::Graphics2D
& graphics_2d
,
82 const std::vector
<pp::Rect
>& paint_rects
,
83 const pp::Rect
& paint_bounds
) {
84 // Make an image just large enough to hold all dirty rects. We won't
85 // actually paint all of these pixels below, but rather just the dirty
86 // ones. Since image allocation can be somewhat heavyweight, we wouldn't
87 // want to allocate separate images in the case of multiple dirty rects.
88 pp::ImageData
updated_image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL
,
89 paint_bounds
.size(), false);
91 // We could repaint everything inside the image we made above. For this
92 // example, that would probably be the easiest thing since updates are
93 // small and typically close to each other. However, for the purposes of
94 // demonstration, here we only actually paint the pixels that changed,
95 // which may be the entire update region, or could be multiple discontigous
96 // regions inside the update region.
98 // Note that the aggregator used by the paint manager won't give us
99 // multiple regions that overlap, so we don't have to worry about double
100 // painting in this code.
101 for (size_t i
= 0; i
< paint_rects
.size(); i
++) {
102 // Since our image is just the invalid region, we need to offset the
103 // areas we paint by that much. This is just a light blue background.
104 FillRect(&updated_image
,
105 paint_rects
[i
].x() - paint_bounds
.x(),
106 paint_rects
[i
].y() - paint_bounds
.y(),
107 paint_rects
[i
].width(),
108 paint_rects
[i
].height(),
112 // Paint the square black. Because we're lazy, we do this outside of the
114 pp::Rect square
= SquareForPoint(last_x_
, last_y_
);
115 FillRect(&updated_image
,
116 square
.x() - paint_bounds
.x(),
117 square
.y() - paint_bounds
.y(),
122 graphics_2d
.PaintImageData(updated_image
, paint_bounds
.point());
127 void UpdateSquare(int x
, int y
) {
128 if (x
== last_x_
&& y
== last_y_
)
129 return; // Nothing changed.
131 // Invalidate the region around the old square which needs to be repainted
132 // because it's no longer there.
133 paint_manager_
.InvalidateRect(SquareForPoint(last_x_
, last_y_
));
135 // Update the current position.
139 // Also invalidate the region around the new square.
140 paint_manager_
.InvalidateRect(SquareForPoint(last_x_
, last_y_
));
143 pp::PaintManager paint_manager_
;
149 class MyModule
: public pp::Module
{
151 virtual pp::Instance
* CreateInstance(PP_Instance instance
) {
152 return new MyInstance(instance
);
158 // Factory function for your specialization of the Module object.
159 Module
* CreateModule() {
160 return new MyModule();