Bug 1444430 - Extend the API of gfxUtils::EncodeSourceSurface to support encoding into a string. r?jrmuizel draft
authorMarkus Stange <mstange@themasta.com>
Thu, 12 Apr 2018 15:20:14 -0400
changeset 781364 5e53627e64e897a82cfe7a3a6455f66778bf1fef
parent 781246 851079810914040fd0673646e55b3441e1ee21c4
child 781365 6abf2a39780c9a5744d2f16495cd7c28470c4d55
child 781544 15b8d2e91339d75a1352989615a8f96324d3bdf6
push id106284
push userbmo:mstange@themasta.com
push dateThu, 12 Apr 2018 21:31:17 +0000
reviewersjrmuizel
bugs1444430
milestone61.0a1
Bug 1444430 - Extend the API of gfxUtils::EncodeSourceSurface to support encoding into a string. r?jrmuizel MozReview-Commit-ID: KxXkOmat9RU
gfx/thebes/gfxUtils.cpp
gfx/thebes/gfxUtils.h
--- a/gfx/thebes/gfxUtils.cpp
+++ b/gfx/thebes/gfxUtils.cpp
@@ -933,23 +933,23 @@ gfxUtils::GetColorForFrameNumber(uint64_
         colors[i++] = gfx::Color::FromABGR(0xff999999);
         MOZ_ASSERT(i == sNumFrameColors);
         initialized = true;
     }
 
     return colors[aFrameNumber % sNumFrameColors];
 }
 
-static nsresult
-EncodeSourceSurfaceInternal(SourceSurface* aSurface,
-                           const nsACString& aMimeType,
-                           const nsAString& aOutputOptions,
-                           gfxUtils::BinaryOrData aBinaryOrData,
-                           FILE* aFile,
-                           nsACString* aStrOut)
+/* static */ nsresult
+gfxUtils::EncodeSourceSurface(SourceSurface* aSurface,
+                              const nsACString& aMimeType,
+                              const nsAString& aOutputOptions,
+                              BinaryOrData aBinaryOrData,
+                              FILE* aFile,
+                              nsACString* aStrOut)
 {
   MOZ_ASSERT(aBinaryOrData == gfxUtils::eDataURIEncode || aFile || aStrOut,
              "Copying binary encoding to clipboard not currently supported");
 
   const IntSize size = aSurface->GetSize();
   if (size.IsEmpty()) {
     return NS_ERROR_INVALID_ARG;
   }
@@ -1088,33 +1088,22 @@ EncodeSourceSurfaceInternal(SourceSurfac
   }
   return NS_OK;
 }
 
 static nsCString
 EncodeSourceSurfaceAsPNGURI(SourceSurface* aSurface)
 {
   nsCString string;
-  EncodeSourceSurfaceInternal(aSurface, NS_LITERAL_CSTRING("image/png"),
-                              EmptyString(), gfxUtils::eDataURIEncode,
-                              nullptr, &string);
+  gfxUtils::EncodeSourceSurface(aSurface, NS_LITERAL_CSTRING("image/png"),
+                                EmptyString(), gfxUtils::eDataURIEncode,
+                                nullptr, &string);
   return string;
 }
 
-/* static */ nsresult
-gfxUtils::EncodeSourceSurface(SourceSurface* aSurface,
-                              const nsACString& aMimeType,
-                              const nsAString& aOutputOptions,
-                              BinaryOrData aBinaryOrData,
-                              FILE* aFile)
-{
-  return EncodeSourceSurfaceInternal(aSurface, aMimeType, aOutputOptions,
-                                     aBinaryOrData, aFile, nullptr);
-}
-
 // https://jdashg.github.io/misc/colors/from-coeffs.html
 const float kBT601NarrowYCbCrToRGB_RowMajor[16] = {
   1.16438f, 0.00000f, 1.59603f,-0.87420f,
   1.16438f,-0.39176f,-0.81297f, 0.53167f,
   1.16438f, 2.01723f, 0.00000f,-1.08563f,
   0.00000f, 0.00000f, 0.00000f, 1.00000f
 };
 const float kBT709NarrowYCbCrToRGB_RowMajor[16] = {
--- a/gfx/thebes/gfxUtils.h
+++ b/gfx/thebes/gfxUtils.h
@@ -216,42 +216,46 @@ public:
 
     enum BinaryOrData {
         eBinaryEncode,
         eDataURIEncode
     };
 
     /**
      * Encodes the given surface to PNG/JPEG/BMP/etc. using imgIEncoder.
+     * If both aFile and aString are null, the encoded data is copied to the
+     * clipboard.
      *
      * @param aMimeType The MIME-type of the image type that the surface is to
      *   be encoded to. Used to create an appropriate imgIEncoder instance to
      *   do the encoding.
      *
      * @param aOutputOptions Passed directly to imgIEncoder::InitFromData as
      *   the value of the |outputOptions| parameter. Callers are responsible
      *   for making sure that this is a sane value for the passed MIME-type
      *   (i.e. for the type of encoder that will be created).
      *
      * @aBinaryOrData Flag used to determine if the surface is simply encoded
      *   to the requested binary image format, or if the binary image is
      *   further converted to base-64 and written out as a 'data:' URI.
      *
-     * @aFile If specified, the encoded data is written out to aFile, otherwise
-     *   it is copied to the clipboard.
+     * @aFile If specified, the encoded data is written out to aFile.
+     *
+     * @aString If specified, the encoded data is written out to aString.
      *
      * TODO: Copying to the clipboard as a binary file is not currently
      * supported.
      */
     static nsresult
     EncodeSourceSurface(SourceSurface* aSurface,
                         const nsACString& aMimeType,
                         const nsAString& aOutputOptions,
                         BinaryOrData aBinaryOrData,
-                        FILE* aFile);
+                        FILE* aFile,
+                        nsACString* aString = nullptr);
 
     /**
      * Write as a PNG file to the path aFile.
      */
     static void WriteAsPNG(SourceSurface* aSurface, const nsAString& aFile);
     static void WriteAsPNG(SourceSurface* aSurface, const char* aFile);
     static void WriteAsPNG(DrawTarget* aDT, const nsAString& aFile);
     static void WriteAsPNG(DrawTarget* aDT, const char* aFile);