Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/dom/OffscreenCanvas.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim:set ts=2 sw=2 sts=2 et cindent: */
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 MOZILLA_DOM_OFFSCREENCANVAS_H_
8
#define MOZILLA_DOM_OFFSCREENCANVAS_H_
9
10
#include "gfxTypes.h"
11
#include "mozilla/DOMEventTargetHelper.h"
12
#include "mozilla/dom/DOMPrefs.h"
13
#include "mozilla/layers/LayersTypes.h"
14
#include "mozilla/RefPtr.h"
15
#include "CanvasRenderingContextHelper.h"
16
#include "nsCycleCollectionParticipant.h"
17
18
struct JSContext;
19
20
namespace mozilla {
21
22
class ErrorResult;
23
24
namespace layers {
25
class AsyncCanvasRenderer;
26
class CanvasClient;
27
} // namespace layers
28
29
namespace dom {
30
class Blob;
31
class ImageBitmap;
32
33
// This is helper class for transferring OffscreenCanvas to worker thread.
34
// Because OffscreenCanvas is not thread-safe. So we cannot pass Offscreen-
35
// Canvas to worker thread directly. Thus, we create this helper class and
36
// store necessary data in it then pass it to worker thread.
37
struct OffscreenCanvasCloneData final
38
{
39
  OffscreenCanvasCloneData(layers::AsyncCanvasRenderer* aRenderer,
40
                           uint32_t aWidth, uint32_t aHeight,
41
                           layers::LayersBackend aCompositorBackend,
42
                           bool aNeutered, bool aIsWriteOnly);
43
  ~OffscreenCanvasCloneData();
44
45
  RefPtr<layers::AsyncCanvasRenderer> mRenderer;
46
  uint32_t mWidth;
47
  uint32_t mHeight;
48
  layers::LayersBackend mCompositorBackendType;
49
  bool mNeutered;
50
  bool mIsWriteOnly;
51
};
52
53
class OffscreenCanvas final : public DOMEventTargetHelper
54
                            , public CanvasRenderingContextHelper
55
{
56
public:
57
  NS_DECL_ISUPPORTS_INHERITED
58
  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(OffscreenCanvas, DOMEventTargetHelper)
59
60
  OffscreenCanvas(nsIGlobalObject* aGlobal,
61
                  uint32_t aWidth,
62
                  uint32_t aHeight,
63
                  layers::LayersBackend aCompositorBackend,
64
                  layers::AsyncCanvasRenderer* aRenderer);
65
66
0
  nsCOMPtr<nsIGlobalObject> GetParentObject() const { return GetOwnerGlobal(); }
67
68
  virtual JSObject* WrapObject(JSContext* aCx,
69
                               JS::Handle<JSObject*> aGivenProto) override;
70
71
  static already_AddRefed<OffscreenCanvas>
72
  Constructor(const GlobalObject& aGlobal,
73
              uint32_t aWidth,
74
              uint32_t aHeight,
75
              ErrorResult& aRv);
76
77
  void ClearResources();
78
79
  uint32_t Width() const
80
0
  {
81
0
    return mWidth;
82
0
  }
83
84
  uint32_t Height() const
85
0
  {
86
0
    return mHeight;
87
0
  }
88
89
  void SetWidth(uint32_t aWidth, ErrorResult& aRv)
90
0
  {
91
0
    if (mNeutered) {
92
0
      aRv.Throw(NS_ERROR_FAILURE);
93
0
      return;
94
0
    }
95
0
96
0
    if (mWidth != aWidth) {
97
0
      mWidth = aWidth;
98
0
      CanvasAttrChanged();
99
0
    }
100
0
  }
101
102
  void SetHeight(uint32_t aHeight, ErrorResult& aRv)
103
0
  {
104
0
    if (mNeutered) {
105
0
      aRv.Throw(NS_ERROR_FAILURE);
106
0
      return;
107
0
    }
108
0
109
0
    if (mHeight != aHeight) {
110
0
      mHeight = aHeight;
111
0
      CanvasAttrChanged();
112
0
    }
113
0
  }
114
115
  already_AddRefed<ImageBitmap>
116
  TransferToImageBitmap(ErrorResult& aRv);
117
118
  already_AddRefed<Promise>
119
  ToBlob(JSContext* aCx,
120
         const nsAString& aType,
121
         JS::Handle<JS::Value> aParams,
122
         ErrorResult& aRv);
123
124
  nsICanvasRenderingContextInternal* GetContext() const
125
0
  {
126
0
    return mCurrentContext;
127
0
  }
128
129
  already_AddRefed<gfx::SourceSurface> GetSurfaceSnapshot(gfxAlphaType* aOutAlphaType = nullptr);
130
131
  static already_AddRefed<OffscreenCanvas>
132
  CreateFromCloneData(nsIGlobalObject* aGlobal, OffscreenCanvasCloneData* aData);
133
134
  // Return true on main-thread, and return gfx.offscreencanvas.enabled
135
  // on worker thread.
136
  static bool PrefEnabledOnWorkerThread(JSContext* aCx, JSObject* aObj);
137
138
  OffscreenCanvasCloneData* ToCloneData();
139
140
  void CommitFrameToCompositor();
141
142
  virtual bool GetOpaqueAttr() override
143
  {
144
    return false;
145
  }
146
147
  virtual nsIntSize GetWidthHeight() override
148
  {
149
    return nsIntSize(mWidth, mHeight);
150
  }
151
152
  virtual already_AddRefed<nsICanvasRenderingContextInternal>
153
  CreateContext(CanvasContextType aContextType) override;
154
155
  virtual already_AddRefed<nsISupports>
156
  GetContext(JSContext* aCx,
157
             const nsAString& aContextId,
158
             JS::Handle<JS::Value> aContextOptions,
159
             ErrorResult& aRv) override;
160
161
  void SetNeutered()
162
0
  {
163
0
    mNeutered = true;
164
0
  }
165
166
  bool IsNeutered() const
167
0
  {
168
0
    return mNeutered;
169
0
  }
170
171
  void SetWriteOnly()
172
0
  {
173
0
    mIsWriteOnly = true;
174
0
  }
175
176
  bool IsWriteOnly() const
177
  {
178
    return mIsWriteOnly;
179
  }
180
181
  layers::LayersBackend GetCompositorBackendType() const
182
0
  {
183
0
    return mCompositorBackendType;
184
0
  }
185
186
private:
187
  ~OffscreenCanvas();
188
189
  nsCOMPtr<nsIGlobalObject> GetGlobalObject();
190
191
  void CanvasAttrChanged()
192
0
  {
193
0
    mAttrDirty = true;
194
0
    ErrorResult dummy;
195
0
    UpdateContext(nullptr, JS::NullHandleValue, dummy);
196
0
  }
197
198
  bool mAttrDirty;
199
  bool mNeutered;
200
  bool mIsWriteOnly;
201
202
  uint32_t mWidth;
203
  uint32_t mHeight;
204
205
  layers::LayersBackend mCompositorBackendType;
206
207
  RefPtr<layers::CanvasClient> mCanvasClient;
208
  RefPtr<layers::AsyncCanvasRenderer> mCanvasRenderer;
209
};
210
211
} // namespace dom
212
} // namespace mozilla
213
214
#endif // MOZILLA_DOM_OFFSCREENCANVAS_H_