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 gstreamerc.gstreamer;
32 public  import gstreamerc.gstreamertypes;
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()
81 	{
82 		return gstMemory;
83 	}
84 
85 	/** the main Gtk struct as a void* */
86 	protected void* getStruct()
87 	{
88 		return cast(void*)gstMemory;
89 	}
90 
91 	/**
92 	 * Sets our main struct and passes it to the parent class.
93 	 */
94 	public this (GstMemory* gstMemory, bool ownedRef = false)
95 	{
96 		this.gstMemory = gstMemory;
97 		this.ownedRef = ownedRef;
98 	}
99 
100 
101 	/** */
102 	public static GType getType()
103 	{
104 		return gst_memory_get_type();
105 	}
106 
107 	/**
108 	 * Allocate a new memory block that wraps the given @data.
109 	 *
110 	 * The prefix/padding must be filled with 0 if @flags contains
111 	 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED respectively.
112 	 *
113 	 * Params:
114 	 *     flags = #GstMemoryFlags
115 	 *     data = data to
116 	 *         wrap
117 	 *     maxsize = allocated size of @data
118 	 *     offset = offset in @data
119 	 *     size = size of valid data
120 	 *     userData = user_data
121 	 *     notify = called with @user_data when the memory is freed
122 	 *
123 	 * Return: a new #GstMemory.
124 	 *
125 	 * Throws: ConstructionException GTK+ fails to create the object.
126 	 */
127 	public this(GstMemoryFlags flags, ubyte[] data, size_t maxsize, size_t offset, void* userData, GDestroyNotify notify)
128 	{
129 		auto p = gst_memory_new_wrapped(flags, data.ptr, maxsize, offset, cast(size_t)data.length, userData, notify);
130 		
131 		if(p is null)
132 		{
133 			throw new ConstructionException("null returned by new_wrapped");
134 		}
135 		
136 		this(cast(GstMemory*) p);
137 	}
138 
139 	/**
140 	 * Return a copy of @size bytes from @mem starting from @offset. This copy is
141 	 * guaranteed to be writable. @size can be set to -1 to return a copy
142 	 * from @offset to the end of the memory region.
143 	 *
144 	 * Params:
145 	 *     offset = offset to copy from
146 	 *     size = size to copy, or -1 to copy to the end of the memory region
147 	 *
148 	 * Return: a new #GstMemory.
149 	 */
150 	public Memory copy(ptrdiff_t offset, ptrdiff_t size)
151 	{
152 		auto p = gst_memory_copy(gstMemory, offset, size);
153 		
154 		if(p is null)
155 		{
156 			return null;
157 		}
158 		
159 		return ObjectG.getDObject!(Memory)(cast(GstMemory*) p, true);
160 	}
161 
162 	/**
163 	 * Get the current @size, @offset and @maxsize of @mem.
164 	 *
165 	 * Params:
166 	 *     offset = pointer to offset
167 	 *     maxsize = pointer to maxsize
168 	 *
169 	 * Return: the current sizes of @mem
170 	 */
171 	public size_t getSizes(size_t* offset, size_t* maxsize)
172 	{
173 		return gst_memory_get_sizes(gstMemory, offset, maxsize);
174 	}
175 
176 	/**
177 	 * Initializes a newly allocated @mem with the given parameters. This function
178 	 * will call gst_mini_object_init() with the default memory parameters.
179 	 *
180 	 * Params:
181 	 *     flags = #GstMemoryFlags
182 	 *     allocator = the #GstAllocator
183 	 *     parent = the parent of @mem
184 	 *     maxsize = the total size of the memory
185 	 *     alig = the alignment of the memory
186 	 *     offset = The offset in the memory
187 	 *     size = the size of valid data in the memory
188 	 */
189 	public void init(GstMemoryFlags flags, Allocator allocator, Memory parent, size_t maxsize, size_t alig, size_t offset, size_t size)
190 	{
191 		gst_memory_init(gstMemory, flags, (allocator is null) ? null : allocator.getAllocatorStruct(), (parent is null) ? null : parent.getMemoryStruct(), maxsize, alig, offset, size);
192 	}
193 
194 	/**
195 	 * Check if @mem1 and mem2 share the memory with a common parent memory object
196 	 * and that the memory is contiguous.
197 	 *
198 	 * If this is the case, the memory of @mem1 and @mem2 can be merged
199 	 * efficiently by performing gst_memory_share() on the parent object from
200 	 * the returned @offset.
201 	 *
202 	 * Params:
203 	 *     mem2 = a #GstMemory
204 	 *     offset = a pointer to a result offset
205 	 *
206 	 * Return: %TRUE if the memory is contiguous and of a common parent.
207 	 */
208 	public bool isSpan(Memory mem2, size_t* offset)
209 	{
210 		return gst_memory_is_span(gstMemory, (mem2 is null) ? null : mem2.getMemoryStruct(), offset) != 0;
211 	}
212 
213 	/**
214 	 * Check if @mem if allocated with an allocator for @mem_type.
215 	 *
216 	 * Params:
217 	 *     memType = a memory type
218 	 *
219 	 * Return: %TRUE if @mem was allocated from an allocator for @mem_type.
220 	 *
221 	 * Since: 1.2
222 	 */
223 	public bool isType(string memType)
224 	{
225 		return gst_memory_is_type(gstMemory, Str.toStringz(memType)) != 0;
226 	}
227 
228 	/**
229 	 * Create a #GstMemory object that is mapped with @flags. If @mem is mappable
230 	 * with @flags, this function returns the mapped @mem directly. Otherwise a
231 	 * mapped copy of @mem is returned.
232 	 *
233 	 * This function takes ownership of old @mem and returns a reference to a new
234 	 * #GstMemory.
235 	 *
236 	 * Params:
237 	 *     info = pointer for info
238 	 *     flags = mapping flags
239 	 *
240 	 * Return: a #GstMemory object mapped
241 	 *     with @flags or %NULL when a mapping is not possible.
242 	 */
243 	public Memory makeMapped(out GstMapInfo info, GstMapFlags flags)
244 	{
245 		auto p = gst_memory_make_mapped(gstMemory, &info, flags);
246 		
247 		if(p is null)
248 		{
249 			return null;
250 		}
251 		
252 		return ObjectG.getDObject!(Memory)(cast(GstMemory*) p, true);
253 	}
254 
255 	/**
256 	 * Fill @info with the pointer and sizes of the memory in @mem that can be
257 	 * accessed according to @flags.
258 	 *
259 	 * This function can return %FALSE for various reasons:
260 	 * - the memory backed by @mem is not accessible with the given @flags.
261 	 * - the memory was already mapped with a different mapping.
262 	 *
263 	 * @info and its contents remain valid for as long as @mem is valid and
264 	 * until gst_memory_unmap() is called.
265 	 *
266 	 * For each gst_memory_map() call, a corresponding gst_memory_unmap() call
267 	 * should be done.
268 	 *
269 	 * Params:
270 	 *     info = pointer for info
271 	 *     flags = mapping flags
272 	 *
273 	 * Return: %TRUE if the map operation was successful.
274 	 */
275 	public bool map(out GstMapInfo info, GstMapFlags flags)
276 	{
277 		return gst_memory_map(gstMemory, &info, flags) != 0;
278 	}
279 
280 	/**
281 	 * Resize the memory region. @mem should be writable and offset + size should be
282 	 * less than the maxsize of @mem.
283 	 *
284 	 * #GST_MEMORY_FLAG_ZERO_PREFIXED and #GST_MEMORY_FLAG_ZERO_PADDED will be
285 	 * cleared when offset or padding is increased respectively.
286 	 *
287 	 * Params:
288 	 *     offset = a new offset
289 	 *     size = a new size
290 	 */
291 	public void resize(ptrdiff_t offset, size_t size)
292 	{
293 		gst_memory_resize(gstMemory, offset, size);
294 	}
295 
296 	/**
297 	 * Return a shared copy of @size bytes from @mem starting from @offset. No
298 	 * memory copy is performed and the memory region is simply shared. The result
299 	 * is guaranteed to be non-writable. @size can be set to -1 to return a shared
300 	 * copy from @offset to the end of the memory region.
301 	 *
302 	 * Params:
303 	 *     offset = offset to share from
304 	 *     size = size to share, or -1 to share to the end of the memory region
305 	 *
306 	 * Return: a new #GstMemory.
307 	 */
308 	public Memory share(ptrdiff_t offset, ptrdiff_t size)
309 	{
310 		auto p = gst_memory_share(gstMemory, offset, size);
311 		
312 		if(p is null)
313 		{
314 			return null;
315 		}
316 		
317 		return ObjectG.getDObject!(Memory)(cast(GstMemory*) p, true);
318 	}
319 
320 	/**
321 	 * Release the memory obtained with gst_memory_map()
322 	 *
323 	 * Params:
324 	 *     info = a #GstMapInfo
325 	 */
326 	public void unmap(GstMapInfo* info)
327 	{
328 		gst_memory_unmap(gstMemory, info);
329 	}
330 }