Plug more leaks (in the test, not the core)
[gnash.git] / libbase / GnashVaapiImage.cpp
blobd23f73ae7fe077184f27579f5548edfcdbd74262
1 // GnashVaapiImage.cpp: GnashImage class used with VA API
2 //
3 // Copyright (C) 2007, 2008, 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 <time.h>
22 #include "log.h"
23 #include "GnashVaapiImage.h"
24 #include "VaapiSurface.h"
26 namespace gnash {
28 /// Get current value of microsecond timer
29 static boost::uint64_t get_ticks_usec(void)
31 #ifdef HAVE_CLOCK_GETTIME
32 struct timespec t;
33 clock_gettime(CLOCK_REALTIME, &t);
34 return (boost::uint64_t)t.tv_sec * 1000000 + t.tv_nsec / 1000;
35 #else
36 struct timeval t;
37 gettimeofday(&t, NULL);
38 return (boost::uint64_t)t.tv_sec * 1000000 + t.tv_usec;
39 #endif
42 GnashVaapiImage::GnashVaapiImage(boost::shared_ptr<VaapiSurface> surface,
43 image::ImageType type)
45 image::GnashImage(NULL, surface->width(), surface->height(), type,
46 image::GNASH_IMAGE_GPU),
47 _surface(surface),
48 _creation_time(get_ticks_usec())
50 log_debug("GnashVaapiImage::GnashVaapiImage(): surface 0x%08x, size %dx%d\n",
51 _surface->get(), _width, _height);
54 GnashVaapiImage::~GnashVaapiImage()
56 log_debug("GnashVaapiImage::~GnashVaapiImage(): surface 0x%08x\n",
57 _surface->get());
60 void GnashVaapiImage::update(boost::shared_ptr<VaapiSurface> surface)
62 _surface = surface;
63 _creation_time = get_ticks_usec();
66 void GnashVaapiImage::update(boost::uint8_t* data)
68 log_debug("GnashVaapi::update(): data %p\n", data);
70 // XXX: use vaPutImage()
71 _creation_time = get_ticks_usec();
74 void GnashVaapiImage::update(const image::GnashImage& from)
76 assert(stride() == from.stride());
77 assert(size() <= from.size());
78 assert(type() == from.type());
80 switch (from.location()) {
81 case image::GNASH_IMAGE_CPU:
82 this->update(const_cast<boost::uint8_t*>(from.begin()));
83 break;
84 case image::GNASH_IMAGE_GPU:
85 this->update(static_cast<const GnashVaapiImage&>(from).surface());
86 break;
87 default:
88 assert(0);
89 break;
93 // Transfer (and convert) VA surface to CPU image data
94 bool GnashVaapiImage::transfer()
96 // NOTE: if VAAPI is used, we have a dedicated backend, so we
97 // should not have to retrieve the VA surface underlying pixels.
98 // Mark this usage scenario as a fatal error and fix the code
99 // instead.
100 log_error("GnashVaapiImage: VA surface to SW pixels are not supported\n");
101 assert(0);
103 _data.reset();
104 return _data.get() != NULL;
107 // Get access to the underlying data
108 image::GnashImage::iterator
109 GnashVaapiImage::begin()
111 log_debug("GnashVaapiImage::data(): surface 0x%08x\n", _surface->get());
112 log_debug(" -> %u usec from creation\n",
113 (boost::uint32_t)(get_ticks_usec() - _creation_time));
115 if (!transfer()) {
116 return NULL;
119 return _data.get();
122 // Get read-only access to the underlying data
123 image::GnashImage::const_iterator
124 GnashVaapiImage::begin() const
126 log_debug("GnashVaapiImage::data() const: surface 0x%08x\n", _surface->get());
127 log_debug(" -> %u usec from creation\n",
128 (boost::uint32_t)(get_ticks_usec() - _creation_time));
130 /* XXX: awful hack... */
131 if (!const_cast<GnashVaapiImage *>(this)->transfer()) {
132 return NULL;
135 return _data.get();
138 } // gnash namespace
140 // local Variables:
141 // mode: C++
142 // indent-tabs-mode: t
143 // End: