Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
|
dmichael (off chromium)
2014/05/05 22:20:32
nit: 2014
tinier nit: I've heard it's preferable t
| |
| 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 "base/debug/trace_event.h" | |
| 8 #include "ppapi/c/pp_input_event.h" | |
| 9 #include "ppapi/cpp/graphics_2d.h" | |
| 10 #include "ppapi/cpp/image_data.h" | |
| 11 #include "ppapi/cpp/input_event.h" | |
| 12 #include "ppapi/cpp/instance.h" | |
| 13 #include "ppapi/cpp/logging.h" | |
| 14 #include "ppapi/cpp/module.h" | |
| 15 #include "ppapi/cpp/private/input_event_private.h" | |
| 16 #include "ppapi/cpp/size.h" | |
| 17 #include "ppapi/cpp/view.h" | |
| 18 #include "ppapi/utility/graphics/paint_manager.h" | |
| 19 | |
| 20 pp::Rect SquareForTouchPoint(int x, int y, int radius) { | |
| 21 radius /= 2; | |
| 22 return PP_MakeRectFromXYWH(x - radius, y - radius, | |
| 23 radius * 2 + 1, radius * 2 + 1); | |
| 24 } | |
| 25 | |
| 26 static void FillRect(pp::ImageData* image, | |
| 27 int left, int top, int width, int height, | |
| 28 uint32_t color) { | |
| 29 for (int y = std::max(0, top); | |
| 30 y < std::min(image->size().height() - 1, top + height); | |
| 31 y++) { | |
| 32 for (int x = std::max(0, left); | |
| 33 x < std::min(image->size().width() - 1, left + width); | |
| 34 x++) | |
| 35 *image->GetAddr32(pp::Point(x, y)) = color; | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 class MyInstance : public pp::Instance, public pp::PaintManager::Client { | |
| 40 public: | |
| 41 MyInstance(PP_Instance instance) | |
| 42 : pp::Instance(instance), | |
| 43 paint_manager_() { | |
| 44 paint_manager_.Initialize(this, this, false); | |
| 45 RequestInputEvents(PP_INPUTEVENT_CLASS_TOUCH); | |
| 46 } | |
| 47 | |
| 48 virtual bool HandleInputEvent(const pp::InputEvent& event) { | |
| 49 switch (event.GetType()) { | |
| 50 case PP_INPUTEVENT_TYPE_TOUCHSTART: | |
| 51 case PP_INPUTEVENT_TYPE_TOUCHEND: | |
| 52 case PP_INPUTEVENT_TYPE_TOUCHCANCEL: { | |
| 53 pp::InputEventPrivate private_event(event); | |
| 54 private_event.TraceInputLatency(false); | |
| 55 return true; | |
| 56 } | |
| 57 | |
| 58 case PP_INPUTEVENT_TYPE_TOUCHMOVE: { | |
| 59 pp::TouchInputEvent touch(event); | |
| 60 uint32_t count = touch.GetTouchCount(PP_TOUCHLIST_TYPE_CHANGEDTOUCHES); | |
| 61 if (count > 0) { | |
| 62 pp::TouchPoint point = touch.GetTouchByIndex( | |
| 63 PP_TOUCHLIST_TYPE_CHANGEDTOUCHES, 0); | |
| 64 UpdateSquareTouch(static_cast<int>(point.position().x()), | |
| 65 static_cast<int>(point.position().y()), | |
| 66 static_cast<int>(point.radii().x())); | |
| 67 pp::InputEventPrivate private_event(event); | |
| 68 private_event.TraceInputLatency(true); | |
| 69 } else { | |
| 70 pp::InputEventPrivate private_event(event); | |
| 71 private_event.TraceInputLatency(false); | |
| 72 } | |
| 73 return true; | |
| 74 } | |
| 75 default: | |
| 76 return false; | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 virtual void DidChangeView(const pp::View& view) { | |
| 81 paint_manager_.SetSize(view.GetRect().size()); | |
| 82 } | |
| 83 | |
| 84 // PaintManager::Client implementation. | |
| 85 virtual bool OnPaint(pp::Graphics2D& graphics_2d, | |
| 86 const std::vector<pp::Rect>& paint_rects, | |
| 87 const pp::Rect& paint_bounds) { | |
| 88 // Make an image just large enough to hold all dirty rects. We won't | |
| 89 // actually paint all of these pixels below, but rather just the dirty | |
| 90 // ones. Since image allocation can be somewhat heavyweight, we wouldn't | |
| 91 // want to allocate separate images in the case of multiple dirty rects. | |
| 92 pp::ImageData updated_image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, | |
| 93 paint_bounds.size(), false); | |
| 94 | |
| 95 for (size_t i = 0; i < paint_rects.size(); i++) { | |
| 96 // Since our image is just the invalid region, we need to offset the | |
| 97 // areas we paint by that much. This is just a light blue background. | |
| 98 FillRect(&updated_image, | |
| 99 paint_rects[i].x(), | |
| 100 paint_rects[i].y(), | |
| 101 paint_rects[i].width(), | |
| 102 paint_rects[i].height(), | |
| 103 0xFF000000); | |
| 104 } | |
| 105 | |
| 106 graphics_2d.PaintImageData(updated_image, paint_bounds.point()); | |
| 107 return true; | |
| 108 } | |
| 109 | |
| 110 private: | |
| 111 void UpdateSquareTouch(int x, int y, int radius) { | |
| 112 paint_manager_.InvalidateRect(SquareForTouchPoint(x, y, radius)); | |
| 113 } | |
| 114 | |
| 115 pp::PaintManager paint_manager_; | |
| 116 }; | |
| 117 | |
| 118 class MyModule : public pp::Module { | |
| 119 public: | |
| 120 virtual pp::Instance* CreateInstance(PP_Instance instance) { | |
| 121 return new MyInstance(instance); | |
| 122 } | |
| 123 }; | |
| 124 | |
| 125 namespace pp { | |
| 126 | |
| 127 // Factory function for your specialization of the Module object. | |
| 128 Module* CreateModule() { | |
| 129 return new MyModule(); | |
| 130 } | |
| 131 | |
| 132 } // namespace pp | |
| OLD | NEW |