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

Side by Side Diff: examples/graphics/life/life.h

Issue 6286025: Port the Life example to Pepper 2. (Closed) Base URL: http://naclports.googlecode.com/svn/trunk/src/
Patch Set: '' Created 9 years, 10 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
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright 2011 The Native Client 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 #ifndef EXAMPLES_GRAPHICS_LIFE_LIFE_H_
6 #define EXAMPLES_GRAPHICS_LIFE_LIFE_H_
7
8 #include <ppapi/cpp/dev/scriptable_object_deprecated.h>
9 #include <ppapi/cpp/graphics_2d.h>
10 #include <ppapi/cpp/image_data.h>
11 #include <ppapi/cpp/instance.h>
12 #include <ppapi/cpp/rect.h>
13 #include <ppapi/cpp/size.h>
14 #include <pthread.h>
15
16 #include <cstdlib>
17 #include <map>
18 #include <vector>
19
20 namespace life {
21 // The main object that runs Conway's Life simulation (for details, see:
22 // http://en.wikipedia.org/wiki/Conway's_Game_of_Life). The Update() method
23 // is called by the browser to do a single tick of the simulation.
24 class Life : public pp::Instance {
25 public:
26 explicit Life(PP_Instance instance);
27 virtual ~Life();
28
29 // 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.
30 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]);
31
32 // 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.
33 // 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.
34 virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip);
35
36 // 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.
37 virtual pp::Var GetInstanceObject();
38
39 // Return a pointer to the pixels represented by |pixel_buffer_|. When this
40 // method returns, the underlying |pixel_buffer_| object is locked. This
41 // call must have a matching UnlockPixels() or various threading errors
42 // (e.g. deadlock) will occur.
43 uint32_t* LockPixels();
44 // Release the image lock acquired by LockPixels().
45 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.
46
47 // Runs a tick of the simulations, updating all buffers. Flushes the
48 // contents of |pixel_buffer_| to the 2D graphics context. This method is
49 // exposed to the browser as "update()".
50 void Update();
51
52 // Plot a new blob of life centered around (|var_x|, |var_y|). This method
53 // is exposed to the browser as "addCellAtPoint()".
54 void AddCellAtPoint(const pp::Var& var_x, const pp::Var& var_y);
55
56 int width() const {
57 return pixel_buffer_ ? pixel_buffer_->size().width() : 0;
58 }
59 int height() const {
60 return pixel_buffer_ ? pixel_buffer_->size().height() : 0;
61 }
62
63 uint32_t* pixels() const {
64 if (pixel_buffer_ != NULL && !pixel_buffer_->is_null()) {
65 return reinterpret_cast<uint32_t*>(pixel_buffer_->data());
66 }
67 return NULL;
68 }
69
70 // Indicate whether a flush is pending. This can only be called from the
71 // main thread; it is not thread safe.
72 bool flush_pending() const {
73 return flush_pending_;
74 }
75 void set_flush_pending(bool flag) {
76 flush_pending_ = flag;
77 }
78
79 private:
80 // This class exposes the scripting interface for this NaCl module. The
81 // HasMethod method is called by the browser when executing a method call on
82 // the |life| object (see, e.g. the update() function in
83 // life.html). The name of the JavaScript function (e.g. "paint") is
84 // passed in the |method| paramter as a string pp::Var. If HasMethod()
85 // returns |true|, then the browser will call the Call() method to actually
86 // invoke the method.
87 class LifeScriptObject : public pp::deprecated::ScriptableObject {
88 public:
89 explicit LifeScriptObject(Life* app_instance)
90 : pp::deprecated::ScriptableObject(),
91 app_instance_(app_instance) {}
92 virtual ~LifeScriptObject() {}
93 // Return |true| if |method| is one of the exposed method names.
94 virtual bool HasMethod(const pp::Var& method, pp::Var* exception);
95
96 // Invoke the function associated with |method|. The argument list passed
97 // in via JavaScript is marshaled into a vector of pp::Vars. None of the
98 // 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.
99 virtual pp::Var Call(const pp::Var& method,
100 const std::vector<pp::Var>& args,
101 pp::Var* exception);
102 private:
103 Life* app_instance_; // weak reference.
104 };
105
106 // Plot a new seed cell in the simulation. If |x| or |y| fall outside of the
107 // size of the 2D context, then do nothing.
108 void Plot(int x, int y);
109
110 // 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.
111 void Stir();
112
113 // Draw the current state of the simulation into the pixel buffer.
114 void UpdateCells();
115
116 // Swap the input and output cell arrays.
117 void Swap();
118
119 // Random number helper: return a random binary bit with value 0 or 1.
120 uint32_t brand() {
121 return static_cast<uint32_t>(rand_r(&brand_seed_) & 1);
122 }
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
123
124 // Create and initialize the 2D context used for drawing.
125 void CreateContext(const pp::Size& size);
126 // Destroy the 2D drawing context.
127 void DestroyContext();
128 // Push the pixels to the browser, then attempt to flush the 2D context. If
129 // there is a pending flush on the 2D context, then update the pixels only
130 // and do not flush.
131 void FlushPixelBuffer();
132
133 bool IsContextValid() const {
134 return graphics_2d_context_ != NULL;
135 }
136
137 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.
138 pp::Graphics2D* graphics_2d_context_;
139 pp::ImageData* pixel_buffer_;
140 bool flush_pending_;
141 unsigned int brand_seed_; // Note: unsigend int is required by rand_r()
142 uint8_t* cell_in_;
143 uint8_t* cell_out_;
144 };
145
146 } // namespace life
147
148 #endif // EXAMPLES_GRAPHICS_LIFE_LIFE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698