Panda3D

parasiteBuffer.cxx

00001 // Filename: parasiteBuffer.cxx
00002 // Created by:  drose (27Feb04)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "parasiteBuffer.h"
00016 #include "texture.h"
00017 
00018 TypeHandle ParasiteBuffer::_type_handle;
00019 
00020 ////////////////////////////////////////////////////////////////////
00021 //     Function: ParasiteBuffer::Constructor
00022 //       Access: Public
00023 //  Description: Normally, the ParasiteBuffer constructor is not
00024 //               called directly; these are created instead via the
00025 //               GraphicsEngine::make_parasite() function.
00026 ////////////////////////////////////////////////////////////////////
00027 ParasiteBuffer::
00028 ParasiteBuffer(GraphicsOutput *host, const string &name,
00029                int x_size, int y_size, int flags) :
00030   GraphicsOutput(host->get_engine(), host->get_pipe(), 
00031                  name, host->get_fb_properties(),
00032                  WindowProperties::size(x_size, y_size), flags, 
00033                  host->get_gsg(), host)
00034 {
00035 #ifdef DO_MEMORY_USAGE
00036   MemoryUsage::update_type(this, this);
00037 #endif
00038   
00039   if (display_cat.is_debug()) {
00040     display_cat.debug()
00041       << "Creating new parasite buffer " << get_name()
00042       << " on " << _host->get_name() << "\n";
00043   }
00044 
00045   _creation_flags = flags;
00046   
00047   if (flags & GraphicsPipe::BF_size_track_host) {
00048     x_size = host->get_x_size();
00049     y_size = host->get_y_size();
00050   }
00051   
00052   _x_size = x_size;
00053   _y_size = y_size;
00054   _has_size = true;
00055   _default_display_region->compute_pixels(_x_size, _y_size);
00056   _is_valid = true;
00057   
00058   set_inverted(host->get_gsg()->get_copy_texture_inverted());
00059 }
00060 
00061 ////////////////////////////////////////////////////////////////////
00062 //     Function: ParasiteBuffer::Destructor
00063 //       Access: Published, Virtual
00064 //  Description: 
00065 ////////////////////////////////////////////////////////////////////
00066 ParasiteBuffer::
00067 ~ParasiteBuffer() {
00068   _is_valid = false;
00069 }
00070 
00071 ////////////////////////////////////////////////////////////////////
00072 //     Function: ParasiteBuffer::set_size
00073 //       Access: Public, Virtual
00074 //  Description: This is called by the GraphicsEngine to request that
00075 //               the buffer resize itself.  Although calls to get the
00076 //               size will return the new value, much of the actual
00077 //               resizing work doesn't take place until the next
00078 //               begin_frame.  Not all buffers are resizeable.
00079 ////////////////////////////////////////////////////////////////////
00080 void ParasiteBuffer::
00081 set_size(int x, int y) {
00082   if ((_creation_flags & GraphicsPipe::BF_resizeable) == 0) {
00083     nassert_raise("Cannot resize buffer unless it is created with BF_resizeable flag");
00084     return;
00085   }
00086   set_size_and_recalc(x, y);
00087 }
00088 
00089 ////////////////////////////////////////////////////////////////////
00090 //     Function: ParasiteBuffer::set_size_and_recalc
00091 //       Access: Public
00092 //  Description: 
00093 ////////////////////////////////////////////////////////////////////
00094 void ParasiteBuffer::
00095 set_size_and_recalc(int x, int y) {
00096   if (!(_creation_flags & GraphicsPipe::BF_size_track_host)) {
00097     if (_creation_flags & GraphicsPipe::BF_size_power_2) {
00098       x = Texture::down_to_power_2(x);
00099       y = Texture::down_to_power_2(y);
00100     }
00101     if (_creation_flags & GraphicsPipe::BF_size_square) {
00102       x = y = min(x, y);
00103     }
00104   }
00105 
00106   GraphicsOutput::set_size_and_recalc(x, y);
00107 }
00108 
00109 ////////////////////////////////////////////////////////////////////
00110 //     Function: ParasiteBuffer::is_active
00111 //       Access: Published, Virtual
00112 //  Description: Returns true if the window is ready to be rendered
00113 //               into, false otherwise.
00114 ////////////////////////////////////////////////////////////////////
00115 bool ParasiteBuffer::
00116 is_active() const {
00117   return _active && _host->is_active();
00118 }
00119 
00120 ////////////////////////////////////////////////////////////////////
00121 //     Function: ParasiteBuffer::get_host
00122 //       Access: Public, Virtual
00123 //  Description: This is normally called only from within
00124 //               make_texture_buffer().  When called on a
00125 //               ParasiteBuffer, it returns the host of that buffer;
00126 //               but when called on some other buffer, it returns the
00127 //               buffer itself.
00128 ////////////////////////////////////////////////////////////////////
00129 GraphicsOutput *ParasiteBuffer::
00130 get_host() {
00131   return _host;
00132 }
00133 
00134 ////////////////////////////////////////////////////////////////////
00135 //     Function: ParasiteBuffer::begin_frame
00136 //       Access: Public, Virtual
00137 //  Description: This function will be called within the draw thread
00138 //               before beginning rendering for a given frame.  It
00139 //               should do whatever setup is required, and return true
00140 //               if the frame should be rendered, or false if it
00141 //               should be skipped.
00142 ////////////////////////////////////////////////////////////////////
00143 bool ParasiteBuffer::
00144 begin_frame(FrameMode mode, Thread *current_thread) {
00145   begin_frame_spam(mode);
00146 
00147   if (!_host->begin_frame(FM_parasite, current_thread)) {
00148     return false;
00149   }
00150 
00151   if (_creation_flags & GraphicsPipe::BF_size_track_host) {
00152     if ((_host->get_x_size() != _x_size)||
00153         (_host->get_y_size() != _y_size)) {
00154       set_size_and_recalc(_host->get_x_size(),
00155                           _host->get_y_size());
00156     }
00157   } else {
00158     if (_host->get_x_size() < _x_size ||
00159         _host->get_y_size() < _y_size) {
00160       set_size_and_recalc(min(_x_size, _host->get_x_size()),
00161                           min(_y_size, _host->get_y_size()));
00162     }
00163   }
00164   
00165   clear_cube_map_selection();
00166   return true;
00167 }
00168 
00169 ////////////////////////////////////////////////////////////////////
00170 //     Function: ParasiteBuffer::end_frame
00171 //       Access: Public, Virtual
00172 //  Description: This function will be called within the draw thread
00173 //               after rendering is completed for a given frame.  It
00174 //               should do whatever finalization is required.
00175 ////////////////////////////////////////////////////////////////////
00176 void ParasiteBuffer::
00177 end_frame(FrameMode mode, Thread *current_thread) {
00178   end_frame_spam(mode);
00179 
00180   nassertv(_gsg != (GraphicsStateGuardian *)NULL);
00181 
00182   _host->end_frame(FM_parasite, current_thread);
00183 
00184   if (mode == FM_refresh) {
00185     return;
00186   }
00187   
00188   if (mode == FM_render) {
00189     for (int i=0; i<count_textures(); i++) {
00190       if (get_rtm_mode(i) == RTM_bind_or_copy) {
00191         _textures[i]._rtm_mode = RTM_copy_texture;
00192       }
00193     }
00194     copy_to_textures();
00195     if (_one_shot) {
00196       prepare_for_deletion();
00197     }
00198     clear_cube_map_selection();
00199   }
00200 }
00201 
 All Classes Functions Variables Enumerations