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