]> git.sesse.net Git - casparcg/blob - core/mixer/gpu/ogl_device.cpp
2.0.0.2: Merged mixer into core/mixer.
[casparcg] / core / mixer / gpu / ogl_device.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\r
6 *    CasparCG is free software: you can redistribute it and/or modify\r
7 *    it under the terms of the GNU General Public License as published by\r
8 *    the Free Software Foundation, either version 3 of the License, or\r
9 *    (at your option) any later version.\r
10 *\r
11 *    CasparCG is distributed in the hope that it will be useful,\r
12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 *    GNU General Public License for more details.\r
15 \r
16 *    You should have received a copy of the GNU General Public License\r
17 *    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 */\r
20 #include "../../stdafx.h"\r
21 \r
22 #include "ogl_device.h"\r
23 \r
24 #include <common/utility/assert.h>\r
25 \r
26 #include <Glee.h>\r
27 #include <SFML/Window.hpp>\r
28 \r
29 #include <boost/foreach.hpp>\r
30 \r
31 namespace caspar { namespace mixer {\r
32 \r
33 ogl_device::ogl_device() : executor_(L"ogl_device")\r
34 {\r
35         executor_.start();\r
36         invoke([=]\r
37         {\r
38                 context_.reset(new sf::Context());\r
39                 context_->SetActive(true);\r
40         });\r
41 }\r
42 \r
43 ogl_device::~ogl_device()\r
44 {\r
45         invoke([=]\r
46         {\r
47                 BOOST_FOREACH(auto& pool, device_pools_)\r
48                         pool.clear();\r
49                 BOOST_FOREACH(auto& pool, host_pools_)\r
50                         pool.clear();\r
51         });\r
52 }\r
53                                 \r
54 safe_ptr<device_buffer> ogl_device::create_device_buffer(size_t width, size_t height, size_t stride)\r
55 {\r
56         CASPAR_VERIFY(stride > 0 && stride < 5);\r
57         CASPAR_VERIFY(width > 0 && height > 0);\r
58         auto pool = &device_pools_[stride-1][((width << 16) & 0xFFFF0000) | (height & 0x0000FFFF)];\r
59         std::shared_ptr<device_buffer> buffer;\r
60         if(!pool->try_pop(buffer))              \r
61         {\r
62                 executor_.invoke([&]\r
63                 {\r
64                         buffer = std::make_shared<device_buffer>(width, height, stride);\r
65                 });     \r
66         }\r
67                 \r
68         return safe_ptr<device_buffer>(buffer.get(), [=](device_buffer*){pool->push(buffer);});\r
69 }\r
70         \r
71 safe_ptr<host_buffer> ogl_device::create_host_buffer(size_t size, host_buffer::usage_t usage)\r
72 {\r
73         CASPAR_VERIFY(usage == host_buffer::write_only || usage == host_buffer::read_only);\r
74         CASPAR_VERIFY(size > 0);\r
75         auto pool = &host_pools_[usage][size];\r
76         std::shared_ptr<host_buffer> buffer;\r
77         if(!pool->try_pop(buffer))\r
78         {\r
79                 executor_.invoke([&]\r
80                 {\r
81                         buffer = std::make_shared<host_buffer>(size, usage);\r
82                         if(usage == host_buffer::write_only)\r
83                                 buffer->map();\r
84                         else\r
85                                 buffer->unmap();\r
86                 });     \r
87         }\r
88 \r
89         return safe_ptr<host_buffer>(buffer.get(), [=](host_buffer*)\r
90         {\r
91                 executor_.begin_invoke([=]\r
92                 {\r
93                         if(usage == host_buffer::write_only)\r
94                                 buffer->map();\r
95                         else\r
96                                 buffer->unmap();\r
97                         pool->push(buffer);\r
98                 });\r
99         });\r
100 }\r
101 \r
102 }}