1// Copyright 2014 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_errors.h" 6#include "ppapi/cpp/core.h" 7#include "ppapi/cpp/graphics_3d.h" 8#include "ppapi/cpp/graphics_3d_client.h" 9#include "ppapi/cpp/input_event.h" 10#include "ppapi/cpp/instance.h" 11#include "ppapi/cpp/module.h" 12#include "ppapi/cpp/rect.h" 13#include "ppapi/examples/gles2_spinning_cube/spinning_cube.h" 14#include "ppapi/lib/gl/gles2/gl2ext_ppapi.h" 15#include "ppapi/utility/completion_callback_factory.h" 16 17// Use assert as a poor-man's CHECK, even in non-debug mode. 18// Since <assert.h> redefines assert on every inclusion (it doesn't use 19// include-guards), make sure this is the last file #include'd in this file. 20#undef NDEBUG 21#include <assert.h> 22 23namespace { 24 25class DemoInstance : public pp::Instance, public pp::Graphics3DClient { 26 public: 27 DemoInstance(PP_Instance instance); 28 virtual ~DemoInstance(); 29 30 // pp::Instance implementation (see PPP_Instance). 31 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]); 32 virtual void DidChangeView(const pp::Rect& position, 33 const pp::Rect& clip); 34 virtual bool HandleInputEvent(const pp::InputEvent& event) { 35 // TODO(yzshen): Handle input events. 36 return true; 37 } 38 39 // pp::Graphics3DClient implementation. 40 virtual void Graphics3DContextLost(); 41 42 private: 43 // GL-related functions. 44 void InitGL(int32_t result); 45 void Paint(int32_t result); 46 47 pp::Size plugin_size_; 48 pp::CompletionCallbackFactory<DemoInstance> callback_factory_; 49 50 // Owned data. 51 pp::Graphics3D* context_; 52 53 SpinningCube cube_; 54}; 55 56DemoInstance::DemoInstance(PP_Instance instance) 57 : pp::Instance(instance), 58 pp::Graphics3DClient(this), 59 callback_factory_(this), 60 context_(NULL) {} 61 62DemoInstance::~DemoInstance() { 63 assert(glTerminatePPAPI()); 64 delete context_; 65} 66 67bool DemoInstance::Init(uint32_t /*argc*/, 68 const char* /*argn*/[], 69 const char* /*argv*/[]) { 70 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE); 71 return !!glInitializePPAPI(pp::Module::Get()->get_browser_interface()); 72} 73 74void DemoInstance::DidChangeView( 75 const pp::Rect& position, const pp::Rect& /*clip*/) { 76 if (position.width() == 0 || position.height() == 0) 77 return; 78 plugin_size_ = position.size(); 79 80 // Initialize graphics. 81 InitGL(0); 82} 83 84void DemoInstance::Graphics3DContextLost() { 85 delete context_; 86 context_ = NULL; 87 pp::CompletionCallback cb = callback_factory_.NewCallback( 88 &DemoInstance::InitGL); 89 pp::Module::Get()->core()->CallOnMainThread(0, cb, 0); 90} 91 92void DemoInstance::InitGL(int32_t /*result*/) { 93 assert(plugin_size_.width() && plugin_size_.height()); 94 95 if (context_) { 96 context_->ResizeBuffers(plugin_size_.width(), plugin_size_.height()); 97 return; 98 } 99 int32_t context_attributes[] = { 100 PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 8, 101 PP_GRAPHICS3DATTRIB_BLUE_SIZE, 8, 102 PP_GRAPHICS3DATTRIB_GREEN_SIZE, 8, 103 PP_GRAPHICS3DATTRIB_RED_SIZE, 8, 104 PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 0, 105 PP_GRAPHICS3DATTRIB_STENCIL_SIZE, 0, 106 PP_GRAPHICS3DATTRIB_SAMPLES, 0, 107 PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS, 0, 108 PP_GRAPHICS3DATTRIB_WIDTH, plugin_size_.width(), 109 PP_GRAPHICS3DATTRIB_HEIGHT, plugin_size_.height(), 110 PP_GRAPHICS3DATTRIB_NONE, 111 }; 112 context_ = new pp::Graphics3D(this, context_attributes); 113 assert(!context_->is_null()); 114 assert(BindGraphics(*context_)); 115 116 glSetCurrentContextPPAPI(context_->pp_resource()); 117 cube_.Init(plugin_size_.width(), plugin_size_.height()); 118 Paint(PP_OK); 119} 120 121void DemoInstance::Paint(int32_t result) { 122 if (result != PP_OK || !context_) 123 return; 124 125 cube_.UpdateForTimeDelta(0.02f); 126 cube_.Draw(); 127 128 context_->SwapBuffers(callback_factory_.NewCallback(&DemoInstance::Paint)); 129} 130 131// This object is the global object representing this plugin library as long 132// as it is loaded. 133class DemoModule : public pp::Module { 134 public: 135 DemoModule() : Module() {} 136 virtual ~DemoModule() {} 137 138 virtual pp::Instance* CreateInstance(PP_Instance instance) { 139 return new DemoInstance(instance); 140 } 141}; 142 143} // anonymous namespace 144 145namespace pp { 146// Factory function for your specialization of the Module object. 147Module* CreateModule() { 148 return new DemoModule(); 149} 150} // namespace pp 151