1 // Copyright (c) 2012 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. 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 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: 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 | PP_INPUTEVENT_CLASS_TOUCH); 49 } 50 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 72 case PP_INPUTEVENT_TYPE_TOUCHSTART: { 73 pp::TouchInputEvent touch(event); 74 // Update the square on a touch down. 75 uint32_t count = touch.GetTouchCount(PP_TOUCHLIST_TYPE_CHANGEDTOUCHES); 76 for (uint32_t i = 0; i < count; ++i) { 77 pp::TouchPoint point = touch.GetTouchByIndex( 78 PP_TOUCHLIST_TYPE_CHANGEDTOUCHES, i); 79 UpdateSquare(static_cast<int>(point.position().x()), 80 static_cast<int>(point.position().y())); 81 } 82 return true; 83 } 84 case PP_INPUTEVENT_TYPE_TOUCHMOVE: 85 case PP_INPUTEVENT_TYPE_TOUCHEND: 86 case PP_INPUTEVENT_TYPE_TOUCHCANCEL: 87 return true; 88 89 default: 90 return false; 91 } 92 } 93 94 virtual void DidChangeView(const pp::View& view) { 95 paint_manager_.SetSize(view.GetRect().size()); 96 } 97 98 // PaintManager::Client implementation. 99 virtual bool OnPaint(pp::Graphics2D& graphics_2d, 100 const std::vector<pp::Rect>& paint_rects, 101 const pp::Rect& paint_bounds) { 102 // Make an image just large enough to hold all dirty rects. We won't 103 // actually paint all of these pixels below, but rather just the dirty 104 // ones. Since image allocation can be somewhat heavyweight, we wouldn't 105 // want to allocate separate images in the case of multiple dirty rects. 106 pp::ImageData updated_image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, 107 paint_bounds.size(), false); 108 109 // We could repaint everything inside the image we made above. For this 110 // example, that would probably be the easiest thing since updates are 111 // small and typically close to each other. However, for the purposes of 112 // demonstration, here we only actually paint the pixels that changed, 113 // which may be the entire update region, or could be multiple discontigous 114 // regions inside the update region. 115 // 116 // Note that the aggregator used by the paint manager won't give us 117 // multiple regions that overlap, so we don't have to worry about double 118 // painting in this code. 119 for (size_t i = 0; i < paint_rects.size(); i++) { 120 // Since our image is just the invalid region, we need to offset the 121 // areas we paint by that much. This is just a light blue background. 122 FillRect(&updated_image, 123 paint_rects[i].x() - paint_bounds.x(), 124 paint_rects[i].y() - paint_bounds.y(), 125 paint_rects[i].width(), 126 paint_rects[i].height(), 127 0xFFAAAAFF); 128 } 129 130 // Paint the square black. Because we're lazy, we do this outside of the 131 // loop above. 132 pp::Rect square = SquareForPoint(last_x_, last_y_); 133 FillRect(&updated_image, 134 square.x() - paint_bounds.x(), 135 square.y() - paint_bounds.y(), 136 square.width(), 137 square.height(), 138 0xFF000000); 139 140 graphics_2d.PaintImageData(updated_image, paint_bounds.point()); 141 return true; 142 } 143 144 private: 145 void UpdateSquare(int x, int y) { 146 if (x == last_x_ && y == last_y_) 147 return; // Nothing changed. 148 149 // Invalidate the region around the old square which needs to be repainted 150 // because it's no longer there. 151 paint_manager_.InvalidateRect(SquareForPoint(last_x_, last_y_)); 152 153 // Update the current position. 154 last_x_ = x; 155 last_y_ = y; 156 157 // Also invalidate the region around the new square. 158 paint_manager_.InvalidateRect(SquareForPoint(last_x_, last_y_)); 159 } 160 161 pp::PaintManager paint_manager_; 162 163 int last_x_; 164 int last_y_; 165 }; 166 167 class MyModule : public pp::Module { 168 public: 169 virtual pp::Instance* CreateInstance(PP_Instance instance) { 170 return new MyInstance(instance); 171 } 172 }; 173 174 namespace pp { 175 176 // Factory function for your specialization of the Module object. 177 Module* CreateModule() { 178 return new MyModule(); 179 } 180 181 } // namespace pp 182