cogl: Turn TextureDriver into an abstract object

The motivation is to ease sharing bits between the various texture
drivers implementations.
The same thing would be done for DriverVtable & WinsysVtable.

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/4163>
This commit is contained in:
Bilal Elmoussaoui 2024-11-30 19:33:19 +01:00 committed by Marge Bot
parent 9181fdf5c2
commit 4f16f6df3b
15 changed files with 504 additions and 249 deletions

View File

@ -193,6 +193,8 @@ _cogl_atlas_get_initial_size (CoglContext *ctx,
unsigned int *map_width,
unsigned int *map_height)
{
CoglTextureDriverClass *tex_driver =
COGL_TEXTURE_DRIVER_GET_CLASS (ctx->texture_driver);
unsigned int size;
GLenum gl_intformat;
GLenum gl_format;
@ -219,12 +221,13 @@ _cogl_atlas_get_initial_size (CoglContext *ctx,
/* Some platforms might not support this large size so we'll
decrease the size until it can */
while (size > 1 &&
!ctx->texture_driver->size_supported (ctx,
GL_TEXTURE_2D,
gl_intformat,
gl_format,
gl_type,
size, size))
!tex_driver->size_supported (ctx->texture_driver,
ctx,
GL_TEXTURE_2D,
gl_intformat,
gl_format,
gl_type,
size, size))
size >>= 1;
*map_width = size;
@ -239,6 +242,8 @@ _cogl_atlas_create_map (CoglContext *ctx,
unsigned int n_textures,
CoglAtlasRepositionData *textures)
{
CoglTextureDriverClass *tex_driver =
COGL_TEXTURE_DRIVER_GET_CLASS (ctx->texture_driver);
GLenum gl_intformat;
GLenum gl_format;
GLenum gl_type;
@ -251,12 +256,13 @@ _cogl_atlas_create_map (CoglContext *ctx,
/* Keep trying increasingly larger atlases until we can fit all of
the textures */
while (ctx->texture_driver->size_supported (ctx,
GL_TEXTURE_2D,
gl_intformat,
gl_format,
gl_type,
map_width, map_height))
while (tex_driver->size_supported (ctx->texture_driver,
ctx,
GL_TEXTURE_2D,
gl_intformat,
gl_format,
gl_type,
map_width, map_height))
{
CoglRectangleMap *new_atlas = _cogl_rectangle_map_new (map_width,
map_height,

View File

@ -73,7 +73,7 @@ struct _CoglContext
/* vtables for the driver functions */
const CoglDriverVtable *driver_vtable;
const CoglTextureDriver *texture_driver;
CoglTextureDriver *texture_driver;
void *driver_context;

View File

@ -462,7 +462,10 @@ gboolean
cogl_context_format_supports_upload (CoglContext *ctx,
CoglPixelFormat format)
{
return ctx->texture_driver->format_supports_upload (ctx, format);
CoglTextureDriverClass *tex_driver =
COGL_TEXTURE_DRIVER_GET_CLASS (ctx->texture_driver);
return tex_driver->format_supports_upload (ctx->texture_driver, ctx, format);
}
void

View File

@ -49,7 +49,7 @@ struct _CoglRenderer
gboolean connected;
CoglDriver driver_override;
const CoglDriverVtable *driver_vtable;
const CoglTextureDriver *texture_driver;
CoglTextureDriver *texture_driver;
const CoglWinsysVtable *winsys_vtable;
void *custom_winsys_user_data;
CoglCustomWinsysVtableGetter custom_winsys_vtable_getter;

View File

@ -53,16 +53,21 @@
#include "cogl/winsys/cogl-winsys-glx-private.h"
#endif
#ifdef HAVE_GL
#include "cogl/driver/gl/gl/cogl-texture-driver-gl3-private.h"
#endif
#ifdef HAVE_GLES2
#include "cogl/driver/gl/gles/cogl-texture-driver-gles2-private.h"
#endif
#ifdef HAVE_X11
#include "cogl/cogl-xlib-renderer.h"
#endif
#ifdef HAVE_GL
extern const CoglTextureDriver _cogl_texture_driver_gl;
extern const CoglDriverVtable _cogl_driver_gl;
#endif
#ifdef HAVE_GLES2
extern const CoglTextureDriver _cogl_texture_driver_gles;
extern const CoglDriverVtable _cogl_driver_gles;
#endif
@ -79,7 +84,6 @@ typedef struct _CoglDriverDescription
* driver. */
const CoglPrivateFeature private_features[8];
const CoglDriverVtable *vtable;
const CoglTextureDriver *texture_driver;
const char *libgl_name;
} CoglDriverDescription;
@ -92,7 +96,6 @@ static CoglDriverDescription _cogl_drivers[] =
{ COGL_PRIVATE_FEATURE_ANY_GL,
-1 },
&_cogl_driver_gl,
&_cogl_texture_driver_gl,
COGL_GL_LIBNAME,
},
#endif
@ -103,7 +106,6 @@ static CoglDriverDescription _cogl_drivers[] =
{ COGL_PRIVATE_FEATURE_ANY_GL,
-1 },
&_cogl_driver_gles,
&_cogl_texture_driver_gles,
COGL_GLES2_LIBNAME,
},
#endif
@ -112,7 +114,6 @@ static CoglDriverDescription _cogl_drivers[] =
"nop",
{ -1 },
&_cogl_driver_nop,
NULL, /* texture driver */
NULL /* libgl_name */
}
};
@ -165,6 +166,8 @@ cogl_renderer_dispose (GObject *object)
g_slist_free_full (renderer->event_filters,
(GDestroyNotify) native_filter_closure_free);
g_clear_object (&renderer->texture_driver);
G_OBJECT_CLASS (cogl_renderer_parent_class)->dispose (object);
}
@ -374,7 +377,26 @@ _cogl_renderer_choose_driver (CoglRenderer *renderer,
desc = state.driver_description;
renderer->driver = desc->id;
renderer->driver_vtable = desc->vtable;
renderer->texture_driver = desc->texture_driver;
switch (renderer->driver)
{
#ifdef HAVE_GL
case COGL_DRIVER_GL3:
renderer->texture_driver = g_object_new (COGL_TYPE_TEXTURE_DRIVER_GL3, NULL);
break;
#endif
#ifdef HAVE_GLES2
case COGL_DRIVER_GLES2:
renderer->texture_driver = g_object_new (COGL_TYPE_TEXTURE_DRIVER_GLES2, NULL);
break;
#endif
case COGL_DRIVER_NOP:
default:
renderer->texture_driver = NULL;
break;
}
libgl_name = desc->libgl_name;
memset(renderer->private_features, 0, sizeof (renderer->private_features));

View File

@ -0,0 +1,45 @@
/*
* Cogl
*
* A Low Level GPU Graphics and Utilities API
*
* Copyright (C) 2024 Red Hat.
*
* 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.
*
*/
#include "config.h"
#include "cogl/cogl-texture-driver.h"
G_DEFINE_ABSTRACT_TYPE (CoglTextureDriver, cogl_texture_driver, G_TYPE_OBJECT)
static void
cogl_texture_driver_class_init (CoglTextureDriverClass *klass)
{
}
static void
cogl_texture_driver_init (CoglTextureDriver *driver)
{
}

View File

@ -30,19 +30,33 @@
#pragma once
typedef struct _CoglTextureDriver CoglTextureDriver;
#include <glib-object.h>
struct _CoglTextureDriver
#include "cogl/cogl-gl-header.h"
#include "cogl/cogl-pixel-format.h"
#include "cogl/cogl-types.h"
G_DECLARE_DERIVABLE_TYPE (CoglTextureDriver,
cogl_texture_driver,
COGL,
TEXTURE_DRIVER,
GObject)
#define COGL_TYPE_TEXTURE_DRIVER (cogl_texture_driver_get_type ())
struct _CoglTextureDriverClass
{
GObjectClass parent_class;
/*
* A very small wrapper around glGenTextures() that ensures we default to
* non-mipmap filters when creating textures. This is to save some memory as
* the driver will not allocate room for the mipmap tree.
*/
GLuint
(* gen) (CoglContext *ctx,
GLenum gl_target,
CoglPixelFormat internal_format);
GLuint (* gen) (CoglTextureDriver *driver,
CoglContext *ctx,
GLenum gl_target,
CoglPixelFormat internal_format);
/*
* This uploads a sub-region from source_bmp to a single GL texture
@ -57,20 +71,20 @@ struct _CoglTextureDriver
*
* XXX: sorry for the ridiculous number of arguments :-(
*/
gboolean
(* upload_subregion_to_gl) (CoglContext *ctx,
CoglTexture *texture,
int src_x,
int src_y,
int dst_x,
int dst_y,
int width,
int height,
int level,
CoglBitmap *source_bmp,
GLuint source_gl_format,
GLuint source_gl_type,
GError **error);
gboolean (* upload_subregion_to_gl) (CoglTextureDriver *driver,
CoglContext *ctx,
CoglTexture *texture,
int src_x,
int src_y,
int dst_x,
int dst_y,
int width,
int height,
int level,
CoglBitmap *source_bmp,
GLuint source_gl_format,
GLuint source_gl_type,
GError **error);
/*
* Replaces the contents of the GL texture with the entire bitmap. On
@ -78,15 +92,15 @@ struct _CoglTextureDriver
* to copy the bitmap if the rowstride is not a multiple of a possible
* alignment value because there is no GL_UNPACK_ROW_LENGTH
*/
gboolean
(* upload_to_gl) (CoglContext *ctx,
GLenum gl_target,
GLuint gl_handle,
CoglBitmap *source_bmp,
GLint internal_gl_format,
GLuint source_gl_format,
GLuint source_gl_type,
GError **error);
gboolean (* upload_to_gl) (CoglTextureDriver *driver,
CoglContext *ctx,
GLenum gl_target,
GLuint gl_handle,
CoglBitmap *source_bmp,
GLint internal_gl_format,
GLuint source_gl_format,
GLuint source_gl_type,
GError **error);
/*
* This sets up the glPixelStore state for an download to a destination with
@ -95,11 +109,11 @@ struct _CoglTextureDriver
/* NB: GLES can't download pixel data into a sub region of a larger
* destination buffer, the GL driver has a more flexible version of
* this function that it uses internally. */
void
(* prep_gl_for_pixels_download) (CoglContext *ctx,
int image_width,
int pixels_rowstride,
int pixels_bpp);
void (* prep_gl_for_pixels_download) (CoglTextureDriver *driver,
CoglContext *ctx,
int image_width,
int pixels_rowstride,
int pixels_bpp);
/*
* This driver abstraction is needed because GLES doesn't support
@ -108,29 +122,29 @@ struct _CoglTextureDriver
* renders the texture and reads it back from the framebuffer. (See
* _cogl_texture_draw_and_read () )
*/
gboolean
(* gl_get_tex_image) (CoglContext *ctx,
GLenum gl_target,
GLenum dest_gl_format,
GLenum dest_gl_type,
uint8_t *dest);
gboolean (* gl_get_tex_image) (CoglTextureDriver *driver,
CoglContext *ctx,
GLenum gl_target,
GLenum dest_gl_format,
GLenum dest_gl_type,
uint8_t *dest);
/*
* It may depend on the driver as to what texture sizes are supported...
*/
gboolean
(* size_supported) (CoglContext *ctx,
GLenum gl_target,
GLenum gl_intformat,
GLenum gl_format,
GLenum gl_type,
int width,
int height);
gboolean (* size_supported) (CoglTextureDriver *driver,
CoglContext *ctx,
GLenum gl_target,
GLenum gl_intformat,
GLenum gl_format,
GLenum gl_type,
int width,
int height);
gboolean
(* format_supports_upload) (CoglContext *ctx,
CoglPixelFormat format);
gboolean (* format_supports_upload) (CoglTextureDriver *driver,
CoglContext *ctx,
CoglPixelFormat format);
/*
* The driver may impose constraints on what formats can be used to store
@ -138,9 +152,9 @@ struct _CoglTextureDriver
* RGBA_8888, and so we need to manually convert the data if the final
* destination has another format.
*/
CoglPixelFormat
(* find_best_gl_get_data_format) (CoglContext *context,
CoglPixelFormat format,
GLenum *closest_gl_format,
GLenum *closest_gl_type);
CoglPixelFormat (* find_best_gl_get_data_format) (CoglTextureDriver *driver,
CoglContext *context,
CoglPixelFormat format,
GLenum *closest_gl_format,
GLenum *closest_gl_type);
};

View File

@ -811,6 +811,7 @@ cogl_texture_get_data (CoglTexture *texture,
uint8_t *data)
{
CoglContext *ctx;
CoglTextureDriverClass *tex_driver;
int bpp;
int byte_size;
CoglPixelFormat closest_format;
@ -848,11 +849,13 @@ cogl_texture_get_data (CoglTexture *texture,
return byte_size;
ctx = cogl_texture_get_context (texture);
tex_driver = COGL_TEXTURE_DRIVER_GET_CLASS (ctx->texture_driver);
closest_format =
ctx->texture_driver->find_best_gl_get_data_format (ctx,
format,
&closest_gl_format,
&closest_gl_type);
tex_driver->find_best_gl_get_data_format (ctx->texture_driver,
ctx,
format,
&closest_gl_format,
&closest_gl_type);
/* We can assume that whatever data GL gives us will have the
premult status of the original texture */

View File

@ -463,6 +463,8 @@ cogl_gl_framebuffer_read_pixels_into_bitmap (CoglFramebufferDriver *driver,
int bpp, rowstride;
uint8_t *tmp_data;
gboolean succeeded;
CoglTextureDriverClass *tex_driver =
COGL_TEXTURE_DRIVER_GET_CLASS (ctx->texture_driver);
if (_cogl_pixel_format_can_have_premult (read_format))
{
@ -480,10 +482,11 @@ cogl_gl_framebuffer_read_pixels_into_bitmap (CoglFramebufferDriver *driver,
bpp = cogl_pixel_format_get_bytes_per_pixel (read_format, 0);
rowstride = cogl_bitmap_get_rowstride (tmp_bmp);
ctx->texture_driver->prep_gl_for_pixels_download (ctx,
rowstride,
width,
bpp);
tex_driver->prep_gl_for_pixels_download (ctx->texture_driver,
ctx,
rowstride,
width,
bpp);
/* Note: we don't worry about catching errors here since we know
* we won't be lazily allocating storage for this buffer so it
@ -522,6 +525,8 @@ cogl_gl_framebuffer_read_pixels_into_bitmap (CoglFramebufferDriver *driver,
gboolean succeeded = FALSE;
uint8_t *pixels;
GError *internal_error = NULL;
CoglTextureDriverClass *tex_driver =
COGL_TEXTURE_DRIVER_GET_CLASS (ctx->texture_driver);
rowstride = cogl_bitmap_get_rowstride (bitmap);
@ -544,10 +549,11 @@ cogl_gl_framebuffer_read_pixels_into_bitmap (CoglFramebufferDriver *driver,
bpp = cogl_pixel_format_get_bytes_per_pixel (bmp_format, 0);
ctx->texture_driver->prep_gl_for_pixels_download (ctx,
rowstride,
width,
bpp);
tex_driver->prep_gl_for_pixels_download (ctx->texture_driver,
ctx,
rowstride,
width,
bpp);
pixels = _cogl_bitmap_gl_bind (shared_bmp,
COGL_BUFFER_ACCESS_WRITE,

View File

@ -78,6 +78,8 @@ _cogl_texture_2d_gl_can_create (CoglContext *ctx,
int height,
CoglPixelFormat internal_format)
{
CoglTextureDriverClass *tex_driver =
COGL_TEXTURE_DRIVER_GET_CLASS (ctx->texture_driver);
GLenum gl_intformat;
GLenum gl_format;
GLenum gl_type;
@ -93,13 +95,14 @@ _cogl_texture_2d_gl_can_create (CoglContext *ctx,
&gl_type);
/* Check that the driver can create a texture with that size */
if (!ctx->texture_driver->size_supported (ctx,
GL_TEXTURE_2D,
gl_intformat,
gl_format,
gl_type,
width,
height))
if (!tex_driver->size_supported (ctx->texture_driver,
ctx,
GL_TEXTURE_2D,
gl_intformat,
gl_format,
gl_type,
width,
height))
return FALSE;
return TRUE;
@ -132,6 +135,8 @@ allocate_with_size (CoglTexture2D *tex_2d,
int width = loader->src.sized.width;
int height = loader->src.sized.height;
CoglContext *ctx = cogl_texture_get_context (tex);
CoglTextureDriverClass *tex_driver =
COGL_TEXTURE_DRIVER_GET_CLASS (ctx->texture_driver);
GLenum gl_intformat;
GLenum gl_format;
GLenum gl_type;
@ -158,7 +163,10 @@ allocate_with_size (CoglTexture2D *tex_2d,
&gl_format,
&gl_type);
gl_texture = ctx->texture_driver->gen (ctx, GL_TEXTURE_2D, internal_format);
gl_texture = tex_driver->gen (ctx->texture_driver,
ctx,
GL_TEXTURE_2D,
internal_format);
tex_2d->gl_internal_format = gl_intformat;
@ -195,6 +203,8 @@ allocate_from_bitmap (CoglTexture2D *tex_2d,
CoglTexture *tex = COGL_TEXTURE (tex_2d);
CoglBitmap *bmp = loader->src.bitmap.bitmap;
CoglContext *ctx = _cogl_bitmap_get_context (bmp);
CoglTextureDriverClass *tex_driver =
COGL_TEXTURE_DRIVER_GET_CLASS (ctx->texture_driver);
CoglPixelFormat internal_format;
int width = cogl_bitmap_get_width (bmp);
int height = cogl_bitmap_get_height (bmp);
@ -235,16 +245,19 @@ allocate_from_bitmap (CoglTexture2D *tex_2d,
NULL,
NULL);
tex_2d->gl_texture =
ctx->texture_driver->gen (ctx, GL_TEXTURE_2D, internal_format);
if (!ctx->texture_driver->upload_to_gl (ctx,
GL_TEXTURE_2D,
tex_2d->gl_texture,
upload_bmp,
gl_intformat,
gl_format,
gl_type,
error))
tex_2d->gl_texture = tex_driver->gen (ctx->texture_driver,
ctx,
GL_TEXTURE_2D,
internal_format);
if (!tex_driver->upload_to_gl (ctx->texture_driver,
ctx,
GL_TEXTURE_2D,
tex_2d->gl_texture,
upload_bmp,
gl_intformat,
gl_format,
gl_type,
error))
{
g_object_unref (upload_bmp);
return FALSE;
@ -270,9 +283,13 @@ allocate_from_egl_image (CoglTexture2D *tex_2d,
CoglTexture *tex = COGL_TEXTURE (tex_2d);
CoglContext *ctx = cogl_texture_get_context (tex);
CoglPixelFormat internal_format = loader->src.egl_image.format;
CoglTextureDriverClass *tex_driver =
COGL_TEXTURE_DRIVER_GET_CLASS (ctx->texture_driver);
tex_2d->gl_texture =
ctx->texture_driver->gen (ctx, GL_TEXTURE_2D, internal_format);
tex_2d->gl_texture = tex_driver->gen (ctx->texture_driver,
ctx,
GL_TEXTURE_2D,
internal_format);
if (!cogl_texture_2d_gl_bind_egl_image (tex_2d,
loader->src.egl_image.image,
@ -567,6 +584,8 @@ _cogl_texture_2d_gl_copy_from_bitmap (CoglTexture2D *tex_2d,
{
CoglTexture *tex = COGL_TEXTURE (tex_2d);
CoglContext *ctx = cogl_texture_get_context (tex);
CoglTextureDriverClass *tex_driver =
COGL_TEXTURE_DRIVER_GET_CLASS (ctx->texture_driver);
CoglBitmap *upload_bmp;
CoglPixelFormat upload_format;
GLenum gl_format;
@ -596,16 +615,17 @@ _cogl_texture_2d_gl_copy_from_bitmap (CoglTexture2D *tex_2d,
if (cogl_texture_get_max_level_set (tex) < level)
cogl_texture_gl_set_max_level (tex, level);
status = ctx->texture_driver->upload_subregion_to_gl (ctx,
tex,
src_x, src_y,
dst_x, dst_y,
width, height,
level,
upload_bmp,
gl_format,
gl_type,
error);
status = tex_driver->upload_subregion_to_gl (ctx->texture_driver,
ctx,
tex,
src_x, src_y,
dst_x, dst_y,
width, height,
level,
upload_bmp,
gl_format,
gl_type,
error);
g_object_unref (upload_bmp);
@ -625,6 +645,8 @@ _cogl_texture_2d_gl_get_data (CoglTexture2D *tex_2d,
uint8_t *data)
{
CoglContext *ctx = cogl_texture_get_context (COGL_TEXTURE (tex_2d));
CoglTextureDriverClass *tex_driver =
COGL_TEXTURE_DRIVER_GET_CLASS (ctx->texture_driver);
uint8_t bpp;
int width = cogl_texture_get_width (COGL_TEXTURE (tex_2d));
GLenum gl_format;
@ -641,17 +663,19 @@ _cogl_texture_2d_gl_get_data (CoglTexture2D *tex_2d,
&gl_format,
&gl_type);
ctx->texture_driver->prep_gl_for_pixels_download (ctx,
rowstride,
width,
bpp);
tex_driver->prep_gl_for_pixels_download (ctx->texture_driver,
ctx,
rowstride,
width,
bpp);
_cogl_bind_gl_texture_transient (ctx, tex_2d->gl_target,
tex_2d->gl_texture);
ctx->texture_driver->gl_get_tex_image (ctx,
tex_2d->gl_target,
gl_format,
gl_type,
data);
tex_driver->gl_get_tex_image (ctx->texture_driver,
ctx,
tex_2d->gl_target,
gl_format,
gl_type,
data);
}

View File

@ -0,0 +1,40 @@
/*
* Cogl
*
* A Low Level GPU Graphics and Utilities API
*
* Copyright (C) 2024 Red Hat.
*
* 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.
*/
#pragma once
#include "cogl/cogl-texture-driver.h"
G_DECLARE_FINAL_TYPE (CoglTextureDriverGL3,
cogl_texture_driver_gl3,
COGL,
TEXTURE_DRIVER_GL3,
CoglTextureDriver)
#define COGL_TYPE_TEXTURE_DRIVER_GL3 (cogl_texture_driver_gl3_get_type ())

View File

@ -42,6 +42,7 @@
#include "cogl/cogl-texture-private.h"
#include "cogl/cogl-pipeline.h"
#include "cogl/cogl-context-private.h"
#include "cogl/driver/gl/gl/cogl-texture-driver-gl3-private.h"
#include "cogl/driver/gl/cogl-pipeline-opengl-private.h"
#include "cogl/driver/gl/cogl-util-gl-private.h"
#include "cogl/driver/gl/cogl-texture-gl-private.h"
@ -55,10 +56,20 @@
#define GL_TEXTURE_SWIZZLE_RGBA 0x8E46
#endif
struct _CoglTextureDriverGL3
{
CoglTextureDriver parent_instance;
};
G_DEFINE_FINAL_TYPE (CoglTextureDriverGL3,
cogl_texture_driver_gl3,
COGL_TYPE_TEXTURE_DRIVER)
static GLuint
_cogl_texture_driver_gen (CoglContext *ctx,
GLenum gl_target,
CoglPixelFormat internal_format)
cogl_texture_driver_gl3_gen (CoglTextureDriver *driver,
CoglContext *ctx,
GLenum gl_target,
CoglPixelFormat internal_format)
{
GLuint tex;
@ -149,10 +160,11 @@ prep_gl_for_pixels_download_full (CoglContext *ctx,
}
static void
_cogl_texture_driver_prep_gl_for_pixels_download (CoglContext *ctx,
int image_width,
int pixels_rowstride,
int pixels_bpp)
cogl_texture_driver_gl3_prep_gl_for_pixels_download (CoglTextureDriver *driver,
CoglContext *ctx,
int image_width,
int pixels_rowstride,
int pixels_bpp)
{
prep_gl_for_pixels_download_full (ctx,
pixels_rowstride,
@ -163,19 +175,20 @@ _cogl_texture_driver_prep_gl_for_pixels_download (CoglContext *ctx,
}
static gboolean
_cogl_texture_driver_upload_subregion_to_gl (CoglContext *ctx,
CoglTexture *texture,
int src_x,
int src_y,
int dst_x,
int dst_y,
int width,
int height,
int level,
CoglBitmap *source_bmp,
GLuint source_gl_format,
GLuint source_gl_type,
GError **error)
cogl_texture_driver_gl3_upload_subregion_to_gl (CoglTextureDriver *driver,
CoglContext *ctx,
CoglTexture *texture,
int src_x,
int src_y,
int dst_x,
int dst_y,
int width,
int height,
int level,
CoglBitmap *source_bmp,
GLuint source_gl_format,
GLuint source_gl_type,
GError **error)
{
GLenum gl_target;
GLuint gl_handle;
@ -280,14 +293,15 @@ _cogl_texture_driver_upload_subregion_to_gl (CoglContext *ctx,
}
static gboolean
_cogl_texture_driver_upload_to_gl (CoglContext *ctx,
GLenum gl_target,
GLuint gl_handle,
CoglBitmap *source_bmp,
GLint internal_gl_format,
GLuint source_gl_format,
GLuint source_gl_type,
GError **error)
cogl_texture_driver_gl3_upload_to_gl (CoglTextureDriver *driver,
CoglContext *ctx,
GLenum gl_target,
GLuint gl_handle,
CoglBitmap *source_bmp,
GLint internal_gl_format,
GLuint source_gl_format,
GLuint source_gl_type,
GError **error)
{
uint8_t *data;
CoglPixelFormat source_format = cogl_bitmap_get_format (source_bmp);
@ -343,11 +357,12 @@ _cogl_texture_driver_upload_to_gl (CoglContext *ctx,
}
static gboolean
_cogl_texture_driver_gl_get_tex_image (CoglContext *ctx,
GLenum gl_target,
GLenum dest_gl_format,
GLenum dest_gl_type,
uint8_t *dest)
cogl_texture_driver_gl3_gl_get_tex_image (CoglTextureDriver *driver,
CoglContext *ctx,
GLenum gl_target,
GLenum dest_gl_format,
GLenum dest_gl_type,
uint8_t *dest)
{
GE (ctx, glGetTexImage (gl_target,
0, /* level */
@ -358,13 +373,14 @@ _cogl_texture_driver_gl_get_tex_image (CoglContext *ctx,
}
static gboolean
_cogl_texture_driver_size_supported (CoglContext *ctx,
GLenum gl_target,
GLenum gl_intformat,
GLenum gl_format,
GLenum gl_type,
int width,
int height)
cogl_texture_driver_gl3_size_supported (CoglTextureDriver *driver,
CoglContext *ctx,
GLenum gl_target,
GLenum gl_intformat,
GLenum gl_format,
GLenum gl_type,
int width,
int height)
{
GLenum proxy_target;
GLint new_width = 0;
@ -389,8 +405,9 @@ _cogl_texture_driver_size_supported (CoglContext *ctx,
}
static gboolean
_cogl_texture_driver_upload_supported (CoglContext *ctx,
CoglPixelFormat format)
cogl_texture_driver_gl3_upload_supported (CoglTextureDriver *driver,
CoglContext *ctx,
CoglPixelFormat format)
{
switch (format)
{
@ -458,11 +475,11 @@ _cogl_texture_driver_upload_supported (CoglContext *ctx,
}
static CoglPixelFormat
_cogl_texture_driver_find_best_gl_get_data_format
(CoglContext *context,
CoglPixelFormat format,
GLenum *closest_gl_format,
GLenum *closest_gl_type)
cogl_texture_driver_gl3_find_best_gl_get_data_format (CoglTextureDriver *driver,
CoglContext *context,
CoglPixelFormat format,
GLenum *closest_gl_format,
GLenum *closest_gl_type)
{
return context->driver_vtable->pixel_format_to_gl (context,
format,
@ -471,15 +488,22 @@ _cogl_texture_driver_find_best_gl_get_data_format
closest_gl_type);
}
const CoglTextureDriver
_cogl_texture_driver_gl =
{
_cogl_texture_driver_gen,
_cogl_texture_driver_upload_subregion_to_gl,
_cogl_texture_driver_upload_to_gl,
_cogl_texture_driver_prep_gl_for_pixels_download,
_cogl_texture_driver_gl_get_tex_image,
_cogl_texture_driver_size_supported,
_cogl_texture_driver_upload_supported,
_cogl_texture_driver_find_best_gl_get_data_format
};
static void
cogl_texture_driver_gl3_class_init (CoglTextureDriverGL3Class *klass)
{
CoglTextureDriverClass *driver_klass = COGL_TEXTURE_DRIVER_CLASS (klass);
driver_klass->gen = cogl_texture_driver_gl3_gen;
driver_klass->upload_subregion_to_gl = cogl_texture_driver_gl3_upload_subregion_to_gl;
driver_klass->upload_to_gl = cogl_texture_driver_gl3_upload_to_gl;
driver_klass->prep_gl_for_pixels_download = cogl_texture_driver_gl3_prep_gl_for_pixels_download;
driver_klass->gl_get_tex_image = cogl_texture_driver_gl3_gl_get_tex_image;
driver_klass->size_supported = cogl_texture_driver_gl3_size_supported;
driver_klass->format_supports_upload = cogl_texture_driver_gl3_upload_supported;
driver_klass->find_best_gl_get_data_format = cogl_texture_driver_gl3_find_best_gl_get_data_format;
}
static void
cogl_texture_driver_gl3_init (CoglTextureDriverGL3 *driver)
{
}

View File

@ -0,0 +1,40 @@
/*
* Cogl
*
* A Low Level GPU Graphics and Utilities API
*
* Copyright (C) 2024 Red Hat.
*
* 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.
*/
#pragma once
#include "cogl/cogl-texture-driver.h"
G_DECLARE_FINAL_TYPE (CoglTextureDriverGLES2,
cogl_texture_driver_gles2,
COGL,
TEXTURE_DRIVER_GLES2,
CoglTextureDriver)
#define COGL_TYPE_TEXTURE_DRIVER_GLES2 (cogl_texture_driver_gles2_get_type ())

View File

@ -42,6 +42,7 @@
#include "cogl/cogl-texture-private.h"
#include "cogl/cogl-pipeline.h"
#include "cogl/cogl-context-private.h"
#include "cogl/driver/gl/gles/cogl-texture-driver-gles2-private.h"
#include "cogl/driver/gl/cogl-pipeline-opengl-private.h"
#include "cogl/driver/gl/cogl-util-gl-private.h"
#include "cogl/driver/gl/cogl-texture-gl-private.h"
@ -70,10 +71,21 @@
#define GL_UNPACK_SKIP_PIXELS 0x0CF4
#endif
struct _CoglTextureDriverGLES2
{
CoglTextureDriver parent_instance;
};
G_DEFINE_FINAL_TYPE (CoglTextureDriverGLES2,
cogl_texture_driver_gles2,
COGL_TYPE_TEXTURE_DRIVER)
static GLuint
_cogl_texture_driver_gen (CoglContext *ctx,
GLenum gl_target,
CoglPixelFormat internal_format)
cogl_texture_driver_gles2_gen (CoglTextureDriver *driver,
CoglContext *ctx,
GLenum gl_target,
CoglPixelFormat internal_format)
{
GLuint tex;
@ -134,10 +146,11 @@ _cogl_texture_driver_prep_gl_for_pixels_upload (CoglContext *ctx,
}
static void
_cogl_texture_driver_prep_gl_for_pixels_download (CoglContext *ctx,
int pixels_rowstride,
int image_width,
int pixels_bpp)
cogl_texture_driver_gles2_prep_gl_for_pixels_download (CoglTextureDriver *driver,
CoglContext *ctx,
int pixels_rowstride,
int image_width,
int pixels_bpp)
{
_cogl_texture_gl_prep_alignment_for_pixels_download (ctx,
pixels_bpp,
@ -180,19 +193,20 @@ prepare_bitmap_alignment_for_upload (CoglContext *ctx,
}
static gboolean
_cogl_texture_driver_upload_subregion_to_gl (CoglContext *ctx,
CoglTexture *texture,
int src_x,
int src_y,
int dst_x,
int dst_y,
int width,
int height,
int level,
CoglBitmap *source_bmp,
GLuint source_gl_format,
GLuint source_gl_type,
GError **error)
cogl_texture_driver_gles2_upload_subregion_to_gl (CoglTextureDriver *driver,
CoglContext *ctx,
CoglTexture *texture,
int src_x,
int src_y,
int dst_x,
int dst_y,
int width,
int height,
int level,
CoglBitmap *source_bmp,
GLuint source_gl_format,
GLuint source_gl_type,
GError **error)
{
GLenum gl_target;
GLuint gl_handle;
@ -335,14 +349,15 @@ _cogl_texture_driver_upload_subregion_to_gl (CoglContext *ctx,
}
static gboolean
_cogl_texture_driver_upload_to_gl (CoglContext *ctx,
GLenum gl_target,
GLuint gl_handle,
CoglBitmap *source_bmp,
GLint internal_gl_format,
GLuint source_gl_format,
GLuint source_gl_type,
GError **error)
cogl_texture_driver_gles2_upload_to_gl (CoglTextureDriver *driver,
CoglContext *ctx,
GLenum gl_target,
GLuint gl_handle,
CoglBitmap *source_bmp,
GLint internal_gl_format,
GLuint source_gl_format,
GLuint source_gl_type,
GError **error)
{
CoglPixelFormat source_format = cogl_bitmap_get_format (source_bmp);
int bpp;
@ -411,23 +426,25 @@ _cogl_texture_driver_upload_to_gl (CoglContext *ctx,
* fallback to a generic render + readpixels approach to downloading
* texture data. (See _cogl_texture_draw_and_read() ) */
static gboolean
_cogl_texture_driver_gl_get_tex_image (CoglContext *ctx,
GLenum gl_target,
GLenum dest_gl_format,
GLenum dest_gl_type,
uint8_t *dest)
cogl_texture_driver_gles2_gl_get_tex_image (CoglTextureDriver *driver,
CoglContext *ctx,
GLenum gl_target,
GLenum dest_gl_format,
GLenum dest_gl_type,
uint8_t *dest)
{
return FALSE;
}
static gboolean
_cogl_texture_driver_size_supported (CoglContext *ctx,
GLenum gl_target,
GLenum gl_intformat,
GLenum gl_format,
GLenum gl_type,
int width,
int height)
cogl_texture_driver_gles2_size_supported (CoglTextureDriver *driver,
CoglContext *ctx,
GLenum gl_target,
GLenum gl_intformat,
GLenum gl_format,
GLenum gl_type,
int width,
int height)
{
GLint max_size;
@ -439,8 +456,9 @@ _cogl_texture_driver_size_supported (CoglContext *ctx,
}
static gboolean
_cogl_texture_driver_upload_supported (CoglContext *ctx,
CoglPixelFormat format)
cogl_texture_driver_gles2_upload_supported (CoglTextureDriver *driver,
CoglContext *ctx,
CoglPixelFormat format)
{
switch (format)
{
@ -527,11 +545,11 @@ _cogl_texture_driver_upload_supported (CoglContext *ctx,
}
static CoglPixelFormat
_cogl_texture_driver_find_best_gl_get_data_format
(CoglContext *context,
CoglPixelFormat format,
GLenum *closest_gl_format,
GLenum *closest_gl_type)
cogl_texture_driver_gles2_find_best_gl_get_data_format (CoglTextureDriver *driver,
CoglContext *context,
CoglPixelFormat format,
GLenum *closest_gl_format,
GLenum *closest_gl_type)
{
/* Find closest format that's supported by GL
(Can't use _cogl_pixel_format_to_gl since available formats
@ -541,15 +559,22 @@ _cogl_texture_driver_find_best_gl_get_data_format
return COGL_PIXEL_FORMAT_RGBA_8888;
}
const CoglTextureDriver
_cogl_texture_driver_gles =
{
_cogl_texture_driver_gen,
_cogl_texture_driver_upload_subregion_to_gl,
_cogl_texture_driver_upload_to_gl,
_cogl_texture_driver_prep_gl_for_pixels_download,
_cogl_texture_driver_gl_get_tex_image,
_cogl_texture_driver_size_supported,
_cogl_texture_driver_upload_supported,
_cogl_texture_driver_find_best_gl_get_data_format
};
static void
cogl_texture_driver_gles2_class_init (CoglTextureDriverGLES2Class *klass)
{
CoglTextureDriverClass *driver_klass = COGL_TEXTURE_DRIVER_CLASS (klass);
driver_klass->gen = cogl_texture_driver_gles2_gen;
driver_klass->upload_subregion_to_gl = cogl_texture_driver_gles2_upload_subregion_to_gl;
driver_klass->upload_to_gl = cogl_texture_driver_gles2_upload_to_gl;
driver_klass->prep_gl_for_pixels_download = cogl_texture_driver_gles2_prep_gl_for_pixels_download;
driver_klass->gl_get_tex_image = cogl_texture_driver_gles2_gl_get_tex_image;
driver_klass->size_supported = cogl_texture_driver_gles2_size_supported;
driver_klass->format_supports_upload = cogl_texture_driver_gles2_upload_supported;
driver_klass->find_best_gl_get_data_format = cogl_texture_driver_gles2_find_best_gl_get_data_format;
}
static void
cogl_texture_driver_gles2_init (CoglTextureDriverGLES2 *driver)
{
}

View File

@ -109,12 +109,14 @@ cogl_common_driver_sources = [
gl_driver_sources = [
'driver/gl/gl/cogl-driver-gl.c',
'driver/gl/gl/cogl-texture-driver-gl.c',
'driver/gl/gl/cogl-texture-driver-gl3.c',
'driver/gl/gl/cogl-texture-driver-gl3-private.h',
]
gles_driver_sources = [
'driver/gl/gles/cogl-driver-gles.c',
'driver/gl/gles/cogl-texture-driver-gles.c',
'driver/gl/gles/cogl-texture-driver-gles2.c',
'driver/gl/gles/cogl-texture-driver-gles2-private.h',
]
cogl_driver_sources = [
@ -255,6 +257,7 @@ cogl_sources = [
'cogl-texture-2d-sliced-private.h',
'cogl-texture-2d-sliced.c',
'cogl-texture-2d.c',
'cogl-texture-driver.c',
'cogl-texture-driver.h',
'cogl-texture-private.h',
'cogl-texture.c',