cogl/texture: Add support for sized textures with explicit format

Add API that introduce a method to allocate 2d textures given a
passed pixel format (e.g. xrgb210101010).

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/2461>
This commit is contained in:
Naveen Kumar 2022-06-06 13:19:11 +00:00 committed by Marge Bot
parent d8612720f6
commit 86b7b5bc06
7 changed files with 74 additions and 11 deletions

View File

@ -693,9 +693,10 @@ cogl_atlas_texture_new_with_size (CoglContext *ctx,
g_return_val_if_fail (width > 0 && height > 0, NULL); g_return_val_if_fail (width > 0 && height > 0, NULL);
loader = _cogl_texture_create_loader (); loader = _cogl_texture_create_loader ();
loader->src_type = COGL_TEXTURE_SOURCE_TYPE_SIZED; loader->src_type = COGL_TEXTURE_SOURCE_TYPE_SIZE;
loader->src.sized.width = width; loader->src.sized.width = width;
loader->src.sized.height = height; loader->src.sized.height = height;
loader->src.sized.format = COGL_PIXEL_FORMAT_ANY;
return _cogl_atlas_texture_create_base (ctx, width, height, return _cogl_atlas_texture_create_base (ctx, width, height,
COGL_PIXEL_FORMAT_RGBA_8888_PRE, COGL_PIXEL_FORMAT_RGBA_8888_PRE,
@ -782,7 +783,11 @@ allocate_with_size (CoglAtlasTexture *atlas_tex,
GError **error) GError **error)
{ {
CoglTexture *tex = COGL_TEXTURE (atlas_tex); CoglTexture *tex = COGL_TEXTURE (atlas_tex);
CoglPixelFormat internal_format = CoglPixelFormat internal_format;
g_warn_if_fail (loader->src.sized.format == COGL_PIXEL_FORMAT_ANY);
internal_format =
_cogl_texture_determine_internal_format (tex, COGL_PIXEL_FORMAT_ANY); _cogl_texture_determine_internal_format (tex, COGL_PIXEL_FORMAT_ANY);
if (allocate_space (atlas_tex, if (allocate_space (atlas_tex,
@ -873,7 +878,7 @@ _cogl_atlas_texture_allocate (CoglTexture *tex,
switch (loader->src_type) switch (loader->src_type)
{ {
case COGL_TEXTURE_SOURCE_TYPE_SIZED: case COGL_TEXTURE_SOURCE_TYPE_SIZE:
return allocate_with_size (atlas_tex, loader, error); return allocate_with_size (atlas_tex, loader, error);
case COGL_TEXTURE_SOURCE_TYPE_BITMAP: case COGL_TEXTURE_SOURCE_TYPE_BITMAP:
return allocate_from_bitmap (atlas_tex, loader, error); return allocate_from_bitmap (atlas_tex, loader, error);

View File

@ -872,9 +872,10 @@ cogl_texture_2d_sliced_new_with_size (CoglContext *ctx,
int max_waste) int max_waste)
{ {
CoglTextureLoader *loader = _cogl_texture_create_loader (); CoglTextureLoader *loader = _cogl_texture_create_loader ();
loader->src_type = COGL_TEXTURE_SOURCE_TYPE_SIZED; loader->src_type = COGL_TEXTURE_SOURCE_TYPE_SIZE;
loader->src.sized.width = width; loader->src.sized.width = width;
loader->src.sized.height = height; loader->src.sized.height = height;
loader->src.sized.format = COGL_PIXEL_FORMAT_ANY;
return _cogl_texture_2d_sliced_create_base (ctx, return _cogl_texture_2d_sliced_create_base (ctx,
width, width,
@ -987,7 +988,11 @@ allocate_with_size (CoglTexture2DSliced *tex_2ds,
GError **error) GError **error)
{ {
CoglTexture *tex = COGL_TEXTURE (tex_2ds); CoglTexture *tex = COGL_TEXTURE (tex_2ds);
CoglPixelFormat internal_format = CoglPixelFormat internal_format;
g_warn_if_fail (loader->src.sized.format == COGL_PIXEL_FORMAT_ANY);
internal_format =
_cogl_texture_determine_internal_format (tex, COGL_PIXEL_FORMAT_ANY); _cogl_texture_determine_internal_format (tex, COGL_PIXEL_FORMAT_ANY);
if (allocate_slices (tex_2ds, if (allocate_slices (tex_2ds,
@ -1071,7 +1076,7 @@ _cogl_texture_2d_sliced_allocate (CoglTexture *tex,
switch (loader->src_type) switch (loader->src_type)
{ {
case COGL_TEXTURE_SOURCE_TYPE_SIZED: case COGL_TEXTURE_SOURCE_TYPE_SIZE:
return allocate_with_size (tex_2ds, loader, error); return allocate_with_size (tex_2ds, loader, error);
case COGL_TEXTURE_SOURCE_TYPE_BITMAP: case COGL_TEXTURE_SOURCE_TYPE_BITMAP:
return allocate_from_bitmap (tex_2ds, loader, error); return allocate_from_bitmap (tex_2ds, loader, error);

View File

@ -110,6 +110,26 @@ _cogl_texture_2d_create_base (CoglContext *ctx,
return _cogl_texture_2d_object_new (tex_2d); return _cogl_texture_2d_object_new (tex_2d);
} }
CoglTexture2D *
cogl_texture_2d_new_with_format (CoglContext *ctx,
int width,
int height,
CoglPixelFormat format)
{
CoglTextureLoader *loader;
g_return_val_if_fail (width >= 1, NULL);
g_return_val_if_fail (height >= 1, NULL);
loader = _cogl_texture_create_loader ();
loader->src_type = COGL_TEXTURE_SOURCE_TYPE_SIZE;
loader->src.sized.width = width;
loader->src.sized.height = height;
loader->src.sized.format = format;
return _cogl_texture_2d_create_base (ctx, width, height, format, loader);
}
CoglTexture2D * CoglTexture2D *
cogl_texture_2d_new_with_size (CoglContext *ctx, cogl_texture_2d_new_with_size (CoglContext *ctx,
int width, int width,
@ -121,9 +141,10 @@ cogl_texture_2d_new_with_size (CoglContext *ctx,
g_return_val_if_fail (height >= 1, NULL); g_return_val_if_fail (height >= 1, NULL);
loader = _cogl_texture_create_loader (); loader = _cogl_texture_create_loader ();
loader->src_type = COGL_TEXTURE_SOURCE_TYPE_SIZED; loader->src_type = COGL_TEXTURE_SOURCE_TYPE_SIZE;
loader->src.sized.width = width; loader->src.sized.width = width;
loader->src.sized.height = height; loader->src.sized.height = height;
loader->src.sized.format = COGL_PIXEL_FORMAT_ANY;
return _cogl_texture_2d_create_base (ctx, width, height, return _cogl_texture_2d_create_base (ctx, width, height,
COGL_PIXEL_FORMAT_RGBA_8888_PRE, loader); COGL_PIXEL_FORMAT_RGBA_8888_PRE, loader);

View File

@ -87,6 +87,37 @@ GType cogl_texture_2d_get_gtype (void);
COGL_EXPORT gboolean COGL_EXPORT gboolean
cogl_is_texture_2d (void *object); cogl_is_texture_2d (void *object);
/**
* cogl_texture_2d_new_with_format: (skip)
* @ctx: A #CoglContext
* @width: Width of the texture to allocate
* @height: Height of the texture to allocate
* @format: format of the texture to allocate
*
* Creates a low-level #CoglTexture2D texture with a given @width and
* @height that your GPU can texture from directly.
*
* The storage for the texture is not allocated before this function
* returns. You can call cogl_texture_allocate() to explicitly
* allocate the underlying storage or preferably let Cogl
* automatically allocate storage lazily when it may know more about
* how the texture is being used and can optimize how it is allocated.
*
* The texture is still configurable until it has been allocated so
* for example you can influence the internal format of the texture
* using cogl_texture_set_components() and
* cogl_texture_set_premultiplied().
*
* Returns: (transfer full): A new #CoglTexture2D object with no storage yet allocated.
*
* Since: 2.0
*/
COGL_EXPORT CoglTexture2D *
cogl_texture_2d_new_with_format (CoglContext *ctx,
int width,
int height,
CoglPixelFormat format);
/** /**
* cogl_texture_2d_new_with_size: (skip) * cogl_texture_2d_new_with_size: (skip)
* @ctx: A #CoglContext * @ctx: A #CoglContext

View File

@ -151,7 +151,7 @@ struct _CoglTextureVtable
}; };
typedef enum _CoglTextureSoureType { typedef enum _CoglTextureSoureType {
COGL_TEXTURE_SOURCE_TYPE_SIZED = 1, COGL_TEXTURE_SOURCE_TYPE_SIZE = 1,
COGL_TEXTURE_SOURCE_TYPE_BITMAP, COGL_TEXTURE_SOURCE_TYPE_BITMAP,
COGL_TEXTURE_SOURCE_TYPE_EGL_IMAGE, COGL_TEXTURE_SOURCE_TYPE_EGL_IMAGE,
COGL_TEXTURE_SOURCE_TYPE_EGL_IMAGE_EXTERNAL COGL_TEXTURE_SOURCE_TYPE_EGL_IMAGE_EXTERNAL
@ -165,6 +165,7 @@ typedef struct _CoglTextureLoader
int width; int width;
int height; int height;
int depth; /* for 3d textures */ int depth; /* for 3d textures */
CoglPixelFormat format;
} sized; } sized;
struct { struct {
CoglBitmap *bitmap; CoglBitmap *bitmap;

View File

@ -147,7 +147,7 @@ _cogl_texture_free_loader (CoglTexture *texture)
CoglTextureLoader *loader = texture->loader; CoglTextureLoader *loader = texture->loader;
switch (loader->src_type) switch (loader->src_type)
{ {
case COGL_TEXTURE_SOURCE_TYPE_SIZED: case COGL_TEXTURE_SOURCE_TYPE_SIZE:
case COGL_TEXTURE_SOURCE_TYPE_EGL_IMAGE: case COGL_TEXTURE_SOURCE_TYPE_EGL_IMAGE:
case COGL_TEXTURE_SOURCE_TYPE_EGL_IMAGE_EXTERNAL: case COGL_TEXTURE_SOURCE_TYPE_EGL_IMAGE_EXTERNAL:
break; break;

View File

@ -137,7 +137,7 @@ allocate_with_size (CoglTexture2D *tex_2d,
GLenum gl_texture; GLenum gl_texture;
internal_format = internal_format =
_cogl_texture_determine_internal_format (tex, COGL_PIXEL_FORMAT_ANY); _cogl_texture_determine_internal_format (tex, loader->src.sized.format);
if (!_cogl_texture_2d_gl_can_create (ctx, if (!_cogl_texture_2d_gl_can_create (ctx,
width, width,
@ -428,7 +428,7 @@ _cogl_texture_2d_gl_allocate (CoglTexture *tex,
switch (loader->src_type) switch (loader->src_type)
{ {
case COGL_TEXTURE_SOURCE_TYPE_SIZED: case COGL_TEXTURE_SOURCE_TYPE_SIZE:
return allocate_with_size (tex_2d, loader, error); return allocate_with_size (tex_2d, loader, error);
case COGL_TEXTURE_SOURCE_TYPE_BITMAP: case COGL_TEXTURE_SOURCE_TYPE_BITMAP:
return allocate_from_bitmap (tex_2d, loader, error); return allocate_from_bitmap (tex_2d, loader, error);