2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-10-17 06:55:04 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef ImageEncoder_h
|
|
|
|
#define ImageEncoder_h
|
|
|
|
|
|
|
|
#include "imgIEncoder.h"
|
|
|
|
#include "nsError.h"
|
2014-10-08 20:15:23 +04:00
|
|
|
#include "mozilla/dom/File.h"
|
2013-10-17 06:55:04 +04:00
|
|
|
#include "mozilla/dom/HTMLCanvasElementBinding.h"
|
2015-10-26 21:31:12 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2013-10-17 06:55:04 +04:00
|
|
|
#include "nsLayoutUtils.h"
|
|
|
|
#include "nsSize.h"
|
|
|
|
|
|
|
|
class nsICanvasRenderingContextInternal;
|
2015-12-18 09:52:17 +03:00
|
|
|
class nsIThreadPool;
|
2013-10-17 06:55:04 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
2014-09-04 04:08:00 +04:00
|
|
|
|
|
|
|
namespace layers {
|
2015-10-12 06:21:03 +03:00
|
|
|
class AsyncCanvasRenderer;
|
2014-09-04 04:08:00 +04:00
|
|
|
class Image;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace layers
|
2014-09-04 04:08:00 +04:00
|
|
|
|
2013-10-17 06:55:04 +04:00
|
|
|
namespace dom {
|
|
|
|
|
2014-09-04 04:08:00 +04:00
|
|
|
class EncodeCompleteCallback;
|
2013-10-17 06:55:04 +04:00
|
|
|
class EncodingRunnable;
|
|
|
|
|
|
|
|
class ImageEncoder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Extracts data synchronously and gives you a stream containing the image
|
|
|
|
// represented by aContext. aType may change to "image/png" if we had to fall
|
|
|
|
// back to a PNG encoder. A return value of NS_OK implies successful data
|
|
|
|
// extraction. If there are any unrecognized custom parse options in
|
|
|
|
// aOptions, NS_ERROR_INVALID_ARG will be returned. When encountering this
|
|
|
|
// error it is usual to call this function again without any options at all.
|
|
|
|
static nsresult ExtractData(nsAString& aType,
|
|
|
|
const nsAString& aOptions,
|
|
|
|
const nsIntSize aSize,
|
2017-08-22 06:12:49 +03:00
|
|
|
bool aUsePlaceholder,
|
2013-10-17 06:55:04 +04:00
|
|
|
nsICanvasRenderingContextInternal* aContext,
|
2015-10-12 06:21:03 +03:00
|
|
|
layers::AsyncCanvasRenderer* aRenderer,
|
2013-10-17 06:55:04 +04:00
|
|
|
nsIInputStream** aStream);
|
|
|
|
|
|
|
|
// Extracts data asynchronously. aType may change to "image/png" if we had to
|
|
|
|
// fall back to a PNG encoder. aOptions are the options to be passed to the
|
|
|
|
// encoder and aUsingCustomOptions specifies whether custom parse options were
|
|
|
|
// used (i.e. by using -moz-parse-options). If there are any unrecognized
|
|
|
|
// custom parse options, we fall back to the default values for the encoder
|
|
|
|
// without any options at all. A return value of NS_OK only implies
|
|
|
|
// successful dispatching of the extraction step to the encoding thread.
|
2014-09-04 04:08:00 +04:00
|
|
|
// aEncodeCallback will be called on main thread when encoding process is
|
|
|
|
// success.
|
2014-10-08 20:15:22 +04:00
|
|
|
// Note: The callback has to set a valid parent for content for the generated
|
|
|
|
// Blob object.
|
2013-10-17 06:55:04 +04:00
|
|
|
static nsresult ExtractDataAsync(nsAString& aType,
|
|
|
|
const nsAString& aOptions,
|
|
|
|
bool aUsingCustomOptions,
|
2015-10-26 21:31:12 +03:00
|
|
|
UniquePtr<uint8_t[]> aImageBuffer,
|
2013-10-17 06:55:04 +04:00
|
|
|
int32_t aFormat,
|
|
|
|
const nsIntSize aSize,
|
2017-08-22 06:12:49 +03:00
|
|
|
bool aUsePlaceholder,
|
2014-09-04 04:08:00 +04:00
|
|
|
EncodeCompleteCallback* aEncodeCallback);
|
|
|
|
|
|
|
|
// Extract an Image asynchronously. Its function is same as ExtractDataAsync
|
|
|
|
// except for the parameters. aImage is the uncompressed data. aEncodeCallback
|
|
|
|
// will be called on main thread when encoding process is success.
|
2014-10-08 20:15:22 +04:00
|
|
|
// Note: The callback has to set a valid parent for content for the generated
|
|
|
|
// Blob object.
|
2014-09-04 04:08:00 +04:00
|
|
|
static nsresult ExtractDataFromLayersImageAsync(nsAString& aType,
|
|
|
|
const nsAString& aOptions,
|
|
|
|
bool aUsingCustomOptions,
|
|
|
|
layers::Image* aImage,
|
2017-08-22 06:12:49 +03:00
|
|
|
bool aUsePlaceholder,
|
2014-09-04 04:08:00 +04:00
|
|
|
EncodeCompleteCallback* aEncodeCallback);
|
2013-10-17 06:55:04 +04:00
|
|
|
|
|
|
|
// 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,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* aEncoderOptions,
|
2013-10-17 06:55:04 +04:00
|
|
|
nsIInputStream** aStream);
|
|
|
|
|
|
|
|
private:
|
2015-10-12 06:21:03 +03:00
|
|
|
// When called asynchronously, aContext and aRenderer are null.
|
2013-10-17 06:55:04 +04:00
|
|
|
static nsresult
|
|
|
|
ExtractDataInternal(const nsAString& aType,
|
|
|
|
const nsAString& aOptions,
|
|
|
|
uint8_t* aImageBuffer,
|
|
|
|
int32_t aFormat,
|
|
|
|
const nsIntSize aSize,
|
2017-08-22 06:12:49 +03:00
|
|
|
bool aUsePlaceholder,
|
2014-09-04 04:08:00 +04:00
|
|
|
layers::Image* aImage,
|
2013-10-17 06:55:04 +04:00
|
|
|
nsICanvasRenderingContextInternal* aContext,
|
2015-10-12 06:21:03 +03:00
|
|
|
layers::AsyncCanvasRenderer* aRenderer,
|
2013-10-17 06:55:04 +04:00
|
|
|
nsIInputStream** aStream,
|
|
|
|
imgIEncoder* aEncoder);
|
|
|
|
|
|
|
|
// Creates and returns an encoder instance of the type specified in aType.
|
|
|
|
// aType may change to "image/png" if no instance of the original type could
|
2013-10-24 00:34:10 +04:00
|
|
|
// be created and we had to fall back to a PNG encoder. A null return value
|
|
|
|
// should be interpreted as NS_IMAGELIB_ERROR_NO_ENCODER and aType is
|
2013-10-17 06:55:04 +04:00
|
|
|
// undefined in this case.
|
|
|
|
static already_AddRefed<imgIEncoder> GetImageEncoder(nsAString& aType);
|
|
|
|
|
2015-12-18 09:52:17 +03:00
|
|
|
static nsresult EnsureThreadPool();
|
|
|
|
|
|
|
|
// Thread pool for dispatching EncodingRunnable.
|
|
|
|
static StaticRefPtr<nsIThreadPool> sThreadPool;
|
|
|
|
|
2013-10-17 06:55:04 +04:00
|
|
|
friend class EncodingRunnable;
|
2016-02-18 09:18:22 +03:00
|
|
|
friend class EncoderThreadPoolTerminator;
|
2013-10-17 06:55:04 +04:00
|
|
|
};
|
|
|
|
|
2014-09-04 04:08:00 +04:00
|
|
|
/**
|
|
|
|
* The callback interface of ExtractDataAsync and ExtractDataFromLayersImageAsync.
|
|
|
|
* ReceiveBlob() is called on main thread when encoding is complete.
|
|
|
|
*/
|
|
|
|
class EncodeCompleteCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(EncodeCompleteCallback)
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
virtual nsresult ReceiveBlob(already_AddRefed<Blob> aBlob) = 0;
|
2014-09-04 04:08:00 +04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual ~EncodeCompleteCallback() {}
|
|
|
|
};
|
|
|
|
|
2013-10-17 06:55:04 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2014-07-01 16:32:47 +04:00
|
|
|
#endif // ImageEncoder_h
|