1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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 MOZILLA_DOM_OFFSCREENCANVAS_H_
#define MOZILLA_DOM_OFFSCREENCANVAS_H_
#include "FontVisibilityProvider.h"
#include "gfxTypes.h"
#include "mozilla/DOMEventTargetHelper.h"
#include "mozilla/Maybe.h"
#include "mozilla/RefPtr.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/dom/CanvasRenderingContextHelper.h"
#include "mozilla/dom/ImageEncoder.h"
#include "mozilla/dom/OffscreenCanvasDisplayHelper.h"
#include "mozilla/layers/LayersTypes.h"
#include "nsCycleCollectionParticipant.h"
struct JSContext;
namespace mozilla {
class CancelableRunnable;
class ErrorResult;
enum class RFPTarget : uint64_t;
namespace gfx {
class SourceSurface;
}
namespace dom {
enum class OffscreenRenderingContextId : uint8_t;
class Blob;
class EncodeCompleteCallback;
class OffscreenCanvasDisplayHelper;
class ImageBitmap;
struct ImageEncodeOptions;
using OwningOffscreenRenderingContext = class
OwningOffscreenCanvasRenderingContext2DOrImageBitmapRenderingContextOrWebGLRenderingContextOrWebGL2RenderingContextOrGPUCanvasContext;
// This is helper class for transferring OffscreenCanvas to worker thread.
// Because OffscreenCanvas is not thread-safe. So we cannot pass Offscreen-
// Canvas to worker thread directly. Thus, we create this helper class and
// store necessary data in it then pass it to worker thread.
struct OffscreenCanvasCloneData final {
OffscreenCanvasCloneData(OffscreenCanvasDisplayHelper* aDisplay,
uint32_t aWidth, uint32_t aHeight,
layers::LayersBackend aCompositorBackend,
bool aNeutered, bool aIsWriteOnly,
nsIPrincipal* aExpandedReader);
~OffscreenCanvasCloneData();
RefPtr<OffscreenCanvasDisplayHelper> mDisplay;
uint32_t mWidth;
uint32_t mHeight;
layers::LayersBackend mCompositorBackendType;
bool mNeutered;
bool mIsWriteOnly;
RefPtr<nsIPrincipal> mExpandedReader;
};
class OffscreenCanvas final : public DOMEventTargetHelper,
public CanvasRenderingContextHelper,
public FontVisibilityProvider {
public:
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(OffscreenCanvas,
DOMEventTargetHelper)
IMPL_EVENT_HANDLER(contextlost);
IMPL_EVENT_HANDLER(contextrestored);
FONT_VISIBILITY_PROVIDER_IMPL
OffscreenCanvas(nsIGlobalObject* aGlobal, uint32_t aWidth, uint32_t aHeight);
OffscreenCanvas(nsIGlobalObject* aGlobal, uint32_t aWidth, uint32_t aHeight,
layers::LayersBackend aCompositorBackend,
already_AddRefed<OffscreenCanvasDisplayHelper> aDisplay);
void Destroy();
nsIGlobalObject* GetParentObject() const { return GetOwnerGlobal(); }
virtual JSObject* WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) override;
static already_AddRefed<OffscreenCanvas> Constructor(
const GlobalObject& aGlobal, uint32_t aWidth, uint32_t aHeight,
ErrorResult& aRv);
void ClearResources();
uint32_t Width() const { return mWidth; }
uint32_t Height() const { return mHeight; }
void SetWidth(uint32_t aWidth, ErrorResult& aRv);
void SetHeight(uint32_t aHeight, ErrorResult& aRv);
void GetContext(JSContext* aCx, const OffscreenRenderingContextId& aContextId,
JS::Handle<JS::Value> aContextOptions,
Nullable<OwningOffscreenRenderingContext>& aResult,
ErrorResult& aRv);
already_AddRefed<ImageBitmap> TransferToImageBitmap(ErrorResult& aRv);
already_AddRefed<Promise> ConvertToBlob(const ImageEncodeOptions& aOptions,
ErrorResult& aRv);
already_AddRefed<Promise> ToBlob(JSContext* aCx, const nsAString& aType,
JS::Handle<JS::Value> aParams,
ErrorResult& aRv);
Maybe<uint64_t> GetWindowID() const;
nsICanvasRenderingContextInternal* GetContext() const {
return mCurrentContext;
}
CanvasContextType GetContextType() const { return mCurrentContextType; }
already_AddRefed<gfx::SourceSurface> GetSurfaceSnapshot(
gfxAlphaType* aOutAlphaType = nullptr);
static already_AddRefed<OffscreenCanvas> CreateFromCloneData(
nsIGlobalObject* aGlobal, OffscreenCanvasCloneData* aData);
UniquePtr<OffscreenCanvasCloneData> ToCloneData(JSContext* aCx);
void UpdateDisplayData(const OffscreenCanvasDisplayData& aData);
void CommitFrameToCompositor();
void DequeueCommitToCompositor();
void QueueCommitToCompositor();
virtual bool GetOpaqueAttr() override { return false; }
CSSIntSize GetWidthHeight() override { return CSSIntSize(mWidth, mHeight); }
virtual already_AddRefed<nsICanvasRenderingContextInternal> CreateContext(
CanvasContextType aContextType) override;
void SetNeutered() {
mWidth = 0;
mHeight = 0;
mNeutered = true;
}
bool MayNeuter() const { return !mNeutered && !mCurrentContext; }
void SetSize(const nsIntSize& aSize, ErrorResult& aRv);
nsIPrincipal* GetExpandedReader() const { return mExpandedReader; }
void SetWriteOnly(RefPtr<nsIPrincipal>&& aExpandedReader);
void SetWriteOnly(nsIPrincipal* aExpandedReader = nullptr) {
RefPtr<nsIPrincipal> expandedReader(aExpandedReader);
SetWriteOnly(std::move(expandedReader));
}
bool IsWriteOnly() const { return mIsWriteOnly; }
// Determines if the caller should be able to read the content.
bool CallerCanRead(nsIPrincipal& aPrincipal) const;
layers::LayersBackend GetCompositorBackendType() const {
return mCompositorBackendType;
}
bool IsTransferredFromElement() const { return !!mDisplay; }
private:
~OffscreenCanvas();
already_AddRefed<EncodeCompleteCallback> CreateEncodeCompleteCallback(
Promise* aPromise);
void CanvasAttrChanged() {
ErrorResult dummy;
UpdateContext(nullptr, JS::NullHandleValue, dummy);
}
bool mNeutered = false;
bool mIsWriteOnly = false;
uint32_t mWidth = 0;
uint32_t mHeight = 0;
layers::LayersBackend mCompositorBackendType =
layers::LayersBackend::LAYERS_NONE;
RefPtr<OffscreenCanvasDisplayHelper> mDisplay;
RefPtr<CancelableRunnable> mPendingCommit;
RefPtr<nsIPrincipal> mExpandedReader;
Maybe<OffscreenCanvasDisplayData> mPendingUpdate;
const FontVisibility mFontVisibility;
};
} // namespace dom
} // namespace mozilla
#endif // MOZILLA_DOM_OFFSCREENCANVAS_H_
|