| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "gpu/demos/framework/plugin.h" | |
| 6 | |
| 7 #include <cassert> | |
| 8 #include "gpu/demos/framework/demo_factory.h" | |
| 9 | |
| 10 using gpu::demos::Plugin; | |
| 11 | |
| 12 namespace { | |
| 13 const int32 kCommandBufferSize = 1024 * 1024; | |
| 14 NPExtensions* g_extensions = NULL; | |
| 15 | |
| 16 // Plugin class functions. | |
| 17 NPObject* PluginAllocate(NPP npp, NPClass* the_class) { | |
| 18 Plugin* plugin = new Plugin(npp); | |
| 19 return plugin; | |
| 20 } | |
| 21 | |
| 22 void PluginDeallocate(NPObject* header) { | |
| 23 Plugin* plugin = static_cast<Plugin*>(header); | |
| 24 delete plugin; | |
| 25 } | |
| 26 | |
| 27 void PluginInvalidate(NPObject* obj) { | |
| 28 } | |
| 29 | |
| 30 bool PluginHasMethod(NPObject* obj, NPIdentifier name) { | |
| 31 return false; | |
| 32 } | |
| 33 | |
| 34 bool PluginInvoke(NPObject* header, | |
| 35 NPIdentifier name, | |
| 36 const NPVariant* args, uint32 arg_count, | |
| 37 NPVariant* result) { | |
| 38 return false; | |
| 39 } | |
| 40 | |
| 41 bool PluginInvokeDefault(NPObject* obj, | |
| 42 const NPVariant* args, uint32 arg_count, | |
| 43 NPVariant* result) { | |
| 44 VOID_TO_NPVARIANT(*result); | |
| 45 return true; | |
| 46 } | |
| 47 | |
| 48 bool PluginHasProperty(NPObject* obj, NPIdentifier name) { | |
| 49 return false; | |
| 50 } | |
| 51 | |
| 52 bool PluginGetProperty(NPObject* obj, | |
| 53 NPIdentifier name, | |
| 54 NPVariant* result) { | |
| 55 return false; | |
| 56 } | |
| 57 | |
| 58 bool PluginSetProperty(NPObject* obj, | |
| 59 NPIdentifier name, | |
| 60 const NPVariant* variant) { | |
| 61 return false; | |
| 62 } | |
| 63 | |
| 64 NPClass plugin_class = { | |
| 65 NP_CLASS_STRUCT_VERSION, | |
| 66 PluginAllocate, | |
| 67 PluginDeallocate, | |
| 68 PluginInvalidate, | |
| 69 PluginHasMethod, | |
| 70 PluginInvoke, | |
| 71 PluginInvokeDefault, | |
| 72 PluginHasProperty, | |
| 73 PluginGetProperty, | |
| 74 PluginSetProperty, | |
| 75 }; | |
| 76 | |
| 77 void TickCallback(void* data) { | |
| 78 reinterpret_cast<gpu::demos::Plugin*>(data)->Tick(); | |
| 79 } | |
| 80 | |
| 81 void RepaintCallback(NPP npp, NPDeviceContext3D* /* context */) { | |
| 82 Plugin* plugin = static_cast<Plugin*>(npp->pdata); | |
| 83 plugin->Paint(); | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 namespace gpu { | |
| 88 namespace demos { | |
| 89 | |
| 90 NPNetscapeFuncs* g_browser; | |
| 91 | |
| 92 Plugin::Plugin(NPP npp) | |
| 93 : npp_(npp), | |
| 94 device3d_(NULL), | |
| 95 pgl_context_(NULL), | |
| 96 demo_(CreateDemo()) { | |
| 97 memset(&context3d_, 0, sizeof(context3d_)); | |
| 98 } | |
| 99 | |
| 100 Plugin::~Plugin() { | |
| 101 // Destroy demo while GL context is current and before it is destroyed. | |
| 102 pglMakeCurrent(pgl_context_); | |
| 103 demo_.reset(); | |
| 104 pglMakeCurrent(PGL_NO_CONTEXT); | |
| 105 | |
| 106 DestroyContext(); | |
| 107 } | |
| 108 | |
| 109 NPClass* Plugin::GetPluginClass() { | |
| 110 return &plugin_class; | |
| 111 } | |
| 112 | |
| 113 void Plugin::New(NPMIMEType pluginType, | |
| 114 int16 argc, char* argn[], char* argv[]) { | |
| 115 if (!g_extensions) { | |
| 116 g_browser->getvalue(npp_, NPNVPepperExtensions, &g_extensions); | |
| 117 assert(g_extensions); | |
| 118 } | |
| 119 | |
| 120 device3d_ = g_extensions->acquireDevice(npp_, NPPepper3DDevice); | |
| 121 assert(device3d_); | |
| 122 } | |
| 123 | |
| 124 void Plugin::SetWindow(const NPWindow& window) { | |
| 125 demo_->InitWindowSize(window.width, window.height); | |
| 126 | |
| 127 if (!pgl_context_) { | |
| 128 if (!CreateContext()) | |
| 129 return; | |
| 130 | |
| 131 // Schedule first call to Tick. | |
| 132 if (demo_->IsAnimated()) | |
| 133 g_browser->pluginthreadasynccall(npp_, TickCallback, this); | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 int32 Plugin::HandleEvent(const NPPepperEvent& event) { | |
| 138 return 0; | |
| 139 } | |
| 140 | |
| 141 void Plugin::Tick() { | |
| 142 Paint(); | |
| 143 | |
| 144 // Schedule another call to Tick. | |
| 145 g_browser->pluginthreadasynccall(npp_, TickCallback, this); | |
| 146 } | |
| 147 | |
| 148 void Plugin::Paint() { | |
| 149 if (!pglMakeCurrent(pgl_context_) && pglGetError() == PGL_CONTEXT_LOST) { | |
| 150 DestroyContext(); | |
| 151 if (!CreateContext()) | |
| 152 return; | |
| 153 | |
| 154 pglMakeCurrent(pgl_context_); | |
| 155 } | |
| 156 | |
| 157 demo_->Draw(); | |
| 158 pglSwapBuffers(); | |
| 159 pglMakeCurrent(PGL_NO_CONTEXT); | |
| 160 } | |
| 161 | |
| 162 bool Plugin::CreateContext() { | |
| 163 assert(!pgl_context_); | |
| 164 | |
| 165 // Initialize a 3D context. | |
| 166 NPDeviceContext3DConfig config; | |
| 167 config.commandBufferSize = kCommandBufferSize; | |
| 168 if (NPERR_NO_ERROR != device3d_->initializeContext(npp_, | |
| 169 &config, | |
| 170 &context3d_)) { | |
| 171 DestroyContext(); | |
| 172 return false; | |
| 173 } | |
| 174 | |
| 175 context3d_.repaintCallback = RepaintCallback; | |
| 176 | |
| 177 // Create a PGL context. | |
| 178 pgl_context_ = pglCreateContext(npp_, device3d_, &context3d_); | |
| 179 if (!pgl_context_) { | |
| 180 DestroyContext(); | |
| 181 return false; | |
| 182 } | |
| 183 | |
| 184 // Initialize demo. | |
| 185 pglMakeCurrent(pgl_context_); | |
| 186 if (!demo_->InitGL()) { | |
| 187 DestroyContext(); | |
| 188 return false; | |
| 189 } | |
| 190 | |
| 191 pglMakeCurrent(PGL_NO_CONTEXT); | |
| 192 | |
| 193 return true; | |
| 194 } | |
| 195 | |
| 196 void Plugin::DestroyContext() { | |
| 197 if (pgl_context_) { | |
| 198 pglDestroyContext(pgl_context_); | |
| 199 pgl_context_ = NULL; | |
| 200 } | |
| 201 | |
| 202 if (context3d_.commandBuffer) { | |
| 203 device3d_->destroyContext(npp_, &context3d_); | |
| 204 memset(&context3d_, 0, sizeof(context3d_)); | |
| 205 } | |
| 206 } | |
| 207 | |
| 208 } // namespace demos | |
| 209 } // namespace gpu | |
| OLD | NEW |