Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef ImageEncoder_h
8 : #define ImageEncoder_h
9 :
10 : #include "imgIEncoder.h"
11 : #include "nsError.h"
12 : #include "mozilla/dom/File.h"
13 : #include "mozilla/dom/HTMLCanvasElementBinding.h"
14 : #include "mozilla/UniquePtr.h"
15 : #include "nsLayoutUtils.h"
16 : #include "nsSize.h"
17 :
18 : class nsICanvasRenderingContextInternal;
19 : class nsIThreadPool;
20 :
21 : namespace mozilla {
22 :
23 : namespace layers {
24 : class AsyncCanvasRenderer;
25 : class Image;
26 : } // namespace layers
27 :
28 : namespace dom {
29 :
30 : class EncodeCompleteCallback;
31 : class EncodingRunnable;
32 :
33 : class ImageEncoder
34 : {
35 : public:
36 : // Extracts data synchronously and gives you a stream containing the image
37 : // represented by aContext. aType may change to "image/png" if we had to fall
38 : // back to a PNG encoder. A return value of NS_OK implies successful data
39 : // extraction. If there are any unrecognized custom parse options in
40 : // aOptions, NS_ERROR_INVALID_ARG will be returned. When encountering this
41 : // error it is usual to call this function again without any options at all.
42 : static nsresult ExtractData(nsAString& aType,
43 : const nsAString& aOptions,
44 : const nsIntSize aSize,
45 : nsICanvasRenderingContextInternal* aContext,
46 : layers::AsyncCanvasRenderer* aRenderer,
47 : nsIInputStream** aStream);
48 :
49 : // Extracts data asynchronously. aType may change to "image/png" if we had to
50 : // fall back to a PNG encoder. aOptions are the options to be passed to the
51 : // encoder and aUsingCustomOptions specifies whether custom parse options were
52 : // used (i.e. by using -moz-parse-options). If there are any unrecognized
53 : // custom parse options, we fall back to the default values for the encoder
54 : // without any options at all. A return value of NS_OK only implies
55 : // successful dispatching of the extraction step to the encoding thread.
56 : // aEncodeCallback will be called on main thread when encoding process is
57 : // success.
58 : // Note: The callback has to set a valid parent for content for the generated
59 : // Blob object.
60 : static nsresult ExtractDataAsync(nsAString& aType,
61 : const nsAString& aOptions,
62 : bool aUsingCustomOptions,
63 : UniquePtr<uint8_t[]> aImageBuffer,
64 : int32_t aFormat,
65 : const nsIntSize aSize,
66 : EncodeCompleteCallback* aEncodeCallback);
67 :
68 : // Extract an Image asynchronously. Its function is same as ExtractDataAsync
69 : // except for the parameters. aImage is the uncompressed data. aEncodeCallback
70 : // will be called on main thread when encoding process is success.
71 : // Note: The callback has to set a valid parent for content for the generated
72 : // Blob object.
73 : static nsresult ExtractDataFromLayersImageAsync(nsAString& aType,
74 : const nsAString& aOptions,
75 : bool aUsingCustomOptions,
76 : layers::Image* aImage,
77 : EncodeCompleteCallback* aEncodeCallback);
78 :
79 : // Gives you a stream containing the image represented by aImageBuffer.
80 : // The format is given in aFormat, for example
81 : // imgIEncoder::INPUT_FORMAT_HOSTARGB.
82 : static nsresult GetInputStream(int32_t aWidth,
83 : int32_t aHeight,
84 : uint8_t* aImageBuffer,
85 : int32_t aFormat,
86 : imgIEncoder* aEncoder,
87 : const char16_t* aEncoderOptions,
88 : nsIInputStream** aStream);
89 :
90 : private:
91 : // When called asynchronously, aContext and aRenderer are null.
92 : static nsresult
93 : ExtractDataInternal(const nsAString& aType,
94 : const nsAString& aOptions,
95 : uint8_t* aImageBuffer,
96 : int32_t aFormat,
97 : const nsIntSize aSize,
98 : layers::Image* aImage,
99 : nsICanvasRenderingContextInternal* aContext,
100 : layers::AsyncCanvasRenderer* aRenderer,
101 : nsIInputStream** aStream,
102 : imgIEncoder* aEncoder);
103 :
104 : // Creates and returns an encoder instance of the type specified in aType.
105 : // aType may change to "image/png" if no instance of the original type could
106 : // be created and we had to fall back to a PNG encoder. A null return value
107 : // should be interpreted as NS_IMAGELIB_ERROR_NO_ENCODER and aType is
108 : // undefined in this case.
109 : static already_AddRefed<imgIEncoder> GetImageEncoder(nsAString& aType);
110 :
111 : static nsresult EnsureThreadPool();
112 :
113 : // Thread pool for dispatching EncodingRunnable.
114 : static StaticRefPtr<nsIThreadPool> sThreadPool;
115 :
116 : friend class EncodingRunnable;
117 : friend class EncoderThreadPoolTerminator;
118 : };
119 :
120 : /**
121 : * The callback interface of ExtractDataAsync and ExtractDataFromLayersImageAsync.
122 : * ReceiveBlob() is called on main thread when encoding is complete.
123 : */
124 0 : class EncodeCompleteCallback
125 : {
126 : public:
127 0 : NS_INLINE_DECL_THREADSAFE_REFCOUNTING(EncodeCompleteCallback)
128 :
129 : virtual nsresult ReceiveBlob(already_AddRefed<Blob> aBlob) = 0;
130 :
131 : protected:
132 0 : virtual ~EncodeCompleteCallback() {}
133 : };
134 :
135 : } // namespace dom
136 : } // namespace mozilla
137 :
138 : #endif // ImageEncoder_h
|