1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD 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 Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gstreamer.Memory;
26 
27 private import glib.ConstructionException;
28 private import glib.Str;
29 private import gobject.ObjectG;
30 private import gstreamer.Allocator;
31 private import gstreamer.c.functions;
32 public  import gstreamer.c.types;
33 
34 
35 /**
36  * GstMemory is a lightweight refcounted object that wraps a region of memory.
37  * They are typically used to manage the data of a #GstBuffer.
38  * 
39  * A GstMemory object has an allocated region of memory of maxsize. The maximum
40  * size does not change during the lifetime of the memory object. The memory
41  * also has an offset and size property that specifies the valid range of memory
42  * in the allocated region.
43  * 
44  * Memory is usually created by allocators with a gst_allocator_alloc()
45  * method call. When %NULL is used as the allocator, the default allocator will
46  * be used.
47  * 
48  * New allocators can be registered with gst_allocator_register().
49  * Allocators are identified by name and can be retrieved with
50  * gst_allocator_find(). gst_allocator_set_default() can be used to change the
51  * default allocator.
52  * 
53  * New memory can be created with gst_memory_new_wrapped() that wraps the memory
54  * allocated elsewhere.
55  * 
56  * Refcounting of the memory block is performed with gst_memory_ref() and
57  * gst_memory_unref().
58  * 
59  * The size of the memory can be retrieved and changed with
60  * gst_memory_get_sizes() and gst_memory_resize() respectively.
61  * 
62  * Getting access to the data of the memory is performed with gst_memory_map().
63  * The call will return a pointer to offset bytes into the region of memory.
64  * After the memory access is completed, gst_memory_unmap() should be called.
65  * 
66  * Memory can be copied with gst_memory_copy(), which will return a writable
67  * copy. gst_memory_share() will create a new memory block that shares the
68  * memory with an existing memory block at a custom offset and with a custom
69  * size.
70  * 
71  * Memory can be efficiently merged when gst_memory_is_span() returns %TRUE.
72  */
73 public class Memory
74 {
75 	/** the main Gtk struct */
76 	protected GstMemory* gstMemory;
77 	protected bool ownedRef;
78 
79 	/** Get the main Gtk struct */
80 	public GstMemory* getMemoryStruct(bool transferOwnership = false)
81 	{
82 		if (transferOwnership)
83 			ownedRef = false;
84 		return gstMemory;
85 	}
86 
87 	/** the main Gtk struct as a void* */
88 	protected void* getStruct()
89 	{
90 		return cast(void*)gstMemory;
91 	}
92 
93 	/**
94 	 * Sets our main struct and passes it to the parent class.
95 	 */
96 	public this (GstMemory* gstMemory, bool ownedRef = false)
97 	{
98 		this.gstMemory = gstMemory;
99 		this.ownedRef = ownedRef;
100 	}
101 
102 
103 	/** */
104 	public static GType getType()
105 	{
106 		return gst_memory_get_type();
107 	}
108 
109 	/**
110 	 * Allocate a new memory block that wraps the given @data.
111 	 *
112 	 * The prefix/padding must be filled with 0 if @flags contains
113 	 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
114 	 *
115 	 * Params:
116 	 *     flags = #GstMemoryFlags
117 	 *     data = data to
118 	 *         wrap
119 	 *     maxsize = allocated size of @data
120 	 *     offset = offset in @data
121 	 *     userData = user_data
122 	 *     notify = called with @user_data when the memory is freed
123 	 *
124 	 * Returns: a new #GstMemory.
125 	 *
126 	 * Throws: ConstructionException GTK+ fails to create the object.
127 	 */
128 	public this(GstMemoryFlags flags, ubyte[] data, size_t maxsize, size_t offset, void* userData, GDestroyNotify notify)
129 	{
130 		auto __p = gst_memory_new_wrapped(flags, data.ptr, maxsize, offset, cast(size_t)data.length, userData, notify);
131 
132 		if(__p is null)
133 		{
134 			throw new ConstructionException("null returned by new_wrapped");
135 		}
136 
137 		this(cast(GstMemory*) __p);
138 	}
139 
140 	/**
141 	 * Return a copy of @size bytes from @mem starting from @offset. This copy is
142 	 * guaranteed to be writable. @size can be set to -1 to return a copy
143 	 * from @offset to the end of the memory region.
144 	 *
145 	 * Params:
146 	 *     offset = offset to copy from
147 	 *     size = size to copy, or -1 to copy to the end of the memory region
148 	 *
149 	 * Returns: a new #GstMemory.
150 	 */
151 	public Memory copy(ptrdiff_t offset, ptrdiff_t size)
152 	{
153 		auto __p = gst_memory_copy(gstMemory, offset, size);
154 
155 		if(__p is null)
156 		{
157 			return null;
158 		}
159 
160 		return ObjectG.getDObject!(Memory)(cast(GstMemory*) __p, true);
161 	}
162 
163 	/**
164 	 * Get the current @size, @offset and @maxsize of @mem.
165 	 *
166 	 * Params:
167 	 *     offset = pointer to offset
168 	 *     maxsize = pointer to maxsize
169 	 *
170 	 * Returns: the current size of @mem
171 	 */
172 	public size_t getSizes(out size_t offset, out size_t maxsize)
173 	{
174 		return gst_memory_get_sizes(gstMemory, &offset, &maxsize);
175 	}
176 
177 	/**
178 	 * Initializes a newly allocated @mem with the given parameters. This function
179 	 * will call gst_mini_object_init() with the default memory parameters.
180 	 *
181 	 * Params:
182 	 *     flags = #GstMemoryFlags
183 	 *     allocator = the #GstAllocator
184 	 *     parent = the parent of @mem
185 	 *     maxsize = the total size of the memory
186 	 *     align_ = the alignment of the memory
187 	 *     offset = The offset in the memory
188 	 *     size = the size of valid data in the memory
189 	 */
190 	public void init(GstMemoryFlags flags, Allocator allocator, Memory parent, size_t maxsize, size_t align_, size_t offset, size_t size)
191 	{
192 		gst_memory_init(gstMemory, flags, (allocator is null) ? null : allocator.getAllocatorStruct(), (parent is null) ? null : parent.getMemoryStruct(), maxsize, align_, offset, size);
193 	}
194 
195 	/**
196 	 * Check if @mem1 and mem2 share the memory with a common parent memory object
197 	 * and that the memory is contiguous.
198 	 *
199 	 * If this is the case, the memory of @mem1 and @mem2 can be merged
200 	 * efficiently by performing gst_memory_share() on the parent object from
201 	 * the returned @offset.
202 	 *
203 	 * Params:
204 	 *     mem2 = a #GstMemory
205 	 *     offset = a pointer to a result offset
206 	 *
207 	 * Returns: %TRUE if the memory is contiguous and of a common parent.
208 	 */
209 	public bool isSpan(Memory mem2, out size_t offset)
210 	{
211 		return gst_memory_is_span(gstMemory, (mem2 is null) ? null : mem2.getMemoryStruct(), &offset) != 0;
212 	}
213 
214 	/**
215 	 * Check if @mem if allocated with an allocator for @mem_type.
216 	 *
217 	 * Params:
218 	 *     memType = a memory type
219 	 *
220 	 * Returns: %TRUE if @mem was allocated from an allocator for @mem_type.
221 	 *
222 	 * Since: 1.2
223 	 */
224 	public bool isType(string memType)
225 	{
226 		return gst_memory_is_type(gstMemory, Str.toStringz(memType)) != 0;
227 	}
228 
229 	/**
230 	 * Create a #GstMemory object that is mapped with @flags. If @mem is mappable
231 	 * with @flags, this function returns the mapped @mem directly. Otherwise a
232 	 * mapped copy of @mem is returned.
233 	 *
234 	 * This function takes ownership of old @mem and returns a reference to a new
235 	 * #GstMemory.
236 	 *
237 	 * Params:
238 	 *     info = pointer for info
239 	 *     flags = mapping flags
240 	 *
241 	 * Returns: a #GstMemory object mapped
242 	 *     with @flags or %NULL when a mapping is not possible.
243 	 */
244 	public Memory makeMapped(out GstMapInfo info, GstMapFlags flags)
245 	{
246 		auto __p = gst_memory_make_mapped(gstMemory, &info, flags);
247 
248 		if(__p is null)
249 		{
250 			return null;
251 		}
252 
253 		return ObjectG.getDObject!(Memory)(cast(GstMemory*) __p, true);
254 	}
255 
256 	/**
257 	 * Fill @info with the pointer and sizes of the memory in @mem that can be
258 	 * accessed according to @flags.
259 	 *
260 	 * This function can return %FALSE for various reasons:
261 	 * - the memory backed by @mem is not accessible with the given @flags.
262 	 * - the memory was already mapped with a different mapping.
263 	 *
264 	 * @info and its contents remain valid for as long as @mem is valid and
265 	 * until gst_memory_unmap() is called.
266 	 *
267 	 * For each gst_memory_map() call, a corresponding gst_memory_unmap() call
268 	 * should be done.
269 	 *
270 	 * Params:
271 	 *     info = pointer for info
272 	 *     flags = mapping flags
273 	 *
274 	 * Returns: %TRUE if the map operation was successful.
275 	 */
276 	public bool map(out GstMapInfo info, GstMapFlags flags)
277 	{
278 		return gst_memory_map(gstMemory, &info, flags) != 0;
279 	}
280 
281 	/**
282 	 * Resize the memory region. @mem should be writable and offset + size should be
283 	 * less than the maxsize of @mem.
284 	 *
285 	 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED will be
286 	 * cleared when offset or padding is increased respectively.
287 	 *
288 	 * Params:
289 	 *     offset = a new offset
290 	 *     size = a new size
291 	 */
292 	public void resize(ptrdiff_t offset, size_t size)
293 	{
294 		gst_memory_resize(gstMemory, offset, size);
295 	}
296 
297 	/**
298 	 * Return a shared copy of @size bytes from @mem starting from @offset. No
299 	 * memory copy is performed and the memory region is simply shared. The result
300 	 * is guaranteed to be non-writable. @size can be set to -1 to return a shared
301 	 * copy from @offset to the end of the memory region.
302 	 *
303 	 * Params:
304 	 *     offset = offset to share from
305 	 *     size = size to share, or -1 to share to the end of the memory region
306 	 *
307 	 * Returns: a new #GstMemory.
308 	 */
309 	public Memory share(ptrdiff_t offset, ptrdiff_t size)
310 	{
311 		auto __p = gst_memory_share(gstMemory, offset, size);
312 
313 		if(__p is null)
314 		{
315 			return null;
316 		}
317 
318 		return ObjectG.getDObject!(Memory)(cast(GstMemory*) __p, true);
319 	}
320 
321 	/**
322 	 * Release the memory obtained with gst_memory_map()
323 	 *
324 	 * Params:
325 	 *     info = a #GstMapInfo
326 	 */
327 	public void unmap(GstMapInfo* info)
328 	{
329 		gst_memory_unmap(gstMemory, info);
330 	}
331 }