Panda3D
 All Classes Functions Variables Enumerations
WebBrowserTexture.cxx
00001 // Filename: WebBrowserTexture.cxx
00002 // Created by:  bei yang (Mar 2010)
00003 //
00004 //
00005 ////////////////////////////////////////////////////////////////////
00006 //
00007 // PANDA 3D SOFTWARE
00008 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00009 //
00010 // All use of this software is subject to the terms of the revised BSD
00011 // license.  You should have received a copy of this license along
00012 // with this source code in a file named "LICENSE."
00013 //
00014 ////////////////////////////////////////////////////////////////////
00015 
00016 #include "config_awesomium.h"
00017 #include "WebBrowserTexture.h"
00018 
00019 TypeHandle WebBrowserTexture::_type_handle;
00020 
00021 ////////////////////////////////////////////////////////////////////
00022 //     Function: WebBrowserTexture::WebBrowserTexture
00023 //       Access: Published
00024 //  Description: Copy constructor for web browser texture.  The behavior
00025 //               of copying a webtexture is that will be the same
00026 //               as a standard texture copy.  However, the content
00027 //               will remain the system until set_web_view is called.
00028 ////////////////////////////////////////////////////////////////////
00029 WebBrowserTexture::WebBrowserTexture(const WebBrowserTexture &copy):
00030 Texture(copy)
00031 {
00032     //this kind of assumes that the previous texture
00033     //was initialized properly
00034     _aw_web_view = copy._aw_web_view;
00035     _update_active = copy._update_active;
00036     _flip_texture_active = copy._flip_texture_active;
00037 }
00038 
00039 ////////////////////////////////////////////////////////////////////
00040 //     Function: WebBrowserTexture::WebBrowserTexture
00041 //       Access: Published
00042 //  Description: This initializes a web browser texture with the given
00043 //               AwWebView class.
00044 ////////////////////////////////////////////////////////////////////
00045 WebBrowserTexture::WebBrowserTexture(const string &name, AwWebView* aw_web_view):
00046 Texture(name),
00047 _update_active(true),
00048 _flip_texture_active(false)
00049 {
00050     set_web_view(aw_web_view);
00051     set_minfilter(FT_linear);
00052     set_magfilter(FT_linear);
00053 
00054 }
00055 
00056 ////////////////////////////////////////////////////////////////////
00057 //     Function: WebBrowserTexture::~WebBrowserTexture
00058 //       Access: Published
00059 //  Description: Standard destructor... doesn't do anything. All
00060 //               destructing happens in parent texture class.
00061 ////////////////////////////////////////////////////////////////////
00062 WebBrowserTexture::~WebBrowserTexture()
00063 {
00064     //do nothing
00065 }
00066 
00067 
00068 ////////////////////////////////////////////////////////////////////
00069 //     Function: WebBrowserTexture::~WebBrowserTexture
00070 //       Access: Published
00071 //  Description: Standard destructor... doesn't do anything. All
00072 //               destructing happens in parent texture class.
00073 ////////////////////////////////////////////////////////////////////
00074 bool WebBrowserTexture::get_keep_ram_image() const {
00075     return true;
00076 }
00077 
00078 ////////////////////////////////////////////////////////////////////
00079 //     Function: WebBrowserTexture::reload_ram_image
00080 //       Access: Protected, Virtual
00081 //  Description: A WebBrowserTexture must always keep its ram image.
00082 //               This is essentially a sub.
00083 ////////////////////////////////////////////////////////////////////
00084 void WebBrowserTexture::do_reload_ram_image() {
00085     // A MovieTexture should never dump its RAM image.
00086     // Therefore, this is not needed.
00087 }
00088 
00089 ////////////////////////////////////////////////////////////////////
00090 //     Function: WebBrowserTexture::has_cull_callback
00091 //       Access: Public, Virtual
00092 //  Description: Should be overridden by derived classes to return
00093 //               true if cull_callback() has been defined.  Otherwise,
00094 //               returns false to indicate cull_callback() does not
00095 //               need to be called for this node during the cull
00096 //               traversal.  
00097 //               
00098 //               This one returns true because it uses
00099 //               the cull traverser method to do the texture udpate.
00100 ////////////////////////////////////////////////////////////////////
00101 bool WebBrowserTexture::has_cull_callback() const {
00102     return true;
00103 }
00104 
00105 ////////////////////////////////////////////////////////////////////
00106 //     Function: WebBrowserTexture::set_web_view
00107 //       Access: Published
00108 //  Description: Sets the internal AwWebView of this texture.
00109 //               After calling this, the texture will automatically
00110 //               set it's width and height to match the AwWebView
00111 //               at the next time it is culled and rendered.
00112 ////////////////////////////////////////////////////////////////////
00113 void WebBrowserTexture::set_web_view(AwWebView* aw_web_view){
00114     _aw_web_view = aw_web_view;
00115 }
00116 
00117 
00118 ////////////////////////////////////////////////////////////////////
00119 //     Function: WebBrowserTexture::get_web_view
00120 //       Access: Published
00121 //  Description: Gets the current internal AwWebView of this texture.
00122 ////////////////////////////////////////////////////////////////////
00123 AwWebView* WebBrowserTexture::get_web_view() const{
00124     return _aw_web_view;
00125 }
00126 
00127 
00128 ////////////////////////////////////////////////////////////////////
00129 //     Function: WebBrowserTexture::set_update_active
00130 //       Access: Published
00131 //  Description: Gives the ability to toggle updating this texture
00132 //               or not.  This can be disabled to improve performance
00133 //               so that only the one that needs to be active is
00134 //               active.
00135 ////////////////////////////////////////////////////////////////////
00136 void WebBrowserTexture::set_update_active(bool active_flag){
00137     _update_active = active_flag;
00138 }
00139 
00140 
00141 ////////////////////////////////////////////////////////////////////
00142 //     Function: WebBrowserTexture::get_update_active
00143 //       Access: Published
00144 //  Description: Gets whether or not this texture is updating
00145 //               itself every time it is rendered.
00146 ////////////////////////////////////////////////////////////////////
00147 bool WebBrowserTexture::get_update_active() const{
00148     return _update_active;
00149 }
00150 
00151 
00152 ////////////////////////////////////////////////////////////////////
00153 //     Function: WebBrowserTexture::set_flip_texture_active
00154 //       Access: Published
00155 //  Description: This toggles on/off automatic flipping of the
00156 //               of the texture at a source level.  Awesomium renders
00157 //               things that are flipped vertically.  This enables
00158 //               automatic flipping of that.
00159 //
00160 //               Since it is doing byte manipulation, this can get 
00161 //               rather slow. Turning this on should be avoided.
00162 //               Instead, flipping should be taken care of via UV
00163 //               coordinates or shaders.
00164 ////////////////////////////////////////////////////////////////////
00165 void WebBrowserTexture::set_flip_texture_active(bool active_flag){
00166     _flip_texture_active = active_flag;
00167 }
00168 
00169 ////////////////////////////////////////////////////////////////////
00170 //     Function: WebBrowserTexture::get_flip_texture_active
00171 //       Access: Published
00172 //  Description: Returns whether automatic texture flipping is
00173 //               enabled.
00174 ////////////////////////////////////////////////////////////////////
00175 bool WebBrowserTexture::get_flip_texture_active() const {
00176     return _flip_texture_active;
00177 }
00178 
00179 
00180 ////////////////////////////////////////////////////////////////////
00181 //     Function: WebBrowserTexture::cull_callback
00182 //       Access: Public, Virtual
00183 //  Description: This function will be called during the cull 
00184 //               traversal to update the WebBrowserTexture.  This
00185 //               method calls the render method of AwWebView but
00186 //               does not call the update method of AwWebCore.
00187 ////////////////////////////////////////////////////////////////////
00188 bool WebBrowserTexture::cull_callback(CullTraverser *trav, const CullTraverserData &data) const{
00189     //see if we are in a state where udpates can happen. else just return
00190     if( !_update_active ) return true;
00191     if( _aw_web_view == NULL ) return true;
00192 
00193     //do we even need to update?
00194     if( !_aw_web_view->is_dirty() ) return true;
00195 
00196     //see if we're the same size, if not we need to make sure this texture
00197     //matches the webview
00198     if( _aw_web_view->get_width() != get_x_size() || _aw_web_view->get_height() != get_y_size() || get_texture_type() != TT_2d_texture){
00199         //these casts are so dirty especially when the method itself is
00200         //labled as const.  Really Texture::cull_callback should be not const
00201         //first clean up
00202         ((WebBrowserTexture*)this)->clear_ram_mipmap_images();
00203         ((WebBrowserTexture*)this)->clear_ram_image();
00204         //now set up the texture again
00205         ((WebBrowserTexture*)this)->setup_2d_texture( _aw_web_view->get_width(), _aw_web_view->get_height(), T_unsigned_byte, F_rgba );
00206         //should be good to go at this point
00207     }
00208 
00209     //get the pointer
00210     PTA_uchar ram_image = ((WebBrowserTexture*)this)->modify_ram_image();
00211     unsigned char* cp_data = ram_image.p();
00212     //render it
00213     _aw_web_view->render((void*)cp_data, get_x_size()*4, 4);
00214 
00215     if(_flip_texture_active){
00216         //flips the texture around... this is super slow. Really this should
00217         //never be enabled.  However beginners might find this useful
00218         size_t width = get_x_size();
00219         size_t height = get_y_size();
00220         for(size_t i=0; i < height/2; i++){
00221             for(size_t j=0; j < width; j++){
00222                 unsigned char tmp[4];
00223                 size_t a_pos = j+width*i;
00224                 size_t b_pos = j + width*(height-i-1);
00225                 memcpy(tmp,&cp_data[4*a_pos], 4); //tmp = a
00226                 memcpy(&cp_data[4*a_pos], &cp_data[4*b_pos], 4); //a = b
00227                 memcpy(&cp_data[4*b_pos], tmp, 4); //b = tmp
00228             }
00229         }
00230     }
00231     //success
00232     return true;
00233 }
 All Classes Functions Variables Enumerations