| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 <assert.h> | 5 #include <assert.h> |
| 6 #include <math.h> | 6 #include <math.h> |
| 7 #include <ppapi/cpp/completion_callback.h> | 7 #include <ppapi/cpp/completion_callback.h> |
| 8 #include <ppapi/cpp/graphics_2d.h> | 8 #include <ppapi/cpp/graphics_2d.h> |
| 9 #include <ppapi/cpp/image_data.h> | 9 #include <ppapi/cpp/image_data.h> |
| 10 #include <ppapi/cpp/input_event.h> | 10 #include <ppapi/cpp/input_event.h> |
| 11 #include <ppapi/cpp/instance.h> | 11 #include <ppapi/cpp/instance.h> |
| 12 #include <ppapi/cpp/module.h> | 12 #include <ppapi/cpp/module.h> |
| 13 #include <ppapi/cpp/rect.h> | 13 #include <ppapi/cpp/rect.h> |
| 14 #include <ppapi/cpp/size.h> | 14 #include <ppapi/cpp/size.h> |
| 15 #include <ppapi/cpp/var.h> | 15 #include <ppapi/cpp/var.h> |
| 16 #include <ppapi/cpp/var_dictionary.h> |
| 16 #include <pthread.h> | 17 #include <pthread.h> |
| 17 #include <stdio.h> | 18 #include <stdio.h> |
| 18 #include <stdlib.h> | 19 #include <stdlib.h> |
| 19 #include <string.h> | 20 #include <string.h> |
| 20 #include <sys/time.h> | 21 #include <sys/time.h> |
| 21 #include <unistd.h> | 22 #include <unistd.h> |
| 22 | 23 |
| 23 #include <algorithm> | 24 #include <algorithm> |
| 24 #include <string> | 25 #include <string> |
| 25 | 26 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 void SuperimposePositions(); | 127 void SuperimposePositions(); |
| 127 void Render(); | 128 void Render(); |
| 128 void Draw(); | 129 void Draw(); |
| 129 void StartBenchmark(); | 130 void StartBenchmark(); |
| 130 void EndBenchmark(); | 131 void EndBenchmark(); |
| 131 | 132 |
| 132 // Runs a tick of the simulations, updating all buffers. Flushes the | 133 // Runs a tick of the simulations, updating all buffers. Flushes the |
| 133 // contents of |image_data_| to the 2D graphics context. | 134 // contents of |image_data_| to the 2D graphics context. |
| 134 void Update(); | 135 void Update(); |
| 135 | 136 |
| 137 // Helper to post small update messages to JS. |
| 138 void PostUpdateMessage(const char* message_name, double value); |
| 136 // Create and initialize the 2D context used for drawing. | 139 // Create and initialize the 2D context used for drawing. |
| 137 void CreateContext(const pp::Size& size); | 140 void CreateContext(const pp::Size& size); |
| 138 // Destroy the 2D drawing context. | 141 // Destroy the 2D drawing context. |
| 139 void DestroyContext(); | 142 void DestroyContext(); |
| 140 // Push the pixels to the browser, then attempt to flush the 2D context. | 143 // Push the pixels to the browser, then attempt to flush the 2D context. |
| 141 void FlushPixelBuffer(); | 144 void FlushPixelBuffer(); |
| 142 static void FlushCallback(void* data, int32_t result); | 145 static void FlushCallback(void* data, int32_t result); |
| 143 | 146 |
| 144 | 147 |
| 145 pp::Graphics2D* graphics_2d_context_; | 148 pp::Graphics2D* graphics_2d_context_; |
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 benchmarking_ = true; | 462 benchmarking_ = true; |
| 460 benchmark_start_time_ = getseconds(); | 463 benchmark_start_time_ = getseconds(); |
| 461 } | 464 } |
| 462 | 465 |
| 463 void Voronoi::EndBenchmark() { | 466 void Voronoi::EndBenchmark() { |
| 464 benchmark_end_time_ = getseconds(); | 467 benchmark_end_time_ = getseconds(); |
| 465 printf("Benchmark ended... time: %2.5f\n", | 468 printf("Benchmark ended... time: %2.5f\n", |
| 466 benchmark_end_time_ - benchmark_start_time_); | 469 benchmark_end_time_ - benchmark_start_time_); |
| 467 benchmarking_ = false; | 470 benchmarking_ = false; |
| 468 benchmark_frame_counter_ = 0; | 471 benchmark_frame_counter_ = 0; |
| 469 pp::Var result(benchmark_end_time_ - benchmark_start_time_); | 472 double result = benchmark_end_time_ - benchmark_start_time_; |
| 470 PostMessage(result); | 473 PostUpdateMessage("benchmark_result", result); |
| 471 } | 474 } |
| 472 | 475 |
| 473 // Handle input events from the user. | 476 // Handle input events from the user. |
| 474 bool Voronoi::HandleInputEvent(const pp::InputEvent& event) { | 477 bool Voronoi::HandleInputEvent(const pp::InputEvent& event) { |
| 475 switch (event.GetType()) { | 478 switch (event.GetType()) { |
| 476 case PP_INPUTEVENT_TYPE_KEYDOWN: { | 479 case PP_INPUTEVENT_TYPE_KEYDOWN: { |
| 477 pp::KeyboardInputEvent key = pp::KeyboardInputEvent(event); | 480 pp::KeyboardInputEvent key = pp::KeyboardInputEvent(event); |
| 478 uint32_t key_code = key.GetKeyCode(); | 481 uint32_t key_code = key.GetKeyCode(); |
| 479 if (key_code == 84) // 't' key | 482 if (key_code == 84) // 't' key |
| 480 if (!benchmarking_) | 483 if (!benchmarking_) |
| 481 StartBenchmark(); | 484 StartBenchmark(); |
| 482 break; | 485 break; |
| 483 } | 486 } |
| 484 default: | 487 default: |
| 485 return false; | 488 return false; |
| 486 } | 489 } |
| 487 return true; | 490 return true; |
| 488 } | 491 } |
| 489 | 492 |
| 490 // Handle messages sent from Javascript. | 493 // Handle messages sent from Javascript. |
| 491 void Voronoi::HandleMessage(const pp::Var& message) { | 494 void Voronoi::HandleMessage(const pp::Var& var) { |
| 492 if (message.is_string()) { | 495 if (var.is_dictionary()) { |
| 493 std::string message_string = message.AsString(); | 496 pp::VarDictionary dictionary = pp::VarDictionary(var); |
| 494 if (message_string == "run benchmark" && !benchmarking_) | 497 std::string message = dictionary.Get("message").AsString(); |
| 498 if (message == "run_benchmark" && !benchmarking_) |
| 495 StartBenchmark(); | 499 StartBenchmark(); |
| 496 else if (message_string == "with points") | 500 else if (message == "draw_points") |
| 497 draw_points_ = true; | 501 draw_points_ = dictionary.Get(pp::Var("value")).AsBool(); |
| 498 else if (message_string == "without points") | 502 else if (message == "draw_interiors") |
| 499 draw_points_ = false; | 503 draw_interiors_ = dictionary.Get(pp::Var("value")).AsBool(); |
| 500 else if (message_string == "with interiors") | 504 else if (message == "set_points") { |
| 501 draw_interiors_ = true; | 505 int num_points = dictionary.Get(pp::Var("value")).AsInt(); |
| 502 else if (message_string == "without interiors") | |
| 503 draw_interiors_ = false; | |
| 504 else if (strstr(message_string.c_str(), "points:")) { | |
| 505 int num_points = atoi(strstr(message_string.c_str(), " ")); | |
| 506 point_count_ = std::min(kMaxPointCount, std::max(0, num_points)); | 506 point_count_ = std::min(kMaxPointCount, std::max(0, num_points)); |
| 507 } else if (strstr(message_string.c_str(), "threads:")) { | 507 } else if (message == "set_threads") { |
| 508 int thread_count = atoi(strstr(message_string.c_str(), " ")); | 508 int thread_count = dictionary.Get(pp::Var("value")).AsInt(); |
| 509 delete workers_; | 509 delete workers_; |
| 510 workers_ = new ThreadPool(thread_count); | 510 workers_ = new ThreadPool(thread_count); |
| 511 } | 511 } |
| 512 } | 512 } |
| 513 } | 513 } |
| 514 | 514 |
| 515 // PostUpdateMessage() helper function for sendimg small messages to JS. |
| 516 void Voronoi::PostUpdateMessage(const char* message_name, double value) { |
| 517 pp::VarDictionary message; |
| 518 message.Set(pp::Var("message"), pp::Var(message_name)); |
| 519 message.Set(pp::Var("value"), pp::Var(value)); |
| 520 PostMessage(message); |
| 521 } |
| 522 |
| 515 void Voronoi::FlushCallback(void* thiz, int32_t result) { | 523 void Voronoi::FlushCallback(void* thiz, int32_t result) { |
| 516 static_cast<Voronoi*>(thiz)->Update(); | 524 static_cast<Voronoi*>(thiz)->Update(); |
| 517 } | 525 } |
| 518 | 526 |
| 519 // Update the 2d region and flush to make it visible on the page. | 527 // Update the 2d region and flush to make it visible on the page. |
| 520 void Voronoi::FlushPixelBuffer() { | 528 void Voronoi::FlushPixelBuffer() { |
| 521 graphics_2d_context_->PaintImageData(*image_data_, pp::Point(0, 0)); | 529 graphics_2d_context_->PaintImageData(*image_data_, pp::Point(0, 0)); |
| 522 graphics_2d_context_->Flush(pp::CompletionCallback(&FlushCallback, this)); | 530 graphics_2d_context_->Flush(pp::CompletionCallback(&FlushCallback, this)); |
| 523 } | 531 } |
| 524 | 532 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 573 return new Voronoi(instance); | 581 return new Voronoi(instance); |
| 574 } | 582 } |
| 575 }; | 583 }; |
| 576 | 584 |
| 577 namespace pp { | 585 namespace pp { |
| 578 Module* CreateModule() { | 586 Module* CreateModule() { |
| 579 return new VoronoiModule(); | 587 return new VoronoiModule(); |
| 580 } | 588 } |
| 581 } // namespace pp | 589 } // namespace pp |
| 582 | 590 |
| OLD | NEW |