/* osgCompute - Copyright (C) 2008-2009 SVT Group
 *                                                                     
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *                                                                     
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesse General Public License for more details.
 *
 * The full license is in LICENSE file included with this distribution.
*/

#ifndef OSGCUDA_CONTEXT
#define OSGCUDA_CONTEXT 1

#include <osg/GL>
#include <driver_types.h>
#include <OpenThreads/Thread>
#include <osgCompute/Context>
#include "osgCuda/Export"

namespace osgCuda
{
    class ReleaseOperation;
    struct ContextMemory;

    /**
    */
    class LIBRARY_EXPORT Context : public osgCompute::Context
    {
    public:
        Context();

        META_Object( osgCuda, Context );

        virtual bool init();
        virtual void apply();

        inline const cudaDeviceProp* getDeviceProperties() const;
        inline int getDevice() const;

        void* mallocHostMemory( unsigned int byteSize ) const;
        void* mallocDeviceMemory( unsigned int byteSize ) const;
        void* mallocDeviceHostMemory( unsigned int byteSize ) const;
        void* mallocDevice2DMemory( unsigned int widthPitch, unsigned int height ) const;
        void* mallocDevice3DMemory( unsigned int widthPitch, unsigned int height, unsigned int depth ) const;
        cudaArray* mallocDeviceArray( unsigned int width, const cudaChannelFormatDesc& desc ) const;
        cudaArray* mallocDevice2DArray( unsigned int width, unsigned int height, const cudaChannelFormatDesc& desc ) const;
        cudaArray* mallocDevice3DArray( unsigned int width, unsigned int height, unsigned int depth, const cudaChannelFormatDesc& desc ) const;

        GLuint mallocBufferObject( unsigned int byteSize ) const;
        bool registerBufferObject( GLuint bo, unsigned int byteSize ) const;
        
        void freeMemory( void* buffer ) const;
        void freeMemory( cudaArray* array ) const;
        void freeBufferObject( GLuint bo ) const;

        inline const OpenThreads::Thread* getAssignedThread() const;

        virtual void clear();

    protected:
        friend class ReleaseOperation;
        virtual ~Context() { clearLocal(); }
        void clearLocal();

        void clearMemory();

        ReleaseOperation*            _releaseOp;
        mutable ContextMemory*       _ctxmem;
        cudaDeviceProp               _deviceProperties;
        int                          _device;
        OpenThreads::Thread*         _asgThread;

    private:
        // copy constructor and operator should not be called
        Context( const Context&,const osg::CopyOp&) {}
        Context &operator=(const Context&) { return *this; }
    };

    //------------------------------------------------------------------------------
    inline const cudaDeviceProp* Context::getDeviceProperties() const 
    { 
        return &_deviceProperties; 
    }

    //------------------------------------------------------------------------------
    inline int Context::getDevice() const 
    { 
        return _device; 
    }

    //------------------------------------------------------------------------------
    inline const OpenThreads::Thread* Context::getAssignedThread() const
    {
        return _asgThread;
    }
}

#endif //OSGCUDA_CONTEXT
