]> git.cworth.org Git - vogl/blob - src/voglcommon/vogl_framebuffer_capturer.h
Initial vogl checkin
[vogl] / src / voglcommon / vogl_framebuffer_capturer.h
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 // File: vogl_framebuffer_capturer.h
27 #ifndef VOGL_FRAMEBUFFER_CAPTURER_H
28 #define VOGL_FRAMEBUFFER_CAPTURER_H
29
30 #include "vogl_common.h"
31 #include "vogl_dynamic_string.h"
32
33 typedef bool (*vogl_write_image_callback_func_ptr)(uint width, uint height, uint pitch, size_t size, GLenum pixel_format, GLenum pixel_type, const void *pImage, void *pOpaque, uint64_t frame_index);
34
35 class vogl_framebuffer_capturer
36 {
37 public:
38     enum
39     {
40         cMaxBufs = 4
41     };
42
43     vogl_framebuffer_capturer();
44
45     ~vogl_framebuffer_capturer();
46
47     bool init(uint num_buffers, vogl_write_image_callback_func_ptr pWrite_func, void *pWrite_opaque, GLenum pixel_format, GLenum pixel_type);
48     void deinit(bool ok_to_make_gl_calls);
49
50     bool is_initialized() const
51     {
52         return m_initialized;
53     }
54
55     uint get_num_buffers() const
56     {
57         return m_num_pbos;
58     }
59     uint get_num_busy_buffers() const
60     {
61         return m_num_busy_pbos;
62     }
63
64     bool did_any_write_fail() const
65     {
66         return m_did_any_write_fail;
67     }
68     void clear_did_any_write_fail_flag()
69     {
70         m_did_any_write_fail = false;
71     }
72
73     bool flush();
74
75     bool capture(uint width, uint height, GLuint framebuffer = 0, GLuint read_buffer = GL_BACK, uint64_t frame_index = 0);
76
77     vogl::vector<GLuint> get_buffer_handles() const;
78
79 private:
80     bool m_initialized;
81     bool m_did_any_write_fail;
82
83     vogl_write_image_callback_func_ptr m_pWrite_func;
84     void *m_pWrite_opaque;
85
86     GLenum m_pixel_format;
87     GLenum m_pixel_type;
88
89     struct pbo
90     {
91         pbo()
92             : m_width(0), m_height(0), m_pitch(0), m_buffer(0), m_busy(false)
93         {
94         }
95
96         void clear()
97         {
98             utils::zero_object(*this);
99         }
100
101         uint64_t m_frame_index;
102         uint m_width;
103         uint m_height;
104         uint m_pitch;
105         size_t m_size;
106         GLuint m_buffer;
107         bool m_busy;
108     };
109
110     uint m_num_pbos;
111     uint m_pbo_head;
112     uint m_pbo_tail;
113     uint m_num_busy_pbos;
114     pbo m_pbos[cMaxBufs];
115
116     uint m_cur_width;
117     uint m_cur_height;
118
119     bool flush_pbo(pbo &buf);
120     void delete_all_bufs();
121     bool recreate_buffers(uint new_width, uint new_height);
122 };
123
124 #endif // VOGL_FRAMEBUFFER_CAPTURER_H