[gtk+/wip/otte/shader: 18/52] gskslcompiler: Provide a way to compile files



commit 9ca398496bf5c1ddf5dfad4589cb54b15f14374f
Author: Benjamin Otte <otte redhat com>
Date:   Mon Oct 2 02:32:04 2017 +0200

    gskslcompiler: Provide a way to compile files
    
    And use it from gtk-glsl.
    
    A neat side effect is that we now get the actual file printed in error
    messages.

 gsk/gskpixelshader.c |    2 +-
 gsk/gskslcompiler.c  |   42 ++++++++++++++++++++++++++++++++++++++----
 gsk/gskslcompiler.h  |    7 +++++--
 gtk/glsl.c           |   42 ++++++++----------------------------------
 4 files changed, 52 insertions(+), 41 deletions(-)
---
diff --git a/gsk/gskpixelshader.c b/gsk/gskpixelshader.c
index 6debd85..a0cadd5 100644
--- a/gsk/gskpixelshader.c
+++ b/gsk/gskpixelshader.c
@@ -151,7 +151,7 @@ gsk_pixel_shader_new_for_data (GBytes             *source,
   g_return_val_if_fail (source != NULL, NULL);
 
   compiler = gsk_sl_compiler_new ();
-  program = gsk_sl_compiler_compile (compiler, source);
+  program = gsk_sl_compiler_compile_bytes (compiler, source);
   g_object_unref (compiler);
   if (program == NULL)
     return NULL;
diff --git a/gsk/gskslcompiler.c b/gsk/gskslcompiler.c
index 1c0b9c6..aa001f3 100644
--- a/gsk/gskslcompiler.c
+++ b/gsk/gskslcompiler.c
@@ -189,16 +189,14 @@ gsk_sl_compiler_copy_defines (GskSlCompiler *compiler)
   return copy;
 }
 
-GskSlProgram *
+static GskSlProgram *
 gsk_sl_compiler_compile (GskSlCompiler *compiler,
-                         GBytes        *bytes)
+                         GskCodeSource *source)
 {
   GskSlPreprocessor *preproc;
-  GskCodeSource *source;
   GskSlProgram *program;
 
   program = g_object_new (GSK_TYPE_SL_PROGRAM, NULL);
-  source = gsk_code_source_new_for_bytes ("<program>", bytes);
 
   preproc = gsk_sl_preprocessor_new (compiler, source);
 
@@ -211,8 +209,44 @@ gsk_sl_compiler_compile (GskSlCompiler *compiler,
     }
 
   gsk_sl_preprocessor_unref (preproc);
+
+  return program;
+}
+
+GskSlProgram *
+gsk_sl_compiler_compile_file (GskSlCompiler *compiler,
+                              GFile         *file)
+{
+  GskSlProgram *program;
+  GskCodeSource *source;
+
+  g_return_val_if_fail (GSK_IS_SL_COMPILER (compiler), NULL);
+  g_return_val_if_fail (G_IS_FILE (file), NULL);
+
+  source = gsk_code_source_new_for_file (file);
+
+  program = gsk_sl_compiler_compile (compiler, source);
+
   g_object_unref (source);
 
   return program;
 }
 
+GskSlProgram *
+gsk_sl_compiler_compile_bytes (GskSlCompiler *compiler,
+                               GBytes        *bytes)
+{
+  GskSlProgram *program;
+  GskCodeSource *source;
+
+  g_return_val_if_fail (GSK_IS_SL_COMPILER (compiler), NULL);
+  g_return_val_if_fail (bytes != NULL, NULL);
+
+  source = gsk_code_source_new_for_bytes ("<program>", bytes);
+
+  program = gsk_sl_compiler_compile (compiler, source);
+
+  g_object_unref (source);
+
+  return program;
+}
diff --git a/gsk/gskslcompiler.h b/gsk/gskslcompiler.h
index 6f0d741..58c9e3a 100644
--- a/gsk/gskslcompiler.h
+++ b/gsk/gskslcompiler.h
@@ -71,8 +71,11 @@ void                    gsk_sl_compiler_remove_define           (GskSlCompiler
                                                                  const char          *name);
 
 GDK_AVAILABLE_IN_3_92
-GskSlProgram *          gsk_sl_compiler_compile                 (GskSlCompiler       *compiler,
-                                                                 GBytes              *source);
+GskSlProgram *          gsk_sl_compiler_compile_file            (GskSlCompiler       *compiler,
+                                                                 GFile               *file);
+GDK_AVAILABLE_IN_3_92
+GskSlProgram *          gsk_sl_compiler_compile_bytes           (GskSlCompiler       *compiler,
+                                                                 GBytes              *bytes);
 
 G_END_DECLS
 
diff --git a/gtk/glsl.c b/gtk/glsl.c
index c698521..840ab53 100644
--- a/gtk/glsl.c
+++ b/gtk/glsl.c
@@ -29,21 +29,6 @@
 #include <unistd.h>
 #endif
 
-static GBytes *
-bytes_new_from_file (const char  *filename,
-                     GError     **error)
-{
-  gchar *data;
-  gsize length;
-
-  if (!g_file_get_contents (filename,
-                            &data, &length,
-                            error))
-    return FALSE;
-
-  return g_bytes_new_take (data, length);
-}
-
 static gboolean
 compile (GskSlCompiler *compiler,
          GOutputStream *output,
@@ -52,17 +37,12 @@ compile (GskSlCompiler *compiler,
   GBytes *bytes;
   GskSlProgram *program;
   GError *error = NULL;
+  GFile *file;
 
-  bytes = bytes_new_from_file (filename, &error);
-  if (bytes == NULL)
-    {
-      g_print (error->message);
-      g_error_free (error);
-      return FALSE;
-    }
+  file = g_file_new_for_commandline_arg (filename);
 
-  program = gsk_sl_compiler_compile (compiler, bytes);
-  g_bytes_unref (bytes);
+  program = gsk_sl_compiler_compile_file (compiler, file);
+  g_object_unref (file);
   if (program == NULL)
     return FALSE;
 
@@ -87,21 +67,15 @@ dump (GskSlCompiler *compiler,
       GOutputStream *output,
       const char    *filename)
 {
-  GBytes *bytes;
   GString *string;
   GskSlProgram *program;
   GError *error = NULL;
+  GFile *file;
 
-  bytes = bytes_new_from_file (filename, &error);
-  if (bytes == NULL)
-    {
-      g_print (error->message);
-      g_error_free (error);
-      return FALSE;
-    }
+  file = g_file_new_for_commandline_arg (filename);
 
-  program = gsk_sl_compiler_compile (compiler, bytes);
-  g_bytes_unref (bytes);
+  program = gsk_sl_compiler_compile_file (compiler, file);
+  g_object_unref (file);
   if (program == NULL)
     return FALSE;
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]