make it possible to configure no sound handler
[gnash.git] / libvaapi / VaapiGlobalContext.cpp
blobf992b05386687f0f4f0d37171b7a2ad1892b365c
1 // VaapiGlobalContext.cpp: VA API global context
2 //
3 // Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 #include "gnashconfig.h"
22 #endif
24 #include "log.h"
25 #include "VaapiGlobalContext.h"
26 #include "VaapiDisplayX11.h"
27 #ifdef HAVE_VA_VA_GLX_H
28 #include "VaapiDisplayGLX.h"
29 #endif
30 #include "VaapiException.h"
31 #include "vaapi_utils.h"
33 namespace gnash {
35 VaapiGlobalContext::VaapiGlobalContext(std::auto_ptr<VaapiDisplay> display)
36 : _display(display)
38 GNASH_REPORT_FUNCTION;
40 if (!init())
41 throw VaapiException("could not initialize VA-API global context");
44 VaapiGlobalContext::~VaapiGlobalContext()
48 bool
49 VaapiGlobalContext::init()
51 GNASH_REPORT_FUNCTION;
53 VADisplay dpy = display();
54 VAStatus status;
56 int num_profiles = 0;
57 _profiles.resize(vaMaxNumProfiles(dpy));
58 status = vaQueryConfigProfiles(dpy, &_profiles[0], &num_profiles);
59 if (!vaapi_check_status(status, "vaQueryConfigProfiles()")) {
60 return false;
62 _profiles.resize(num_profiles);
64 int num_image_formats = 0;
65 _image_formats.resize(vaMaxNumImageFormats(dpy));
66 status = vaQueryImageFormats(dpy, &_image_formats[0], &num_image_formats);
67 if (!vaapi_check_status(status, "vaQueryImageFormats()")) {
68 return false;
70 _image_formats.resize(num_image_formats);
72 unsigned int num_subpicture_formats = 0;
73 std::vector<unsigned int> flags;
74 flags.resize(vaMaxNumSubpictureFormats(dpy));
75 _subpicture_formats.resize(vaMaxNumSubpictureFormats(dpy));
76 status = vaQuerySubpictureFormats(dpy, &_subpicture_formats[0], &flags[0], &num_subpicture_formats);
77 if (!vaapi_check_status(status, "vaQuerySubpictureFormats()")) {
78 return false;
80 _subpicture_formats.resize(num_subpicture_formats);
81 return true;
84 bool
85 VaapiGlobalContext::hasProfile(VAProfile profile) const
87 for (unsigned int i = 0; i < _profiles.size(); i++) {
88 if (_profiles[i] == profile) {
89 return true;
92 return false;
95 const VAImageFormat *
96 VaapiGlobalContext::getImageFormat(VaapiImageFormat format) const
98 for (unsigned int i = 0; i < _image_formats.size(); i++) {
99 if (vaapi_get_image_format(_image_formats[i]) == format)
100 return &_image_formats[i];
102 return NULL;
105 static std::vector<VaapiImageFormat>
106 get_formats(std::vector<VAImageFormat> const &vaFormats)
108 std::vector<VaapiImageFormat> formats;
109 for (unsigned int i = 0; i < vaFormats.size(); i++) {
110 VaapiImageFormat format = vaapi_get_image_format(vaFormats[i]);
111 if (format != VAAPI_IMAGE_NONE)
112 formats.push_back(format);
114 return formats;
117 std::vector<VaapiImageFormat>
118 VaapiGlobalContext::getImageFormats() const
120 return get_formats(_image_formats);
123 std::vector<VaapiImageFormat>
124 VaapiGlobalContext::getSubpictureFormats() const
126 return get_formats(_subpicture_formats);
129 /// A wrapper around a VaapiGlobalContext to ensure it's free'd on destruction.
130 VaapiGlobalContext *VaapiGlobalContext::get()
132 LOG_ONCE(GNASH_REPORT_FUNCTION);
134 static std::auto_ptr<VaapiGlobalContext> vaapi_global_context;
136 if (!vaapi_global_context.get()) {
137 std::auto_ptr<VaapiDisplay> dpy;
138 /* XXX: this won't work with multiple renders built-in */
139 try {
140 #if HAVE_VA_VA_GLX_H
141 dpy.reset(new VaapiDisplayGLX());
142 #else
143 dpy.reset(new VaapiDisplayX11());
144 #endif
145 if (!dpy.get()) {
146 return NULL;
148 vaapi_global_context.reset(new VaapiGlobalContext(dpy));
150 catch (...) {
151 vaapi_set_is_enabled(false);
152 return NULL;
155 return vaapi_global_context.get();
158 } // end of gnash namespace
160 // local Variables:
161 // mode: C++
162 // indent-tabs-mode: nil
163 // End: