python: More efficient blits from surfaces.
authorJosé Fonseca <jfonseca@vmware.com>
Fri, 20 Feb 2009 16:50:02 +0000 (16:50 +0000)
committerJosé Fonseca <jfonseca@vmware.com>
Fri, 20 Feb 2009 16:50:02 +0000 (16:50 +0000)
C code instead of interpreted python code.

src/gallium/state_trackers/python/gallium.i
src/gallium/state_trackers/python/p_texture.i
src/gallium/state_trackers/python/retrace/interpreter.py
src/gallium/state_trackers/python/samples/tri.py

index f4c4b36ea7d5d18da84b78dabe8c3a2d07a8843c..bd30d9068f33f9be787ef6aa2303e3a693a4ed70 100644 (file)
@@ -47,6 +47,7 @@
 #include "cso_cache/cso_context.h"
 #include "util/u_draw_quad.h" 
 #include "util/u_tile.h" 
+#include "util/u_math.h" 
 #include "tgsi/tgsi_text.h" 
 #include "tgsi/tgsi_dump.h" 
 
index 08ba0ebe4d0b566f93474eabedaf70e7dd9b88cb..9396522961e62669c7a9979e57f888f35d518061 100644 (file)
       pipe_put_tile_rgba($self, x, y, w, h, rgba);
    }
 
+   %cstring_output_allocate_size(char **STRING, int *LENGTH, free(*$1));
+   void
+   get_tile_rgba8(unsigned x, unsigned y, unsigned w, unsigned h, char **STRING, int *LENGTH) 
+   {
+      unsigned surface_usage;
+      float *rgba;
+      unsigned char *rgba8;
+      unsigned i, j, k;
+
+      *LENGTH = 0;
+      *STRING = NULL;
+      
+      if (!$self)
+         return;
+
+      *LENGTH = h*w*4;
+      *STRING = (char *) malloc(*LENGTH);
+      if(!*STRING)
+         return;
+      
+      rgba = malloc(w*4*sizeof(float));
+      if(!rgba)
+         return;
+      
+      rgba8 = (unsigned char *) *STRING;
+
+      /* XXX: force mappable surface */
+      surface_usage = $self->usage;
+      $self->usage |= PIPE_BUFFER_USAGE_CPU_READ;
+
+      for(j = 0; j < h; ++j) {
+         pipe_get_tile_rgba($self,
+                            x, y + j, w, 1,
+                            rgba);
+         for(i = 0; i < w; ++i)
+            for(k = 0; k <4; ++k)
+               rgba8[j*w*4 + i*4 + k] = float_to_ubyte(rgba[i*4 + k]);
+      }
+      
+      $self->usage = surface_usage;
+      
+      free(rgba);
+   }
+
    void
    get_tile_z(unsigned x, unsigned y, unsigned w, unsigned h, unsigned *z) {
       pipe_get_tile_z($self, x, y, w, h, z);
index 2a75ee82d5d49cb96a6a3f177635cf9423c2893b..e6999a2211ea58459c41841429ac3c206b76979c 100755 (executable)
@@ -44,20 +44,10 @@ except ImportError:
 
 
 def make_image(surface):
-    pixels = gallium.FloatArray(surface.height*surface.width*4)
-    surface.get_tile_rgba(0, 0, surface.width, surface.height, pixels)
+    data = surface.get_tile_rgba8(0, 0, surface.width, surface.height)
 
     import Image
-    outimage = Image.new(
-        mode='RGB',
-        size=(surface.width, surface.height),
-        color=(0,0,0))
-    outpixels = outimage.load()
-    for y in range(0, surface.height):
-        for x in range(0, surface.width):
-            offset = (y*surface.width + x)*4
-            r, g, b, a = [int(pixels[offset + ch]*255) for ch in range(4)]
-            outpixels[x, y] = r, g, b
+    outimage = Image.fromstring('RGBA', (surface.width, surface.height), data, "raw", 'RGBA', 0, 1)
     return outimage
 
 def save_image(filename, surface):
index 193479f7d6cc31d08416e20cbedb35452de84a9b..d3ccb6c2f466324eef1acc0b14453c7f26c842e8 100644 (file)
@@ -31,20 +31,10 @@ from gallium import *
 
 
 def make_image(surface):
-    pixels = FloatArray(surface.height*surface.width*4)
-    surface.get_tile_rgba(0, 0, surface.width, surface.height, pixels)
+    data = surface.get_tile_rgba8(0, 0, surface.width, surface.height)
 
     import Image
-    outimage = Image.new(
-        mode='RGB',
-        size=(surface.width, surface.height),
-        color=(0,0,0))
-    outpixels = outimage.load()
-    for y in range(0, surface.height):
-        for x in range(0, surface.width):
-            offset = (y*surface.width + x)*4
-            r, g, b, a = [int(pixels[offset + ch]*255) for ch in range(4)]
-            outpixels[x, y] = r, g, b
+    outimage = Image.fromstring('RGBA', (surface.width, surface.height), data, "raw", 'RGBA', 0, 1)
     return outimage
 
 def save_image(filename, surface):