OLD | NEW |
| (Empty) |
1 // Copyright 2010 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 C_SALT_OPENGL_CONTEXT_H_ | |
6 #define C_SALT_OPENGL_CONTEXT_H_ | |
7 | |
8 /// | |
9 /// @file | |
10 /// OpenGLContext manages the OpenGL context in the browser that is associated | |
11 /// with a @a c_salt::Instance instance. | |
12 /// @see c_salt/embedded_app.h | |
13 /// | |
14 | |
15 #include <pgl/pgl.h> | |
16 #include <pthread.h> | |
17 | |
18 #include <algorithm> | |
19 #include <string> | |
20 | |
21 #include "boost/noncopyable.hpp" | |
22 #include "boost/scoped_ptr.hpp" | |
23 #include "c_salt/opengl_context_ptrs.h" | |
24 | |
25 namespace c_salt { | |
26 class Browser3DDevice; | |
27 class Instance; | |
28 | |
29 /// OpenGLContext manages an OpenGL rendering context in the browser. | |
30 /// Each OpenGLContext is associated with a @a c_salt::Instance; by default, | |
31 /// the OpenGLContext renders directly into the browser area owned by the | |
32 /// @a c_salt::Instance instance. You can also create an OpenGLContext that | |
33 /// renders into an off-screen FBO. | |
34 /// | |
35 /// Note that all OpenGLContext instance must be bound to a | |
36 /// @a c_salt::Instance instance. They cannot operate stand-alone, this | |
37 /// includes contexts that only render to an FBO. | |
38 /// | |
39 class OpenGLContext : public boost::noncopyable { | |
40 public: | |
41 /// Bind to @a instance. The in-browser context is not actually created | |
42 /// until it's needed, which is usually the first time MakeContextCurrent() | |
43 /// is called. | |
44 /// @param instance The Instance instance associated with this 3D device | |
45 /// and context. | |
46 /// @see MakeContextCurrent() | |
47 explicit OpenGLContext(const Instance& instance); | |
48 | |
49 /// Dtor makes this the current context, then posts a |kDeleteOpenGLContext| | |
50 /// notification. Destroys the underlying OpenGL context once the | |
51 /// notification handlers have all returned. | |
52 virtual ~OpenGLContext(); | |
53 | |
54 /// Get the current context on the calling thread. | |
55 /// @return The current context. If there is no OpenGLContext for the | |
56 /// calling thread, return NULL. | |
57 static SharedOpenGLContext CurrentContext(); | |
58 | |
59 /// Make this context the current rendering context. If the underlying OpenGL | |
60 /// context is not valid, this routine makes the current context invalid. | |
61 bool MakeContextCurrent(); | |
62 | |
63 /// Flush the contents of this context to the browser's 3D device. | |
64 void FlushContext() const; | |
65 | |
66 /// Makes the OpenGL context owned by this instance the current context on | |
67 /// the calling thread, and then posts a |kRenderOpenGLContext| notification. | |
68 /// When the notification handlers have all returned, the context is flushed | |
69 /// to the browser's 3D device. This OpenGL context remains the current | |
70 /// context. | |
71 /// @see FlushContext() | |
72 void RenderContext(); | |
73 | |
74 /// Release all the in-browser resources used by this context, and make this | |
75 /// context invalid. First, make this context current and post the a | |
76 /// |kDeleteOpenGLContext| notification; this allows observers to perform any | |
77 /// necessary clean-up. | |
78 void DeleteContext(); | |
79 | |
80 /// @return The Instance instance associated with this context. | |
81 const Instance& instance() const { | |
82 return instance_; | |
83 } | |
84 | |
85 /// Get the OpenGL context owned by this instance. Modifications made to the | |
86 /// raw context are not tracked by this class. If you delete the raw context | |
87 /// directly, then further use of this class will have unpredictable results. | |
88 /// @return The raw OpenGL context owned by this instance. | |
89 PGLContext pgl_context() const { | |
90 return pgl_context_; | |
91 } | |
92 | |
93 /// @return @a true if the context is valid. The context is valid if the | |
94 /// raw OpenGL context exists. | |
95 bool is_valid() const { | |
96 return pgl_context_ != PGL_NO_CONTEXT; | |
97 } | |
98 | |
99 /// Get the instance name used when posting notifications. Observers can use | |
100 /// this name to receive notificaiton from this instance only. | |
101 /// @return The unique name for this instance. | |
102 const std::string& instance_name() const { | |
103 return instance_name_; | |
104 } | |
105 | |
106 // Notifications posted by this class. | |
107 | |
108 /// Posted once when the context is first made current. Observers can perform | |
109 /// any one-time context initialization in their notification handlers. The | |
110 /// OpenGL context owned by this instance is guaranteed to be the current | |
111 /// context when this notification is posted. | |
112 static const char* const kInitializeOpenGLContextNotification; | |
113 | |
114 /// Posted once when the context is about to be deleted. The OpenGL context | |
115 /// owned by this instance is guaranteed to be the current context when this | |
116 /// notification is published. | |
117 static const char* const kDeleteOpenGLContextNotification; | |
118 | |
119 /// Posted whenever the context needs to be re-rendered. This can be in | |
120 /// response to a repaint event from the browser. The OpenGL context owned | |
121 /// by this instance is guaranteed to be current when this notification is | |
122 /// posted. Observers do not need to flush the context while handling this | |
123 /// notification, the context is flushed to the in-browser 3D device when all | |
124 /// the observers have returned from their handlers. An observer can flush | |
125 /// the context if needed, however (although performance may suffer if the | |
126 /// flush blocks). | |
127 static const char* const kRenderOpenGLContextNotification; | |
128 | |
129 private: | |
130 // Create the PGL context, return true on success. On successful return, | |
131 // |pgl_context_| will be valid and ready for rendering. | |
132 bool CreatePGLContext(); | |
133 // Destroy the PGL context. On return, |pgl_context_| will be invalid and | |
134 // have a value of PGL_NO_CONTEXT. | |
135 void DestroyPGLContext(); | |
136 | |
137 // Post |notification_name| to the default notification center associated | |
138 // with |instance_|. | |
139 void PostNotification(const char* const notification_name) const; | |
140 | |
141 const Instance& instance_; // Weak reference. | |
142 PGLContext pgl_context_; // The OpenGL context. | |
143 // The unique name of this instance, used for publishing notifications. | |
144 std::string instance_name_; | |
145 boost::scoped_ptr<Browser3DDevice> browser_device_; | |
146 | |
147 OpenGLContext(); // Not implemented, do not use. | |
148 }; | |
149 | |
150 } // namespace c_salt | |
151 | |
152 #endif // C_SALT_OPENGL_CONTEXT_H_ | |
OLD | NEW |