| OLD | NEW |
| (Empty) | |
| 1 /* |
| 2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. |
| 5 */ |
| 6 |
| 7 // NaCl Earth demo |
| 8 // Pepper code in C++ |
| 9 |
| 10 #include "native_client/tests/earth/earth.h" |
| 11 |
| 12 // Pepper includes |
| 13 #include "ppapi/c/pp_bool.h" |
| 14 #include "ppapi/c/pp_errors.h" |
| 15 #include "ppapi/cpp/completion_callback.h" |
| 16 #include "ppapi/cpp/graphics_2d.h" |
| 17 #include "ppapi/cpp/image_data.h" |
| 18 #include "ppapi/cpp/instance.h" |
| 19 #include "ppapi/cpp/module.h" |
| 20 #include "ppapi/cpp/rect.h" |
| 21 #include "ppapi/cpp/size.h" |
| 22 |
| 23 const int kNumberOfImages = 2; |
| 24 |
| 25 void RepaintCallback(void* data, int32_t result); |
| 26 |
| 27 class GlobeInstance : public pp::Instance { |
| 28 public: |
| 29 explicit GlobeInstance(PP_Instance instance) : pp::Instance(instance), |
| 30 ready_(false), |
| 31 window_width_(0), |
| 32 window_height_(0), |
| 33 which_image_(0) { |
| 34 DebugPrintf("GlobeInstance::GlobeInstance()\n"); |
| 35 } |
| 36 |
| 37 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { |
| 38 Earth_Init(argc, argn, argv); |
| 39 |
| 40 return true; |
| 41 } |
| 42 |
| 43 virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) { |
| 44 size_ = position.size(); |
| 45 if (false == ready_) { |
| 46 // Create double buffered image data. |
| 47 // Note: This example does not use transparent pixels. All pixels are |
| 48 // written into the framebuffer with alpha set to 255 (opaque) |
| 49 // Note: Pepper uses premultiplied alpha. |
| 50 for (int i = 0; i < kNumberOfImages; ++i) { |
| 51 pixel_buffer_[i] = new pp::ImageData(this, |
| 52 PP_IMAGEDATAFORMAT_BGRA_PREMUL, |
| 53 size_, PP_TRUE); |
| 54 if (!pixel_buffer_[i]) { |
| 55 DebugPrintf("couldn't allocate pixel_buffer_.\n"); |
| 56 return; |
| 57 } |
| 58 } |
| 59 graphics_2d_context_ = new pp::Graphics2D(this, size_, false); |
| 60 if (!BindGraphics(*graphics_2d_context_)) { |
| 61 DebugPrintf("couldn't bind the device context.\n"); |
| 62 return; |
| 63 } |
| 64 ready_ = true; |
| 65 if (window_width_ != position.size().width() || |
| 66 window_height_ != position.size().height()) { |
| 67 // Got a resize, repaint the plugin. |
| 68 window_width_ = position.size().width(); |
| 69 window_height_ = position.size().height(); |
| 70 Repaint(); |
| 71 } |
| 72 } |
| 73 } |
| 74 |
| 75 void Repaint() { |
| 76 if (ready_ != true) return; |
| 77 |
| 78 int show = which_image_; |
| 79 |
| 80 // Wait for rendering to complete. |
| 81 Earth_Sync(); |
| 82 |
| 83 // Don't use ReplaceContents; it causes the image to flicker! |
| 84 graphics_2d_context_->PaintImageData(*pixel_buffer_[show], pp::Point()); |
| 85 graphics_2d_context_->Flush(pp::CompletionCallback(&RepaintCallback, this)); |
| 86 |
| 87 int render = (which_image_ + 1) % kNumberOfImages; |
| 88 |
| 89 // Start rendering into other image while presenting. |
| 90 Earth_Draw(static_cast<uint32_t*>(pixel_buffer_[render]->data()), |
| 91 window_width_, window_height_); |
| 92 |
| 93 which_image_ = render; |
| 94 } |
| 95 |
| 96 private: |
| 97 bool ready_; |
| 98 int window_width_; |
| 99 int window_height_; |
| 100 int which_image_; |
| 101 pp::Size size_; |
| 102 pp::ImageData* pixel_buffer_[kNumberOfImages]; |
| 103 pp::Graphics2D* graphics_2d_context_; |
| 104 }; |
| 105 |
| 106 void RepaintCallback(void* data, int32_t result) { |
| 107 static_cast<GlobeInstance*>(data)->Repaint(); |
| 108 } |
| 109 |
| 110 class GlobeModule : public pp::Module { |
| 111 public: |
| 112 // Override CreateInstance to create your customized Instance object. |
| 113 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| 114 return new GlobeInstance(instance); |
| 115 } |
| 116 }; |
| 117 |
| 118 namespace pp { |
| 119 |
| 120 // factory function for your specialization of the Module object |
| 121 Module* CreateModule() { |
| 122 Module* mm; |
| 123 mm = new GlobeModule(); |
| 124 return mm; |
| 125 } |
| 126 |
| 127 } // namespace pp |
| OLD | NEW |