Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(56)

Side by Side Diff: native_client_sdk/src/examples/demo/voronoi/voronoi.cc

Issue 23072027: Merge 217400 "Change ppapi_simple to support pixel format select..." (Closed) Base URL: svn://svn.chromium.org/chrome/branches/1599/src/
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/c/ppb_input_event.h> 7 #include <ppapi/c/ppb_input_event.h>
8 #include <ppapi/cpp/input_event.h> 8 #include <ppapi/cpp/input_event.h>
9 #include <ppapi/cpp/var.h> 9 #include <ppapi/cpp/var.h>
10 #include <ppapi/cpp/var_array.h> 10 #include <ppapi/cpp/var_array.h>
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 } 66 }
67 67
68 inline double getseconds() { 68 inline double getseconds() {
69 const double usec_to_sec = 0.000001; 69 const double usec_to_sec = 0.000001;
70 timeval tv; 70 timeval tv;
71 if (0 == gettimeofday(&tv, NULL)) 71 if (0 == gettimeofday(&tv, NULL))
72 return tv.tv_sec + tv.tv_usec * usec_to_sec; 72 return tv.tv_sec + tv.tv_usec * usec_to_sec;
73 return 0.0; 73 return 0.0;
74 } 74 }
75 75
76 inline uint32_t MakeRGBA(uint32_t r, uint32_t g, uint32_t b, uint32_t a) { 76 // BGRA helper function, for constructing a pixel for a BGRA buffer.
77 inline uint32_t MakeBGRA(uint32_t b, uint32_t g, uint32_t r, uint32_t a) {
77 return (((a) << 24) | ((r) << 16) | ((g) << 8) | (b)); 78 return (((a) << 24) | ((r) << 16) | ((g) << 8) | (b));
78 } 79 }
79 } // namespace 80 } // namespace
80 81
81 // Vec2, simple 2D vector 82 // Vec2, simple 2D vector
82 struct Vec2 { 83 struct Vec2 {
83 float x, y; 84 float x, y;
84 Vec2() {} 85 Vec2() {}
85 Vec2(float px, float py) { 86 Vec2(float px, float py) {
86 x = px; 87 x = px;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 // random initial start position 156 // random initial start position
156 const float x = frand(); 157 const float x = frand();
157 const float y = frand(); 158 const float y = frand();
158 positions_[i].Set(x, y); 159 positions_[i].Set(x, y);
159 // random directional velocity ( -1..1, -1..1 ) 160 // random directional velocity ( -1..1, -1..1 )
160 const float speed = 0.0005f; 161 const float speed = 0.0005f;
161 const float u = (frand() * 2.0f - 1.0f) * speed; 162 const float u = (frand() * 2.0f - 1.0f) * speed;
162 const float v = (frand() * 2.0f - 1.0f) * speed; 163 const float v = (frand() * 2.0f - 1.0f) * speed;
163 velocities_[i].Set(u, v); 164 velocities_[i].Set(u, v);
164 // 'unique' color (well... unique enough for our purposes) 165 // 'unique' color (well... unique enough for our purposes)
165 colors_[i] = MakeRGBA(rand255(), rand255(), rand255(), 255); 166 colors_[i] = MakeBGRA(rand255(), rand255(), rand255(), 255);
166 } 167 }
167 } 168 }
168 169
169 Voronoi::Voronoi() : num_regions_(kDefaultNumRegions), num_threads_(0), 170 Voronoi::Voronoi() : num_regions_(kDefaultNumRegions), num_threads_(0),
170 point_count_(kStartPointCount), draw_points_(true), draw_interiors_(true), 171 point_count_(kStartPointCount), draw_points_(true), draw_interiors_(true),
171 benchmark_frame_counter_(0), benchmarking_(false) { 172 benchmark_frame_counter_(0), benchmarking_(false) {
172 Reset(); 173 Reset();
173 // By default, render from the dispatch thread. 174 // By default, render from the dispatch thread.
174 workers_ = new ThreadPool(num_threads_); 175 workers_ = new ThreadPool(num_threads_);
175 PSEventSetFilter(PSE_ALL); 176 PSEventSetFilter(PSE_ALL);
176 ps_context_ = PSContext2DAllocate(); 177 ps_context_ = PSContext2DAllocate(PP_IMAGEDATAFORMAT_BGRA_PREMUL);
177 } 178 }
178 179
179 Voronoi::~Voronoi() { 180 Voronoi::~Voronoi() {
180 delete workers_; 181 delete workers_;
181 PSContext2DFree(ps_context_); 182 PSContext2DFree(ps_context_);
182 } 183 }
183 184
184 inline uint32_t* Voronoi::wGetAddr(int x, int y) { 185 inline uint32_t* Voronoi::wGetAddr(int x, int y) {
185 return ps_context_->data + x + y * ps_context_->stride / sizeof(uint32_t); 186 return ps_context_->data + x + y * ps_context_->stride / sizeof(uint32_t);
186 } 187 }
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 // all 4 corners belong to the same cell 237 // all 4 corners belong to the same cell
237 *m = m0; 238 *m = m0;
238 return true; 239 return true;
239 } 240 }
240 241
241 // Quickly fill a span of pixels with a solid color. Assumes 242 // Quickly fill a span of pixels with a solid color. Assumes
242 // span width is divisible by 4. 243 // span width is divisible by 4.
243 // If multithreading, this function is only called by the worker threads. 244 // If multithreading, this function is only called by the worker threads.
244 inline void Voronoi::wFillSpan(uint32_t* pixels, uint32_t color, int width) { 245 inline void Voronoi::wFillSpan(uint32_t* pixels, uint32_t color, int width) {
245 if (!draw_interiors_) { 246 if (!draw_interiors_) {
246 const uint32_t gray = MakeRGBA(128, 128, 128, 255); 247 const uint32_t gray = MakeBGRA(128, 128, 128, 255);
247 color = gray; 248 color = gray;
248 } 249 }
249 for (int i = 0; i < width; i += 4) { 250 for (int i = 0; i < width; i += 4) {
250 *pixels++ = color; 251 *pixels++ = color;
251 *pixels++ = color; 252 *pixels++ = color;
252 *pixels++ = color; 253 *pixels++ = color;
253 *pixels++ = color; 254 *pixels++ = color;
254 } 255 }
255 } 256 }
256 257
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 // render dot as a small diamond 388 // render dot as a small diamond
388 *pixel = color1; 389 *pixel = color1;
389 *(pixel - 1) = color2; 390 *(pixel - 1) = color2;
390 *(pixel + 1) = color2; 391 *(pixel + 1) = color2;
391 *(pixel - stride_in_pixels) = color2; 392 *(pixel - stride_in_pixels) = color2;
392 *(pixel + stride_in_pixels) = color2; 393 *(pixel + stride_in_pixels) = color2;
393 } 394 }
394 395
395 // Superimposes dots on the positions. 396 // Superimposes dots on the positions.
396 void Voronoi::SuperimposePositions() { 397 void Voronoi::SuperimposePositions() {
397 const uint32_t white = MakeRGBA(255, 255, 255, 255); 398 const uint32_t white = MakeBGRA(255, 255, 255, 255);
398 const uint32_t gray = MakeRGBA(192, 192, 192, 255); 399 const uint32_t gray = MakeBGRA(192, 192, 192, 255);
399 for (int i = 0; i < point_count_; i++) { 400 for (int i = 0; i < point_count_; i++) {
400 RenderDot( 401 RenderDot(
401 screen_positions_[i].x, screen_positions_[i].y, white, gray); 402 screen_positions_[i].x, screen_positions_[i].y, white, gray);
402 } 403 }
403 } 404 }
404 405
405 // Renders the Voronoi diagram, dispatching the work to multiple threads. 406 // Renders the Voronoi diagram, dispatching the work to multiple threads.
406 void Voronoi::Render() { 407 void Voronoi::Render() {
407 workers_->Dispatch(num_regions_, wRenderRegionEntry, this); 408 workers_->Dispatch(num_regions_, wRenderRegionEntry, this);
408 if (draw_points_) 409 if (draw_points_)
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 // Do simulation, render and present. 516 // Do simulation, render and present.
516 voronoi.Update(); 517 voronoi.Update();
517 } 518 }
518 519
519 return 0; 520 return 0;
520 } 521 }
521 522
522 // Register the function to call once the Instance Object is initialized. 523 // Register the function to call once the Instance Object is initialized.
523 // see: pappi_simple/ps_main.h 524 // see: pappi_simple/ps_main.h
524 PPAPI_SIMPLE_REGISTER_MAIN(example_main); 525 PPAPI_SIMPLE_REGISTER_MAIN(example_main);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698