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.
4
5 #include <algorithm>
6
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"
16
17 // Number of pixels to each side of the center of the square that we draw.
18 static const int kSquareRadius = 2;
19
20 // We identify our square by the center point. This computes the rect for the
21 // square given that point.
SquareForPoint(int x,int y)22 pp::Rect SquareForPoint(int x, int y) {
23 return PP_MakeRectFromXYWH(x - kSquareRadius, y - kSquareRadius,
24 kSquareRadius * 2 + 1, kSquareRadius * 2 + 1);
25 }
26
FillRect(pp::ImageData * image,int left,int top,int width,int height,uint32_t color)27 static void FillRect(pp::ImageData* image,
28 int left, int top, int width, int height,
29 uint32_t color) {
30 for (int y = std::max(0, top);
31 y < std::min(image->size().height() - 1, top + height);
32 y++) {
33 for (int x = std::max(0, left);
34 x < std::min(image->size().width() - 1, left + width);
35 x++)
36 *image->GetAddr32(pp::Point(x, y)) = color;
37 }
38 }
39
40 class MyInstance : public pp::Instance, public pp::PaintManager::Client {
41 public:
MyInstance(PP_Instance instance)42 MyInstance(PP_Instance instance)
43 : pp::Instance(instance),
44 paint_manager_(),
45 last_x_(0),
46 last_y_(0) {
47 paint_manager_.Initialize(this, this, false);
48 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE);
49 }
50
HandleInputEvent(const pp::InputEvent & event)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()));
59 }
60 return true;
61 }
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()));
68 }
69 return true;
70 }
71 default:
72 return false;
73 }
74 }
75
DidChangeView(const pp::View & view)76 virtual void DidChangeView(const pp::View& view) {
77 paint_manager_.SetSize(view.GetRect().size());
78 }
79
80 // PaintManager::Client implementation.
OnPaint(pp::Graphics2D & graphics_2d,const std::vector<pp::Rect> & paint_rects,const pp::Rect & paint_bounds)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);
90
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.
97 //
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(),
109 0xFFAAAAFF);
110 }
111
112 // Paint the square black. Because we're lazy, we do this outside of the
113 // loop above.
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(),
118 square.width(),
119 square.height(),
120 0xFF000000);
121
122 graphics_2d.PaintImageData(updated_image, paint_bounds.point());
123 return true;
124 }
125
126 private:
UpdateSquare(int x,int y)127 void UpdateSquare(int x, int y) {
128 if (x == last_x_ && y == last_y_)
129 return; // Nothing changed.
130
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_));
134
135 // Update the current position.
136 last_x_ = x;
137 last_y_ = y;
138
139 // Also invalidate the region around the new square.
140 paint_manager_.InvalidateRect(SquareForPoint(last_x_, last_y_));
141 }
142
143 pp::PaintManager paint_manager_;
144
145 int last_x_;
146 int last_y_;
147 };
148
149 class MyModule : public pp::Module {
150 public:
CreateInstance(PP_Instance instance)151 virtual pp::Instance* CreateInstance(PP_Instance instance) {
152 return new MyInstance(instance);
153 }
154 };
155
156 namespace pp {
157
158 // Factory function for your specialization of the Module object.
CreateModule()159 Module* CreateModule() {
160 return new MyModule();
161 }
162
163 } // namespace pp
164