[Cogl] [PATCH 2/4] Add a CoglPrimitiveTexture interface

Neil Roberts neil at linux.intel.com
Wed Apr 4 09:13:10 PDT 2012


This interface represents any textures that are backed by a single
texture in GL and that can be used directly with the
cogl_framebuffer_draw_attributes family of functions. This currently
equates to CoglTexture2D, CoglTexture3D and CoglTextureRectangle.

The interface currently has only one method called
cogl_primitive_set_auto_mipmap. This replaces the
COGL_TEXTURE_NO_AUTO_MIPMAP flag from the CoglTextureFlags parameter
in the constructors. None of the other flags in CoglTextureFlags make
sense for primitive textures so it doesn't seem like a good idea to
need them for primitive constructors.

There is a boolean in the vtable to mark whether a texture type is
primitive which the new cogl_is_primitive function uses. There is also
a new texture virtual called set_auto_mipmap which is only required to
be implemented for primitive textures.
---
 cogl/Makefile.am                                   |    2 +
 cogl/cogl-atlas-texture.c                          |    4 +-
 cogl/cogl-primitive-texture.c                      |   54 +++++++++++
 cogl/cogl-primitive-texture.h                      |   98 ++++++++++++++++++++
 cogl/cogl-sub-texture.c                            |    4 +-
 cogl/cogl-texture-2d-sliced.c                      |    4 +-
 cogl/cogl-texture-2d.c                             |   26 ++++--
 cogl/cogl-texture-3d.c                             |   13 +++-
 cogl/cogl-texture-private.h                        |    6 +
 cogl/cogl-texture-rectangle.c                      |   12 ++-
 cogl/cogl.h                                        |    1 +
 cogl/winsys/cogl-texture-pixmap-x11.c              |    4 +-
 .../cogl-2.0-experimental-docs.xml.in              |   11 ++-
 .../cogl-2.0-experimental-sections.txt             |    8 ++
 14 files changed, 231 insertions(+), 16 deletions(-)
 create mode 100644 cogl/cogl-primitive-texture.c
 create mode 100644 cogl/cogl-primitive-texture.h

diff --git a/cogl/Makefile.am b/cogl/Makefile.am
index 69cb5df..21d6676 100644
--- a/cogl/Makefile.am
+++ b/cogl/Makefile.am
@@ -108,6 +108,7 @@ cogl_experimental_h = \
 	$(srcdir)/cogl-texture-2d-sliced.h      \
 	$(srcdir)/cogl-sub-texture.h            \
 	$(srcdir)/cogl-meta-texture.h		\
+	$(srcdir)/cogl-primitive-texture.h	\
 	$(srcdir)/cogl-depth-state.h 		\
 	$(srcdir)/cogl-buffer.h 		\
 	$(srcdir)/cogl-pixel-buffer.h		\
@@ -310,6 +311,7 @@ cogl_sources_c = \
 	$(srcdir)/cogl-atlas-texture-private.h          \
 	$(srcdir)/cogl-atlas-texture.c                  \
 	$(srcdir)/cogl-meta-texture.c			\
+	$(srcdir)/cogl-primitive-texture.c		\
 	$(srcdir)/cogl-blit.h				\
 	$(srcdir)/cogl-blit.c				\
 	$(srcdir)/cogl-spans.h				\
diff --git a/cogl/cogl-atlas-texture.c b/cogl/cogl-atlas-texture.c
index d0be83d..53435d3 100644
--- a/cogl/cogl-atlas-texture.c
+++ b/cogl/cogl-atlas-texture.c
@@ -817,6 +817,7 @@ _cogl_atlas_texture_get_type (CoglTexture *tex)
 static const CoglTextureVtable
 cogl_atlas_texture_vtable =
   {
+    FALSE, /* not primitive */
     _cogl_atlas_texture_set_region,
     NULL, /* get_data */
     _cogl_atlas_texture_foreach_sub_texture_in_region,
@@ -835,5 +836,6 @@ cogl_atlas_texture_vtable =
     _cogl_atlas_texture_get_width,
     _cogl_atlas_texture_get_height,
     _cogl_atlas_texture_get_type,
-    NULL /* is_foreign */
+    NULL, /* is_foreign */
+    NULL /* set_auto_mipmap */
   };
diff --git a/cogl/cogl-primitive-texture.c b/cogl/cogl-primitive-texture.c
new file mode 100644
index 0000000..b748a87
--- /dev/null
+++ b/cogl/cogl-primitive-texture.c
@@ -0,0 +1,54 @@
+/*
+ * Cogl
+ *
+ * An object oriented GL/GLES Abstraction/Utility Layer
+ *
+ * Copyright (C) 2012 Intel Corporation.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ *
+ * Authors:
+ *  Neil Roberts <neil at linux.intel.com>
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "cogl-primitive-texture.h"
+#include "cogl-texture-private.h"
+
+gboolean
+cogl_is_primitive_texture (void *object)
+{
+  return (cogl_is_texture (object) &&
+          COGL_TEXTURE (object)->vtable->is_primitive);
+}
+
+void
+cogl_primitive_texture_set_auto_mipmap (CoglPrimitiveTexture *primitive_texture,
+                                        gboolean value)
+{
+  CoglTexture *texture;
+
+  _COGL_RETURN_IF_FAIL (cogl_is_primitive_texture (primitive_texture));
+
+  texture = COGL_TEXTURE (primitive_texture);
+
+  g_assert (texture->vtable->set_auto_mipmap != NULL);
+
+  texture->vtable->set_auto_mipmap (texture, value);
+}
diff --git a/cogl/cogl-primitive-texture.h b/cogl/cogl-primitive-texture.h
new file mode 100644
index 0000000..f35918b
--- /dev/null
+++ b/cogl/cogl-primitive-texture.h
@@ -0,0 +1,98 @@
+/*
+ * Cogl
+ *
+ * An object oriented GL/GLES Abstraction/Utility Layer
+ *
+ * Copyright (C) 2012 Intel Corporation.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ *
+ */
+
+#if !defined(__COGL_H_INSIDE__) && !defined(CLUTTER_COMPILATION)
+#error "Only <cogl/cogl.h> can be included directly."
+#endif
+
+#ifndef __COGL_PRIMITIVE_TEXTURE_H__
+#define __COGL_PRIMITIVE_TEXTURE_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+/**
+ * SECTION:cogl-primitive-texture
+ * @short_description: Interface for low-level textures like
+ *                     #CoglTexture2D and #CoglTexture3D.
+ *
+ * A #CoglPrimitiveTexture is a texture that is directly represented
+ * by a single texture on the GPU. For example these could be a
+ * #CoglTexture2D, #CoglTexture3D or #CoglTextureRectangle. This is
+ * opposed to high level meta textures which may be composed of
+ * multiple primitive textures or a sub-region of another texture such
+ * as #CoglAtlasTexture and #CoglTexture2DSliced.
+ *
+ * A texture that implements this interface can be directly used with
+ * the attributes API such as cogl_framebuffer_draw_attributes().
+ * Other types of textures need to be first resolved to primitive
+ * textures using the #CoglMetaTexture interface.
+ *
+ * <note>Most developers won't need to use this interface directly but
+ * still it is worth understanding the distinction between high-level
+ * and primitive textures because you may find other references in the
+ * documentation that detail limitations of using
+ * primitive textures.</note>
+ */
+
+typedef struct _CoglPrimitiveTexture CoglPrimitiveTexture;
+#define COGL_PRIMITIVE_TEXTURE(X) ((CoglPrimitiveTexture *)X)
+
+/**
+ * cogl_is_primitive_texture:
+ * @object: A #CoglObject pointer
+ *
+ * Gets whether the given object references a primitive texture object.
+ *
+ * Return value: %TRUE if the pointer references a primitive texture, and
+ *   %FALSE otherwise
+ * Since: 2.0
+ * Stability: unstable
+ */
+gboolean
+cogl_is_primitive_texture (void *object);
+
+/**
+ * cogl_primitive_texture_set_auto_mipmap:
+ * @primitive_texture: A #CoglPrimitiveTexture
+ * @value: The new value for whether to auto mipmap
+ *
+ * Sets whether the texture will automatically update the smaller
+ * mipmap levels after any part of level 0 is updated. The update will
+ * only occur whenever the texture is used for drawing with a texture
+ * filter that requires the lower mipmap levels. An application should
+ * disable this if it wants to upload its own data for the other
+ * levels. By default auto mipmapping is enabled.
+ *
+ * Since: 2.0
+ * Stability: unstable
+ */
+void
+cogl_primitive_texture_set_auto_mipmap (CoglPrimitiveTexture *primitive_texture,
+                                        gboolean value);
+
+G_END_DECLS
+
+#endif /* __COGL_PRIMITIVE_TEXTURE_H__ */
diff --git a/cogl/cogl-sub-texture.c b/cogl/cogl-sub-texture.c
index 925a878..ee8a10f 100644
--- a/cogl/cogl-sub-texture.c
+++ b/cogl/cogl-sub-texture.c
@@ -427,6 +427,7 @@ _cogl_sub_texture_get_type (CoglTexture *tex)
 static const CoglTextureVtable
 cogl_sub_texture_vtable =
   {
+    FALSE, /* not primitive */
     _cogl_sub_texture_set_region,
     NULL, /* get_data */
     _cogl_sub_texture_foreach_sub_texture_in_region,
@@ -445,5 +446,6 @@ cogl_sub_texture_vtable =
     _cogl_sub_texture_get_width,
     _cogl_sub_texture_get_height,
     _cogl_sub_texture_get_type,
-    NULL /* is_foreign */
+    NULL, /* is_foreign */
+    NULL /* set_auto_mipmap */
   };
diff --git a/cogl/cogl-texture-2d-sliced.c b/cogl/cogl-texture-2d-sliced.c
index 275088f..3fb0338 100644
--- a/cogl/cogl-texture-2d-sliced.c
+++ b/cogl/cogl-texture-2d-sliced.c
@@ -1303,6 +1303,7 @@ _cogl_texture_2d_sliced_get_type (CoglTexture *tex)
 static const CoglTextureVtable
 cogl_texture_2d_sliced_vtable =
   {
+    FALSE, /* not primitive */
     _cogl_texture_2d_sliced_set_region,
     NULL, /* get_data */
     _cogl_texture_2d_sliced_foreach_sub_texture_in_region,
@@ -1321,5 +1322,6 @@ cogl_texture_2d_sliced_vtable =
     _cogl_texture_2d_sliced_get_width,
     _cogl_texture_2d_sliced_get_height,
     _cogl_texture_2d_sliced_get_type,
-    _cogl_texture_2d_sliced_is_foreign
+    _cogl_texture_2d_sliced_is_foreign,
+    NULL /* set_auto_mipmap */
   };
diff --git a/cogl/cogl-texture-2d.c b/cogl/cogl-texture-2d.c
index b14c615..da78f13 100644
--- a/cogl/cogl-texture-2d.c
+++ b/cogl/cogl-texture-2d.c
@@ -138,10 +138,18 @@ _cogl_texture_2d_can_create (unsigned int width,
   return TRUE;
 }
 
+static void
+_cogl_texture_2d_set_auto_mipmap (CoglTexture *tex,
+                                  gboolean value)
+{
+  CoglTexture2D *tex_2d = COGL_TEXTURE_2D (tex);
+
+  tex_2d->auto_mipmap = value;
+}
+
 static CoglTexture2D *
 _cogl_texture_2d_create_base (unsigned int     width,
                               unsigned int     height,
-                              CoglTextureFlags flags,
                               CoglPixelFormat  internal_format)
 {
   CoglTexture2D *tex_2d = g_new (CoglTexture2D, 1);
@@ -152,7 +160,7 @@ _cogl_texture_2d_create_base (unsigned int     width,
   tex_2d->width = width;
   tex_2d->height = height;
   tex_2d->mipmaps_dirty = TRUE;
-  tex_2d->auto_mipmap = (flags & COGL_TEXTURE_NO_AUTO_MIPMAP) == 0;
+  tex_2d->auto_mipmap = TRUE;
 
   /* We default to GL_LINEAR for both filters */
   tex_2d->min_filter = GL_LINEAR;
@@ -200,7 +208,7 @@ cogl_texture_2d_new_with_size (CoglContext *ctx,
                                                             &gl_format,
                                                             &gl_type);
 
-  tex_2d = _cogl_texture_2d_create_base (width, height, COGL_TEXTURE_NONE,
+  tex_2d = _cogl_texture_2d_create_base (width, height,
                                          internal_format);
 
   ctx->texture_driver->gen (ctx, GL_TEXTURE_2D, 1, &tex_2d->gl_texture);
@@ -262,7 +270,6 @@ _cogl_texture_2d_new_from_bitmap (CoglBitmap      *bmp,
 
   tex_2d = _cogl_texture_2d_create_base (cogl_bitmap_get_width (bmp),
                                          cogl_bitmap_get_height (bmp),
-                                         flags,
                                          internal_format);
 
   /* Keep a copy of the first pixel so that if glGenerateMipmap isn't
@@ -294,6 +301,9 @@ _cogl_texture_2d_new_from_bitmap (CoglBitmap      *bmp,
 
   cogl_object_unref (dst_bmp);
 
+  _cogl_texture_2d_set_auto_mipmap (COGL_TEXTURE (tex_2d),
+                                    !(flags & COGL_TEXTURE_NO_AUTO_MIPMAP));
+
   return _cogl_texture_2d_handle_new (tex_2d);
 }
 
@@ -432,8 +442,8 @@ cogl_texture_2d_new_from_foreign (CoglContext *ctx,
 
   /* Create new texture */
   tex_2d = _cogl_texture_2d_create_base (width, height,
-                                         COGL_TEXTURE_NO_AUTO_MIPMAP,
                                          format);
+  _cogl_texture_2d_set_auto_mipmap (COGL_TEXTURE (tex_2d), FALSE);
 
   /* Setup bitmap info */
   tex_2d->is_foreign = TRUE;
@@ -474,7 +484,7 @@ _cogl_egl_texture_2d_new_from_image (CoglContext *ctx,
                         COGL_PRIVATE_FEATURE_TEXTURE_2D_FROM_EGL_IMAGE,
                         NULL);
 
-  tex_2d = _cogl_texture_2d_create_base (width, height, COGL_TEXTURE_NONE,
+  tex_2d = _cogl_texture_2d_create_base (width, height,
                                          format);
 
   ctx->texture_driver->gen (ctx, GL_TEXTURE_2D, 1, &tex_2d->gl_texture);
@@ -877,6 +887,7 @@ _cogl_texture_2d_get_type (CoglTexture *tex)
 static const CoglTextureVtable
 cogl_texture_2d_vtable =
   {
+    TRUE, /* primitive */
     _cogl_texture_2d_set_region,
     _cogl_texture_2d_get_data,
     NULL, /* foreach_sub_texture_in_region */
@@ -895,5 +906,6 @@ cogl_texture_2d_vtable =
     _cogl_texture_2d_get_width,
     _cogl_texture_2d_get_height,
     _cogl_texture_2d_get_type,
-    _cogl_texture_2d_is_foreign
+    _cogl_texture_2d_is_foreign,
+    _cogl_texture_2d_set_auto_mipmap
   };
diff --git a/cogl/cogl-texture-3d.c b/cogl/cogl-texture-3d.c
index 4e9621e..9d5b01e 100644
--- a/cogl/cogl-texture-3d.c
+++ b/cogl/cogl-texture-3d.c
@@ -99,6 +99,15 @@ _cogl_texture_3d_free (CoglTexture3D *tex_3d)
   _cogl_texture_free (COGL_TEXTURE (tex_3d));
 }
 
+static void
+_cogl_texture_3d_set_auto_mipmap (CoglTexture *tex,
+                                  gboolean value)
+{
+  CoglTexture3D *tex_3d = COGL_TEXTURE_3D (tex);
+
+  tex_3d->auto_mipmap = value;
+}
+
 static CoglTexture3D *
 _cogl_texture_3d_create_base (CoglContext *ctx,
                               int width,
@@ -609,6 +618,7 @@ _cogl_texture_3d_get_type (CoglTexture *tex)
 static const CoglTextureVtable
 cogl_texture_3d_vtable =
   {
+    TRUE, /* primitive */
     _cogl_texture_3d_set_region,
     _cogl_texture_3d_get_data,
     NULL, /* foreach_sub_texture_in_region */
@@ -627,5 +637,6 @@ cogl_texture_3d_vtable =
     _cogl_texture_3d_get_width,
     _cogl_texture_3d_get_height,
     _cogl_texture_3d_get_type,
-    NULL /* is_foreign */
+    NULL, /* is_foreign */
+    _cogl_texture_3d_set_auto_mipmap
   };
diff --git a/cogl/cogl-texture-private.h b/cogl/cogl-texture-private.h
index 9695179..1fd6bb1 100644
--- a/cogl/cogl-texture-private.h
+++ b/cogl/cogl-texture-private.h
@@ -58,6 +58,8 @@ struct _CoglTextureVtable
   /* Virtual functions that must be implemented for a texture
      backend */
 
+  gboolean is_primitive;
+
   /* This should update the specified sub region of the texture with a
      sub region of the given bitmap. The bitmap is not converted
      before being passed so the implementation is expected to call
@@ -126,6 +128,10 @@ struct _CoglTextureVtable
   CoglTextureType (* get_type) (CoglTexture *tex);
 
   gboolean (* is_foreign) (CoglTexture *tex);
+
+  /* Only needs to be implemented if is_primitive == TRUE */
+  void (* set_auto_mipmap) (CoglTexture *texture,
+                            gboolean value);
 };
 
 struct _CoglTexture
diff --git a/cogl/cogl-texture-rectangle.c b/cogl/cogl-texture-rectangle.c
index a4d3dd9..cc665a0 100644
--- a/cogl/cogl-texture-rectangle.c
+++ b/cogl/cogl-texture-rectangle.c
@@ -152,6 +152,14 @@ _cogl_texture_rectangle_can_create (unsigned int width,
   return TRUE;
 }
 
+static void
+_cogl_texture_rectangle_set_auto_mipmap (CoglTexture *tex,
+                                         gboolean value)
+{
+  /* Rectangle textures currently never support mipmapping so there's
+     no point in doing anything here */
+}
+
 static CoglTextureRectangle *
 _cogl_texture_rectangle_create_base (unsigned int     width,
                                      unsigned int     height,
@@ -604,6 +612,7 @@ _cogl_texture_rectangle_get_type (CoglTexture *tex)
 static const CoglTextureVtable
 cogl_texture_rectangle_vtable =
   {
+    TRUE, /* primitive */
     _cogl_texture_rectangle_set_region,
     _cogl_texture_rectangle_get_data,
     NULL, /* foreach_sub_texture_in_region */
@@ -622,5 +631,6 @@ cogl_texture_rectangle_vtable =
     _cogl_texture_rectangle_get_width,
     _cogl_texture_rectangle_get_height,
     _cogl_texture_rectangle_get_type,
-    _cogl_texture_rectangle_is_foreign
+    _cogl_texture_rectangle_is_foreign,
+    _cogl_texture_rectangle_set_auto_mipmap
   };
diff --git a/cogl/cogl.h b/cogl/cogl.h
index b27b759..8663f95 100644
--- a/cogl/cogl.h
+++ b/cogl/cogl.h
@@ -89,6 +89,7 @@
 #include <cogl/cogl-texture-2d-sliced.h>
 #include <cogl/cogl-sub-texture.h>
 #include <cogl/cogl-meta-texture.h>
+#include <cogl/cogl-primitive-texture.h>
 #include <cogl/cogl-index-buffer.h>
 #include <cogl/cogl-attribute-buffer.h>
 #include <cogl/cogl-indices.h>
diff --git a/cogl/winsys/cogl-texture-pixmap-x11.c b/cogl/winsys/cogl-texture-pixmap-x11.c
index 0723ab3..3e6b12a 100644
--- a/cogl/winsys/cogl-texture-pixmap-x11.c
+++ b/cogl/winsys/cogl-texture-pixmap-x11.c
@@ -1009,6 +1009,7 @@ _cogl_texture_pixmap_x11_free (CoglTexturePixmapX11 *tex_pixmap)
 static const CoglTextureVtable
 cogl_texture_pixmap_x11_vtable =
   {
+    FALSE, /* not primitive */
     _cogl_texture_pixmap_x11_set_region,
     _cogl_texture_pixmap_x11_get_data,
     _cogl_texture_pixmap_x11_foreach_sub_texture_in_region,
@@ -1027,5 +1028,6 @@ cogl_texture_pixmap_x11_vtable =
     _cogl_texture_pixmap_x11_get_width,
     _cogl_texture_pixmap_x11_get_height,
     _cogl_texture_pixmap_x11_get_type,
-    NULL /* is_foreign */
+    NULL, /* is_foreign */
+    NULL /* set_auto_mipmap */
   };
diff --git a/doc/reference/cogl-2.0-experimental/cogl-2.0-experimental-docs.xml.in b/doc/reference/cogl-2.0-experimental/cogl-2.0-experimental-docs.xml.in
index 3f49e62..92a47b6 100644
--- a/doc/reference/cogl-2.0-experimental/cogl-2.0-experimental-docs.xml.in
+++ b/doc/reference/cogl-2.0-experimental/cogl-2.0-experimental-docs.xml.in
@@ -94,9 +94,6 @@
     <section id="cogl-textures">
       <title>Textures</title>
       <xi:include href="xml/cogl-texture.xml"/>
-      <xi:include href="xml/cogl-texture-2d.xml"/>
-      <xi:include href="xml/cogl-texture-3d.xml"/>
-      <xi:include href="xml/cogl-texture-rectangle.xml"/>
     </section>
 
     <section id="cogl-meta-textures">
@@ -107,6 +104,14 @@
       <xi:include href="xml/cogl-texture-pixmap-x11.xml"/>
     </section>
 
+    <section id="cogl-primitive-textures">
+      <title>Primitive Textures</title>
+      <xi:include href="xml/cogl-primitive-texture.xml"/>
+      <xi:include href="xml/cogl-texture-2d.xml"/>
+      <xi:include href="xml/cogl-texture-3d.xml"/>
+      <xi:include href="xml/cogl-texture-rectangle.xml"/>
+    </section>
+
     <xi:include href="xml/cogl-clipping.xml"/>
     <section id="cogl-framebuffer-apis">
       <title>Framebuffers</title>
diff --git a/doc/reference/cogl-2.0-experimental/cogl-2.0-experimental-sections.txt b/doc/reference/cogl-2.0-experimental/cogl-2.0-experimental-sections.txt
index ca3340d..ccf4a6d 100644
--- a/doc/reference/cogl-2.0-experimental/cogl-2.0-experimental-sections.txt
+++ b/doc/reference/cogl-2.0-experimental/cogl-2.0-experimental-sections.txt
@@ -345,6 +345,14 @@ cogl_meta_texture_foreach_in_region
 </SECTION>
 
 <SECTION>
+<FILE>cogl-primitive-texture</FILE>
+<TITLE>Low-level primitive textures</TITLE>
+CoglPrimitiveTexture
+cogl_is_primitive_texture
+cogl_primitive_texture_set_auto_mipmap
+</SECTION>
+
+<SECTION>
 <FILE>cogl-sub-texture</FILE>
 <TITLE>Sub Textures</TITLE>
 CoglSubTexture
-- 
1.7.3.16.g9464b



More information about the Cogl mailing list