Bug 1031079 - Get rid of the Thebes backed gfxContexts in gfxASurface, removing gfxASurface::WriteAsPNG_internal and its consumers. r=mattwoodrow
authorJonathan Watt <jwatt@jwatt.org>
Fri, 27 Jun 2014 12:19:40 +0100
changeset 213139 7e8a25258eb937603280b31c231d64b100c917a7
parent 213138 a9321d58fc9807b7e0327837320a1cdc7272f83b
child 213140 2c64038181063d93859477d05edd2f2c0fd3e173
push id515
push userraliiev@mozilla.com
push dateMon, 06 Oct 2014 12:51:51 +0000
treeherdermozilla-release@267c7a481bef [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmattwoodrow
bugs1031079
milestone33.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1031079 - Get rid of the Thebes backed gfxContexts in gfxASurface, removing gfxASurface::WriteAsPNG_internal and its consumers. r=mattwoodrow
gfx/thebes/gfxASurface.cpp
gfx/thebes/gfxASurface.h
gfx/thebes/gfxContext.cpp
gfx/thebes/gfxContext.h
--- a/gfx/thebes/gfxASurface.cpp
+++ b/gfx/thebes/gfxASurface.cpp
@@ -679,183 +679,16 @@ gfxASurface::BytesPerPixel(gfxImageForma
     case gfxImageFormat::Unknown:
     default:
       NS_NOTREACHED("Not really sure what you want me to say here");
       return 0;
   }
 }
 
 void
-gfxASurface::WriteAsPNG(const char* aFile)
-{
-    FILE *file = fopen(aFile, "wb");
-    if (file) {
-      WriteAsPNG_internal(file, true);
-      fclose(file);
-    } else {
-      NS_WARNING("Failed to create file!\n");
-    }
-}
-
-void
-gfxASurface::CopyAsDataURL()
-{
-  WriteAsPNG_internal(nullptr, false);
-}
-
-/**
- * Write to a PNG file. If aBinary is true, then it is written
- * as binary, otherwise as a data URL. If no file is specified then
- * data is copied to the clipboard (must not be binary!).
- */
-void
-gfxASurface::WriteAsPNG_internal(FILE* aFile, bool aBinary)
-{
-  nsRefPtr<gfxImageSurface> imgsurf = GetAsImageSurface();
-  nsIntSize size;
-
-  // FIXME/bug 831898: hack r5g6b5 for now.
-  if (!imgsurf || imgsurf->Format() == gfxImageFormat::RGB16_565) {
-    size = GetSize();
-    if (size.width == -1 && size.height == -1) {
-      printf("Could not determine surface size\n");
-      return;
-    }
-
-    imgsurf =
-      new gfxImageSurface(nsIntSize(size.width, size.height),
-                          gfxImageFormat::ARGB32);
-
-    if (!imgsurf || imgsurf->CairoStatus()) {
-      printf("Could not allocate image surface\n");
-      return;
-    }
-
-    nsRefPtr<gfxContext> ctx = new gfxContext(imgsurf);
-    if (!ctx || ctx->HasError()) {
-      printf("Could not allocate image context\n");
-      return;
-    }
-
-    ctx->SetOperator(gfxContext::OPERATOR_SOURCE);
-    ctx->SetSource(this, gfxPoint(0, 0));
-    ctx->Paint();
-  }
-  size = imgsurf->GetSize();
-
-  nsCOMPtr<imgIEncoder> encoder =
-    do_CreateInstance("@mozilla.org/image/encoder;2?type=image/png");
-  if (!encoder) {
-    int32_t w = std::min(size.width, 8);
-    int32_t h = std::min(size.height, 8);
-    printf("Could not create encoder. Printing %dx%d pixels.\n", w, h);
-    for (int32_t y = 0; y < h; ++y) {
-      for (int32_t x = 0; x < w; ++x) {
-        printf("%x ", reinterpret_cast<uint32_t*>(imgsurf->Data())[y*imgsurf->Stride()+ x]);
-      }
-    }
-    return;
-  }
-
-  nsresult rv = encoder->InitFromData(imgsurf->Data(),
-                                      size.width * size.height * 4,
-                                      size.width,
-                                      size.height,
-                                      imgsurf->Stride(),
-                                      imgIEncoder::INPUT_FORMAT_HOSTARGB,
-                                      NS_LITERAL_STRING(""));
-  if (NS_FAILED(rv))
-    return;
-
-  nsCOMPtr<nsIInputStream> imgStream;
-  CallQueryInterface(encoder.get(), getter_AddRefs(imgStream));
-  if (!imgStream)
-    return;
-
-  uint64_t bufSize64;
-  rv = imgStream->Available(&bufSize64);
-  if (NS_FAILED(rv))
-    return;
-
-  if (bufSize64 > UINT32_MAX - 16)
-    return;
-
-  uint32_t bufSize = (uint32_t)bufSize64;
-
-  // ...leave a little extra room so we can call read again and make sure we
-  // got everything. 16 bytes for better padding (maybe)
-  bufSize += 16;
-  uint32_t imgSize = 0;
-  char* imgData = (char*)moz_malloc(bufSize);
-  if (!imgData)
-    return;
-  uint32_t numReadThisTime = 0;
-  while ((rv = imgStream->Read(&imgData[imgSize],
-                               bufSize - imgSize,
-                               &numReadThisTime)) == NS_OK && numReadThisTime > 0)
-  {
-    imgSize += numReadThisTime;
-    if (imgSize == bufSize) {
-      // need a bigger buffer, just double
-      bufSize *= 2;
-      char* newImgData = (char*)moz_realloc(imgData, bufSize);
-      if (!newImgData) {
-        moz_free(imgData);
-        return;
-      }
-      imgData = newImgData;
-    }
-  }
-
-  if (aBinary) {
-    if (aFile) {
-      fwrite(imgData, 1, imgSize, aFile);
-    } else {
-      NS_WARNING("Can't write binary image data without a file!");
-    }
-    return;
-  }
-
-  // base 64, result will be null-terminated
-  nsCString encodedImg;
-  rv = Base64Encode(Substring(imgData, imgSize), encodedImg);
-  moz_free(imgData);
-  if (NS_FAILED(rv)) // not sure why this would fail
-    return;
-
-  nsCString string("data:image/png;base64,");
-  string.Append(encodedImg);
-
-  if (aFile) {
-#ifdef ANDROID
-     if (aFile == stdout || aFile == stderr) {
-       // ADB logcat cuts off long strings so we will break it down
-       const char* cStr = string.BeginReading();
-       size_t len = strlen(cStr);
-       while (true) {
-         printf_stderr("IMG: %.140s\n", cStr);
-         if (len <= 140)
-           break;
-         len -= 140;
-         cStr += 140;
-       }
-     }
-#endif
-    fprintf_stderr(aFile, "%s", string.BeginReading());
-  } else {
-    nsCOMPtr<nsIClipboardHelper> clipboard(do_GetService("@mozilla.org/widget/clipboardhelper;1", &rv));
-    if (clipboard) {
-      clipboard->CopyString(NS_ConvertASCIItoUTF16(string), nullptr);
-    }
-  }
-
-  return;
-}
-
-void
 gfxASurface::SetOpaqueRect(const gfxRect& aRect)
 {
     if (aRect.IsEmpty()) {
         mOpaqueRect = nullptr;
     } else if (!!mOpaqueRect) {
         *mOpaqueRect = aRect;
     } else {
         mOpaqueRect = new gfxRect(aRect);
--- a/gfx/thebes/gfxASurface.h
+++ b/gfx/thebes/gfxASurface.h
@@ -170,32 +170,16 @@ public:
      * process's heap.
      */
     virtual gfxMemoryLocation GetMemoryLocation() const;
 
     static int32_t BytePerPixelFromFormat(gfxImageFormat format);
 
     virtual const nsIntSize GetSize() const;
 
-    /**
-     * Debug functions to encode the current image as a PNG and export it.
-     */
-
-    /**
-     * Writes a binary PNG file.
-     */
-    void WriteAsPNG(const char* aFile);
-
-    /**
-     * Copy a PNG encoded Data URL to the clipboard.
-     */
-    void CopyAsDataURL();
-
-    void WriteAsPNG_internal(FILE* aFile, bool aBinary);
-
     void SetOpaqueRect(const gfxRect& aRect);
 
     const gfxRect& GetOpaqueRect() {
         if (!!mOpaqueRect)
             return *mOpaqueRect;
         return GetEmptyOpaqueRect();
     }
 
--- a/gfx/thebes/gfxContext.cpp
+++ b/gfx/thebes/gfxContext.cpp
@@ -1953,43 +1953,41 @@ gfxContext::RoundedRectangle(const gfxRe
                      ToSize(corners[NS_CORNER_BOTTOM_LEFT]) };
     AppendRoundedRectToPath(mPathBuilder, ToRect(rect), radii, draw_clockwise);
   }
 }
 
 #ifdef MOZ_DUMP_PAINTING
 void
 gfxContext::WriteAsPNG(const char* aFile)
-{ 
-  nsRefPtr<gfxASurface> surf = CurrentSurface();
-  if (surf) {
-    surf->WriteAsPNG(aFile);
+{
+  if (mDT) {
+    gfxUtils::WriteAsPNG(mDT, aFile);
   } else {
-    NS_WARNING("No surface found!");
+    NS_WARNING("No DrawTarget found!");
   }
 }
 
 void 
-gfxContext::DumpAsDataURL()
+gfxContext::DumpAsDataURI()
 {
   if (mDT) {
     gfxUtils::DumpAsDataURI(mDT);
   } else {
     NS_WARNING("No DrawTarget found!");
   }
 }
 
 void 
-gfxContext::CopyAsDataURL()
-{ 
-  nsRefPtr<gfxASurface> surf = CurrentSurface();
-  if (surf) {
-    surf->CopyAsDataURL();
+gfxContext::CopyAsDataURI()
+{
+  if (mDT) {
+    gfxUtils::CopyAsDataURI(mDT);
   } else {
-    NS_WARNING("No surface found!");
+    NS_WARNING("No DrawTarget found!");
   }
 }
 #endif
 
 void
 gfxContext::EnsurePath()
 {
   if (mPathBuilder) {
--- a/gfx/thebes/gfxContext.h
+++ b/gfx/thebes/gfxContext.h
@@ -702,22 +702,22 @@ public:
     /**
      * Writes a binary PNG file.
      */
     void WriteAsPNG(const char* aFile);
 
     /**
      * Write as a PNG encoded Data URL to stdout.
      */
-    void DumpAsDataURL();
+    void DumpAsDataURI();
 
     /**
      * Copy a PNG encoded Data URL to the clipboard.
      */
-    void CopyAsDataURL();
+    void CopyAsDataURI();
 #endif
 
     static mozilla::gfx::UserDataKey sDontUseAsSourceKey;
 
 private:
     ~gfxContext();
 
   friend class GeneralPattern;