#ifndef _SHADERYUV4210_H_
#define _SHADERYUV4210_H_

#include "ShaderBase.h"

struct Image_YUV420
{
    int width;          /* 宽度 */
    int height;         /* 高度 */
    QByteArray yData;   /* Y分量数据 */
    QByteArray uData;   /* U分量数据 */
    QByteArray vData;   /* V分量数据 */

    Image_YUV420() : width(0), height(0) {}
    Image_YUV420(Image_YUV420&& other);
    Image_YUV420(const Image_YUV420& other);
    Image_YUV420& operator=(Image_YUV420&& other);
    Image_YUV420& operator=(const Image_YUV420& other);
    bool isValid() const;
    void clear();
};


class ShaderYUV420 : public ShaderBase
{

public:
    ShaderYUV420();
    ~ShaderYUV420() override;

    /* 初始化形状 */
    GLuint initShape() override;
    /* 初始化纹理对象 */
    bool initTextures() override;
    /* 绘制图形 */
    void drawShape() override;

    /* 刷新一帧 */
    bool refreshFrameYUV420(const Image_YUV420& yuvData);
    /* 刷新一帧 */
    bool refreshFrameRGBA(const QImage& image, int textureUnit = 0);

private:
    /* RGBA转换成YUV420 */
    bool convertRGBAToYUV420(const QImage& image, Image_YUV420& yuvData);

private:
    GLuint m_VAO = 0;              // 顶点数组对象的ID
    GLuint m_VBO = 0;              // 顶点缓冲对象的ID
    GLuint m_EBO = 0;              // 索引缓冲对象的ID
    QSize m_lastSize;               /* 上一张图片的大小 */

    Image_YUV420 m_yuvData;         /* YUV420数据 */
    QSize m_lastYUVSize;          /* 上一张YUV图片的大小 */

    const QString m_textureYName = "textureY"; // Y分量纹理名称
    const QString m_textureUName = "textureU"; // U分量纹理名称
    const QString m_textureVName = "textureV"; // V分量纹理名称
};

#endif // _SHADERYUV4210_H_

