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