Bug 1542344 - fix API mismatches in ImageEncoder; r=mccr8
authorNathan Froyd <froydnj@mozilla.com>
Mon, 08 Apr 2019 01:51:17 +0000
changeset 468383 1c0a3a443f9734d543535c61bdc3ceb1ad3002b3
parent 468382 eade880c4185727f7e2b301d6efd3c825b58949e
child 468384 95b3298fd2d462ecf0aab134289838317009fb3c
push id35835
push useraciure@mozilla.com
push dateMon, 08 Apr 2019 19:00:29 +0000
treeherdermozilla-central@40456af7da1c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmccr8
bugs1542344
milestone68.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 1542344 - fix API mismatches in ImageEncoder; r=mccr8 ImageEncoder::ExtractDataInternal takes a `const nsAString&` for its options, but flattens it into a null-terminated `nsString` so callees can take a `char16_t*`. But nearly all of those callees eventually wind up calling ImageEncoder::GetInputStream, which just constructs an `nsDependentString` from the passed character pointer. There's no reason to do all this extra work. We can just pass the original options reference all the way through the stack and avoid needless conversions. Differential Revision: https://phabricator.services.mozilla.com/D26353
dom/base/ImageEncoder.cpp
dom/base/ImageEncoder.h
dom/canvas/CanvasRenderingContext2D.cpp
dom/canvas/CanvasRenderingContext2D.h
dom/canvas/ImageBitmapRenderingContext.cpp
dom/canvas/ImageBitmapRenderingContext.h
dom/canvas/WebGLContext.cpp
dom/canvas/WebGLContext.h
dom/canvas/nsICanvasRenderingContextInternal.h
gfx/layers/AsyncCanvasRenderer.cpp
gfx/layers/AsyncCanvasRenderer.h
gfx/thebes/gfxUtils.cpp
gfx/thebes/gfxUtils.h
--- a/dom/base/ImageEncoder.cpp
+++ b/dom/base/ImageEncoder.cpp
@@ -280,21 +280,21 @@ nsresult ImageEncoder::ExtractDataAsync(
       aFormat, aSize, aUsePlaceholder, aUsingCustomOptions);
   return sThreadPool->Dispatch(event, NS_DISPATCH_NORMAL);
 }
 
 /*static*/
 nsresult ImageEncoder::GetInputStream(int32_t aWidth, int32_t aHeight,
                                       uint8_t* aImageBuffer, int32_t aFormat,
                                       imgIEncoder* aEncoder,
-                                      const char16_t* aEncoderOptions,
+                                      const nsAString& aEncoderOptions,
                                       nsIInputStream** aStream) {
   nsresult rv = aEncoder->InitFromData(aImageBuffer, aWidth * aHeight * 4,
                                        aWidth, aHeight, aWidth * 4, aFormat,
-                                       nsDependentString(aEncoderOptions));
+                                       aEncoderOptions);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<imgIEncoder> encoder(aEncoder);
   encoder.forget(aStream);
   return NS_OK;
 }
 
 /* static */
@@ -314,26 +314,24 @@ nsresult ImageEncoder::ExtractDataIntern
   nsresult rv;
   if (aImageBuffer && !aUsePlaceholder) {
     if (BufferSizeFromDimensions(aSize.width, aSize.height, 4) == 0) {
       return NS_ERROR_INVALID_ARG;
     }
 
     rv = ImageEncoder::GetInputStream(
         aSize.width, aSize.height, aImageBuffer, aFormat, aEncoder,
-        nsPromiseFlatString(aOptions).get(), getter_AddRefs(imgStream));
+        aOptions, getter_AddRefs(imgStream));
   } else if (aContext && !aUsePlaceholder) {
     NS_ConvertUTF16toUTF8 encoderType(aType);
-    rv = aContext->GetInputStream(encoderType.get(),
-                                  nsPromiseFlatString(aOptions).get(),
+    rv = aContext->GetInputStream(encoderType.get(), aOptions,
                                   getter_AddRefs(imgStream));
   } else if (aRenderer && !aUsePlaceholder) {
     NS_ConvertUTF16toUTF8 encoderType(aType);
-    rv = aRenderer->GetInputStream(encoderType.get(),
-                                   nsPromiseFlatString(aOptions).get(),
+    rv = aRenderer->GetInputStream(encoderType.get(), aOptions,
                                    getter_AddRefs(imgStream));
   } else if (aImage && !aUsePlaceholder) {
     // It is safe to convert PlanarYCbCr format from YUV to RGB off-main-thread.
     // Other image formats could have problem to convert format off-main-thread.
     // So here it uses a help function GetBRGADataSourceSurfaceSync() to convert
     // format on main thread.
     if (aImage->GetFormat() == ImageFormat::PLANAR_YCBCR) {
       nsTArray<uint8_t> data;
--- a/dom/base/ImageEncoder.h
+++ b/dom/base/ImageEncoder.h
@@ -73,17 +73,17 @@ class ImageEncoder {
       EncodeCompleteCallback* aEncodeCallback);
 
   // Gives you a stream containing the image represented by aImageBuffer.
   // The format is given in aFormat, for example
   // imgIEncoder::INPUT_FORMAT_HOSTARGB.
   static nsresult GetInputStream(int32_t aWidth, int32_t aHeight,
                                  uint8_t* aImageBuffer, int32_t aFormat,
                                  imgIEncoder* aEncoder,
-                                 const char16_t* aEncoderOptions,
+                                 const nsAString& aEncoderOptions,
                                  nsIInputStream** aStream);
 
  private:
   // When called asynchronously, aContext and aRenderer are null.
   static nsresult ExtractDataInternal(
       const nsAString& aType, const nsAString& aOptions, uint8_t* aImageBuffer,
       int32_t aFormat, const nsIntSize aSize, bool aUsePlaceholder,
       layers::Image* aImage, nsICanvasRenderingContextInternal* aContext,
--- a/dom/canvas/CanvasRenderingContext2D.cpp
+++ b/dom/canvas/CanvasRenderingContext2D.cpp
@@ -1643,17 +1643,17 @@ nsString CanvasRenderingContext2D::GetHi
       return info.mId;
     }
   }
   return nsString();
 }
 
 NS_IMETHODIMP
 CanvasRenderingContext2D::GetInputStream(const char* aMimeType,
-                                         const char16_t* aEncoderOptions,
+                                         const nsAString& aEncoderOptions,
                                          nsIInputStream** aStream) {
   nsCString enccid("@mozilla.org/image/encoder;2?type=");
   enccid += aMimeType;
   nsCOMPtr<imgIEncoder> encoder = do_CreateInstance(enccid.get());
   if (!encoder) {
     return NS_ERROR_FAILURE;
   }
 
--- a/dom/canvas/CanvasRenderingContext2D.h
+++ b/dom/canvas/CanvasRenderingContext2D.h
@@ -400,17 +400,17 @@ class CanvasRenderingContext2D final : p
     }
     return nullptr;
   }
   NS_IMETHOD SetDimensions(int32_t aWidth, int32_t aHeight) override;
   NS_IMETHOD InitializeWithDrawTarget(
       nsIDocShell* aShell, NotNull<gfx::DrawTarget*> aTarget) override;
 
   NS_IMETHOD GetInputStream(const char* aMimeType,
-                            const char16_t* aEncoderOptions,
+                            const nsAString& aEncoderOptions,
                             nsIInputStream** aStream) override;
 
   already_AddRefed<mozilla::gfx::SourceSurface> GetSurfaceSnapshot(
       gfxAlphaType* aOutAlphaType = nullptr) override {
     EnsureTarget();
     if (aOutAlphaType) {
       *aOutAlphaType = (mOpaque ? gfxAlphaType::Opaque : gfxAlphaType::Premult);
     }
--- a/dom/canvas/ImageBitmapRenderingContext.cpp
+++ b/dom/canvas/ImageBitmapRenderingContext.cpp
@@ -134,17 +134,17 @@ mozilla::UniquePtr<uint8_t[]> ImageBitma
   }
 
   *aFormat = imgIEncoder::INPUT_FORMAT_HOSTARGB;
   return SurfaceToPackedBGRA(data);
 }
 
 NS_IMETHODIMP
 ImageBitmapRenderingContext::GetInputStream(const char* aMimeType,
-                                            const char16_t* aEncoderOptions,
+                                            const nsAString& aEncoderOptions,
                                             nsIInputStream** aStream) {
   nsCString enccid("@mozilla.org/image/encoder;2?type=");
   enccid += aMimeType;
   nsCOMPtr<imgIEncoder> encoder = do_CreateInstance(enccid.get());
   if (!encoder) {
     return NS_ERROR_FAILURE;
   }
 
--- a/dom/canvas/ImageBitmapRenderingContext.h
+++ b/dom/canvas/ImageBitmapRenderingContext.h
@@ -57,17 +57,17 @@ class ImageBitmapRenderingContext final
   NS_IMETHOD SetDimensions(int32_t aWidth, int32_t aHeight) override;
 
   NS_IMETHOD InitializeWithDrawTarget(
       nsIDocShell* aDocShell, NotNull<gfx::DrawTarget*> aTarget) override;
 
   virtual mozilla::UniquePtr<uint8_t[]> GetImageBuffer(
       int32_t* aFormat) override;
   NS_IMETHOD GetInputStream(const char* aMimeType,
-                            const char16_t* aEncoderOptions,
+                            const nsAString& aEncoderOptions,
                             nsIInputStream** aStream) override;
 
   virtual already_AddRefed<mozilla::gfx::SourceSurface> GetSurfaceSnapshot(
       gfxAlphaType* aOutAlphaType) override;
 
   virtual void SetOpaqueValueFromOpaqueAttr(bool aOpaqueAttrValue) override;
   virtual bool GetIsOpaque() override;
   NS_IMETHOD Reset() override;
--- a/dom/canvas/WebGLContext.cpp
+++ b/dom/canvas/WebGLContext.cpp
@@ -1064,17 +1064,17 @@ UniquePtr<uint8_t[]> WebGLContext::GetIm
   RefPtr<DataSourceSurface> dataSurface = snapshot->GetDataSurface();
 
   return gfxUtils::GetImageBuffer(dataSurface, mOptions.premultipliedAlpha,
                                   out_format);
 }
 
 NS_IMETHODIMP
 WebGLContext::GetInputStream(const char* mimeType,
-                             const char16_t* encoderOptions,
+                             const nsAString& encoderOptions,
                              nsIInputStream** out_stream) {
   NS_ASSERTION(gl, "GetInputStream on invalid context?");
   if (!gl) return NS_ERROR_FAILURE;
 
   // Use GetSurfaceSnapshot() to make sure that appropriate y-flip gets applied
   gfxAlphaType any;
   RefPtr<SourceSurface> snapshot = GetSurfaceSnapshot(&any);
   if (!snapshot) return NS_ERROR_FAILURE;
--- a/dom/canvas/WebGLContext.h
+++ b/dom/canvas/WebGLContext.h
@@ -364,17 +364,17 @@ class WebGLContext : public nsICanvasRen
 
   NS_IMETHOD Reset() override {
     /* (InitializeWithSurface) */
     return NS_ERROR_NOT_IMPLEMENTED;
   }
 
   virtual UniquePtr<uint8_t[]> GetImageBuffer(int32_t* out_format) override;
   NS_IMETHOD GetInputStream(const char* mimeType,
-                            const char16_t* encoderOptions,
+                            const nsAString& encoderOptions,
                             nsIInputStream** out_stream) override;
 
   virtual already_AddRefed<mozilla::gfx::SourceSurface> GetSurfaceSnapshot(
       gfxAlphaType* out_alphaType) override;
 
   virtual void SetOpaqueValueFromOpaqueAttr(bool) override{};
   bool GetIsOpaque() override { return !mOptions.alpha; }
   NS_IMETHOD SetContextOptions(JSContext* cx, JS::Handle<JS::Value> options,
--- a/dom/canvas/nsICanvasRenderingContextInternal.h
+++ b/dom/canvas/nsICanvasRenderingContextInternal.h
@@ -107,17 +107,17 @@ class nsICanvasRenderingContextInternal 
 
   // Gives you a stream containing the image represented by this context.
   // The format is given in mimeTime, for example "image/png".
   //
   // If the image format does not support transparency or includeTransparency
   // is false, alpha will be discarded and the result will be the image
   // composited on black.
   NS_IMETHOD GetInputStream(const char* mimeType,
-                            const char16_t* encoderOptions,
+                            const nsAString& encoderOptions,
                             nsIInputStream** stream) = 0;
 
   // This gets an Azure SourceSurface for the canvas, this will be a snapshot
   // of the canvas at the time it was called.
   // If premultAlpha is provided, then it assumed the callee can handle
   // un-premultiplied surfaces, and *premultAlpha will be set to false
   // if one is returned.
   virtual already_AddRefed<mozilla::gfx::SourceSurface> GetSurfaceSnapshot(
--- a/gfx/layers/AsyncCanvasRenderer.cpp
+++ b/gfx/layers/AsyncCanvasRenderer.cpp
@@ -237,17 +237,17 @@ already_AddRefed<gfx::DataSourceSurface>
            srcMap.GetStride() * mSurfaceForBasic->GetSize().height);
     return result.forget();
   } else {
     return UpdateTarget();
   }
 }
 
 nsresult AsyncCanvasRenderer::GetInputStream(const char* aMimeType,
-                                             const char16_t* aEncoderOptions,
+                                             const nsAString& aEncoderOptions,
                                              nsIInputStream** aStream) {
   MOZ_ASSERT(NS_IsMainThread());
   RefPtr<gfx::DataSourceSurface> surface = GetSurface();
   if (!surface) {
     return NS_ERROR_FAILURE;
   }
 
   gfx::DataSourceSurface::ScopedMap map(surface, gfx::DataSourceSurface::READ);
--- a/gfx/layers/AsyncCanvasRenderer.h
+++ b/gfx/layers/AsyncCanvasRenderer.h
@@ -84,17 +84,17 @@ class AsyncCanvasRenderer final {
   // shared. We don't want to readback it again here, so just copy the content
   // of that texture client here to avoid readback again.
   void CopyFromTextureClient(TextureClient* aClient);
 
   // Readback current WebGL's content and convert it to InputStream. This
   // function called GetSurface implicitly and GetSurface handles only get
   // called in the main thread. So this function can be called in main thread.
   nsresult GetInputStream(const char* aMimeType,
-                          const char16_t* aEncoderOptions,
+                          const nsAString& aEncoderOptions,
                           nsIInputStream** aStream);
 
   gfx::IntSize GetSize() const { return gfx::IntSize(mWidth, mHeight); }
 
   CompositableHandle GetCanvasClientAsyncHandle() const {
     return mCanvasClientAsyncHandle;
   }
 
--- a/gfx/thebes/gfxUtils.cpp
+++ b/gfx/thebes/gfxUtils.cpp
@@ -1338,17 +1338,17 @@ void gfxUtils::CopyAsDataURI(DrawTarget*
   *outFormat = format;
   return imageBuffer;
 }
 
 /* static */
 nsresult gfxUtils::GetInputStream(gfx::DataSourceSurface* aSurface,
                                   bool aIsAlphaPremultiplied,
                                   const char* aMimeType,
-                                  const char16_t* aEncoderOptions,
+                                  const nsAString& aEncoderOptions,
                                   nsIInputStream** outStream) {
   nsCString enccid("@mozilla.org/image/encoder;2?type=");
   enccid += aMimeType;
   nsCOMPtr<imgIEncoder> encoder = do_CreateInstance(enccid.get());
   if (!encoder) return NS_ERROR_FAILURE;
 
   int32_t format = 0;
   UniquePtr<uint8_t[]> imageBuffer =
--- a/gfx/thebes/gfxUtils.h
+++ b/gfx/thebes/gfxUtils.h
@@ -290,17 +290,17 @@ class gfxUtils {
 
   static mozilla::UniquePtr<uint8_t[]> GetImageBuffer(
       DataSourceSurface* aSurface, bool aIsAlphaPremultiplied,
       int32_t* outFormat);
 
   static nsresult GetInputStream(DataSourceSurface* aSurface,
                                  bool aIsAlphaPremultiplied,
                                  const char* aMimeType,
-                                 const char16_t* aEncoderOptions,
+                                 const nsAString& aEncoderOptions,
                                  nsIInputStream** outStream);
 
   static nsresult ThreadSafeGetFeatureStatus(
       const nsCOMPtr<nsIGfxInfo>& gfxInfo, int32_t feature,
       nsACString& failureId, int32_t* status);
 
   static void RemoveShaderCacheFromDiskIfNecessary();