OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Native Client 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 |
| 6 #include <stdint.h> |
| 7 |
| 8 #include <cmath> |
| 9 #include <limits> |
| 10 #include <string> |
| 11 |
| 12 #include <nacl/nacl_check.h> |
| 13 #include <nacl/nacl_log.h> |
| 14 |
| 15 #include "ppapi/c/pp_rect.h" |
| 16 #include "ppapi/c/pp_bool.h" |
| 17 #include "ppapi/c/pp_errors.h" |
| 18 #include "ppapi/cpp/rect.h" |
| 19 #include "ppapi/cpp/graphics_2d.h" |
| 20 #include "ppapi/cpp/image_data.h" |
| 21 #include "ppapi/cpp/completion_callback.h" |
| 22 #include "ppapi/cpp/instance.h" |
| 23 #include "ppapi/cpp/module.h" |
| 24 |
| 25 const uint32_t kDefaultColor = 0x2266aa; |
| 26 |
| 27 void FillRect(pp::ImageData* image, 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 { |
| 40 private: |
| 41 uint32_t color_; |
| 42 pp::Size size_; |
| 43 pp::Graphics2D device_context_; |
| 44 |
| 45 void ParseArgs(uint32_t argc, const char* argn[], const char* argv[]) { |
| 46 for (uint32_t i = 0; i < argc; ++i) { |
| 47 const std::string tag = argn[i]; |
| 48 if (tag == "color") color_ = strtol(argv[i], 0, 0); |
| 49 } |
| 50 } |
| 51 |
| 52 static void FlushCallback(void* thiz, int32_t result) { |
| 53 // If necessary we can get at the instabce like so: |
| 54 // MyInstance* instance = static_cast<MyInstance*>(thiz); |
| 55 } |
| 56 |
| 57 public: |
| 58 explicit MyInstance(PP_Instance instance) |
| 59 : pp::Instance(instance), color_(kDefaultColor), size_(0, 0) {} |
| 60 |
| 61 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { |
| 62 ParseArgs(argc, argn, argv); |
| 63 return true; |
| 64 } |
| 65 |
| 66 virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) { |
| 67 if (position.size().width() == size_.width() && |
| 68 position.size().height() == size_.height()) { |
| 69 // No change. We don't care about the position, only the size. |
| 70 return; |
| 71 } |
| 72 |
| 73 device_context_ = pp::Graphics2D(this, position.size(), false); |
| 74 CHECK(BindGraphics(device_context_)); |
| 75 size_ = position.size(); |
| 76 |
| 77 pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, size_, true); |
| 78 CHECK(!image.is_null()); |
| 79 FillRect(&image, 0, 0, size_.width(), size_.height(), color_); |
| 80 |
| 81 device_context_.ReplaceContents(&image); |
| 82 pp::CompletionCallback cc(FlushCallback, this); |
| 83 |
| 84 device_context_.Flush(cc); |
| 85 } |
| 86 }; |
| 87 |
| 88 |
| 89 class MyModule : public pp::Module { |
| 90 public: |
| 91 // Override CreateInstance to create your customized Instance object. |
| 92 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| 93 return new MyInstance(instance); |
| 94 } |
| 95 }; |
| 96 |
| 97 namespace pp { |
| 98 |
| 99 // Factory function for your specialization of the Module object. |
| 100 Module* CreateModule() { |
| 101 return new MyModule(); |
| 102 } |
| 103 |
| 104 } // namespace pp |
OLD | NEW |