OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 // Needed on Windows to get |M_PI| from math.h. | 5 // Needed on Windows to get |M_PI| from math.h. |
6 #ifdef _WIN32 | 6 #ifdef _WIN32 |
7 #define _USE_MATH_DEFINES | 7 #define _USE_MATH_DEFINES |
8 #endif | 8 #endif |
9 | 9 |
10 #include <math.h> | 10 #include <math.h> |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 virtual bool HandleInputEvent(const pp::InputEvent& event); | 65 virtual bool HandleInputEvent(const pp::InputEvent& event); |
66 | 66 |
67 // pp::Graphics3DClient implementation. | 67 // pp::Graphics3DClient implementation. |
68 virtual void Graphics3DContextLost(); | 68 virtual void Graphics3DContextLost(); |
69 | 69 |
70 private: | 70 private: |
71 // GL-related functions. | 71 // GL-related functions. |
72 void InitGL(int32_t result); | 72 void InitGL(int32_t result); |
73 GLuint PrepareFramebuffer(); | 73 GLuint PrepareFramebuffer(); |
74 pp::ImageData PrepareImage(); | 74 pp::ImageData PrepareImage(); |
75 void PrepareLayers(int32_t frame); | |
76 void Paint(int32_t result, int32_t frame); | 75 void Paint(int32_t result, int32_t frame); |
77 void OnTextureReleased(int32_t result, GLuint texture); | 76 void OnTextureReleased(int32_t result, GLuint texture); |
78 void OnImageReleased(int32_t result, const pp::ImageData& image); | 77 void OnImageReleased(int32_t result, const pp::ImageData& image); |
79 | 78 |
80 pp::CompletionCallbackFactory<DemoInstance> callback_factory_; | 79 pp::CompletionCallbackFactory<DemoInstance> callback_factory_; |
81 | 80 |
82 // Owned data. | 81 // Owned data. |
83 pp::Graphics3D* context_; | 82 pp::Graphics3D* context_; |
84 | 83 |
85 GLuint fbo_; | 84 GLuint fbo_; |
(...skipping 14 matching lines...) Expand all Loading... |
100 SpinningCube* cube_; | 99 SpinningCube* cube_; |
101 }; | 100 }; |
102 | 101 |
103 DemoInstance::DemoInstance(PP_Instance instance) | 102 DemoInstance::DemoInstance(PP_Instance instance) |
104 : pp::Instance(instance), | 103 : pp::Instance(instance), |
105 pp::Graphics3DClient(this), | 104 pp::Graphics3DClient(this), |
106 callback_factory_(this), | 105 callback_factory_(this), |
107 context_(NULL), | 106 context_(NULL), |
108 fbo_(0), | 107 fbo_(0), |
109 rbo_(0), | 108 rbo_(0), |
110 rebuild_layers_(true), | 109 compositor_(this), |
| 110 rebuild_layers_(false), |
111 total_resource_(0), | 111 total_resource_(0), |
112 cube_(new SpinningCube()) { | 112 cube_(new SpinningCube()) { |
113 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE); | 113 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE); |
114 } | 114 } |
115 | 115 |
116 DemoInstance::~DemoInstance() { | 116 DemoInstance::~DemoInstance() { |
117 delete cube_; | 117 delete cube_; |
118 assert(glTerminatePPAPI()); | 118 assert(glTerminatePPAPI()); |
119 delete context_; | 119 delete context_; |
120 } | 120 } |
(...skipping 19 matching lines...) Expand all Loading... |
140 return true; | 140 return true; |
141 default: | 141 default: |
142 break; | 142 break; |
143 } | 143 } |
144 return false; | 144 return false; |
145 } | 145 } |
146 | 146 |
147 void DemoInstance::Graphics3DContextLost() { | 147 void DemoInstance::Graphics3DContextLost() { |
148 fbo_ = 0; | 148 fbo_ = 0; |
149 rbo_ = 0; | 149 rbo_ = 0; |
150 rebuild_layers_ = true; | 150 compositor_.ResetLayers(); |
| 151 color_layer_ = pp::CompositorLayer(); |
| 152 stable_texture_layer_ = pp::CompositorLayer(); |
| 153 texture_layer_ = pp::CompositorLayer(); |
| 154 image_layer_ = pp::CompositorLayer(); |
151 total_resource_ -= static_cast<int32_t>(textures_.size()); | 155 total_resource_ -= static_cast<int32_t>(textures_.size()); |
152 textures_.clear(); | 156 textures_.clear(); |
153 delete context_; | 157 delete context_; |
154 context_ = NULL; | 158 context_ = NULL; |
155 cube_->OnGLContextLost(); | 159 cube_->OnGLContextLost(); |
156 pp::CompletionCallback cb = callback_factory_.NewCallback( | 160 pp::CompletionCallback cb = callback_factory_.NewCallback( |
157 &DemoInstance::InitGL); | 161 &DemoInstance::InitGL); |
158 pp::Module::Get()->core()->CallOnMainThread(0, cb, 0); | 162 pp::Module::Get()->core()->CallOnMainThread(0, cb, 0); |
159 } | 163 } |
160 | 164 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 pp::Size(kImageWidth, kImageHeight), | 253 pp::Size(kImageWidth, kImageHeight), |
250 false); | 254 false); |
251 } | 255 } |
252 pp::ImageData image = images_.back(); | 256 pp::ImageData image = images_.back(); |
253 images_.pop_back(); | 257 images_.pop_back(); |
254 return image; | 258 return image; |
255 } | 259 } |
256 | 260 |
257 void DemoInstance::Paint(int32_t result, int32_t frame) { | 261 void DemoInstance::Paint(int32_t result, int32_t frame) { |
258 assert(result == PP_OK); | 262 assert(result == PP_OK); |
| 263 |
259 if (result != PP_OK || !context_) | 264 if (result != PP_OK || !context_) |
260 return; | 265 return; |
261 | 266 |
| 267 int32_t rv; |
| 268 |
262 if (rebuild_layers_) { | 269 if (rebuild_layers_) { |
263 compositor_ = pp::Compositor(this); | 270 compositor_.ResetLayers(); |
264 assert(BindGraphics(compositor_)); | |
265 color_layer_ = pp::CompositorLayer(); | 271 color_layer_ = pp::CompositorLayer(); |
266 stable_texture_layer_ = pp::CompositorLayer(); | 272 stable_texture_layer_ = pp::CompositorLayer(); |
267 texture_layer_ = pp::CompositorLayer(); | 273 texture_layer_ = pp::CompositorLayer(); |
268 image_layer_ = pp::CompositorLayer(); | 274 image_layer_ = pp::CompositorLayer(); |
269 frame = 0; | 275 frame = 0; |
270 rebuild_layers_ = false; | 276 rebuild_layers_ = false; |
271 } | 277 } |
272 | 278 |
273 PrepareLayers(frame); | |
274 | |
275 int32_t rv = compositor_.CommitLayers( | |
276 callback_factory_.NewCallback(&DemoInstance::Paint, ++frame)); | |
277 assert(rv == PP_OK_COMPLETIONPENDING); | |
278 | |
279 pp::VarDictionary dict; | |
280 dict.Set("total_resource", total_resource_); | |
281 size_t free_resource = textures_.size() + images_.size(); | |
282 dict.Set("free_resource", static_cast<int32_t>(free_resource)); | |
283 PostMessage(dict); | |
284 } | |
285 | |
286 void DemoInstance::PrepareLayers(int32_t frame) { | |
287 int32_t rv; | |
288 float factor_sin = sin(M_PI / 180 * frame); | 279 float factor_sin = sin(M_PI / 180 * frame); |
289 float factor_cos = cos(M_PI / 180 * frame); | 280 float factor_cos = cos(M_PI / 180 * frame); |
290 { | 281 { |
291 // Set the background color layer. | 282 // Set the background color layer. |
292 if (color_layer_.is_null()) { | 283 if (color_layer_.is_null()) { |
293 color_layer_ = compositor_.AddLayer(); | 284 color_layer_ = compositor_.AddLayer(); |
294 assert(!color_layer_.is_null()); | 285 assert(!color_layer_.is_null()); |
295 static const float transform[16] = { | 286 static const float transform[16] = { |
296 1.0f, 0.0f, 0.0f, 0.0f, | 287 1.0f, 0.0f, 0.0f, 0.0f, |
297 0.0f, 1.0f, 0.0f, 0.0f, | 288 0.0f, 1.0f, 0.0f, 0.0f, |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 GLuint texture = PrepareFramebuffer(); | 388 GLuint texture = PrepareFramebuffer(); |
398 cube_->UpdateForTimeDelta(0.02f); | 389 cube_->UpdateForTimeDelta(0.02f); |
399 cube_->Draw(); | 390 cube_->Draw(); |
400 rv = texture_layer_.SetTexture(*context_, texture, pp::Size(400, 400), | 391 rv = texture_layer_.SetTexture(*context_, texture, pp::Size(400, 400), |
401 callback_factory_.NewCallback(&DemoInstance::OnTextureReleased, | 392 callback_factory_.NewCallback(&DemoInstance::OnTextureReleased, |
402 texture)); | 393 texture)); |
403 assert(rv == PP_OK_COMPLETIONPENDING); | 394 assert(rv == PP_OK_COMPLETIONPENDING); |
404 rv = texture_layer_.SetPremultipliedAlpha(PP_FALSE); | 395 rv = texture_layer_.SetPremultipliedAlpha(PP_FALSE); |
405 assert(rv == PP_OK); | 396 assert(rv == PP_OK); |
406 } | 397 } |
| 398 |
| 399 rv = compositor_.CommitLayers( |
| 400 callback_factory_.NewCallback(&DemoInstance::Paint, ++frame)); |
| 401 assert(rv == PP_OK_COMPLETIONPENDING); |
| 402 |
| 403 pp::VarDictionary dict; |
| 404 dict.Set(pp::Var("total_resource"), pp::Var(total_resource_)); |
| 405 dict.Set(pp::Var("free_resource"), |
| 406 pp::Var((int32_t)(textures_.size() + images_.size()))); |
| 407 PostMessage(dict); |
407 } | 408 } |
408 | 409 |
409 void DemoInstance::OnTextureReleased(int32_t result, GLuint texture) { | 410 void DemoInstance::OnTextureReleased(int32_t result, GLuint texture) { |
410 if (result == PP_OK) { | 411 if (result == PP_OK) |
411 textures_.push_back(texture); | 412 textures_.push_back(texture); |
412 } else { | |
413 glDeleteTextures(1, &texture); | |
414 total_resource_--; | |
415 } | |
416 } | 413 } |
417 | 414 |
418 void DemoInstance::OnImageReleased(int32_t result, const pp::ImageData& image) { | 415 void DemoInstance::OnImageReleased(int32_t result, const pp::ImageData& image) { |
419 if (result == PP_OK) { | 416 if (result == PP_OK) |
420 images_.push_back(image); | 417 images_.push_back(image); |
421 } else { | |
422 total_resource_--; | |
423 } | |
424 } | 418 } |
425 | 419 |
426 // This object is the global object representing this plugin library as long | 420 // This object is the global object representing this plugin library as long |
427 // as it is loaded. | 421 // as it is loaded. |
428 class DemoModule : public pp::Module { | 422 class DemoModule : public pp::Module { |
429 public: | 423 public: |
430 DemoModule() : Module() {} | 424 DemoModule() : Module() {} |
431 virtual ~DemoModule() {} | 425 virtual ~DemoModule() {} |
432 | 426 |
433 virtual pp::Instance* CreateInstance(PP_Instance instance) { | 427 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
434 return new DemoInstance(instance); | 428 return new DemoInstance(instance); |
435 } | 429 } |
436 }; | 430 }; |
437 | 431 |
438 } // anonymous namespace | 432 } // anonymous namespace |
439 | 433 |
440 namespace pp { | 434 namespace pp { |
441 // Factory function for your specialization of the Module object. | 435 // Factory function for your specialization of the Module object. |
442 Module* CreateModule() { | 436 Module* CreateModule() { |
443 return new DemoModule(); | 437 return new DemoModule(); |
444 } | 438 } |
445 } // namespace pp | 439 } // namespace pp |
OLD | NEW |