File size: 2,815 Bytes
ea55f45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*

* Copyright 2017-2024 NVIDIA Corporation.  All rights reserved.

*

* Please refer to the NVIDIA end user license agreement (EULA) associated

* with this source code for terms and conditions that govern your use of

* this software. Any use, reproduction, disclosure, or distribution of

* this software and related documentation outside the terms of the EULA

* is strictly prohibited.

*

*/
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include <iostream>
#include <mutex>

#include <cuda.h>
#include <cudaGL.h>

#include "../Utils/NvCodecUtils.h"
#include "FramePresenter.h"

#include <thread>
#include <queue>

#pragma once

// Controls the number of OpenGL and CUDA resources to be created.
// Increasing this will increase the GPU memory utilization.
#define BUFFER_COUNT 2

// Singleton class to manage graphics resources
class FramePresenterGLX : public FramePresenter {

    CUgraphicsResource cuResource[BUFFER_COUNT];
    GLuint pbo[BUFFER_COUNT];                 /*!< Buffer object to upload texture data */
    GLuint tex[BUFFER_COUNT];                 /*!< OpenGL texture handle */
    GLuint program;

    // GLX resources
    Display *display;
    Window win;
    GLXContext ctx;
    GLXContext shared_ctx;
    Colormap cmap;

    CUcontext cuContext;

    int currentFrame;
    float totalWaitTime;

    NvThread renderingThread;

    public:

    FramePresenterGLX(int w, int h);

    ~FramePresenterGLX();

    void init(int w, int h, CUcontext context);

    // Following are pure virtual functions in FramePresenter class
    void initWindowSystem();
    void initOpenGLResources();
    void releaseWindowSystem();

    bool isVendorNvidia();
    void Render();

    bool GetDeviceFrameBuffer(CUdeviceptr*, int *);
    void ReleaseDeviceFrameBuffer();

    ConcurrentQueue<int> frameFeeder;

    Display*& getDisplay() {
        return this->display;
    }

    Window& getWindow() {
        return this->win;
    }

    GLXContext& getContext() {
        return this->ctx;
    }

    GLXContext& getSharedContext() {
        return this->shared_ctx;
    }

    Colormap& getColorMap() {
        return this->cmap;
    }

    GLuint* getPBO() {
        return this->pbo;
    }

    GLuint* getTextures() {
        return this->tex;
    }

    GLuint& getProgramObject() {
        return this->program;
    }

    void setDimensions(unsigned int w, unsigned int h) {

        this->width = w;
        this->height = h;
    }

    int getWidth() {
        return this->width;
    }

    int getHeight() {
        return this->height;
    }

    void mapBufferObject(CUdeviceptr*);
    void unmapBufferObject();
};