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