Chromium Code Reviews| Index: examples/graphics/life/life.h |
| =================================================================== |
| --- examples/graphics/life/life.h (revision 0) |
| +++ examples/graphics/life/life.h (revision 0) |
| @@ -0,0 +1,148 @@ |
| +// Copyright 2011 The Native Client Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can |
| +// be found in the LICENSE file. |
| + |
| +#ifndef EXAMPLES_GRAPHICS_LIFE_LIFE_H_ |
| +#define EXAMPLES_GRAPHICS_LIFE_LIFE_H_ |
| + |
| +#include <ppapi/cpp/dev/scriptable_object_deprecated.h> |
| +#include <ppapi/cpp/graphics_2d.h> |
| +#include <ppapi/cpp/image_data.h> |
| +#include <ppapi/cpp/instance.h> |
| +#include <ppapi/cpp/rect.h> |
| +#include <ppapi/cpp/size.h> |
| +#include <pthread.h> |
| + |
| +#include <cstdlib> |
| +#include <map> |
| +#include <vector> |
| + |
| +namespace life { |
| +// The main object that runs Conway's Life simulation (for details, see: |
| +// http://en.wikipedia.org/wiki/Conway's_Game_of_Life). The Update() method |
| +// is called by the browser to do a single tick of the simulation. |
| +class Life : public pp::Instance { |
| + public: |
| + explicit Life(PP_Instance instance); |
| + virtual ~Life(); |
| + |
| + // Start up the ComputePi() thread. |
|
dmichael(do not use this one)
2011/02/02 23:03:59
Bad comment
David Springer
2011/02/03 18:12:27
Done.
|
| + virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]); |
| + |
| + // Update the graphcs context to the new size, and regnerate |pixel_buffer_| |
|
dmichael(do not use this one)
2011/02/02 23:03:59
s/regnerate/regenerate
David Springer
2011/02/03 18:12:27
Done.
|
| + // to fit the new size as well. |
|
dmichael(do not use this one)
2011/02/02 23:03:59
also cell_in_ and cell_out_
David Springer
2011/02/03 18:12:27
Done.
|
| + virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip); |
| + |
| + // The pp::Var takes over ownership of the returned script object. |
|
dmichael(do not use this one)
2011/02/02 23:03:59
We can probably say more... e.g. Return a pp::Var
David Springer
2011/02/03 18:12:27
Done.
|
| + virtual pp::Var GetInstanceObject(); |
| + |
| + // Return a pointer to the pixels represented by |pixel_buffer_|. When this |
| + // method returns, the underlying |pixel_buffer_| object is locked. This |
| + // call must have a matching UnlockPixels() or various threading errors |
| + // (e.g. deadlock) will occur. |
| + uint32_t* LockPixels(); |
| + // Release the image lock acquired by LockPixels(). |
| + void UnlockPixels() const; |
|
dmichael(do not use this one)
2011/02/02 23:03:59
These appear to be neither defined nor used, and t
David Springer
2011/02/03 18:12:27
Done.
|
| + |
| + // Runs a tick of the simulations, updating all buffers. Flushes the |
| + // contents of |pixel_buffer_| to the 2D graphics context. This method is |
| + // exposed to the browser as "update()". |
| + void Update(); |
| + |
| + // Plot a new blob of life centered around (|var_x|, |var_y|). This method |
| + // is exposed to the browser as "addCellAtPoint()". |
| + void AddCellAtPoint(const pp::Var& var_x, const pp::Var& var_y); |
| + |
| + int width() const { |
| + return pixel_buffer_ ? pixel_buffer_->size().width() : 0; |
| + } |
| + int height() const { |
| + return pixel_buffer_ ? pixel_buffer_->size().height() : 0; |
| + } |
| + |
| + uint32_t* pixels() const { |
| + if (pixel_buffer_ != NULL && !pixel_buffer_->is_null()) { |
| + return reinterpret_cast<uint32_t*>(pixel_buffer_->data()); |
| + } |
| + return NULL; |
| + } |
| + |
| + // Indicate whether a flush is pending. This can only be called from the |
| + // main thread; it is not thread safe. |
| + bool flush_pending() const { |
| + return flush_pending_; |
| + } |
| + void set_flush_pending(bool flag) { |
| + flush_pending_ = flag; |
| + } |
| + |
| + private: |
| + // This class exposes the scripting interface for this NaCl module. The |
| + // HasMethod method is called by the browser when executing a method call on |
| + // the |life| object (see, e.g. the update() function in |
| + // life.html). The name of the JavaScript function (e.g. "paint") is |
| + // passed in the |method| paramter as a string pp::Var. If HasMethod() |
| + // returns |true|, then the browser will call the Call() method to actually |
| + // invoke the method. |
| + class LifeScriptObject : public pp::deprecated::ScriptableObject { |
| + public: |
| + explicit LifeScriptObject(Life* app_instance) |
| + : pp::deprecated::ScriptableObject(), |
| + app_instance_(app_instance) {} |
| + virtual ~LifeScriptObject() {} |
| + // Return |true| if |method| is one of the exposed method names. |
| + virtual bool HasMethod(const pp::Var& method, pp::Var* exception); |
| + |
| + // Invoke the function associated with |method|. The argument list passed |
| + // in via JavaScript is marshaled into a vector of pp::Vars. None of the |
| + // functions in this example take arguments, so this vector is always empty. |
|
dmichael(do not use this one)
2011/02/02 23:03:59
Not true... AddCellAtPoint takes arguments.
David Springer
2011/02/03 18:12:27
Done.
|
| + virtual pp::Var Call(const pp::Var& method, |
| + const std::vector<pp::Var>& args, |
| + pp::Var* exception); |
| + private: |
| + Life* app_instance_; // weak reference. |
| + }; |
| + |
| + // Plot a new seed cell in the simulation. If |x| or |y| fall outside of the |
| + // size of the 2D context, then do nothing. |
| + void Plot(int x, int y); |
| + |
| + // Add in some random noise to the cells, this is part of a simulation tick. |
|
dmichael(do not use this one)
2011/02/02 23:03:59
Maybe mention that the randomness is added to the
David Springer
2011/02/03 18:12:27
Done.
|
| + void Stir(); |
| + |
| + // Draw the current state of the simulation into the pixel buffer. |
| + void UpdateCells(); |
| + |
| + // Swap the input and output cell arrays. |
| + void Swap(); |
| + |
| + // Random number helper: return a random binary bit with value 0 or 1. |
| + uint32_t brand() { |
| + return static_cast<uint32_t>(rand_r(&brand_seed_) & 1); |
| + } |
|
dmichael(do not use this one)
2011/02/02 23:03:59
I think this should go back in the body... callin
David Springer
2011/02/03 18:12:27
Done.
Actually, I factored the random bit generat
|
| + |
| + // Create and initialize the 2D context used for drawing. |
| + void CreateContext(const pp::Size& size); |
| + // Destroy the 2D drawing context. |
| + void DestroyContext(); |
| + // Push the pixels to the browser, then attempt to flush the 2D context. If |
| + // there is a pending flush on the 2D context, then update the pixels only |
| + // and do not flush. |
| + void FlushPixelBuffer(); |
| + |
| + bool IsContextValid() const { |
| + return graphics_2d_context_ != NULL; |
| + } |
| + |
| + mutable pthread_mutex_t pixel_buffer_mutex_; |
|
dmichael(do not use this one)
2011/02/02 23:03:59
Unused.
David Springer
2011/02/03 18:12:27
Done.
|
| + pp::Graphics2D* graphics_2d_context_; |
| + pp::ImageData* pixel_buffer_; |
| + bool flush_pending_; |
| + unsigned int brand_seed_; // Note: unsigend int is required by rand_r() |
| + uint8_t* cell_in_; |
| + uint8_t* cell_out_; |
| +}; |
| + |
| +} // namespace life |
| + |
| +#endif // EXAMPLES_GRAPHICS_LIFE_LIFE_H_ |
| Property changes on: examples/graphics/life/life.h |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| + LF |