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); |
75 void Paint(int32_t result, int32_t frame); | 76 void Paint(int32_t result, int32_t frame); |
76 void OnTextureReleased(int32_t result, GLuint texture); | 77 void OnTextureReleased(int32_t result, GLuint texture); |
77 void OnImageReleased(int32_t result, const pp::ImageData& image); | 78 void OnImageReleased(int32_t result, const pp::ImageData& image); |
78 | 79 |
79 pp::CompletionCallbackFactory<DemoInstance> callback_factory_; | 80 pp::CompletionCallbackFactory<DemoInstance> callback_factory_; |
80 | 81 |
81 // Owned data. | 82 // Owned data. |
82 pp::Graphics3D* context_; | 83 pp::Graphics3D* context_; |
83 | 84 |
84 GLuint fbo_; | 85 GLuint fbo_; |
(...skipping 14 matching lines...) Expand all Loading... |
99 SpinningCube* cube_; | 100 SpinningCube* cube_; |
100 }; | 101 }; |
101 | 102 |
102 DemoInstance::DemoInstance(PP_Instance instance) | 103 DemoInstance::DemoInstance(PP_Instance instance) |
103 : pp::Instance(instance), | 104 : pp::Instance(instance), |
104 pp::Graphics3DClient(this), | 105 pp::Graphics3DClient(this), |
105 callback_factory_(this), | 106 callback_factory_(this), |
106 context_(NULL), | 107 context_(NULL), |
107 fbo_(0), | 108 fbo_(0), |
108 rbo_(0), | 109 rbo_(0), |
109 compositor_(this), | 110 rebuild_layers_(true), |
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 compositor_.ResetLayers(); | 150 rebuild_layers_ = true; |
151 color_layer_ = pp::CompositorLayer(); | |
152 stable_texture_layer_ = pp::CompositorLayer(); | |
153 texture_layer_ = pp::CompositorLayer(); | |
154 image_layer_ = pp::CompositorLayer(); | |
155 total_resource_ -= static_cast<int32_t>(textures_.size()); | 151 total_resource_ -= static_cast<int32_t>(textures_.size()); |
156 textures_.clear(); | 152 textures_.clear(); |
157 delete context_; | 153 delete context_; |
158 context_ = NULL; | 154 context_ = NULL; |
159 cube_->OnGLContextLost(); | 155 cube_->OnGLContextLost(); |
160 pp::CompletionCallback cb = callback_factory_.NewCallback( | 156 pp::CompletionCallback cb = callback_factory_.NewCallback( |
161 &DemoInstance::InitGL); | 157 &DemoInstance::InitGL); |
162 pp::Module::Get()->core()->CallOnMainThread(0, cb, 0); | 158 pp::Module::Get()->core()->CallOnMainThread(0, cb, 0); |
163 } | 159 } |
164 | 160 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 pp::Size(kImageWidth, kImageHeight), | 249 pp::Size(kImageWidth, kImageHeight), |
254 false); | 250 false); |
255 } | 251 } |
256 pp::ImageData image = images_.back(); | 252 pp::ImageData image = images_.back(); |
257 images_.pop_back(); | 253 images_.pop_back(); |
258 return image; | 254 return image; |
259 } | 255 } |
260 | 256 |
261 void DemoInstance::Paint(int32_t result, int32_t frame) { | 257 void DemoInstance::Paint(int32_t result, int32_t frame) { |
262 assert(result == PP_OK); | 258 assert(result == PP_OK); |
263 | |
264 if (result != PP_OK || !context_) | 259 if (result != PP_OK || !context_) |
265 return; | 260 return; |
266 | 261 |
267 int32_t rv; | |
268 | |
269 if (rebuild_layers_) { | 262 if (rebuild_layers_) { |
270 compositor_.ResetLayers(); | 263 compositor_ = pp::Compositor(this); |
| 264 assert(BindGraphics(compositor_)); |
271 color_layer_ = pp::CompositorLayer(); | 265 color_layer_ = pp::CompositorLayer(); |
272 stable_texture_layer_ = pp::CompositorLayer(); | 266 stable_texture_layer_ = pp::CompositorLayer(); |
273 texture_layer_ = pp::CompositorLayer(); | 267 texture_layer_ = pp::CompositorLayer(); |
274 image_layer_ = pp::CompositorLayer(); | 268 image_layer_ = pp::CompositorLayer(); |
275 frame = 0; | 269 frame = 0; |
276 rebuild_layers_ = false; | 270 rebuild_layers_ = false; |
277 } | 271 } |
278 | 272 |
| 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; |
279 float factor_sin = sin(M_PI / 180 * frame); | 288 float factor_sin = sin(M_PI / 180 * frame); |
280 float factor_cos = cos(M_PI / 180 * frame); | 289 float factor_cos = cos(M_PI / 180 * frame); |
281 { | 290 { |
282 // Set the background color layer. | 291 // Set the background color layer. |
283 if (color_layer_.is_null()) { | 292 if (color_layer_.is_null()) { |
284 color_layer_ = compositor_.AddLayer(); | 293 color_layer_ = compositor_.AddLayer(); |
285 assert(!color_layer_.is_null()); | 294 assert(!color_layer_.is_null()); |
286 static const float transform[16] = { | 295 static const float transform[16] = { |
287 1.0f, 0.0f, 0.0f, 0.0f, | 296 1.0f, 0.0f, 0.0f, 0.0f, |
288 0.0f, 1.0f, 0.0f, 0.0f, | 297 0.0f, 1.0f, 0.0f, 0.0f, |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 GLuint texture = PrepareFramebuffer(); | 397 GLuint texture = PrepareFramebuffer(); |
389 cube_->UpdateForTimeDelta(0.02f); | 398 cube_->UpdateForTimeDelta(0.02f); |
390 cube_->Draw(); | 399 cube_->Draw(); |
391 rv = texture_layer_.SetTexture(*context_, texture, pp::Size(400, 400), | 400 rv = texture_layer_.SetTexture(*context_, texture, pp::Size(400, 400), |
392 callback_factory_.NewCallback(&DemoInstance::OnTextureReleased, | 401 callback_factory_.NewCallback(&DemoInstance::OnTextureReleased, |
393 texture)); | 402 texture)); |
394 assert(rv == PP_OK_COMPLETIONPENDING); | 403 assert(rv == PP_OK_COMPLETIONPENDING); |
395 rv = texture_layer_.SetPremultipliedAlpha(PP_FALSE); | 404 rv = texture_layer_.SetPremultipliedAlpha(PP_FALSE); |
396 assert(rv == PP_OK); | 405 assert(rv == PP_OK); |
397 } | 406 } |
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); | |
408 } | 407 } |
409 | 408 |
410 void DemoInstance::OnTextureReleased(int32_t result, GLuint texture) { | 409 void DemoInstance::OnTextureReleased(int32_t result, GLuint texture) { |
411 if (result == PP_OK) | 410 if (result == PP_OK) { |
412 textures_.push_back(texture); | 411 textures_.push_back(texture); |
| 412 } else { |
| 413 glDeleteTextures(1, &texture); |
| 414 total_resource_--; |
| 415 } |
413 } | 416 } |
414 | 417 |
415 void DemoInstance::OnImageReleased(int32_t result, const pp::ImageData& image) { | 418 void DemoInstance::OnImageReleased(int32_t result, const pp::ImageData& image) { |
416 if (result == PP_OK) | 419 if (result == PP_OK) { |
417 images_.push_back(image); | 420 images_.push_back(image); |
| 421 } else { |
| 422 total_resource_--; |
| 423 } |
418 } | 424 } |
419 | 425 |
420 // This object is the global object representing this plugin library as long | 426 // This object is the global object representing this plugin library as long |
421 // as it is loaded. | 427 // as it is loaded. |
422 class DemoModule : public pp::Module { | 428 class DemoModule : public pp::Module { |
423 public: | 429 public: |
424 DemoModule() : Module() {} | 430 DemoModule() : Module() {} |
425 virtual ~DemoModule() {} | 431 virtual ~DemoModule() {} |
426 | 432 |
427 virtual pp::Instance* CreateInstance(PP_Instance instance) { | 433 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
428 return new DemoInstance(instance); | 434 return new DemoInstance(instance); |
429 } | 435 } |
430 }; | 436 }; |
431 | 437 |
432 } // anonymous namespace | 438 } // anonymous namespace |
433 | 439 |
434 namespace pp { | 440 namespace pp { |
435 // Factory function for your specialization of the Module object. | 441 // Factory function for your specialization of the Module object. |
436 Module* CreateModule() { | 442 Module* CreateModule() { |
437 return new DemoModule(); | 443 return new DemoModule(); |
438 } | 444 } |
439 } // namespace pp | 445 } // namespace pp |
OLD | NEW |