| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <GLES2/gl2.h> |
| 6 |
| 5 #include "base/at_exit.h" | 7 #include "base/at_exit.h" |
| 6 #include "base/scoped_ptr.h" | 8 #include "base/scoped_ptr.h" |
| 7 #include "gpu/demos/framework/demo.h" | 9 #include "gpu/demos/framework/demo.h" |
| 8 #include "gpu/demos/framework/demo_factory.h" | 10 #include "gpu/demos/framework/demo_factory.h" |
| 9 #include "ppapi/cpp/completion_callback.h" | 11 #include "ppapi/cpp/completion_callback.h" |
| 10 #include "ppapi/cpp/instance.h" | 12 #include "ppapi/cpp/instance.h" |
| 11 #include "ppapi/cpp/dev/graphics_3d_dev.h" | 13 #include "ppapi/cpp/dev/graphics_3d_dev.h" |
| 12 #include "ppapi/cpp/module.h" | 14 #include "ppapi/cpp/module.h" |
| 13 #include "ppapi/cpp/rect.h" | 15 #include "ppapi/cpp/rect.h" |
| 14 #include "ppapi/cpp/size.h" | 16 #include "ppapi/cpp/size.h" |
| 17 #include "ppapi/lib/gl/gles2/gl2ext_ppapi.h" |
| 15 | 18 |
| 16 namespace gpu { | 19 namespace gpu { |
| 17 namespace demos { | 20 namespace demos { |
| 18 | 21 |
| 19 class PluginInstance : public pp::Instance { | 22 class PluginInstance : public pp::Instance { |
| 20 public: | 23 public: |
| 21 PluginInstance(PP_Instance instance, pp::Module* module) | 24 PluginInstance(PP_Instance instance, pp::Module* module) |
| 22 : pp::Instance(instance), | 25 : pp::Instance(instance), |
| 23 module_(module), | 26 module_(module), |
| 24 demo_(CreateDemo()) { | 27 demo_(CreateDemo()) { |
| 25 // Set the callback object outside of the initializer list to avoid a | 28 // Set the callback object outside of the initializer list to avoid a |
| 26 // compiler warning about using "this" in an initializer list. | 29 // compiler warning about using "this" in an initializer list. |
| 27 callback_factory_.Initialize(this); | 30 callback_factory_.Initialize(this); |
| 28 } | 31 } |
| 29 | 32 |
| 30 ~PluginInstance() { | 33 ~PluginInstance() { |
| 31 if (!graphics_.is_null()) { | 34 if (!graphics_.is_null()) { |
| 32 graphics_.MakeCurrent(); | 35 glSetCurrentContextPPAPI(graphics_.pp_resource()); |
| 33 demo_.reset(); | 36 demo_.reset(); |
| 34 pp::Graphics3D_Dev::ResetCurrent(); | 37 glSetCurrentContextPPAPI(0); |
| 35 } | 38 } |
| 36 } | 39 } |
| 37 | 40 |
| 38 virtual void DidChangeView(const pp::Rect& position, | 41 virtual void DidChangeView(const pp::Rect& position, |
| 39 const pp::Rect& /*clip*/) { | 42 const pp::Rect& /*clip*/) { |
| 40 if (size_ == position.size()) | 43 if (size_ == position.size()) |
| 41 return; | 44 return; |
| 42 | 45 |
| 43 size_ = position.size(); | 46 size_ = position.size(); |
| 44 demo_->InitWindowSize(size_.width(), size_.height()); | 47 demo_->InitWindowSize(size_.width(), size_.height()); |
| 45 | 48 |
| 46 if (graphics_.is_null()) { | 49 if (graphics_.is_null()) { |
| 47 graphics_ = pp::Graphics3D_Dev(*this, 0, NULL, NULL); | 50 graphics_ = pp::Graphics3D_Dev(*this, 0, NULL, NULL); |
| 48 if (graphics_.is_null()) | 51 if (graphics_.is_null()) |
| 49 return; | 52 return; |
| 50 | 53 |
| 51 if (!pp::Instance::BindGraphics(graphics_)) | 54 if (!pp::Instance::BindGraphics(graphics_)) |
| 52 return; | 55 return; |
| 53 | 56 |
| 54 graphics_.MakeCurrent(); | 57 glSetCurrentContextPPAPI(graphics_.pp_resource()); |
| 55 demo_->InitGL(); | 58 demo_->InitGL(); |
| 56 pp::Graphics3D_Dev::ResetCurrent(); | 59 glSetCurrentContextPPAPI(0); |
| 57 } | 60 } |
| 58 | 61 |
| 59 if (demo_->IsAnimated()) | 62 if (demo_->IsAnimated()) |
| 60 Animate(0); | 63 Animate(0); |
| 61 else | 64 else |
| 62 Paint(); | 65 Paint(); |
| 63 } | 66 } |
| 64 | 67 |
| 65 void Paint() { | 68 void Paint() { |
| 66 graphics_.MakeCurrent(); | 69 glSetCurrentContextPPAPI(graphics_.pp_resource()); |
| 67 demo_->Draw(); | 70 demo_->Draw(); |
| 68 graphics_.SwapBuffers(); | 71 graphics_.SwapBuffers(); |
| 69 pp::Graphics3D_Dev::ResetCurrent(); | 72 glSetCurrentContextPPAPI(0); |
| 70 } | 73 } |
| 71 | 74 |
| 72 private: | 75 private: |
| 73 void Animate(int delay) { | 76 void Animate(int delay) { |
| 74 Paint(); | 77 Paint(); |
| 75 module_->core()->CallOnMainThread(delay, | 78 module_->core()->CallOnMainThread(delay, |
| 76 callback_factory_.NewCallback(&PluginInstance::Animate), delay); | 79 callback_factory_.NewCallback(&PluginInstance::Animate), delay); |
| 77 } | 80 } |
| 78 | 81 |
| 79 pp::Module* module_; | 82 pp::Module* module_; |
| 80 scoped_ptr<Demo> demo_; | 83 scoped_ptr<Demo> demo_; |
| 81 pp::Graphics3D_Dev graphics_; | 84 pp::Graphics3D_Dev graphics_; |
| 82 pp::Size size_; | 85 pp::Size size_; |
| 83 pp::CompletionCallbackFactory<PluginInstance> callback_factory_; | 86 pp::CompletionCallbackFactory<PluginInstance> callback_factory_; |
| 84 }; | 87 }; |
| 85 | 88 |
| 86 class PluginModule : public pp::Module { | 89 class PluginModule : public pp::Module { |
| 87 public: | 90 public: |
| 88 PluginModule() : pp::Module(), at_exit_manager_(new base::AtExitManager) {} | 91 PluginModule() : at_exit_manager_(new base::AtExitManager) {} |
| 92 ~PluginModule() { |
| 93 glTerminatePPAPI(); |
| 94 } |
| 95 |
| 96 virtual bool Init() { |
| 97 return glInitializePPAPI(get_browser_interface()) == GL_TRUE ? true : false; |
| 98 } |
| 89 | 99 |
| 90 virtual pp::Instance* CreateInstance(PP_Instance instance) { | 100 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| 91 return new PluginInstance(instance, this); | 101 return new PluginInstance(instance, this); |
| 92 } | 102 } |
| 93 | 103 |
| 94 private: | 104 private: |
| 95 scoped_ptr<base::AtExitManager> at_exit_manager_; | 105 scoped_ptr<base::AtExitManager> at_exit_manager_; |
| 96 }; | 106 }; |
| 97 | 107 |
| 98 } // namespace demos | 108 } // namespace demos |
| 99 } // namespace gpu | 109 } // namespace gpu |
| 100 | 110 |
| 101 namespace pp { | 111 namespace pp { |
| 102 | 112 |
| 103 Module* CreateModule() { | 113 Module* CreateModule() { |
| 104 return new gpu::demos::PluginModule(); | 114 return new gpu::demos::PluginModule(); |
| 105 } | 115 } |
| 106 | 116 |
| 107 } // namespace pp | 117 } // namespace pp |
| 108 | 118 |
| OLD | NEW |