"Initial commit to Gerrit"
[profile/ivi/cogl.git] / cogl / cogl-pixel-buffer.c
1 /*
2  * Cogl
3  *
4  * An object oriented GL/GLES Abstraction/Utility Layer
5  *
6  * Copyright (C) 2010 Intel Corporation.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20  *
21  *
22  *
23  * Authors:
24  *   Damien Lespiau <damien.lespiau@intel.com>
25  *   Robert Bragg <robert@linux.intel.com>
26  */
27
28 /* For an overview of the functionality implemented here, please see
29  * cogl-buffer-array.h, which contains the gtk-doc section overview for the
30  * Pixel Buffers API.
31  */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include <stdio.h>
38 #include <string.h>
39 #include <glib.h>
40
41 #include "cogl-private.h"
42 #include "cogl-util.h"
43 #include "cogl-context-private.h"
44 #include "cogl-object.h"
45 #include "cogl-pixel-buffer-private.h"
46 #include "cogl-pixel-buffer.h"
47
48 /*
49  * GL/GLES compatibility defines for the buffer API:
50  */
51
52 #if defined (HAVE_COGL_GL)
53
54 #ifndef GL_PIXEL_UNPACK_BUFFER
55 #define GL_PIXEL_UNPACK_BUFFER GL_PIXEL_UNPACK_BUFFER_ARB
56 #endif
57
58 #ifndef GL_PIXEL_PACK_BUFFER
59 #define GL_PIXEL_PACK_BUFFER GL_PIXEL_PACK_BUFFER_ARB
60 #endif
61
62 #endif
63
64 static void
65 _cogl_pixel_buffer_free (CoglPixelBuffer *buffer);
66
67 COGL_BUFFER_DEFINE (PixelBuffer, pixel_buffer)
68
69 CoglPixelBuffer *
70 cogl_pixel_buffer_new (CoglContext *context,
71                        gsize size,
72                        const void *data)
73 {
74   CoglPixelBuffer *pixel_buffer = g_slice_new0 (CoglPixelBuffer);
75   CoglBuffer *buffer = COGL_BUFFER (pixel_buffer);
76   gboolean use_malloc;
77
78   if (!(context->private_feature_flags & COGL_PRIVATE_FEATURE_PBOS))
79     use_malloc = TRUE;
80   else
81     use_malloc = FALSE;
82
83   /* parent's constructor */
84   _cogl_buffer_initialize (buffer,
85                            context,
86                            size,
87                            use_malloc,
88                            COGL_BUFFER_BIND_TARGET_PIXEL_UNPACK,
89                            COGL_BUFFER_USAGE_HINT_TEXTURE,
90                            COGL_BUFFER_UPDATE_HINT_STATIC);
91
92   _cogl_pixel_buffer_object_new (pixel_buffer);
93
94   if (data)
95     cogl_buffer_set_data (COGL_BUFFER (pixel_buffer),
96                           0,
97                           data,
98                           size);
99
100   return pixel_buffer;
101 }
102
103 static void
104 _cogl_pixel_buffer_free (CoglPixelBuffer *buffer)
105 {
106   /* parent's destructor */
107   _cogl_buffer_fini (COGL_BUFFER (buffer));
108
109   g_slice_free (CoglPixelBuffer, buffer);
110 }
111