Bug 1105834 - Part 3: Add lz4 support. r=jrmuizel
☠☠ backed out by f2bd10f0159c ☠ ☠
authorBenoit Girard <b56girard@gmail.com>
Mon, 08 Dec 2014 14:42:33 -0500
changeset 246278 33592fd41f1f77c7917f7e8bac5c03dd37e26e79
parent 246277 c3e505887e9fdbd4ceda38eee644f3f028052bea
child 246279 35f5ec149ad5343e6f62c9f6788ddb952f9732fb
push idunknown
push userunknown
push dateunknown
reviewersjrmuizel
bugs1105834
milestone37.0a1
Bug 1105834 - Part 3: Add lz4 support. r=jrmuizel
gfx/layers/composite/CompositableHost.cpp
gfx/layers/composite/TextureHost.cpp
gfx/thebes/gfxUtils.cpp
gfx/thebes/gfxUtils.h
--- a/gfx/layers/composite/CompositableHost.cpp
+++ b/gfx/layers/composite/CompositableHost.cpp
@@ -213,22 +213,17 @@ CompositableHost::DumpTextureHost(std::s
 {
   if (!aTexture) {
     return;
   }
   RefPtr<gfx::DataSourceSurface> dSurf = aTexture->GetAsSurface();
   if (!dSurf) {
     return;
   }
-  gfxPlatform *platform = gfxPlatform::GetPlatform();
-  RefPtr<gfx::DrawTarget> dt = platform->CreateDrawTargetForData(dSurf->GetData(),
-                                                                 dSurf->GetSize(),
-                                                                 dSurf->Stride(),
-                                                                 dSurf->GetFormat());
-  aStream << gfxUtils::GetAsDataURI(dt).get();
+  aStream << gfxUtils::GetAsLZ4Base64Str(dSurf).get();
 }
 #endif
 
 namespace CompositableMap {
 
 typedef std::map<uint64_t, PCompositableParent*> CompositableMap_t;
 static CompositableMap_t* sCompositableMap = nullptr;
 bool IsCreated() {
--- a/gfx/layers/composite/TextureHost.cpp
+++ b/gfx/layers/composite/TextureHost.cpp
@@ -330,17 +330,17 @@ TextureHost::PrintInfo(std::stringstream
 #ifdef MOZ_DUMP_PAINTING
   if (gfxPrefs::LayersDumpTexture()) {
     nsAutoCString pfx(aPrefix);
     pfx += "  ";
 
     aStream << "\n" << pfx.get() << "Surface: ";
     RefPtr<gfx::DataSourceSurface> dSurf = GetAsSurface();
     if (dSurf) {
-      aStream << gfxUtils::GetAsDataURI(dSurf).get();
+      aStream << gfxUtils::GetAsLZ4Base64Str(dSurf).get();
     }
   }
 #endif
 }
 
 TextureSource::TextureSource()
 : mCompositableCount(0)
 {
--- a/gfx/thebes/gfxUtils.cpp
+++ b/gfx/thebes/gfxUtils.cpp
@@ -38,16 +38,20 @@
 
 using namespace mozilla;
 using namespace mozilla::image;
 using namespace mozilla::layers;
 using namespace mozilla::gfx;
 
 #include "DeprecatedPremultiplyTables.h"
 
+#undef compress
+#include "mozilla/Compression.h"
+
+using namespace mozilla::Compression;
 extern "C" {
 
 /**
  * Dump a raw image to the default log.  This function is exported
  * from libxul, so it can be called from any library in addition to
  * (of course) from a debugger.
  *
  * Note: this helper currently assumes that all 2-bytepp images are
@@ -1374,16 +1378,42 @@ gfxUtils::DumpAsDataURI(DrawTarget* aDT,
   if (surface) {
     DumpAsDataURI(surface, aFile);
   } else {
     NS_WARNING("Failed to get surface!");
   }
 }
 
 /* static */ nsCString
+gfxUtils::GetAsLZ4Base64Str(DataSourceSurface* aSourceSurface)
+{
+  int32_t dataSize = aSourceSurface->GetSize().height * aSourceSurface->Stride();
+  auto compressedData = MakeUnique<char[]>(LZ4::maxCompressedSize(dataSize));
+  if (compressedData) {
+    int nDataSize = LZ4::compress((char*)aSourceSurface->GetData(),
+                                  dataSize,
+                                  compressedData.get());
+    if (nDataSize > 0) {
+      nsCString encodedImg;
+      nsresult rv = Base64Encode(Substring(compressedData.get(), nDataSize), encodedImg);
+      if (rv == NS_OK) {
+        nsCString string("");
+        string.AppendPrintf("data:image/lz4bgra;base64,%i,%i,%i,",
+                             aSourceSurface->GetSize().width,
+                             aSourceSurface->Stride(),
+                             aSourceSurface->GetSize().height);
+        string.Append(encodedImg);
+        return string;
+      }
+    }
+  }
+  return nsCString("");
+}
+
+/* static */ nsCString
 gfxUtils::GetAsDataURI(DrawTarget* aDT)
 {
   RefPtr<SourceSurface> surface = aDT->Snapshot();
   if (surface) {
     return EncodeSourceSurfaceAsPNGURI(surface);
   } else {
     NS_WARNING("Failed to get surface!");
     return nsCString("");
--- a/gfx/thebes/gfxUtils.h
+++ b/gfx/thebes/gfxUtils.h
@@ -278,16 +278,17 @@ public:
         DumpAsDataURI(aSourceSurface, stdout);
     }
     static void DumpAsDataURI(DrawTarget* aDT, FILE* aFile);
     static inline void DumpAsDataURI(DrawTarget* aDT) {
         DumpAsDataURI(aDT, stdout);
     }
     static nsCString GetAsDataURI(SourceSurface* aSourceSurface);
     static nsCString GetAsDataURI(DrawTarget* aDT);
+    static nsCString GetAsLZ4Base64Str(DataSourceSurface* aSourceSurface);
 
     /**
      * Copy to the clipboard as a PNG encoded Data URL.
      */
     static void CopyAsDataURI(SourceSurface* aSourceSurface);
     static void CopyAsDataURI(DrawTarget* aDT);
 
 #ifdef MOZ_DUMP_PAINTING