OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Native Client SDK Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can |
| 3 // be found in the LICENSE file. |
| 4 |
| 5 #include <assert.h> |
| 6 #include <math.h> |
| 7 #include <ppapi/cpp/completion_callback.h> |
| 8 #include <ppapi/cpp/graphics_2d.h> |
| 9 #include <ppapi/cpp/image_data.h> |
| 10 #include <ppapi/cpp/input_event.h> |
| 11 #include <ppapi/cpp/instance.h> |
| 12 #include <ppapi/cpp/module.h> |
| 13 #include <ppapi/cpp/rect.h> |
| 14 #include <ppapi/cpp/size.h> |
| 15 #include <ppapi/cpp/var.h> |
| 16 #include <pthread.h> |
| 17 #include <stdio.h> |
| 18 #include <stdlib.h> |
| 19 #include <string.h> |
| 20 #include <sys/time.h> |
| 21 #include <unistd.h> |
| 22 |
| 23 #include <string> |
| 24 |
| 25 #include "threadpool.h" |
| 26 |
| 27 // Global properties used to setup Voronoi demo. |
| 28 namespace { |
| 29 const int kMinRectSize = 4; |
| 30 const int kStartRecurseSize = 32; |
| 31 const float kHugeZ = 1.0e38f; |
| 32 const float kPI = M_PI; |
| 33 const float kTwoPI = kPI * 2.0f; |
| 34 const int kFramesToBenchmark = 100; |
| 35 const unsigned int kRandomStartSeed = 0xC0DE533D; |
| 36 const int kMaxPointCount = 1024; |
| 37 const int kStartPointCount = 256; |
| 38 |
| 39 unsigned int g_rand_state = kRandomStartSeed; |
| 40 |
| 41 // random number helper |
| 42 inline unsigned char rand255() { |
| 43 return static_cast<unsigned char>(rand_r(&g_rand_state) & 255); |
| 44 } |
| 45 |
| 46 // random number helper |
| 47 inline float frand() { |
| 48 return (static_cast<float>(rand_r(&g_rand_state)) / |
| 49 static_cast<float>(RAND_MAX)); |
| 50 } |
| 51 |
| 52 // reset random seed |
| 53 inline void rand_reset(unsigned int seed) { |
| 54 g_rand_state = seed; |
| 55 } |
| 56 |
| 57 inline double getseconds() { |
| 58 const double usec_to_sec = 0.000001; |
| 59 timeval tv; |
| 60 if (0 == gettimeofday(&tv, NULL)) |
| 61 return tv.tv_sec + tv.tv_usec * usec_to_sec; |
| 62 return 0.0; |
| 63 } |
| 64 |
| 65 inline uint32_t MakeRGBA(uint32_t r, uint32_t g, uint32_t b, uint32_t a) { |
| 66 return (((a) << 24) | ((r) << 16) | ((g) << 8) | (b)); |
| 67 } |
| 68 } // namespace |
| 69 |
| 70 // Vec2, simple 2D vector |
| 71 struct Vec2 { |
| 72 float x, y; |
| 73 Vec2() { ; } |
| 74 Vec2(float px, float py) { |
| 75 x = px; |
| 76 y = py; |
| 77 } |
| 78 void Set(float px, float py) { |
| 79 x = px; |
| 80 y = py; |
| 81 } |
| 82 }; |
| 83 |
| 84 // The main object that runs Voronoi simulation. |
| 85 class Voronoi : public pp::Instance { |
| 86 public: |
| 87 explicit Voronoi(PP_Instance instance); |
| 88 virtual ~Voronoi(); |
| 89 |
| 90 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { |
| 91 return true; |
| 92 } |
| 93 |
| 94 virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip); |
| 95 |
| 96 // Catch events. |
| 97 virtual bool HandleInputEvent(const pp::InputEvent& event); |
| 98 |
| 99 // Catch messages posted from Javascript. |
| 100 virtual void HandleMessage(const pp::Var& message); |
| 101 |
| 102 private: |
| 103 // Methods prefixed with 'w' are run on worker threads. |
| 104 int wCell(float x, float y); |
| 105 inline void wFillSpan(uint32_t *pixels, uint32_t color, int width); |
| 106 void wRenderTile(int x, int y, int w, int h); |
| 107 void wProcessTile(int x, int y, int w, int h); |
| 108 void wSubdivide(int x, int y, int w, int h); |
| 109 void wMakeRect(int region, int *x, int *y, int *w, int *h); |
| 110 bool wTestRect(int *m, int x, int y, int w, int h); |
| 111 void wFillRect(int x, int y, int w, int h, uint32_t color); |
| 112 void wRenderRect(int x0, int y0, int x1, int y1); |
| 113 void wRenderRegion(int region); |
| 114 static void wRenderRegionEntry(int region, void *thiz); |
| 115 |
| 116 // These methods are only called by the main thread. |
| 117 void Reset(); |
| 118 void UpdateSim(); |
| 119 void RenderDot(float x, float y, uint32_t color1, uint32_t color2); |
| 120 void SuperimposePositions(); |
| 121 void Render(); |
| 122 void Draw(); |
| 123 void StartBenchmark(); |
| 124 void EndBenchmark(); |
| 125 |
| 126 // Runs a tick of the simulations, updating all buffers. Flushes the |
| 127 // contents of |image_data_| to the 2D graphics context. |
| 128 void Update(); |
| 129 |
| 130 // Create and initialize the 2D context used for drawing. |
| 131 void CreateContext(const pp::Size& size); |
| 132 // Destroy the 2D drawing context. |
| 133 void DestroyContext(); |
| 134 // Push the pixels to the browser, then attempt to flush the 2D context. |
| 135 void FlushPixelBuffer(); |
| 136 static void FlushCallback(void* data, int32_t result); |
| 137 |
| 138 |
| 139 pp::Graphics2D* graphics_2d_context_; |
| 140 pp::ImageData* image_data_; |
| 141 Vec2 positions_[kMaxPointCount]; |
| 142 Vec2 screen_positions_[kMaxPointCount]; |
| 143 Vec2 velocities_[kMaxPointCount]; |
| 144 uint32_t colors_[kMaxPointCount]; |
| 145 float ang_; |
| 146 int point_count_; |
| 147 int num_threads_; |
| 148 int num_regions_; |
| 149 bool draw_points_; |
| 150 bool draw_interiors_; |
| 151 ThreadPool *workers_; |
| 152 int width_; |
| 153 int height_; |
| 154 uint32_t *pixel_buffer_; |
| 155 int benchmark_frame_counter_; |
| 156 bool benchmarking_; |
| 157 double benchmark_start_time_; |
| 158 double benchmark_end_time_; |
| 159 }; |
| 160 |
| 161 |
| 162 |
| 163 void Voronoi::Reset() { |
| 164 rand_reset(kRandomStartSeed); |
| 165 ang_ = 0.0f; |
| 166 for (int i = 0; i < kMaxPointCount; i++) { |
| 167 // random initial start position |
| 168 const float x = frand(); |
| 169 const float y = frand(); |
| 170 positions_[i].Set(x, y); |
| 171 // random directional velocity ( -1..1, -1..1 ) |
| 172 const float speed = 0.0005f; |
| 173 const float u = (frand() * 2.0f - 1.0f) * speed; |
| 174 const float v = (frand() * 2.0f - 1.0f) * speed; |
| 175 velocities_[i].Set(u, v); |
| 176 // 'unique' color (well... unique enough for our purposes) |
| 177 colors_[i] = MakeRGBA(rand255(), rand255(), rand255(), 255); |
| 178 } |
| 179 } |
| 180 |
| 181 Voronoi::Voronoi(PP_Instance instance) : pp::Instance(instance), |
| 182 graphics_2d_context_(NULL), |
| 183 image_data_(NULL) { |
| 184 draw_points_ = true; |
| 185 draw_interiors_ = true; |
| 186 width_ = 0; |
| 187 height_ = 0; |
| 188 pixel_buffer_ = NULL; |
| 189 benchmark_frame_counter_ = 0; |
| 190 benchmarking_ = false; |
| 191 |
| 192 point_count_ = kStartPointCount; |
| 193 Reset(); |
| 194 |
| 195 // By default, do single threaded rendering. |
| 196 num_regions_ = 256; |
| 197 num_threads_ = 1; |
| 198 workers_ = new ThreadPool(num_threads_); |
| 199 |
| 200 // Request PPAPI input events for mouse & keyboard. |
| 201 RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE); |
| 202 RequestInputEvents(PP_INPUTEVENT_CLASS_KEYBOARD); |
| 203 } |
| 204 |
| 205 Voronoi::~Voronoi() { |
| 206 delete workers_; |
| 207 DestroyContext(); |
| 208 } |
| 209 |
| 210 // This is the core of the Voronoi calculation. At a given point on the |
| 211 // screen, iterate through all voronoi positions and render them as 3D cones. |
| 212 // We're looking for the voronoi cell that generates the closest z value. |
| 213 // (not really cones - since it is all realative, we avoid doing the |
| 214 // expensive sqrt and test against z*z instead) |
| 215 // If multithreading, this function is only called by the worker threads. |
| 216 int Voronoi::wCell(float x, float y) { |
| 217 int n = 0; |
| 218 float zz = kHugeZ; |
| 219 Vec2 *pos = screen_positions_; |
| 220 for (int i = 0; i < point_count_; ++i) { |
| 221 // measured 5.18 cycles per iteration on a core2 |
| 222 float dx = x - pos[i].x; |
| 223 float dy = y - pos[i].y; |
| 224 float dd = (dx * dx + dy * dy); |
| 225 if (dd < zz) { |
| 226 zz = dd; |
| 227 n = i; |
| 228 } |
| 229 } |
| 230 // Return the closest cell to point x,y. |
| 231 return n; |
| 232 } |
| 233 |
| 234 // Given a region r, derive a non-overlapping rectangle for a thread to |
| 235 // work on. |
| 236 // If multithreading, this function is only called by the worker threads. |
| 237 void Voronoi::wMakeRect(int r, int *x, int *y, int *w, int *h) { |
| 238 const int parts = 16; |
| 239 assert(parts * parts == num_regions_); |
| 240 *w = width_ / parts; |
| 241 *h = height_ / parts; |
| 242 *x = *w * (r % parts); |
| 243 *y = *h * ((r / parts) % parts); |
| 244 } |
| 245 |
| 246 // Test 4 corners of a rectangle to see if they all belong to the same |
| 247 // voronoi cell. Each test is expensive so bail asap. Returns true |
| 248 // if all 4 corners match. |
| 249 // If multithreading, this function is only called by the worker threads. |
| 250 bool Voronoi::wTestRect(int *m, int x, int y, int w, int h) { |
| 251 // each test is expensive, so exit ASAP |
| 252 const int m0 = wCell(x, y); |
| 253 const int m1 = wCell(x + w - 1, y); |
| 254 if (m0 != m1) return false; |
| 255 const int m2 = wCell(x, y + h - 1); |
| 256 if (m0 != m2) return false; |
| 257 const int m3 = wCell(x + w - 1, y + h - 1); |
| 258 if (m0 != m3) return false; |
| 259 // all 4 corners belong to the same cell |
| 260 *m = m0; |
| 261 return true; |
| 262 } |
| 263 |
| 264 // Quickly fill a span of pixels with a solid color. Assumes |
| 265 // span width is divisible by 4. |
| 266 // If multithreading, this function is only called by the worker threads. |
| 267 inline void Voronoi::wFillSpan(uint32_t *pixels, uint32_t color, int width) { |
| 268 if (!draw_interiors_) { |
| 269 const uint32_t gray = MakeRGBA(128, 128, 128, 255); |
| 270 color = gray; |
| 271 } |
| 272 for (int i = 0; i < width; i += 4) { |
| 273 *pixels++ = color; |
| 274 *pixels++ = color; |
| 275 *pixels++ = color; |
| 276 *pixels++ = color; |
| 277 } |
| 278 } |
| 279 |
| 280 // Quickly fill a rectangle with a solid color. Assumes |
| 281 // the width w parameter is evenly divisible by 4. |
| 282 // If multithreading, this function is only called by the worker threads. |
| 283 void Voronoi::wFillRect(int x, int y, int w, int h, uint32_t color) { |
| 284 const uint32_t pitch = width_; |
| 285 uint32_t *pixels = pixel_buffer_ + y * pitch + x; |
| 286 for (int j = 0; j < h; j++) { |
| 287 wFillSpan(pixels, color, w); |
| 288 pixels += pitch; |
| 289 } |
| 290 } |
| 291 |
| 292 // When recursive subdivision reaches a certain minimum without finding a |
| 293 // rectangle that has four matching corners belonging to the same voronoi |
| 294 // cell, this function will break the retangular 'tile' into smaller scanlines |
| 295 // and look for opportunities to quick fill at the scanline level. If the |
| 296 // scanline can't be quick filled, it will slow down even further and compute |
| 297 // voronoi membership per pixel. |
| 298 void Voronoi::wRenderTile(int x, int y, int w, int h) { |
| 299 // rip through a tile |
| 300 const uint32_t pitch = width_; |
| 301 uint32_t *pixels = pixel_buffer_ + y * pitch + x; |
| 302 for (int j = 0; j < h; j++) { |
| 303 // get start and end cell values |
| 304 int ms = wCell(x + 0, y + j); |
| 305 int me = wCell(x + w - 1, y + j); |
| 306 // if the end points are the same, quick fill the span |
| 307 if (ms == me) { |
| 308 wFillSpan(pixels, colors_[ms], w); |
| 309 } else { |
| 310 // else compute each pixel in the span... this is the slow part! |
| 311 uint32_t *p = pixels; |
| 312 *p++ = colors_[ms]; |
| 313 for (int i = 1; i < (w - 1); i++) { |
| 314 int m = wCell(x + i, y + j); |
| 315 *p++ = colors_[m]; |
| 316 } |
| 317 *p++ = colors_[me]; |
| 318 } |
| 319 pixels += pitch; |
| 320 } |
| 321 } |
| 322 |
| 323 // Take a rectangular region and do one of - |
| 324 // If all four corners below to the same voronoi cell, stop recursion and |
| 325 // quick fill the rectangle. |
| 326 // If the minimum rectangle size has been reached, break out of recursion |
| 327 // and process the rectangle. This small rectangle is called a tile. |
| 328 // Otherwise, keep recursively subdividing the rectangle into 4 equally |
| 329 // sized smaller rectangles. |
| 330 // Note: at the moment, these will always be squares, not rectangles. |
| 331 // If multithreading, this function is only called by the worker threads. |
| 332 void Voronoi::wSubdivide(int x, int y, int w, int h) { |
| 333 int m; |
| 334 // if all 4 corners are equal, quick fill interior |
| 335 if (wTestRect(&m, x, y, w, h)) { |
| 336 wFillRect(x, y, w, h, colors_[m]); |
| 337 } else { |
| 338 // did we reach the minimum rectangle size? |
| 339 if ((w <= kMinRectSize) || (h <= kMinRectSize)) { |
| 340 wRenderTile(x, y, w, h); |
| 341 } else { |
| 342 // else recurse into smaller rectangles |
| 343 const int half_w = w / 2; |
| 344 const int half_h = h / 2; |
| 345 wSubdivide(x, y, half_w, half_h); |
| 346 wSubdivide(x + half_w, y, half_w, half_h); |
| 347 wSubdivide(x, y + half_h, half_w, half_h); |
| 348 wSubdivide(x + half_w, y + half_h, half_w, half_h); |
| 349 } |
| 350 } |
| 351 } |
| 352 |
| 353 // This function cuts up the rectangle into power of 2 sized squares. It |
| 354 // assumes the input rectangle w & h are evenly divisible by |
| 355 // kStartRecurseSize. |
| 356 // If multithreading, this function is only called by the worker threads. |
| 357 void Voronoi::wRenderRect(int x, int y, int w, int h) { |
| 358 for (int iy = y; iy < (y + h); iy += kStartRecurseSize) { |
| 359 for (int ix = x; ix < (x + w); ix += kStartRecurseSize) { |
| 360 wSubdivide(ix, iy, kStartRecurseSize, kStartRecurseSize); |
| 361 } |
| 362 } |
| 363 } |
| 364 |
| 365 // If multithreading, this function is only called by the worker threads. |
| 366 void Voronoi::wRenderRegion(int region) { |
| 367 // convert region # into x0, y0, x1, y1 rectangle |
| 368 int x, y, w, h; |
| 369 wMakeRect(region, &x, &y, &w, &h); |
| 370 // render this rectangle |
| 371 wRenderRect(x, y, w, h); |
| 372 } |
| 373 |
| 374 // Entry point for worker thread. Can't pass a member function around, so we |
| 375 // have to do this little round-about. |
| 376 void Voronoi::wRenderRegionEntry(int region, void *thiz) { |
| 377 static_cast<Voronoi*>(thiz)->wRenderRegion(region); |
| 378 } |
| 379 |
| 380 // Function Voronoi::UpdateSim() |
| 381 // Run a simple sim to move the voronoi positions. This update loop |
| 382 // is run once per frame. Called from the main thread only, and only |
| 383 // when the worker threads are idle. |
| 384 void Voronoi::UpdateSim() { |
| 385 ang_ += 0.002f; |
| 386 if (ang_ > kTwoPI) { |
| 387 ang_ = ang_ - kTwoPI; |
| 388 } |
| 389 float z = cosf(ang_) * 3.0f; |
| 390 // push the points around on the screen for animation |
| 391 for (int j = 0; j < kMaxPointCount; j++) { |
| 392 positions_[j].x += (velocities_[j].x) * z; |
| 393 positions_[j].y += (velocities_[j].y) * z; |
| 394 screen_positions_[j].x = positions_[j].x * width_; |
| 395 screen_positions_[j].y = positions_[j].y * height_; |
| 396 } |
| 397 } |
| 398 |
| 399 // Renders a small diamond shaped dot at x, y clipped against the window |
| 400 void Voronoi::RenderDot(float x, float y, uint32_t color1, uint32_t color2) { |
| 401 const int ix = static_cast<int>(x); |
| 402 const int iy = static_cast<int>(y); |
| 403 int pitch = width_; |
| 404 // clip it against window |
| 405 if (ix < 1) return; |
| 406 if (ix >= (width_ - 1)) return; |
| 407 if (iy < 1) return; |
| 408 if (iy >= (height_ - 1)) return; |
| 409 uint32_t *pixel = pixel_buffer_ + iy * pitch + ix; |
| 410 // render dot as a small diamond |
| 411 *pixel = color1; |
| 412 *(pixel - 1) = color2; |
| 413 *(pixel + 1) = color2; |
| 414 *(pixel - pitch) = color2; |
| 415 *(pixel + pitch) = color2; |
| 416 } |
| 417 |
| 418 // Superimposes dots on the positions. |
| 419 void Voronoi::SuperimposePositions() { |
| 420 const uint32_t white = MakeRGBA(255, 255, 255, 255); |
| 421 const uint32_t gray = MakeRGBA(192, 192, 192, 255); |
| 422 for (int i = 0; i < point_count_; i++) { |
| 423 RenderDot( |
| 424 screen_positions_[i].x, screen_positions_[i].y, white, gray); |
| 425 } |
| 426 } |
| 427 |
| 428 // Renders the Voronoi diagram, dispatching the work to multiple threads. |
| 429 // Note: This Dispatch() is from the main PPAPI thread, so care must be taken |
| 430 // not to attempt PPAPI calls from the worker threads, since Dispatch() will |
| 431 // block here until all work is complete. The worker threads are compute only |
| 432 // and do not make any PPAPI calls. |
| 433 void Voronoi::Render() { |
| 434 workers_->Dispatch(num_regions_, wRenderRegionEntry, this); |
| 435 if (draw_points_) |
| 436 SuperimposePositions(); |
| 437 } |
| 438 |
| 439 void Voronoi::DidChangeView(const pp::Rect& position, const pp::Rect& clip) { |
| 440 if (position.size().width() == width_ && |
| 441 position.size().height() == height_) |
| 442 return; // Size didn't change, no need to update anything. |
| 443 |
| 444 // Create a new device context with the new size. |
| 445 DestroyContext(); |
| 446 CreateContext(position.size()); |
| 447 Update(); |
| 448 } |
| 449 |
| 450 void Voronoi::StartBenchmark() { |
| 451 Reset(); |
| 452 printf("Benchmark started...\n"); |
| 453 benchmark_frame_counter_ = kFramesToBenchmark; |
| 454 benchmarking_ = true; |
| 455 benchmark_start_time_ = getseconds(); |
| 456 } |
| 457 |
| 458 void Voronoi::EndBenchmark() { |
| 459 benchmark_end_time_ = getseconds(); |
| 460 printf("Benchmark ended... time: %2.5f\n", |
| 461 benchmark_end_time_ - benchmark_start_time_); |
| 462 benchmarking_ = false; |
| 463 benchmark_frame_counter_ = 0; |
| 464 pp::Var result(benchmark_end_time_ - benchmark_start_time_); |
| 465 PostMessage(result); |
| 466 } |
| 467 |
| 468 // Handle input events from the user. |
| 469 bool Voronoi::HandleInputEvent(const pp::InputEvent& event) { |
| 470 switch (event.GetType()) { |
| 471 case PP_INPUTEVENT_TYPE_KEYDOWN: { |
| 472 pp::KeyboardInputEvent key = pp::KeyboardInputEvent(event); |
| 473 uint32_t key_code = key.GetKeyCode(); |
| 474 if (key_code == 84) // 't' key |
| 475 if (!benchmarking_) |
| 476 StartBenchmark(); |
| 477 break; |
| 478 } |
| 479 default: |
| 480 return false; |
| 481 } |
| 482 return true; |
| 483 } |
| 484 |
| 485 // Handle messages sent from Javascript. |
| 486 void Voronoi::HandleMessage(const pp::Var& message) { |
| 487 if (message.is_string()) { |
| 488 std::string message_string = message.AsString(); |
| 489 if (message_string == "run benchmark" && !benchmarking_) |
| 490 StartBenchmark(); |
| 491 else if (message_string == "with points") |
| 492 draw_points_ = true; |
| 493 else if (message_string == "without points") |
| 494 draw_points_ = false; |
| 495 else if (message_string == "with interiors") |
| 496 draw_interiors_ = true; |
| 497 else if (message_string == "without interiors") |
| 498 draw_interiors_ = false; |
| 499 else if (strstr(message_string.c_str(), "points:")) { |
| 500 int num_points = atoi(strstr(message_string.c_str(), " ")); |
| 501 point_count_ = num_points < kMaxPointCount ? num_points : kMaxPointCount; |
| 502 } else if (strstr(message_string.c_str(), "threads:")) { |
| 503 int thread_count = atoi(strstr(message_string.c_str(), " ")); |
| 504 delete workers_; |
| 505 workers_ = new ThreadPool(thread_count); |
| 506 } |
| 507 } |
| 508 } |
| 509 |
| 510 void Voronoi::FlushCallback(void* thiz, int32_t result) { |
| 511 static_cast<Voronoi*>(thiz)->Update(); |
| 512 } |
| 513 |
| 514 // Update the 2d region and flush to make it visible on the page. |
| 515 void Voronoi::FlushPixelBuffer() { |
| 516 graphics_2d_context_->PaintImageData(*image_data_, pp::Point(0, 0)); |
| 517 graphics_2d_context_->Flush(pp::CompletionCallback(&FlushCallback, this)); |
| 518 } |
| 519 |
| 520 void Voronoi::Update() { |
| 521 // Don't call FlushPixelBuffer() when benchmarking - vsync is enabled by |
| 522 // default, and will throttle the benchmark results. |
| 523 do { |
| 524 UpdateSim(); |
| 525 Render(); |
| 526 if (!benchmarking_) break; |
| 527 --benchmark_frame_counter_; |
| 528 } while (benchmark_frame_counter_ > 0); |
| 529 if (benchmarking_) |
| 530 EndBenchmark(); |
| 531 FlushPixelBuffer(); |
| 532 } |
| 533 |
| 534 void Voronoi::CreateContext(const pp::Size& size) { |
| 535 const size_t bytes = size.width() * size.height(); |
| 536 graphics_2d_context_ = new pp::Graphics2D(this, size, false); |
| 537 if (!BindGraphics(*graphics_2d_context_)) |
| 538 printf("Couldn't bind the device context\n"); |
| 539 image_data_ = new pp::ImageData(this, |
| 540 PP_IMAGEDATAFORMAT_BGRA_PREMUL, |
| 541 size, |
| 542 false); |
| 543 width_ = image_data_->size().width(); |
| 544 height_ = image_data_->size().height(); |
| 545 pixel_buffer_ = reinterpret_cast<uint32_t*>(image_data_->data()); |
| 546 } |
| 547 |
| 548 void Voronoi::DestroyContext() { |
| 549 delete graphics_2d_context_; |
| 550 delete image_data_; |
| 551 graphics_2d_context_ = NULL; |
| 552 image_data_ = NULL; |
| 553 width_ = 0; |
| 554 height_ = 0; |
| 555 pixel_buffer_ = NULL; |
| 556 } |
| 557 |
| 558 // The Module class. The browser calls the CreateInstance() method to create |
| 559 // an instance of you NaCl module on the web page. The browser creates a new |
| 560 // instance for each <embed> tag with type="application/x-nacl". |
| 561 class VoronoiModule : public pp::Module { |
| 562 public: |
| 563 VoronoiModule() : pp::Module() {} |
| 564 virtual ~VoronoiModule() {} |
| 565 |
| 566 // Create and return a Voronoi instance. |
| 567 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| 568 return new Voronoi(instance); |
| 569 } |
| 570 }; |
| 571 |
| 572 // Factory function called by the browser when the module is first loaded. |
| 573 // The browser keeps a singleton of this module. It calls the |
| 574 // CreateInstance() method on the object you return to make instances. There |
| 575 // is one instance per <embed> tag on the page. This is the main binding |
| 576 // point for your NaCl module with the browser. |
| 577 namespace pp { |
| 578 Module* CreateModule() { |
| 579 return new VoronoiModule(); |
| 580 } |
| 581 } // namespace pp |
| 582 |
OLD | NEW |