summaryrefslogtreecommitdiff
path: root/cogl/cogl-texture.h
blob: b07da9bb6b058717b09d4d4ec486ea2be7a23424 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/*
 * Cogl
 *
 * A Low-Level GPU Graphics and Utilities API
 *
 * Copyright (C) 2007,2008,2009,2010,2011,2012,2013 Intel Corporation.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 *
 */

#if !defined(__COGL_H_INSIDE__) && !defined(COGL_COMPILATION)
#error "Only <cogl/cogl.h> can be included directly."
#endif

#ifndef __COGL_TEXTURE_H__
#define __COGL_TEXTURE_H__

/* We forward declare the CoglTexture type here to avoid some circular
 * dependency issues with the following headers.
 */
#ifdef __COGL_H_INSIDE__
/* For the public C api we typedef interface types as void to avoid needing
 * lots of casting in code and instead we will rely on runtime type checking
 * for these objects. */
typedef void CoglTexture;
#else
typedef struct _CoglTexture CoglTexture;
#define COGL_TEXTURE(X) ((CoglTexture *)X)
#endif

#include <cogl/cogl-types.h>
#include <cogl/cogl-defines.h>
#include <cogl/cogl-pixel-buffer.h>
#include <cogl/cogl-bitmap.h>
#include <cogl/cogl-framebuffer.h>

COGL_BEGIN_DECLS

/**
 * SECTION:cogl-texture
 * @short_description: Common interface for manipulating textures
 *
 * Cogl provides several different types of textures such as
 * #CoglTexture2D, #CoglTexture3D, #CoglTextureRectangle,
 * #CoglTexture2DSliced, #CoglAtlasTexture, #CoglSubTexture and
 * #CoglTexturePixmapX11 that each have specific apis for creating
 * and manipulating them, but there are a number of common operations
 * that can be applied to any of these texture types which are handled
 * via this #CoglTexture interface.
 */

#define COGL_TEXTURE_MAX_WASTE  127

/**
 * COGL_TEXTURE_ERROR:
 *
 * #CoglError domain for texture errors.
 *
 * Since: 1.8
 * Stability: Unstable
 */
#define COGL_TEXTURE_ERROR (cogl_texture_error_domain ())


/**
 * CoglTextureError:
 * @COGL_TEXTURE_ERROR_SIZE: Unsupported size
 * @COGL_TEXTURE_ERROR_FORMAT: Unsupported format
 * @COGL_TEXTURE_ERROR_TYPE: A primitive texture type that is
 *   unsupported by the driver was used
 *
 * Error codes that can be thrown when allocating textures.
 *
 * Since: 1.8
 * Stability: Unstable
 */
typedef enum {
  COGL_TEXTURE_ERROR_SIZE,
  COGL_TEXTURE_ERROR_FORMAT,
  COGL_TEXTURE_ERROR_BAD_PARAMETER,
  COGL_TEXTURE_ERROR_TYPE
} CoglTextureError;

/**
 * CoglTextureType:
 * @COGL_TEXTURE_TYPE_2D: A #CoglTexture2D
 * @COGL_TEXTURE_TYPE_3D: A #CoglTexture3D
 * @COGL_TEXTURE_TYPE_RECTANGLE: A #CoglTextureRectangle
 *
 * Constants representing the underlying hardware texture type of a
 * #CoglTexture.
 *
 * Stability: unstable
 * Since: 1.10
 */
typedef enum {
  COGL_TEXTURE_TYPE_2D,
  COGL_TEXTURE_TYPE_3D,
  COGL_TEXTURE_TYPE_RECTANGLE
} CoglTextureType;

uint32_t cogl_texture_error_domain (void);

/**
 * cogl_is_texture:
 * @object: A #CoglObject pointer
 *
 * Gets whether the given object references a texture object.
 *
 * Return value: %TRUE if the @object references a texture, and
 *   %FALSE otherwise
 */
CoglBool
cogl_is_texture (void *object);

/**
 * CoglTextureComponents:
 * @COGL_TEXTURE_COMPONENTS_A: Only the alpha component
 * @COGL_TEXTURE_COMPONENTS_RG: Red and green components. Note that
 *   this can only be used if the %COGL_FEATURE_ID_TEXTURE_RG feature
 *   is advertised.
 * @COGL_TEXTURE_COMPONENTS_RGB: Red, green and blue components
 * @COGL_TEXTURE_COMPONENTS_RGBA: Red, green, blue and alpha components
 * @COGL_TEXTURE_COMPONENTS_DEPTH: Only a depth component
 *
 * See cogl_texture_set_components().
 *
 * Since: 1.18
 */
typedef enum _CoglTextureComponents
{
  COGL_TEXTURE_COMPONENTS_A = 1,
  COGL_TEXTURE_COMPONENTS_RG,
  COGL_TEXTURE_COMPONENTS_RGB,
  COGL_TEXTURE_COMPONENTS_RGBA,
  COGL_TEXTURE_COMPONENTS_DEPTH
} CoglTextureComponents;

/**
 * cogl_texture_set_components:
 * @texture: a #CoglTexture pointer.
 *
 * Affects the internal storage format for this texture by specifying
 * what components will be required for sampling later.
 *
 * This api affects how data is uploaded to the GPU since unused
 * components can potentially be discarded from source data.
 *
 * For textures created by the ‘_with_size’ constructors the default
 * is %COGL_TEXTURE_COMPONENTS_RGBA. The other constructors which take
 * a %CoglBitmap or a data pointer default to the same components as
 * the pixel format of the data.
 *
 * Note that the %COGL_TEXTURE_COMPONENTS_RG format is not available
 * on all drivers. The availability can be determined by checking for
 * the %COGL_FEATURE_ID_TEXTURE_RG feature. If this format is used on
 * a driver where it is not available then %COGL_TEXTURE_ERROR_FORMAT
 * will be raised when the texture is allocated. Even if the feature
 * is not available then %COGL_PIXEL_FORMAT_RG_88 can still be used as
 * an image format as long as %COGL_TEXTURE_COMPONENTS_RG isn't used
 * as the texture's components.
 *
 * Since: 1.18
 */
void
cogl_texture_set_components (CoglTexture *texture,
                             CoglTextureComponents components);

/**
 * cogl_texture_get_components:
 * @texture: a #CoglTexture pointer.
 *
 * Queries what components the given @texture stores internally as set
 * via cogl_texture_set_components().
 *
 * For textures created by the ‘_with_size’ constructors the default
 * is %COGL_TEXTURE_COMPONENTS_RGBA. The other constructors which take
 * a %CoglBitmap or a data pointer default to the same components as
 * the pixel format of the data.
 *
 * Since: 1.18
 */
CoglTextureComponents
cogl_texture_get_components (CoglTexture *texture);

/**
 * cogl_texture_set_premultiplied:
 * @texture: a #CoglTexture pointer.
 * @premultiplied: Whether any internally stored red, green or blue
 *                 components are pre-multiplied by an alpha
 *                 component.
 *
 * Affects the internal storage format for this texture by specifying
 * whether red, green and blue color components should be stored as
 * pre-multiplied alpha values.
 *
 * This api affects how data is uploaded to the GPU since Cogl will
 * convert source data to have premultiplied or unpremultiplied
 * components according to this state.
 *
 * For example if you create a texture via
 * cogl_texture_2d_new_with_size() and then upload data via
 * cogl_texture_set_data() passing a source format of
 * %COGL_PIXEL_FORMAT_RGBA_8888 then Cogl will internally multiply the
 * red, green and blue components of the source data by the alpha
 * component, for each pixel so that the internally stored data has
 * pre-multiplied alpha components. If you instead upload data that
 * already has pre-multiplied components by passing
 * %COGL_PIXEL_FORMAT_RGBA_8888_PRE as the source format to
 * cogl_texture_set_data() then the data can be uploaded without being
 * converted.
 *
 * By default the @premultipled state is @TRUE.
 *
 * Since: 1.18
 */
void
cogl_texture_set_premultiplied (CoglTexture *texture,
                                CoglBool premultiplied);

/**
 * cogl_texture_get_premultiplied:
 * @texture: a #CoglTexture pointer.
 *
 * Queries the pre-multiplied alpha status for internally stored red,
 * green and blue components for the given @texture as set by
 * cogl_texture_set_premultiplied().
 *
 * By default the pre-multipled state is @TRUE.
 *
 * Return value: %TRUE if red, green and blue components are
 *               internally stored pre-multiplied by the alpha
 *               value or %FALSE if not.
 * Since: 1.18
 */
CoglBool
cogl_texture_get_premultiplied (CoglTexture *texture);

/**
 * cogl_texture_get_width:
 * @texture: a #CoglTexture pointer.
 *
 * Queries the width of a cogl texture.
 *
 * Return value: the width of the GPU side texture in pixels
 */
int
cogl_texture_get_width (CoglTexture *texture);

/**
 * cogl_texture_get_height:
 * @texture: a #CoglTexture pointer.
 *
 * Queries the height of a cogl texture.
 *
 * Return value: the height of the GPU side texture in pixels
 */
int
cogl_texture_get_height (CoglTexture *texture);

/**
 * cogl_texture_is_sliced:
 * @texture: a #CoglTexture pointer.
 *
 * Queries if a texture is sliced (stored as multiple GPU side tecture
 * objects).
 *
 * Return value: %TRUE if the texture is sliced, %FALSE if the texture
 *   is stored as a single GPU texture
 */
CoglBool
cogl_texture_is_sliced (CoglTexture *texture);

/**
 * cogl_texture_get_gl_texture:
 * @texture: a #CoglTexture pointer.
 * @out_gl_handle: (out) (allow-none): pointer to return location for the
 *   textures GL handle, or %NULL.
 * @out_gl_target: (out) (allow-none): pointer to return location for the
 *   GL target type, or %NULL.
 *
 * Queries the GL handles for a GPU side texture through its #CoglTexture.
 *
 * If the texture is spliced the data for the first sub texture will be
 * queried.
 *
 * Return value: %TRUE if the handle was successfully retrieved, %FALSE
 *   if the handle was invalid
 */
CoglBool
cogl_texture_get_gl_texture (CoglTexture *texture,
                             unsigned int *out_gl_handle,
                             unsigned int *out_gl_target);

/**
 * cogl_texture_get_data:
 * @texture: a #CoglTexture pointer.
 * @format: the #CoglPixelFormat to store the texture as.
 * @rowstride: the rowstride of @data in bytes or pass 0 to calculate
 *             from the bytes-per-pixel of @format multiplied by the
 *             @texture width.
 * @data: memory location to write the @texture's contents, or %NULL
 * to only query the data size through the return value.
 *
 * Copies the pixel data from a cogl texture to system memory.
 *
 * <note>The rowstride should be the rowstride you want for the
 * destination @data buffer you don't need to try and calculate the
 * rowstride of the source texture</note>
 *
 * Return value: the size of the texture data in bytes
 */
int
cogl_texture_get_data (CoglTexture *texture,
                       CoglPixelFormat format,
                       unsigned int rowstride,
                       uint8_t *data);

/**
 * cogl_texture_draw_and_read_to_bitmap:
 * @texture: The #CoglTexture to read
 * @framebuffer: The intermediate framebuffer needed to render the
 *               texture
 * @target_bmp: The destination bitmap
 * @error: A #CoglError to catch any exceptional errors
 *
 * Only to be used in exceptional circumstances, this api reads back
 * the contents of a @texture by rendering it to the given
 * @framebuffer and reading back the resulting pixels to be stored in
 * @bitmap. If the @texture is larger than the given @framebuffer then
 * multiple renders will be done to read the texture back in chunks.
 *
 * Any viewport, projection or modelview matrix state associated with
 * @framebuffer will be saved and restored, but other state such as
 * the color mask state is ignored and may affect the result of
 * reading back the texture.
 *
 * This API should only be used in exceptional circumstances when
 * alternative apis such as cogl_texture_get_data() have failed. For
 * example compressed textures can not be read back directly and so
 * a render is required if you want read back the image data. Ideally
 * applications should aim to avoid needing to read back textures in
 * the first place and perhaps only use this api for debugging
 * purposes.
 *
 * Stability: unstable
 */
CoglBool
cogl_texture_draw_and_read_to_bitmap (CoglTexture *texture,
                                      CoglFramebuffer *framebuffer,
                                      CoglBitmap *target_bmp,
                                      CoglError **error);

/**
 * cogl_texture_set_region:
 * @texture: a #CoglTexture.
 * @width: width of the region to set.
 * @height: height of the region to set.
 * @format: the #CoglPixelFormat used in the source @data buffer.
 * @rowstride: rowstride in bytes of the source @data buffer (computed
 *             from @width and @format if it equals 0)
 * @data: the source data, pointing to the first top-left pixel to set
 * @dst_x: upper left destination x coordinate.
 * @dst_y: upper left destination y coordinate.
 * @level: The mipmap level to update (Normally 0 for the largest,
 *         base image)
 * @error: A #CoglError to return exceptional errors
 *
 * Sets the pixels in a rectangular subregion of @texture from an
 * in-memory buffer containing pixel @data.
 *
 * @data should point to the first pixel to copy corresponding
 * to the top left of the region being set.
 *
 * The rowstride determines how many bytes between the first pixel of
 * a row of @data and the first pixel of the next row. If @rowstride
 * equals 0 then it will be automatically calculated from @width and
 * the bytes-per-pixel for the given @format.
 *
 * A mipmap @level of 0 corresponds to the largest, base image of a
 * texture and @level 1 is half the width and height of level 0. The
 * size of any level can be calculated from the size of the base
 * level as follows:
 *
 * |[
 *  width = MAX (1, floor (base_width / 2 ^ level));
 *  height = MAX (1, floor (base_height / 2 ^ level));
 * ]|
 *
 * Or more succinctly put using C:
 *
 * |[
 *  width = MAX (1, base_width >> level);
 *  height = MAX (1, base_height >> level);
 * ]|
 *
 * You can get the size of the base level using
 * cogl_texture_get_width() and cogl_texture_get_height().
 *
 * You can determine the number of mipmap levels for a given texture
 * like this:
 *
 * |[
 *  n_levels = 1 + floor (log2 (max_dimension));
 * ]|
 *
 * Or more succinctly in C using the fls() - "Find Last Set" - function:
 *
 * |[
 *  n_levels = fls (max_dimension);
 * ]|

 * Where %max_dimension is the larger of cogl_texture_get_width() and
 * cogl_texture_get_height().
 *
 * It is an error to pass a @level number >= the number of levels that
 * @texture can have according to the above calculation.
 *
 * <note>Since the storage for a #CoglTexture is allocated lazily then
 * if the given @texture has not previously been allocated then this
 * api can return %FALSE and throw an exceptional @error if there is
 * not enough memory to allocate storage for @texture.</note>
 *
 * Return value: %TRUE if the subregion upload was successful, and
 *   %FALSE otherwise
 */
CoglBool
cogl_texture_set_region (CoglTexture *texture,
                         int width,
                         int height,
                         CoglPixelFormat format,
                         int rowstride,
                         const uint8_t *data,
			 int dst_x,
			 int dst_y,
                         int level,
                         CoglError **error);

/**
 * cogl_texture_set_data:
 * @texture a #CoglTexture.
 * @format: the #CoglPixelFormat used in the source @data buffer.
 * @rowstride: rowstride of the source @data buffer (computed from
 *             the texture width and @format if it equals 0)
 * @data: the source data, pointing to the first top-left pixel to set
 * @level: The mipmap level to update (Normally 0 for the largest,
 *         base texture)
 * @error: A #CoglError to return exceptional errors
 *
 * Sets all the pixels for a given mipmap @level by copying the pixel
 * data pointed to by the @data argument into the given @texture.
 *
 * @data should point to the first pixel to copy corresponding
 * to the top left of the mipmap @level being set.
 *
 * If @rowstride equals 0 then it will be automatically calculated
 * from the width of the mipmap level and the bytes-per-pixel for the
 * given @format.
 *
 * A mipmap @level of 0 corresponds to the largest, base image of a
 * texture and @level 1 is half the width and height of level 0. If
 * dividing any dimension of the previous level by two results in a
 * fraction then round the number down (floor()), but clamp to 1
 * something like this:
 *
 * |[
 *  next_width = MAX (1, floor (prev_width));
 * ]|
 *
 * You can determine the number of mipmap levels for a given texture
 * like this:
 *
 * |[
 *  n_levels = 1 + floor (log2 (max_dimension));
 * ]|
 *
 * Where %max_dimension is the larger of cogl_texture_get_width() and
 * cogl_texture_get_height().
 *
 * It is an error to pass a @level number >= the number of levels that
 * @texture can have according to the above calculation.
 *
 * <note>Since the storage for a #CoglTexture is allocated lazily then
 * if the given @texture has not previously been allocated then this
 * api can return %FALSE and throw an exceptional @error if there is
 * not enough memory to allocate storage for @texture.</note>
 *
 * Return value: %TRUE if the data upload was successful, and
 *               %FALSE otherwise
 */
CoglBool
cogl_texture_set_data (CoglTexture *texture,
                       CoglPixelFormat format,
                       int rowstride,
                       const uint8_t *data,
                       int level,
                       CoglError **error);

/**
 * cogl_texture_set_region_from_bitmap:
 * @texture: a #CoglTexture pointer
 * @src_x: upper left x coordinate of the region in the source bitmap.
 * @src_y: upper left y coordinate of the region in the source bitmap
 * @width: width of the region to copy. (Must be less than or equal to
 *         the bitmap width)
 * @height: height of the region to copy. (Must be less than or equal
 *          to the bitmap height)
 * @bitmap: The source bitmap to copy from
 * @dst_x: upper left destination x coordinate to copy to.
 * @dst_y: upper left destination y coordinate to copy to.
 * @error: A #CoglError to return exceptional errors or %NULL
 *
 * Copies a rectangular region from @bitmap to the position
 * (@dst_x, @dst_y) of the given destination @texture.
 *
 * The source region's top left coordinate is (@src_x, @src_y) within
 * the source @bitmap and the region is @width pixels wide and @height
 * pixels high.
 *
 * <note>The source region must not extend outside the bounds of the
 * source @bitmap.</note>
 *
 * Return value: %TRUE if the subregion upload was successful, and
 *   %FALSE otherwise
 *
 * Since: 1.8
 * Stability: unstable
 */
CoglBool
cogl_texture_set_region_from_bitmap (CoglTexture *texture,
                                     int src_x,
                                     int src_y,
                                     int width,
                                     int height,
                                     CoglBitmap *bitmap,
                                     int dst_x,
                                     int dst_y,
                                     int level,
                                     CoglError **error);

/**
 * cogl_texture_allocate:
 * @texture: A #CoglTexture
 * @error: A #CoglError to return exceptional errors or %NULL
 *
 * Explicitly allocates the storage for the given @texture which
 * allows you to be sure that there is enough memory for the
 * texture and if not then the error can be handled gracefully.
 *
 * <note>Normally applications don't need to use this api directly
 * since the texture will be implicitly allocated when data is set on
 * the texture, or if the texture is attached to a #CoglOffscreen
 * framebuffer and rendered too.</note>
 *
 * Return value: %TRUE if the texture was successfully allocated,
 *               otherwise %FALSE and @error will be updated if it
 *               wasn't %NULL.
 */
CoglBool
cogl_texture_allocate (CoglTexture *texture,
                       CoglError **error);

COGL_END_DECLS

#endif /* __COGL_TEXTURE_H__ */