Panda3D

tinyGraphicsBuffer.cxx

00001 // Filename: tinyGraphicsBuffer.cxx
00002 // Created by:  drose (08Aug08)
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 "pandabase.h"
00016 
00017 #include "tinyGraphicsBuffer.h"
00018 #include "config_tinydisplay.h"
00019 #include "tinyGraphicsStateGuardian.h"
00020 #include "pStatTimer.h"
00021 
00022 TypeHandle TinyGraphicsBuffer::_type_handle;
00023 
00024 ////////////////////////////////////////////////////////////////////
00025 //     Function: TinyGraphicsBuffer::Constructor
00026 //       Access: Public
00027 //  Description:
00028 ////////////////////////////////////////////////////////////////////
00029 TinyGraphicsBuffer::
00030 TinyGraphicsBuffer(GraphicsEngine *engine, GraphicsPipe *pipe, 
00031                    const string &name,
00032                    const FrameBufferProperties &fb_prop,
00033                    const WindowProperties &win_prop,
00034                    int flags,
00035                    GraphicsStateGuardian *gsg,
00036                    GraphicsOutput *host) :
00037   GraphicsBuffer(engine, pipe, name, fb_prop, win_prop, flags, gsg, host)
00038 {
00039   _frame_buffer = NULL;
00040 }
00041 
00042 ////////////////////////////////////////////////////////////////////
00043 //     Function: TinyGraphicsBuffer::Destructor
00044 //       Access: Public, Virtual
00045 //  Description:
00046 ////////////////////////////////////////////////////////////////////
00047 TinyGraphicsBuffer::
00048 ~TinyGraphicsBuffer() {
00049 }
00050 
00051 ////////////////////////////////////////////////////////////////////
00052 //     Function: TinyGraphicsBuffer::begin_frame
00053 //       Access: Public, Virtual
00054 //  Description: This function will be called within the draw thread
00055 //               before beginning rendering for a given frame.  It
00056 //               should do whatever setup is required, and return true
00057 //               if the frame should be rendered, or false if it
00058 //               should be skipped.
00059 ////////////////////////////////////////////////////////////////////
00060 bool TinyGraphicsBuffer::
00061 begin_frame(FrameMode mode, Thread *current_thread) {
00062   begin_frame_spam(mode);
00063   if (_gsg == (GraphicsStateGuardian *)NULL) {
00064     return false;
00065   }
00066 
00067   TinyGraphicsStateGuardian *tinygsg;
00068   DCAST_INTO_R(tinygsg, _gsg, false);
00069 
00070   tinygsg->_current_frame_buffer = _frame_buffer;
00071   tinygsg->reset_if_new();
00072   
00073   _gsg->set_current_properties(&get_fb_properties());
00074   return _gsg->begin_frame(current_thread);
00075 }
00076 
00077 ////////////////////////////////////////////////////////////////////
00078 //     Function: TinyGraphicsBuffer::end_frame
00079 //       Access: Public, Virtual
00080 //  Description: This function will be called within the draw thread
00081 //               after rendering is completed for a given frame.  It
00082 //               should do whatever finalization is required.
00083 ////////////////////////////////////////////////////////////////////
00084 void TinyGraphicsBuffer::
00085 end_frame(FrameMode mode, Thread *current_thread) {
00086   end_frame_spam(mode);
00087   nassertv(_gsg != (GraphicsStateGuardian *)NULL);
00088 
00089   if (mode == FM_render) {
00090     // end_render_texture();
00091     copy_to_textures();
00092   }
00093 
00094   _gsg->end_frame(current_thread);
00095 
00096   if (mode == FM_render) {
00097     trigger_flip();
00098     if (_one_shot) {
00099       prepare_for_deletion();
00100     }
00101     clear_cube_map_selection();
00102   }
00103 }
00104 
00105 ////////////////////////////////////////////////////////////////////
00106 //     Function: TinyGraphicsBuffer::close_buffer
00107 //       Access: Protected, Virtual
00108 //  Description: Closes the buffer right now.  Called from the buffer
00109 //               thread.
00110 ////////////////////////////////////////////////////////////////////
00111 void TinyGraphicsBuffer::
00112 close_buffer() {
00113   if (_gsg != (GraphicsStateGuardian *)NULL) {
00114     TinyGraphicsStateGuardian *tinygsg;
00115     DCAST_INTO_V(tinygsg, _gsg);
00116     tinygsg->_current_frame_buffer = NULL;
00117     _gsg.clear();
00118     _active = false;
00119   }
00120 
00121   _is_valid = false;
00122 }
00123 
00124 ////////////////////////////////////////////////////////////////////
00125 //     Function: TinyGraphicsBuffer::open_buffer
00126 //       Access: Protected, Virtual
00127 //  Description: Opens the buffer right now.  Called from the buffer
00128 //               thread.  Returns true if the buffer is successfully
00129 //               opened, or false if there was a problem.
00130 ////////////////////////////////////////////////////////////////////
00131 bool TinyGraphicsBuffer::
00132 open_buffer() {
00133   // GSG Creation/Initialization
00134   TinyGraphicsStateGuardian *tinygsg;
00135   if (_gsg == 0) {
00136     // There is no old gsg.  Create a new one.
00137     tinygsg = new TinyGraphicsStateGuardian(_engine, _pipe, NULL);
00138     _gsg = tinygsg;
00139   } else {
00140     DCAST_INTO_R(tinygsg, _gsg, false);
00141   }
00142   
00143   create_frame_buffer();
00144   if (_frame_buffer == NULL) {
00145     tinydisplay_cat.error()
00146       << "Could not create frame buffer.\n";
00147     return false;
00148   }
00149 
00150   tinygsg->_current_frame_buffer = _frame_buffer;
00151   
00152   tinygsg->reset_if_new();
00153   if (!tinygsg->is_valid()) {
00154     close_buffer();
00155     return false;
00156   }
00157 
00158   _is_valid = true;
00159   return true;
00160 }
00161 
00162 ////////////////////////////////////////////////////////////////////
00163 //     Function: TinyGraphicsBuffer::create_frame_buffer
00164 //       Access: Private
00165 //  Description: Creates a suitable frame buffer for the current
00166 //               window size.
00167 ////////////////////////////////////////////////////////////////////
00168 void TinyGraphicsBuffer::
00169 create_frame_buffer() {
00170   if (_frame_buffer != NULL) {
00171     ZB_close(_frame_buffer);
00172     _frame_buffer = NULL;
00173   }
00174 
00175   _frame_buffer = ZB_open(get_fb_x_size(), get_fb_y_size(), ZB_MODE_RGBA, 0, 0, 0, 0);
00176 }
00177 
 All Classes Functions Variables Enumerations