Backslash ${prefix} for kde3 too...
[gnash.git] / libvaapi / VaapiImage.cpp
blob5d98d6a75ca0ade7a3248980d8e1c9e6438f80ab
1 // VaapiImage.cpp: VA image abstraction
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 #include <boost/format.hpp>
21 #include <cstring>
23 #include "log.h"
24 #include "VaapiImage.h"
25 #include "VaapiSurface.h"
26 #include "VaapiGlobalContext.h"
27 #include "VaapiException.h"
28 #include "vaapi_utils.h"
30 namespace gnash {
32 VaapiImage::VaapiImage(unsigned int width,
33 unsigned int height,
34 VaapiImageFormat format)
35 : _format(format)
36 , _image_data(NULL)
38 log_debug("VaapiImage::VaapiImage(): format '%s'\n", string_of_FOURCC(format));
40 memset(&_image, 0, sizeof(_image));
41 _image.image_id = VA_INVALID_ID;
43 if (!create(width, height)) {
44 boost::format msg;
45 msg = boost::format("Could not create %s image")
46 % string_of_FOURCC(_format);
47 throw VaapiException(msg.str());
51 VaapiImage::~VaapiImage()
54 GNASH_REPORT_FUNCTION;
56 destroy();
59 // Create VA image
60 bool VaapiImage::create(unsigned int width, unsigned int height)
62 GNASH_REPORT_FUNCTION;
64 VaapiGlobalContext * const gvactx = VaapiGlobalContext::get();
65 if (!gvactx)
66 return false;
68 const VAImageFormat *va_format = gvactx->getImageFormat(_format);
69 if (!va_format) {
70 return false;
73 VAStatus status;
74 _image.image_id = VA_INVALID_ID;
75 status = vaCreateImage(gvactx->display(),
76 const_cast<VAImageFormat *>(va_format),
77 width, height,
78 &_image);
79 if (!vaapi_check_status(status, "vaCreateImage()"))
80 return false;
82 log_debug(" image 0x%08x, format '%s'\n", get(), string_of_FOURCC(_format));
84 return true;
87 // Destroy VA image
88 void VaapiImage::destroy()
90 unmap();
92 if (_image.image_id == VA_INVALID_ID) {
93 return;
96 VaapiGlobalContext * const gvactx = VaapiGlobalContext::get();
97 if (!gvactx) {
98 return;
101 VAStatus status;
102 status = vaDestroyImage(gvactx->display(), _image.image_id);
103 if (!vaapi_check_status(status, "vaDestroyImage()")) {
104 return;
108 // Map image data
109 bool VaapiImage::map()
111 if (isMapped()) {
112 return true;
115 if (_image.image_id == VA_INVALID_ID) {
116 return false;
119 VaapiGlobalContext * const gvactx = VaapiGlobalContext::get();
120 if (!gvactx) {
121 return false;
124 VAStatus status;
125 status = vaMapBuffer(gvactx->display(), _image.buf, (void **)&_image_data);
126 if (!vaapi_check_status(status, "vaMapBuffer()")) {
127 return false;
130 return true;
133 // Unmap image data
134 bool VaapiImage::unmap()
136 if (!isMapped()) {
137 return true;
140 _image_data = NULL;
142 VaapiGlobalContext * const gvactx = VaapiGlobalContext::get();
143 if (!gvactx) {
144 return false;
147 VAStatus status;
148 status = vaUnmapBuffer(gvactx->display(), _image.buf);
149 if (!vaapi_check_status(status, "vaUnmapBuffer()")) {
150 return false;
152 return true;
155 // Get pixels for the specified plane
156 boost::uint8_t *VaapiImage::getPlane(int plane) const
158 if (!isMapped()) {
159 throw VaapiException("VaapiImage::getPixels(): unmapped image");
162 return _image_data + _image.offsets[plane];
165 // Get scanline pitch for the specified plane
166 unsigned int VaapiImage::getPitch(int plane) const
168 if (!isMapped()) {
169 throw VaapiException("VaapiImage::getPitch(): unmapped image");
172 return _image.pitches[plane];
175 } // end of gnash namespace
177 // local Variables:
178 // mode: C++
179 // indent-tabs-mode: nil
180 // End: