#pragma once
#include "Gui/2D/Surface.h"
#include "DeviceSkia.h"

#ifdef GUI_ENABLE_SKIA

namespace gui {

	/**
	 * A Skia surface.
	 */
	class SkiaSurface : public Surface2D {
	public:
		// Create.
		SkiaSurface(Size size, Float scale, SkiaContext *context);

		// Destroy.
		~SkiaSurface();

		// Present.
		virtual PresentStatus present(bool waitForVSync);

		// Paint.
		virtual void repaint(RepaintParams *params);

		// Resize the surface.
		virtual void resize(Size size, Float scale);

		// Create a Graphics object.
		virtual WindowGraphics *createGraphics(Engine &e);

		// Canvas for the surface (a mirror of surface->getCanvas()). Updated whenever needed.
		SkCanvas *canvas;

		// Skia surface.
		sk_sp<SkSurface> surface;

		// Get the device.
		GrDirectContext *device() {
			return context->skia.get();
		}

		// Ensure that the surface is active.
		void makeCurrent() { context->makeCurrent(); }

		// Grab an image.
		virtual Image *grabImage(Engine &e);

	private:
		// Context.
		SkiaContext *context;

		// Frame buffer object.
		GLuint framebuffer;

		// Color buffer we're rendering to.
		GLuint colorbuffer;

		// Stencil buffer.
		GLuint stencilbuffer;

		// Backend render target. Must be kept alive as long as the surface is alive.
		GrBackendRenderTarget target;
	};

}

#endif
