]> git.notmuchmail.org Git - apitrace/blobdiff - retrace/glstate_images.cpp
glstate: Use stringstream for holding the temporary PNG images.
[apitrace] / retrace / glstate_images.cpp
index 6390980966a91c8e17048718b20f01630e6aa338..295ebe8797d3396922533154731d8b910c5fef5e 100644 (file)
@@ -28,6 +28,7 @@
 
 #include <algorithm>
 #include <iostream>
+#include <sstream>
 
 #include "image.hpp"
 #include "json.hpp"
@@ -88,6 +89,31 @@ struct ImageDesc
 };
 
 
+/**
+ * Sames as enumToString, but with special provision to handle formatsLUMINANCE_ALPHA.
+ *
+ * OpenGL 2.1 specification states that "internalFormat may (for backwards
+ * compatibility with the 1.0 version of the GL) also take on the integer
+ * values 1, 2, 3, and 4, which are equivalent to symbolic constants LUMINANCE,
+ * LUMINANCE ALPHA, RGB, and RGBA respectively". 
+ */
+const char *
+formatToString(GLenum internalFormat) {
+    switch (internalFormat) {
+    case 1:
+        return "GL_LUMINANCE";
+    case 2:
+        return "GL_LUMINANCE_ALPHA";
+    case 3:
+        return "GL_RGB";
+    case 4:
+        return "GL_RGBA";
+    default:
+        return enumToString(internalFormat);
+    }
+}
+
+
 /**
  * OpenGL ES does not support glGetTexLevelParameteriv, but it is possible to
  * probe whether a texture has a given size by crafting a dummy glTexSubImage()
@@ -232,6 +258,8 @@ getActiveTextureLevelDescOES(Context &context, GLenum target, GLint level, Image
 static inline bool
 getActiveTextureLevelDesc(Context &context, GLenum target, GLint level, ImageDesc &desc)
 {
+    assert(target != GL_TEXTURE_CUBE_MAP);
+
     if (context.ES) {
         return getActiveTextureLevelDescOES(context, target, level, desc);
     }
@@ -382,17 +410,17 @@ dumpActiveTextureLevel(JSONWriter &json, Context &context, GLenum target, GLint
     // Tell the GUI this is no ordinary object, but an image
     json.writeStringMember("__class__", "image");
 
-    json.writeNumberMember("__width__", desc.width);
-    json.writeNumberMember("__height__", desc.height);
-    json.writeNumberMember("__depth__", desc.depth);
+    json.writeIntMember("__width__", desc.width);
+    json.writeIntMember("__height__", desc.height);
+    json.writeIntMember("__depth__", desc.depth);
 
-    json.writeStringMember("__format__", enumToString(desc.internalFormat));
+    json.writeStringMember("__format__", formatToString(desc.internalFormat));
 
     // Hardcoded for now, but we could chose types more adequate to the
     // texture internal format
     json.writeStringMember("__type__", "uint8");
     json.writeBoolMember("__normalized__", true);
-    json.writeNumberMember("__channels__", channels);
+    json.writeIntMember("__channels__", channels);
 
     GLubyte *pixels = new GLubyte[desc.depth*desc.width*desc.height*channels];
 
@@ -407,11 +435,10 @@ dumpActiveTextureLevel(JSONWriter &json, Context &context, GLenum target, GLint
     context.restorePixelPackState();
 
     json.beginMember("__data__");
-    char *pngBuffer;
-    int pngBufferSize;
-    image::writePixelsToBuffer(pixels, desc.width, desc.height, channels, true, &pngBuffer, &pngBufferSize);
-    json.writeBase64(pngBuffer, pngBufferSize);
-    free(pngBuffer);
+    std::stringstream ss;
+    image::writePixelsToBuffer(ss, pixels, desc.width, desc.depth * desc.height, channels, true);
+    const std::string & s = ss.str();
+    json.writeBase64(s.data(), s.size());
     json.endMember(); // __data__
 
     delete [] pixels;
@@ -431,15 +458,18 @@ dumpTexture(JSONWriter &json, Context &context, GLenum target, GLenum binding)
     GLint level = 0;
     do {
         ImageDesc desc;
-        if (!getActiveTextureLevelDesc(context, target, level, desc)) {
-            break;
-        }
 
         if (target == GL_TEXTURE_CUBE_MAP) {
             for (int face = 0; face < 6; ++face) {
+                if (!getActiveTextureLevelDesc(context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, desc)) {
+                    return;
+                }
                 dumpActiveTextureLevel(json, context, GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level);
             }
         } else {
+            if (!getActiveTextureLevelDesc(context, target, level, desc)) {
+                return;
+            }
             dumpActiveTextureLevel(json, context, target, level);
         }
 
@@ -829,17 +859,17 @@ dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format,
     // Tell the GUI this is no ordinary object, but an image
     json.writeStringMember("__class__", "image");
 
-    json.writeNumberMember("__width__", width);
-    json.writeNumberMember("__height__", height);
-    json.writeNumberMember("__depth__", 1);
+    json.writeIntMember("__width__", width);
+    json.writeIntMember("__height__", height);
+    json.writeIntMember("__depth__", 1);
 
-    json.writeStringMember("__format__", enumToString(internalFormat));
+    json.writeStringMember("__format__", formatToString(internalFormat));
 
     // Hardcoded for now, but we could chose types more adequate to the
     // texture internal format
     json.writeStringMember("__type__", "uint8");
     json.writeBoolMember("__normalized__", true);
-    json.writeNumberMember("__channels__", channels);
+    json.writeIntMember("__channels__", channels);
 
     GLenum type = GL_UNSIGNED_BYTE;
 
@@ -860,13 +890,12 @@ dumpReadBufferImage(JSONWriter &json, GLint width, GLint height, GLenum format,
     context.restorePixelPackState();
 
     json.beginMember("__data__");
-    char *pngBuffer;
-    int pngBufferSize;
-    image::writePixelsToBuffer(pixels, width, height, channels, true, &pngBuffer, &pngBufferSize);
+    std::stringstream ss;
+    image::writePixelsToBuffer(ss, pixels, width, height, channels, true);
     //std::cerr <<" Before = "<<(width * height * channels * sizeof *pixels)
     //          <<", after = "<<pngBufferSize << ", ratio = " << double(width * height * channels * sizeof *pixels)/pngBufferSize;
-    json.writeBase64(pngBuffer, pngBufferSize);
-    free(pngBuffer);
+    const std::string & s = ss.str();
+    json.writeBase64(s.data(), s.size());
     json.endMember(); // __data__
 
     delete [] pixels;