diff options
author | Diane Trout <diane@ghic.org> | 2014-06-09 23:01:57 -0700 |
---|---|---|
committer | George Kiagiadakis <george.kiagiadakis@collabora.com> | 2014-06-14 15:11:12 +0200 |
commit | 1fb631350cab5fe30749dae93b4b32e7c86cb3ea (patch) | |
tree | 869803801147c0395e5cc8d53afcba009ca553b2 | |
parent | 2a9664aff4980380afb93720795d326dfc76785f (diff) |
Implement QGst::Allocator and a unit test for it
-rw-r--r-- | src/QGst/Allocator | 1 | ||||
-rw-r--r-- | src/QGst/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/QGst/allocator.cpp | 135 | ||||
-rw-r--r-- | src/QGst/allocator.h | 83 | ||||
-rw-r--r-- | src/QGst/global.h | 3 | ||||
-rw-r--r-- | src/QGst/memory.cpp | 5 | ||||
-rw-r--r-- | src/QGst/memory.h | 4 | ||||
-rw-r--r-- | tests/auto/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/auto/allocatortest.cpp | 87 |
9 files changed, 317 insertions, 4 deletions
diff --git a/src/QGst/Allocator b/src/QGst/Allocator new file mode 100644 index 0000000..30c4efa --- /dev/null +++ b/src/QGst/Allocator @@ -0,0 +1 @@ +#include "allocator.h" diff --git a/src/QGst/CMakeLists.txt b/src/QGst/CMakeLists.txt index 98f6877..b780fb8 100644 --- a/src/QGst/CMakeLists.txt +++ b/src/QGst/CMakeLists.txt @@ -24,6 +24,7 @@ set(QtGStreamer_SRCS colorbalance.cpp query.cpp clock.cpp + allocator.cpp memory.cpp buffer.cpp event.cpp @@ -86,6 +87,7 @@ set(QtGStreamer_INSTALLED_HEADERS clock.h Clock buffer.h Buffer sample.h Sample + allocator.h Allocator memory.h Memory event.h Event clocktime.h ClockTime diff --git a/src/QGst/allocator.cpp b/src/QGst/allocator.cpp new file mode 100644 index 0000000..05f01ae --- /dev/null +++ b/src/QGst/allocator.cpp @@ -0,0 +1,135 @@ +/* + Copyright (C) 2014 Diane Trout <diane@ghic.org> + + This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "allocator.h" +#include <gst/gstobject.h> +#include <gst/gstminiobject.h> +#include <gst/gstallocator.h> + +namespace QGst { + +struct AllocationParamsPrivate : GstAllocationParams +{ +}; + +AllocationParams::AllocationParams() + : d_ptr(new AllocationParamsPrivate) +{ + Q_D(AllocationParams); + gst_allocation_params_init(d); +} + +AllocationParams::AllocationParams(AllocationParams &other) + : d_ptr(static_cast<AllocationParamsPrivate *>(gst_allocation_params_copy(other.d_ptr))) +{ +} + +AllocationParams::~AllocationParams() +{ + gst_allocation_params_free(d_ptr); +} + +MemoryFlags AllocationParams::flags() const +{ + Q_D(const AllocationParams); + return static_cast<QGst::MemoryFlags>(static_cast<unsigned int>(d->flags)); +} + +void AllocationParams::setFlags(MemoryFlags flags) +{ + Q_D(AllocationParams); + d->flags = static_cast<GstMemoryFlags>(static_cast<unsigned int>(flags)); +} + +size_t AllocationParams::align() const +{ + Q_D(const AllocationParams); + return d->align; +} + +void AllocationParams::setAlign(size_t align) +{ + Q_D(AllocationParams); + d->align = align; +} + +size_t AllocationParams::prefix() const +{ + Q_D(const AllocationParams); + return d->prefix; +} + +void AllocationParams::setPrefix(size_t align) +{ + Q_D(AllocationParams); + d->prefix = align; +} + +size_t AllocationParams::padding() const +{ + Q_D(const AllocationParams); + return d->padding; +} + +void AllocationParams::setPadding(size_t padding) +{ + Q_D(AllocationParams); + d->padding = padding; +} + +Allocator::Allocator(GstAllocator *g_alloc) +{ + m_object = static_cast<void *>(g_alloc); +} + +Allocator::operator GstAllocator *() +{ + return static_cast<GstAllocator *>(m_object); +} + +Allocator::operator GstAllocator *() const +{ + return static_cast<GstAllocator *>(m_object); +} + + +AllocatorPtr Allocator::find(const char *name) +{ + return AllocatorPtr(new Allocator(gst_allocator_find(name))); +} + +void Allocator::registerAllocator(const char *name) +{ + gst_allocator_register(name, object<GstAllocator>()); +} + +void Allocator::setDefault() +{ + gst_allocator_set_default(object<GstAllocator>()); +} + +MemoryPtr Allocator::alloc(size_t size, AllocationParams ¶ms) +{ + return MemoryPtr::wrap(gst_allocator_alloc(object<GstAllocator>(), size, params.d_ptr)); +} + +void Allocator::free(MemoryPtr memory) +{ + gst_allocator_free(object<GstAllocator>(), memory); +} + +} /* QGst */ diff --git a/src/QGst/allocator.h b/src/QGst/allocator.h new file mode 100644 index 0000000..b87acdd --- /dev/null +++ b/src/QGst/allocator.h @@ -0,0 +1,83 @@ +/* + Copyright (C) 2014 Diane Trout <diane@ghic.org> + + This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef QGST_ALLOCATOR_H +#define QGST_ALLOCATOR_H + +#include "global.h" +#include "object.h" +#include "enums.h" +#include "memory.h" +#include <qglobal.h> + +namespace QGst { + +class AllocationParamsPrivate; +class QTGSTREAMER_EXPORT AllocationParams +{ +public: + AllocationParams(); + AllocationParams(AllocationParams &other); + ~AllocationParams(); + + MemoryFlags flags() const; + void setFlags(MemoryFlags flags); + size_t align() const; + void setAlign(size_t align); + size_t prefix() const; + void setPrefix(size_t align); + size_t padding() const; + void setPadding(size_t padding); + +protected: + AllocationParamsPrivate * const d_ptr; + +private: + Q_DECLARE_PRIVATE(AllocationParams) + friend Allocator; +}; + + +class QTGSTREAMER_EXPORT Allocator : public Object +{ + QGST_WRAPPER(Allocator) +public: + explicit Allocator(GstAllocator *g_alloc); + + operator GstAllocator *(); + operator GstAllocator *() const; + + // search for an already registered allocator. + static AllocatorPtr find(const char *name); + // this corresponds to gst_allocator_register. + void registerAllocator(const char *name); + // sets this allocator to be the default allocator + void setDefault(); + + // using this allocator create a chunk of memory + MemoryPtr alloc(size_t size, AllocationParams ¶ms); + // release memory allocated with alloc + void free(MemoryPtr memory); +// MemoryPtr newWrapped(MemoryFlags flags, void *data, size_t maxsize, size_t offset, size_t size, void *user_data, GDestroyNotify); + + friend Memory; +}; + +} /* QGst */ + +QGST_REGISTER_TYPE(QGst::Allocator) +#endif /* QGST_ALLOCATOR_H */ diff --git a/src/QGst/global.h b/src/QGst/global.h index 07f9815..26f01ce 100644 --- a/src/QGst/global.h +++ b/src/QGst/global.h @@ -120,6 +120,7 @@ QGST_WRAPPER_REFPOINTER_DECLARATION(FormatsQuery) QGST_WRAPPER_REFPOINTER_DECLARATION(BufferingQuery) QGST_WRAPPER_REFPOINTER_DECLARATION(UriQuery) QGST_WRAPPER_DECLARATION(Buffer) +QGST_WRAPPER_DECLARATION(Allocator) QGST_WRAPPER_DECLARATION(Memory) QGST_WRAPPER_DECLARATION(BufferList) QGST_WRAPPER_DECLARATION(Event) @@ -145,6 +146,8 @@ namespace QGst { class Structure; class SharedStructure; typedef QSharedPointer<SharedStructure> StructurePtr; + class AllocationParams; + typedef QSharedPointer<AllocationParams> AllocationParamsPtr; class MapInfo; class Segment; } diff --git a/src/QGst/memory.cpp b/src/QGst/memory.cpp index 8b33d4f..361b842 100644 --- a/src/QGst/memory.cpp +++ b/src/QGst/memory.cpp @@ -14,6 +14,7 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include "allocator.h" #include "memory.h" #include "buffer.h" @@ -62,13 +63,13 @@ MemoryPtr Memory::create(size_t size) return MemoryPtr::wrap(gst_allocator_alloc(NULL, size, NULL)); } -MemoryPtr Memory::create(MemoryFlags flags, void *allocator, MemoryPtr parent, +MemoryPtr Memory::create(MemoryFlags flags, AllocatorPtr allocator, MemoryPtr parent, size_t maxsize, size_t align, size_t offset, size_t size) { MemoryPtr mem; gst_memory_init(mem, static_cast<GstMemoryFlags>(static_cast<int>(flags)), - static_cast<GstAllocator *>(allocator), parent, maxsize, align, offset, size); + allocator->object<GstAllocator>(), parent, maxsize, align, offset, size); return mem; } diff --git a/src/QGst/memory.h b/src/QGst/memory.h index 7ff0e50..aa3bd1d 100644 --- a/src/QGst/memory.h +++ b/src/QGst/memory.h @@ -1,5 +1,5 @@ /* - Copyright (C) 2013 Diane Trout <diane@ghic.org> + Copyright (C) 2014 Diane Trout <diane@ghic.org> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published @@ -53,7 +53,7 @@ class QTGSTREAMER_EXPORT Memory : public MiniObject QGST_WRAPPER(Memory) public: static MemoryPtr create(size_t size); - static MemoryPtr create(MemoryFlags flags, void *allocator, MemoryPtr parent, + static MemoryPtr create(MemoryFlags flags, AllocatorPtr allocator, MemoryPtr parent, size_t maxsize, size_t align, size_t offset, size_t size); size_t getSizes(size_t &offset, size_t &maxsize); diff --git a/tests/auto/CMakeLists.txt b/tests/auto/CMakeLists.txt index 54a93ae..fd09bf8 100644 --- a/tests/auto/CMakeLists.txt +++ b/tests/auto/CMakeLists.txt @@ -30,5 +30,6 @@ qgst_test(eventtest) qgst_test(messagetest) qgst_test(taglisttest) qgst_test(discoverertest) +qgst_test(allocatortest) qgst_test(memorytest) qgst_test(padtest) diff --git a/tests/auto/allocatortest.cpp b/tests/auto/allocatortest.cpp new file mode 100644 index 0000000..e9fddf3 --- /dev/null +++ b/tests/auto/allocatortest.cpp @@ -0,0 +1,87 @@ +/* + Copyright (C) 2010 Diane Trout <diane@ghic.org> + + This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "qgsttest.h" +#include <QGlib/Error> +#include <QGst/Allocator> +#include <QGst/Memory> + +class AllocatorTest : public QGstTest +{ + Q_OBJECT +private Q_SLOTS: + + void testAllocationParams(); + void testAllocator(); +}; + +void AllocatorTest::testAllocationParams() +{ + QGst::AllocationParams *p(new QGst::AllocationParams); + + // its really pathetic but sometimes getters and + // setters break + p->setFlags(QGst::MemoryFlagReadonly | QGst::MemoryFlagNotMappable); + p->setAlign(10ul); + p->setPrefix(20ul); + p->setPadding(30ul); + + QCOMPARE(p->flags(), QGst::MemoryFlagReadonly | QGst::MemoryFlagNotMappable); + QCOMPARE(p->align(), 10ul); + QCOMPARE(p->prefix(), 20ul); + QCOMPARE(p->padding(), 30ul); + + // Does copy work? + QGst::AllocationParams c(*p); + + QCOMPARE(c.flags(), QGst::MemoryFlagReadonly | QGst::MemoryFlagNotMappable); + QCOMPARE(c.align(), 10ul); + QCOMPARE(c.prefix(), 20ul); + QCOMPARE(c.padding(), 30ul); + + // Does copy really work. (delete the source) + delete p; + + QCOMPARE(c.flags(), QGst::MemoryFlagReadonly | QGst::MemoryFlagNotMappable); + QCOMPARE(c.align(), 10ul); + QCOMPARE(c.prefix(), 20ul); + QCOMPARE(c.padding(), 30ul); +} + +void AllocatorTest::testAllocator() +{ + GstAllocator *g_system = gst_allocator_find("SystemMemory"); + QGst::AllocatorPtr system(QGst::Allocator::find("SystemMemory")); + QVERIFY(system); + QCOMPARE(g_system, static_cast<GstAllocator *>(system)); + + QGst::AllocationParams params; + params.setFlags(QGst::MemoryFlagNotMappable); + + QGst::MemoryPtr mem = system->alloc(100, params); + QVERIFY(mem); + + size_t offset; + size_t maxsize; + mem->getSizes(offset, maxsize); + QVERIFY(maxsize >= 100ul); +} + +QTEST_APPLESS_MAIN(AllocatorTest) + +#include "moc_qgsttest.cpp" +#include "allocatortest.moc" |