1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright 2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved. 4 */ 5 6 #ifndef CAPTURE_WIN_GL_H 7 #define CAPTURE_WIN_GL_H 8 9 #define GL_GLEXT_PROTOTYPES 1 10 #define QT_NO_OPENGL_ES_2 11 12 #include <QOpenGLWidget> 13 #include <QOpenGLFunctions> 14 #include <QFile> 15 #include <QMenu> 16 #include <QAction> 17 #include <QActionGroup> 18 #include <QScrollArea> 19 #if QT_VERSION < 0x060000 20 #include <QtGui/QOpenGLShaderProgram> 21 #else 22 #include <QOpenGLShaderProgram> 23 #endif 24 25 #include "qvidcap.h" 26 27 extern "C" { 28 #include "v4l2-tpg.h" 29 #include "v4l-stream.h" 30 } 31 32 extern const __u32 formats[]; 33 extern const __u32 fields[]; 34 extern const __u32 colorspaces[]; 35 extern const __u32 xfer_funcs[]; 36 extern const __u32 ycbcr_encs[]; 37 extern const __u32 hsv_encs[]; 38 extern const __u32 quantizations[]; 39 40 class QOpenGLPaintDevice; 41 42 enum AppMode { 43 AppModeV4L2, 44 AppModeFile, 45 AppModeSocket, 46 AppModeTPG, 47 AppModeTest 48 }; 49 50 #define FMT_MASK (1 << 0) 51 #define FIELD_MASK (1 << 1) 52 #define COLORSPACE_MASK (1 << 2) 53 #define XFER_FUNC_MASK (1 << 3) 54 #define YCBCR_HSV_ENC_MASK (1 << 4) 55 #define QUANT_MASK (1 << 5) 56 57 struct TestState { 58 unsigned fmt_idx; 59 unsigned field_idx; 60 unsigned colorspace_idx; 61 unsigned xfer_func_idx; 62 unsigned ycbcr_enc_idx; 63 unsigned hsv_enc_idx; 64 unsigned quant_idx; 65 unsigned mask; 66 }; 67 68 // This must be equal to the max number of textures that any shader uses 69 #define MAX_TEXTURES_NEEDED 3 70 71 class CaptureWin : public QOpenGLWidget, protected QOpenGLFunctions 72 { 73 Q_OBJECT 74 public: 75 explicit CaptureWin(QScrollArea *sa, QWidget *parent = 0); 76 ~CaptureWin(); 77 78 void setModeV4L2(cv4l_fd *fd); 79 void setModeSocket(int sock, int port); 80 void setModeFile(const QString &filename); 81 void setModeTPG(); 82 void setModeTest(unsigned cnt); 83 void setQueue(cv4l_queue *q); 84 bool setV4LFormat(cv4l_fmt &fmt); 85 void setPixelAspect(const v4l2_fract &pixelaspect); 86 bool updateV4LFormat(const cv4l_fmt &fmt); 87 void setOverrideWidth(__u32 w); 88 void setOverrideHeight(__u32 h); 89 void setOverrideHorPadding(__u32 p); setCount(unsigned cnt)90 void setCount(unsigned cnt) { m_cnt = cnt; } setReportTimings(bool report)91 void setReportTimings(bool report) { m_reportTimings = report; } setVerbose(bool verbose)92 void setVerbose(bool verbose) { m_verbose = verbose; } setOverridePixelFormat(__u32 fmt)93 void setOverridePixelFormat(__u32 fmt) { m_overridePixelFormat = fmt; } setOverrideField(__u32 field)94 void setOverrideField(__u32 field) { m_overrideField = field; } setOverrideColorspace(__u32 colsp)95 void setOverrideColorspace(__u32 colsp) { m_overrideColorspace = colsp; } setOverrideYCbCrEnc(__u32 ycbcr)96 void setOverrideYCbCrEnc(__u32 ycbcr) { m_overrideYCbCrEnc = ycbcr; } setOverrideHSVEnc(__u32 hsv)97 void setOverrideHSVEnc(__u32 hsv) { m_overrideHSVEnc = hsv; } setOverrideXferFunc(__u32 xfer_func)98 void setOverrideXferFunc(__u32 xfer_func) { m_overrideXferFunc = xfer_func; } setOverrideQuantization(__u32 quant)99 void setOverrideQuantization(__u32 quant) { m_overrideQuantization = quant; } setFps(double fps)100 void setFps(double fps) { m_fps = fps; } setSingleStepStart(unsigned start)101 void setSingleStepStart(unsigned start) { m_singleStep = true; m_singleStepStart = start; } setTestState(const TestState & state)102 void setTestState(const TestState &state) { m_testState = state; } 103 QSize correctAspect(const QSize &s) const; 104 void startTimer(); getTPG()105 struct tpg_data *getTPG() { return &m_tpg; } 106 107 private slots: 108 void v4l2ReadEvent(); 109 void v4l2ExceptionEvent(); 110 void sockReadEvent(); 111 void tpgUpdateFrame(); 112 113 void restoreAll(bool checked); 114 void restoreSize(bool checked = false); 115 void fmtChanged(QAction *a); 116 void fieldChanged(QAction *a); 117 void colorspaceChanged(QAction *a); 118 void xferFuncChanged(QAction *a); 119 void ycbcrEncChanged(QAction *a); 120 void hsvEncChanged(QAction *a); 121 void quantChanged(QAction *a); 122 void windowScalingChanged(QAction *a); 123 void resolutionOverrideChanged(bool); 124 void toggleFullScreen(bool b = false); 125 126 private: 127 void resizeEvent(QResizeEvent *event); 128 void paintGL(); 129 void initializeGL(); 130 void resizeGL(int w, int h); 131 void contextMenuEvent(QContextMenuEvent *event); 132 void keyPressEvent(QKeyEvent *event); 133 void mouseDoubleClickEvent(QMouseEvent * e); 134 void listenForNewConnection(); 135 int read_u32(__u32 &v); 136 void showCurrentOverrides(); 137 void cycleMenu(__u32 &overrideVal, __u32 origVal, 138 const __u32 values[], bool hasShift, bool hasCtrl); 139 140 bool supportedFmt(__u32 fmt); 141 void checkError(const char *msg); 142 void configureTexture(size_t idx); 143 void initImageFormat(); 144 void updateOrigValues(); 145 void updateShader(); 146 void changeShader(); 147 148 // Colorspace conversion shaders 149 void shader_YUV(); 150 void shader_NV12(); 151 void shader_NV16(); 152 void shader_NV24(); 153 void shader_RGB(); 154 void shader_Bayer(); 155 void shader_YUV_packed(); 156 void shader_YUY2(); 157 158 // Colorspace conversion render 159 void render_RGB(__u32 format); 160 void render_Bayer(__u32 format); 161 void render_YUY2(__u32 format); 162 void render_YUV(__u32 format); 163 void render_YUV_packed(__u32 format); 164 void render_NV12(__u32 format); 165 void render_NV16(__u32 format); 166 void render_NV24(__u32 format); 167 168 enum AppMode m_mode; 169 cv4l_fd *m_fd; 170 int m_sock; 171 int m_port; 172 QFile m_file; 173 bool m_v4l2; 174 cv4l_fmt m_v4l_fmt; 175 v4l2_fract m_pixelaspect; 176 cv4l_queue *m_v4l_queue; 177 unsigned m_cnt; 178 unsigned m_frame; 179 unsigned m_test; 180 TestState m_testState; 181 unsigned m_imageSize; 182 bool m_verbose; 183 bool m_reportTimings; 184 bool m_is_sdtv; 185 v4l2_std_id m_std; 186 bool m_is_rgb; 187 bool m_is_hsv; 188 bool m_is_bayer; 189 bool m_uses_gl_red; 190 bool m_accepts_srgb; 191 bool m_haveSwapBytes; 192 bool m_updateShader; 193 QSize m_viewSize; 194 bool m_canOverrideResolution; 195 codec_ctx *m_ctx; 196 197 __u32 m_overridePixelFormat; 198 __u32 m_overrideWidth; 199 __u32 m_overrideHeight; 200 __u32 m_overrideHorPadding; 201 __u32 m_overrideField; 202 __u32 m_overrideColorspace; 203 __u32 m_overrideYCbCrEnc; 204 __u32 m_overrideHSVEnc; 205 __u32 m_overrideXferFunc; 206 __u32 m_overrideQuantization; 207 __u32 m_origPixelFormat; 208 __u32 m_origWidth; 209 __u32 m_origHeight; 210 __u32 m_origField; 211 __u32 m_origColorspace; 212 __u32 m_origYCbCrEnc; 213 __u32 m_origHSVEnc; 214 __u32 m_origXferFunc; 215 __u32 m_origQuantization; 216 double m_fps; 217 bool m_singleStep; 218 unsigned m_singleStepStart; 219 bool m_singleStepNext; 220 221 QTimer *m_timer; 222 int m_screenTextureCount; 223 GLuint m_screenTexture[MAX_TEXTURES_NEEDED]; 224 QOpenGLShaderProgram *m_program; 225 __u8 *m_curData[MAX_TEXTURES_NEEDED]; 226 unsigned m_curSize[MAX_TEXTURES_NEEDED]; 227 __u8 *m_nextData[MAX_TEXTURES_NEEDED]; 228 unsigned m_nextSize[MAX_TEXTURES_NEEDED]; 229 int m_curIndex; 230 int m_nextIndex; 231 struct tpg_data m_tpg; 232 233 QScrollArea *m_scrollArea; 234 QAction *m_resolutionOverride; 235 QAction *m_exitFullScreen; 236 QAction *m_enterFullScreen; 237 QMenu *m_fmtMenu; 238 QMenu *m_fieldMenu; 239 QMenu *m_colorspaceMenu; 240 QMenu *m_xferFuncMenu; 241 QMenu *m_ycbcrEncMenu; 242 QMenu *m_hsvEncMenu; 243 QMenu *m_quantMenu; 244 QMenu *m_displayMenu; 245 }; 246 247 #endif 248