Panda3D
WebBrowserTexture.cxx
1 // Filename: WebBrowserTexture.cxx
2 // Created by: bei yang (Mar 2010)
3 //
4 //
5 ////////////////////////////////////////////////////////////////////
6 //
7 // PANDA 3D SOFTWARE
8 // Copyright (c) Carnegie Mellon University. All rights reserved.
9 //
10 // All use of this software is subject to the terms of the revised BSD
11 // license. You should have received a copy of this license along
12 // with this source code in a file named "LICENSE."
13 //
14 ////////////////////////////////////////////////////////////////////
15 
16 #include "config_awesomium.h"
17 #include "WebBrowserTexture.h"
18 
19 TypeHandle WebBrowserTexture::_type_handle;
20 
21 ////////////////////////////////////////////////////////////////////
22 // Function: WebBrowserTexture::WebBrowserTexture
23 // Access: Published
24 // Description: Copy constructor for web browser texture. The behavior
25 // of copying a webtexture is that will be the same
26 // as a standard texture copy. However, the content
27 // will remain the system until set_web_view is called.
28 ////////////////////////////////////////////////////////////////////
29 WebBrowserTexture::WebBrowserTexture(const WebBrowserTexture &copy):
30 Texture(copy)
31 {
32  //this kind of assumes that the previous texture
33  //was initialized properly
34  _aw_web_view = copy._aw_web_view;
35  _update_active = copy._update_active;
36  _flip_texture_active = copy._flip_texture_active;
37 }
38 
39 ////////////////////////////////////////////////////////////////////
40 // Function: WebBrowserTexture::WebBrowserTexture
41 // Access: Published
42 // Description: This initializes a web browser texture with the given
43 // AwWebView class.
44 ////////////////////////////////////////////////////////////////////
45 WebBrowserTexture::WebBrowserTexture(const string &name, AwWebView* aw_web_view):
46 Texture(name),
47 _update_active(true),
48 _flip_texture_active(false)
49 {
50  set_web_view(aw_web_view);
51  set_minfilter(FT_linear);
52  set_magfilter(FT_linear);
53 
54 }
55 
56 ////////////////////////////////////////////////////////////////////
57 // Function: WebBrowserTexture::~WebBrowserTexture
58 // Access: Published
59 // Description: Standard destructor... doesn't do anything. All
60 // destructing happens in parent texture class.
61 ////////////////////////////////////////////////////////////////////
63 {
64  //do nothing
65 }
66 
67 
68 ////////////////////////////////////////////////////////////////////
69 // Function: WebBrowserTexture::~WebBrowserTexture
70 // Access: Published
71 // Description: Standard destructor... doesn't do anything. All
72 // destructing happens in parent texture class.
73 ////////////////////////////////////////////////////////////////////
74 bool WebBrowserTexture::get_keep_ram_image() const {
75  return true;
76 }
77 
78 ////////////////////////////////////////////////////////////////////
79 // Function: WebBrowserTexture::reload_ram_image
80 // Access: Protected, Virtual
81 // Description: A WebBrowserTexture must always keep its ram image.
82 // This is essentially a sub.
83 ////////////////////////////////////////////////////////////////////
84 void WebBrowserTexture::do_reload_ram_image() {
85  // A MovieTexture should never dump its RAM image.
86  // Therefore, this is not needed.
87 }
88 
89 ////////////////////////////////////////////////////////////////////
90 // Function: WebBrowserTexture::has_cull_callback
91 // Access: Public, Virtual
92 // Description: Should be overridden by derived classes to return
93 // true if cull_callback() has been defined. Otherwise,
94 // returns false to indicate cull_callback() does not
95 // need to be called for this node during the cull
96 // traversal.
97 //
98 // This one returns true because it uses
99 // the cull traverser method to do the texture udpate.
100 ////////////////////////////////////////////////////////////////////
102  return true;
103 }
104 
105 ////////////////////////////////////////////////////////////////////
106 // Function: WebBrowserTexture::set_web_view
107 // Access: Published
108 // Description: Sets the internal AwWebView of this texture.
109 // After calling this, the texture will automatically
110 // set it's width and height to match the AwWebView
111 // at the next time it is culled and rendered.
112 ////////////////////////////////////////////////////////////////////
114  _aw_web_view = aw_web_view;
115 }
116 
117 
118 ////////////////////////////////////////////////////////////////////
119 // Function: WebBrowserTexture::get_web_view
120 // Access: Published
121 // Description: Gets the current internal AwWebView of this texture.
122 ////////////////////////////////////////////////////////////////////
124  return _aw_web_view;
125 }
126 
127 
128 ////////////////////////////////////////////////////////////////////
129 // Function: WebBrowserTexture::set_update_active
130 // Access: Published
131 // Description: Gives the ability to toggle updating this texture
132 // or not. This can be disabled to improve performance
133 // so that only the one that needs to be active is
134 // active.
135 ////////////////////////////////////////////////////////////////////
137  _update_active = active_flag;
138 }
139 
140 
141 ////////////////////////////////////////////////////////////////////
142 // Function: WebBrowserTexture::get_update_active
143 // Access: Published
144 // Description: Gets whether or not this texture is updating
145 // itself every time it is rendered.
146 ////////////////////////////////////////////////////////////////////
148  return _update_active;
149 }
150 
151 
152 ////////////////////////////////////////////////////////////////////
153 // Function: WebBrowserTexture::set_flip_texture_active
154 // Access: Published
155 // Description: This toggles on/off automatic flipping of the
156 // of the texture at a source level. Awesomium renders
157 // things that are flipped vertically. This enables
158 // automatic flipping of that.
159 //
160 // Since it is doing byte manipulation, this can get
161 // rather slow. Turning this on should be avoided.
162 // Instead, flipping should be taken care of via UV
163 // coordinates or shaders.
164 ////////////////////////////////////////////////////////////////////
166  _flip_texture_active = active_flag;
167 }
168 
169 ////////////////////////////////////////////////////////////////////
170 // Function: WebBrowserTexture::get_flip_texture_active
171 // Access: Published
172 // Description: Returns whether automatic texture flipping is
173 // enabled.
174 ////////////////////////////////////////////////////////////////////
176  return _flip_texture_active;
177 }
178 
179 
180 ////////////////////////////////////////////////////////////////////
181 // Function: WebBrowserTexture::cull_callback
182 // Access: Public, Virtual
183 // Description: This function will be called during the cull
184 // traversal to update the WebBrowserTexture. This
185 // method calls the render method of AwWebView but
186 // does not call the update method of AwWebCore.
187 ////////////////////////////////////////////////////////////////////
189  //see if we are in a state where udpates can happen. else just return
190  if( !_update_active ) return true;
191  if( _aw_web_view == NULL ) return true;
192 
193  //do we even need to update?
194  if( !_aw_web_view->is_dirty() ) return true;
195 
196  //see if we're the same size, if not we need to make sure this texture
197  //matches the webview
198  if( _aw_web_view->get_width() != get_x_size() || _aw_web_view->get_height() != get_y_size() || get_texture_type() != TT_2d_texture){
199  //these casts are so dirty especially when the method itself is
200  //labled as const. Really Texture::cull_callback should be not const
201  //first clean up
202  ((WebBrowserTexture*)this)->clear_ram_mipmap_images();
203  ((WebBrowserTexture*)this)->clear_ram_image();
204  //now set up the texture again
205  ((WebBrowserTexture*)this)->setup_2d_texture( _aw_web_view->get_width(), _aw_web_view->get_height(), T_unsigned_byte, F_rgba );
206  //should be good to go at this point
207  }
208 
209  //get the pointer
210  PTA_uchar ram_image = ((WebBrowserTexture*)this)->modify_ram_image();
211  unsigned char* cp_data = ram_image.p();
212  //render it
213  _aw_web_view->render((void*)cp_data, get_x_size()*4, 4);
214 
215  if(_flip_texture_active){
216  //flips the texture around... this is super slow. Really this should
217  //never be enabled. However beginners might find this useful
218  size_t width = get_x_size();
219  size_t height = get_y_size();
220  for(size_t i=0; i < height/2; i++){
221  for(size_t j=0; j < width; j++){
222  unsigned char tmp[4];
223  size_t a_pos = j+width*i;
224  size_t b_pos = j + width*(height-i-1);
225  memcpy(tmp,&cp_data[4*a_pos], 4); //tmp = a
226  memcpy(&cp_data[4*a_pos], &cp_data[4*b_pos], 4); //a = b
227  memcpy(&cp_data[4*b_pos], tmp, 4); //b = tmp
228  }
229  }
230  }
231  //success
232  return true;
233 }
void set_flip_texture_active(bool active_flag)
This toggles on/off automatic flipping of the of the texture at a source level.
TextureType get_texture_type() const
Returns the overall interpretation of the texture.
Definition: texture.I:859
virtual ~WebBrowserTexture()
Standard destructor...
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
Definition: texture.h:75
This collects together the pieces of data that are accumulated for each node while walking the scene ...
void set_update_active(bool active_flag)
Gives the ability to toggle updating this texture or not.
void set_magfilter(FilterType filter)
Sets the filtering method that should be used when viewing the texture up close.
Definition: texture.I:967
void set_web_view(AwWebView *aw_web_view)
Sets the internal AwWebView of this texture.
virtual bool has_cull_callback() const
Should be overridden by derived classes to return true if cull_callback() has been defined...
const Element * p() const
Function p() is similar to the function from ConstPointerTo.
virtual bool cull_callback(CullTraverser *trav, const CullTraverserData &data) const
This function will be called during the cull traversal to update the WebBrowserTexture.
bool get_flip_texture_active() const
Returns whether automatic texture flipping is enabled.
bool get_update_active() const
Gets whether or not this texture is updating itself every time it is rendered.
int get_y_size() const
Returns the height of the texture image in texels.
Definition: texture.I:650
Thin bindings, wraps a WebView * returned from WebCore.createWebView.
Definition: awWebView.h:29
AwWebView * get_web_view() const
Gets the current internal AwWebView of this texture.
void set_minfilter(FilterType filter)
Sets the filtering method that should be used when viewing the texture from a distance.
Definition: texture.I:951
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
This object performs a depth-first traversal of the scene graph, with optional view-frustum culling...
Definition: cullTraverser.h:48
A Wrapper class for Awesomium webview.
int get_x_size() const
Returns the width of the texture image in texels.
Definition: texture.I:638