osMesaGraphicsBuffer.cxx

00001 // Filename: osMesaGraphicsBuffer.cxx
00002 // Created by:  drose (09Feb04)
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 "osMesaGraphicsBuffer.h"
00016 #include "config_mesadisplay.h"
00017 #include "osMesaGraphicsPipe.h"
00018 #include "osMesaGraphicsStateGuardian.h"
00019 
00020 TypeHandle OsMesaGraphicsBuffer::_type_handle;
00021 
00022 ////////////////////////////////////////////////////////////////////
00023 //     Function: OsMesaGraphicsBuffer::Constructor
00024 //       Access: Public
00025 //  Description:
00026 ////////////////////////////////////////////////////////////////////
00027 OsMesaGraphicsBuffer::
00028 OsMesaGraphicsBuffer(GraphicsEngine *engine, GraphicsPipe *pipe,
00029                      const string &name,
00030                      const FrameBufferProperties &fb_prop,
00031                      const WindowProperties &win_prop,
00032                      int flags,
00033                      GraphicsStateGuardian *gsg,
00034                      GraphicsOutput *host) :
00035   GraphicsBuffer(engine, pipe, name, fb_prop, win_prop, flags, gsg, host)
00036 {
00037   _type = GL_UNSIGNED_BYTE;
00038   _screenshot_buffer_type = _draw_buffer_type;
00039 }
00040 
00041 ////////////////////////////////////////////////////////////////////
00042 //     Function: OsMesaGraphicsBuffer::Destructor
00043 //       Access: Public, Virtual
00044 //  Description:
00045 ////////////////////////////////////////////////////////////////////
00046 OsMesaGraphicsBuffer::
00047 ~OsMesaGraphicsBuffer() {
00048 }
00049 
00050 ////////////////////////////////////////////////////////////////////
00051 //     Function: OsMesaGraphicsBuffer::begin_frame
00052 //       Access: Public, Virtual
00053 //  Description: This function will be called within the draw thread
00054 //               before beginning rendering for a given frame.  It
00055 //               should do whatever setup is required, and return true
00056 //               if the frame should be rendered, or false if it
00057 //               should be skipped.
00058 ////////////////////////////////////////////////////////////////////
00059 bool OsMesaGraphicsBuffer::
00060 begin_frame(FrameMode mode, Thread *current_thread) {
00061   begin_frame_spam(mode);
00062   if (_gsg == (GraphicsStateGuardian *)NULL) {
00063     return false;
00064   }
00065 
00066   OSMesaGraphicsStateGuardian *mesagsg;
00067   DCAST_INTO_R(mesagsg, _gsg, false);
00068   OSMesaMakeCurrent(mesagsg->_context, _image.p(), _type,
00069                     _x_size, _y_size);
00070 
00071   mesagsg->reset_if_new();
00072 
00073   if (mode == FM_render) {
00074     CDLockedReader cdata(_cycler);
00075     for (size_t i = 0; i != cdata->_textures.size(); ++i) {
00076       const RenderTexture &rt = cdata->_textures[i];
00077       RenderTextureMode rtm_mode = rt._rtm_mode;
00078       if (rtm_mode == RTM_bind_or_copy) {
00079         CDWriter cdataw(_cycler, cdata, false);
00080         nassertr(cdata->_textures.size() == cdataw->_textures.size(), false);
00081         cdataw->_textures[i]._rtm_mode = RTM_copy_texture;
00082       }
00083     }
00084     clear_cube_map_selection();
00085   }
00086   _gsg->set_current_properties(&get_fb_properties());
00087   return _gsg->begin_frame(current_thread);
00088 }
00089 
00090 ////////////////////////////////////////////////////////////////////
00091 //     Function: OsMesaGraphicsBuffer::end_frame
00092 //       Access: Public, Virtual
00093 //  Description: This function will be called within the draw thread
00094 //               after rendering is completed for a given frame.  It
00095 //               should do whatever finalization is required.
00096 ////////////////////////////////////////////////////////////////////
00097 void OsMesaGraphicsBuffer::
00098 end_frame(FrameMode mode, Thread *current_thread) {
00099   end_frame_spam(mode);
00100   nassertv(_gsg != (GraphicsStateGuardian *)NULL);
00101 
00102   if (mode == FM_render) {
00103     copy_to_textures();
00104   }
00105 
00106   _gsg->end_frame(current_thread);
00107 
00108   if (mode == FM_render) {
00109     trigger_flip();
00110     clear_cube_map_selection();
00111   }
00112 }
00113 
00114 ////////////////////////////////////////////////////////////////////
00115 //     Function: OsMesaGraphicsBuffer::close_buffer
00116 //       Access: Protected, Virtual
00117 //  Description: Closes the buffer right now.  Called from the window
00118 //               thread.
00119 ////////////////////////////////////////////////////////////////////
00120 void OsMesaGraphicsBuffer::
00121 close_buffer() {
00122   _image.clear();
00123   _is_valid = false;
00124 }
00125 
00126 ////////////////////////////////////////////////////////////////////
00127 //     Function: OsMesaGraphicsBuffer::open_buffer
00128 //       Access: Protected, Virtual
00129 //  Description: Opens the buffer right now.  Called from the window
00130 //               thread.  Returns true if the buffer is successfully
00131 //               opened, or false if there was a problem.
00132 ////////////////////////////////////////////////////////////////////
00133 bool OsMesaGraphicsBuffer::
00134 open_buffer() {
00135   if (_gsg == 0) {
00136     _gsg = new OSMesaGraphicsStateGuardian(_engine, _pipe, NULL);
00137   }
00138   
00139   _image = PTA_uchar::empty_array(_x_size * _y_size * 4);
00140   _is_valid = true;
00141 
00142   // All OSMesa buffers (that we create) always have the same format.
00143   _fb_properties.clear();
00144   _fb_properties.set_rgb_color(1);
00145   _fb_properties.set_color_bits(24);
00146   _fb_properties.set_alpha_bits(8);
00147   _fb_properties.set_stencil_bits(8);
00148   _fb_properties.set_depth_bits(16);
00149   _fb_properties.set_accum_bits(8);
00150   _fb_properties.set_force_software(1);
00151 
00152   return true;
00153 }