/* -*-c++-*- */
/* osgEarth - Geospatial SDK for OpenSceneGraph
* Copyright 2020 Pelican Mapping
* http://osgearth.org
*
* osgEarth 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 2 of the License, or
* (at your option) any later version.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>
*/
#ifndef OSGEARTH_TILE_RASTERIZER_H
#define OSGEARTH_TILE_RASTERIZER_H 1

#include <osgEarth/Common>
#include <osgEarth/GeoData>
#include <osgEarth/Threading>
#include <osgEarth/TileKey>
#include <osg/Camera>
#include <osg/BufferObject>
#include <osg/Texture2D>
#include <osgViewer/Viewer>
#include <queue>

namespace osgEarth
{
    using namespace Threading;

    /**
    * Render node graphs to textures, one at a time.
    */
    class OSGEARTH_EXPORT TileRasterizer : public osg::Node
    {
    public:
        //! Construct a new tile rasterizer camera
        TileRasterizer(unsigned width, unsigned height);

        /**
        * Schedule a rasterization to an osg::Image.
        * @param node Node to render to the image
        * @param size of the target image (both dimensions)
        * @param extent geospatial extent of the node to render.
        * @return Future image - blocks on .get() or .release()
        */
        Future<osg::ref_ptr<osg::Image>> render(
            osg::Node* node, 
            const GeoExtent& extent);

        //! destructor
        virtual ~TileRasterizer();

    public: // osg::Node

        void traverse(osg::NodeVisitor& nv) override;

        void releaseGLObjects(osg::State*) const override;

        void resizeGLObjectBuffers(unsigned) override;

    private:


        struct RenderJob
        {
            osg::ref_ptr<osg::Node> _node;
            GeoExtent _extent;
            Promise<osg::ref_ptr<osg::Image>> _promise;
        };
        Mutexed<std::queue<RenderJob*>> _queue;

        struct RenderContext
        {
            RenderContext() : _samplesQuery(0), _pbo(0) { }
            GLuint _samplesQuery;
            GLuint _pbo;
            unsigned _width, _height;
            osg::ref_ptr<osg::Texture2D> _tex;
            osg::ref_ptr<osg::Camera> _rtt;
            GLuint _samples;
            std::atomic_bool _rttActive;
            RenderJob* _activeJob;
        };
        mutable std::shared_ptr<RenderContext> _cx;

        // simple adapter to make draw callbacks smoother
        struct DrawCallback : public osg::Camera::DrawCallback
        {
            DrawCallback(std::function<void(osg::RenderInfo&)> func) : _func(func) { }
            std::function<void(osg::RenderInfo&)> _func;
            void operator()(osg::RenderInfo& ri) const override { _func(ri); }
            friend class TileRasterizer;
        };

        void preDraw(osg::RenderInfo& ri);
        void postDraw(osg::RenderInfo& ri);
    };

} // namespace osgEarth

#endif // OSGEARTH_TILE_RASTERIZER_H
