Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/base/ImageEncoder.h
Line
Count
Source (jump to first uncovered line)
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
                              bool aUsePlaceholder,
46
                              nsICanvasRenderingContextInternal* aContext,
47
                              layers::AsyncCanvasRenderer* aRenderer,
48
                              nsIInputStream** aStream);
49
50
  // Extracts data asynchronously. aType may change to "image/png" if we had to
51
  // fall back to a PNG encoder. aOptions are the options to be passed to the
52
  // encoder and aUsingCustomOptions specifies whether custom parse options were
53
  // used (i.e. by using -moz-parse-options). If there are any unrecognized
54
  // custom parse options, we fall back to the default values for the encoder
55
  // without any options at all. A return value of NS_OK only implies
56
  // successful dispatching of the extraction step to the encoding thread.
57
  // aEncodeCallback will be called on main thread when encoding process is
58
  // success.
59
  // Note: The callback has to set a valid parent for content for the generated
60
  // Blob object.
61
  static nsresult ExtractDataAsync(nsAString& aType,
62
                                   const nsAString& aOptions,
63
                                   bool aUsingCustomOptions,
64
                                   UniquePtr<uint8_t[]> aImageBuffer,
65
                                   int32_t aFormat,
66
                                   const nsIntSize aSize,
67
                                   bool aUsePlaceholder,
68
                                   EncodeCompleteCallback* aEncodeCallback);
69
70
  // Extract an Image asynchronously. Its function is same as ExtractDataAsync
71
  // except for the parameters. aImage is the uncompressed data. aEncodeCallback
72
  // will be called on main thread when encoding process is success.
73
  // Note: The callback has to set a valid parent for content for the generated
74
  // Blob object.
75
  static nsresult ExtractDataFromLayersImageAsync(nsAString& aType,
76
                                                  const nsAString& aOptions,
77
                                                  bool aUsingCustomOptions,
78
                                                  layers::Image* aImage,
79
                                                  bool aUsePlaceholder,
80
                                                  EncodeCompleteCallback* aEncodeCallback);
81
82
  // Gives you a stream containing the image represented by aImageBuffer.
83
  // The format is given in aFormat, for example
84
  // imgIEncoder::INPUT_FORMAT_HOSTARGB.
85
  static nsresult GetInputStream(int32_t aWidth,
86
                                 int32_t aHeight,
87
                                 uint8_t* aImageBuffer,
88
                                 int32_t aFormat,
89
                                 imgIEncoder* aEncoder,
90
                                 const char16_t* aEncoderOptions,
91
                                 nsIInputStream** aStream);
92
93
private:
94
  // When called asynchronously, aContext and aRenderer are null.
95
  static nsresult
96
  ExtractDataInternal(const nsAString& aType,
97
                      const nsAString& aOptions,
98
                      uint8_t* aImageBuffer,
99
                      int32_t aFormat,
100
                      const nsIntSize aSize,
101
                      bool aUsePlaceholder,
102
                      layers::Image* aImage,
103
                      nsICanvasRenderingContextInternal* aContext,
104
                      layers::AsyncCanvasRenderer* aRenderer,
105
                      nsIInputStream** aStream,
106
                      imgIEncoder* aEncoder);
107
108
  // Creates and returns an encoder instance of the type specified in aType.
109
  // aType may change to "image/png" if no instance of the original type could
110
  // be created and we had to fall back to a PNG encoder. A null return value
111
  // should be interpreted as NS_IMAGELIB_ERROR_NO_ENCODER and aType is
112
  // undefined in this case.
113
  static already_AddRefed<imgIEncoder> GetImageEncoder(nsAString& aType);
114
115
  static nsresult EnsureThreadPool();
116
117
  // Thread pool for dispatching EncodingRunnable.
118
  static StaticRefPtr<nsIThreadPool> sThreadPool;
119
120
  friend class EncodingRunnable;
121
  friend class EncoderThreadPoolTerminator;
122
};
123
124
/**
125
 *  The callback interface of ExtractDataAsync and ExtractDataFromLayersImageAsync.
126
 *  ReceiveBlob() is called on main thread when encoding is complete.
127
 */
128
class EncodeCompleteCallback
129
{
130
public:
131
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(EncodeCompleteCallback)
132
133
  virtual nsresult ReceiveBlob(already_AddRefed<Blob> aBlob) = 0;
134
135
protected:
136
0
  virtual ~EncodeCompleteCallback() {}
137
};
138
139
} // namespace dom
140
} // namespace mozilla
141
142
#endif // ImageEncoder_h