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: */
|
2012-05-21 15:12:37 +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/. */
|
2005-04-18 10:33:23 +04:00
|
|
|
|
2013-01-04 09:16:14 +04:00
|
|
|
#include "mozilla/dom/HTMLCanvasElement.h"
|
2010-05-18 08:04:22 +04:00
|
|
|
|
2017-08-03 08:55:15 +03:00
|
|
|
#include "gfxPrefs.h"
|
2013-10-17 06:55:08 +04:00
|
|
|
#include "ImageEncoder.h"
|
2013-04-13 11:03:57 +04:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsfriendapi.h"
|
2013-10-17 06:55:08 +04:00
|
|
|
#include "Layers.h"
|
2015-09-17 07:36:57 +03:00
|
|
|
#include "MediaSegment.h"
|
2014-11-26 22:29:49 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
2011-06-08 22:12:18 +04:00
|
|
|
#include "mozilla/Base64.h"
|
2012-05-14 23:50:20 +04:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2015-05-13 09:04:30 +03:00
|
|
|
#include "mozilla/dom/CanvasCaptureMediaStream.h"
|
2013-04-13 11:03:57 +04:00
|
|
|
#include "mozilla/dom/CanvasRenderingContext2D.h"
|
2014-10-08 20:15:23 +04:00
|
|
|
#include "mozilla/dom/File.h"
|
2013-04-13 11:05:51 +04:00
|
|
|
#include "mozilla/dom/HTMLCanvasElementBinding.h"
|
2016-04-06 15:46:56 +03:00
|
|
|
#include "mozilla/dom/MediaStreamTrack.h"
|
2014-05-20 08:54:00 +04:00
|
|
|
#include "mozilla/dom/MouseEvent.h"
|
2015-10-12 06:21:03 +03:00
|
|
|
#include "mozilla/dom/OffscreenCanvas.h"
|
2014-05-20 08:54:00 +04:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2012-11-04 12:05:25 +04:00
|
|
|
#include "mozilla/gfx/Rect.h"
|
2015-10-12 06:21:02 +03:00
|
|
|
#include "mozilla/layers/AsyncCanvasRenderer.h"
|
2017-08-03 08:55:15 +03:00
|
|
|
#include "mozilla/layers/WebRenderCanvasRenderer.h"
|
2018-03-16 00:35:56 +03:00
|
|
|
#include "mozilla/layers/WebRenderUserData.h"
|
2014-05-20 08:54:00 +04:00
|
|
|
#include "mozilla/MouseEvents.h"
|
2012-11-04 12:05:25 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/Telemetry.h"
|
2013-04-13 11:03:57 +04:00
|
|
|
#include "nsAttrValueInlines.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDisplayList.h"
|
2013-10-17 06:55:08 +04:00
|
|
|
#include "nsDOMJSUtils.h"
|
2006-05-03 04:48:27 +04:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2013-04-13 11:03:57 +04:00
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "nsIWritablePropertyBag2.h"
|
2006-05-03 04:48:27 +04:00
|
|
|
#include "nsIXPConnect.h"
|
2010-12-03 11:24:17 +03:00
|
|
|
#include "nsJSUtils.h"
|
2014-05-20 08:54:00 +04:00
|
|
|
#include "nsLayoutUtils.h"
|
2011-05-26 11:28:26 +04:00
|
|
|
#include "nsMathUtils.h"
|
2013-04-13 11:03:57 +04:00
|
|
|
#include "nsNetUtil.h"
|
2015-09-17 07:36:57 +03:00
|
|
|
#include "nsRefreshDriver.h"
|
2012-02-03 18:23:18 +04:00
|
|
|
#include "nsStreamUtils.h"
|
2013-09-04 15:30:57 +04:00
|
|
|
#include "ActiveLayerTracker.h"
|
2017-08-22 09:23:41 +03:00
|
|
|
#include "CanvasUtils.h"
|
2016-02-25 02:54:50 +03:00
|
|
|
#include "VRManagerChild.h"
|
2014-11-11 02:16:50 +03:00
|
|
|
#include "WebGL1Context.h"
|
2014-07-26 01:15:35 +04:00
|
|
|
#include "WebGL2Context.h"
|
2013-07-17 17:58:09 +04:00
|
|
|
|
2010-05-18 08:04:22 +04:00
|
|
|
using namespace mozilla::layers;
|
2014-04-17 09:29:52 +04:00
|
|
|
using namespace mozilla::gfx;
|
2005-04-18 10:33:23 +04:00
|
|
|
|
2013-04-13 11:03:57 +04:00
|
|
|
NS_IMPL_NS_NEW_HTML_ELEMENT(Canvas)
|
2013-01-04 09:16:14 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2015-09-17 07:36:57 +03:00
|
|
|
class RequestedFrameRefreshObserver : public nsARefreshObserver
|
|
|
|
{
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RequestedFrameRefreshObserver, override)
|
|
|
|
|
|
|
|
public:
|
|
|
|
RequestedFrameRefreshObserver(HTMLCanvasElement* const aOwningElement,
|
2017-08-22 09:23:41 +03:00
|
|
|
nsRefreshDriver* aRefreshDriver,
|
|
|
|
bool aReturnPlaceholderData)
|
2015-09-17 07:36:57 +03:00
|
|
|
: mRegistered(false),
|
2017-08-22 09:23:41 +03:00
|
|
|
mReturnPlaceholderData(aReturnPlaceholderData),
|
2015-09-17 07:36:57 +03:00
|
|
|
mOwningElement(aOwningElement),
|
|
|
|
mRefreshDriver(aRefreshDriver)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwningElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
static already_AddRefed<DataSourceSurface>
|
2017-08-22 09:23:41 +03:00
|
|
|
CopySurface(const RefPtr<SourceSurface>& aSurface,
|
|
|
|
bool aReturnPlaceholderData)
|
2015-09-17 07:36:57 +03:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DataSourceSurface> data = aSurface->GetDataSurface();
|
2015-09-17 07:36:57 +03:00
|
|
|
if (!data) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataSourceSurface::ScopedMap read(data, DataSourceSurface::READ);
|
|
|
|
if (!read.IsMapped()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DataSourceSurface> copy =
|
2015-09-17 07:36:57 +03:00
|
|
|
Factory::CreateDataSourceSurfaceWithStride(data->GetSize(),
|
|
|
|
data->GetFormat(),
|
|
|
|
read.GetStride());
|
|
|
|
if (!copy) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataSourceSurface::ScopedMap write(copy, DataSourceSurface::WRITE);
|
|
|
|
if (!write.IsMapped()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(read.GetStride() == write.GetStride());
|
|
|
|
MOZ_ASSERT(data->GetSize() == copy->GetSize());
|
|
|
|
MOZ_ASSERT(data->GetFormat() == copy->GetFormat());
|
|
|
|
|
2017-08-22 09:23:41 +03:00
|
|
|
if (aReturnPlaceholderData) {
|
|
|
|
// If returning placeholder data, fill the frame copy with white pixels.
|
|
|
|
memset(write.GetData(), 0xFF,
|
|
|
|
write.GetStride() * copy->GetSize().height);
|
|
|
|
} else {
|
|
|
|
memcpy(write.GetData(), read.GetData(),
|
|
|
|
write.GetStride() * copy->GetSize().height);
|
|
|
|
}
|
2015-09-17 07:36:57 +03:00
|
|
|
|
|
|
|
return copy.forget();
|
|
|
|
}
|
|
|
|
|
2017-08-22 09:23:41 +03:00
|
|
|
void SetReturnPlaceholderData(bool aReturnPlaceholderData)
|
|
|
|
{
|
|
|
|
mReturnPlaceholderData = aReturnPlaceholderData;
|
|
|
|
}
|
|
|
|
|
2015-09-17 07:36:57 +03:00
|
|
|
void WillRefresh(TimeStamp aTime) override
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
Bug 1375392 - Tweak the PROFILER_LABEL* macros. r=mstange.
This patch makes the following changes to the macros.
- Removes PROFILER_LABEL_FUNC. It's only suitable for use in functions outside
classes, due to PROFILER_FUNCTION_NAME not getting class names, and it was
mostly misused.
- Removes PROFILER_FUNCTION_NAME. It's no longer used, and __func__ is
universally available now anyway.
- Combines the first two string literal arguments of PROFILER_LABEL and
PROFILER_LABEL_DYNAMIC into a single argument. There was no good reason for
them to be separate, and it forced a '::' in the label, which isn't always
appropriate. Also, the meaning of the "name_space" argument was interpreted
in an interesting variety of ways.
- Adds an "AUTO_" prefix to PROFILER_LABEL and PROFILER_LABEL_DYNAMIC, to make
it clearer they construct RAII objects rather than just being function calls.
(I myself have screwed up the scoping because of this in the past.)
- Fills in the 'js::ProfileEntry::Category::' qualifier within the macro, so
the caller doesn't need to. This makes a *lot* more of the uses fit onto a
single line.
The patch also makes the following changes to the macro uses (beyond those
required by the changes described above).
- Fixes a bunch of labels that had gotten out of sync with the name of the
class and/or function that encloses them.
- Removes a useless PROFILER_LABEL use within a trivial scope in
EventStateManager::DispatchMouseOrPointerEvent(). It clearly wasn't serving
any useful purpose. It also serves as extra evidence that the AUTO_ prefix is
a good idea.
- Tweaks DecodePool::SyncRunIf{Preferred,Possible} so that the labelling is
done within them, instead of at their callsites, because that's a more
standard way of doing things.
--HG--
extra : rebase_source : 318d1bc6fc1425a94aacbf489dd46e4f83211de4
2017-06-22 10:08:53 +03:00
|
|
|
AUTO_PROFILER_LABEL("RequestedFrameRefreshObserver::WillRefresh", OTHER);
|
2017-01-12 15:57:15 +03:00
|
|
|
|
2015-09-17 07:36:57 +03:00
|
|
|
if (!mOwningElement) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mOwningElement->IsWriteOnly()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mOwningElement->IsContextCleanForFrameCapture()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-12 18:59:38 +03:00
|
|
|
mOwningElement->ProcessDestroyedFrameListeners();
|
|
|
|
|
2015-09-17 07:36:57 +03:00
|
|
|
if (!mOwningElement->IsFrameCaptureRequested()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-12 15:57:15 +03:00
|
|
|
RefPtr<SourceSurface> snapshot;
|
|
|
|
{
|
Bug 1375392 - Tweak the PROFILER_LABEL* macros. r=mstange.
This patch makes the following changes to the macros.
- Removes PROFILER_LABEL_FUNC. It's only suitable for use in functions outside
classes, due to PROFILER_FUNCTION_NAME not getting class names, and it was
mostly misused.
- Removes PROFILER_FUNCTION_NAME. It's no longer used, and __func__ is
universally available now anyway.
- Combines the first two string literal arguments of PROFILER_LABEL and
PROFILER_LABEL_DYNAMIC into a single argument. There was no good reason for
them to be separate, and it forced a '::' in the label, which isn't always
appropriate. Also, the meaning of the "name_space" argument was interpreted
in an interesting variety of ways.
- Adds an "AUTO_" prefix to PROFILER_LABEL and PROFILER_LABEL_DYNAMIC, to make
it clearer they construct RAII objects rather than just being function calls.
(I myself have screwed up the scoping because of this in the past.)
- Fills in the 'js::ProfileEntry::Category::' qualifier within the macro, so
the caller doesn't need to. This makes a *lot* more of the uses fit onto a
single line.
The patch also makes the following changes to the macro uses (beyond those
required by the changes described above).
- Fixes a bunch of labels that had gotten out of sync with the name of the
class and/or function that encloses them.
- Removes a useless PROFILER_LABEL use within a trivial scope in
EventStateManager::DispatchMouseOrPointerEvent(). It clearly wasn't serving
any useful purpose. It also serves as extra evidence that the AUTO_ prefix is
a good idea.
- Tweaks DecodePool::SyncRunIf{Preferred,Possible} so that the labelling is
done within them, instead of at their callsites, because that's a more
standard way of doing things.
--HG--
extra : rebase_source : 318d1bc6fc1425a94aacbf489dd46e4f83211de4
2017-06-22 10:08:53 +03:00
|
|
|
AUTO_PROFILER_LABEL(
|
|
|
|
"RequestedFrameRefreshObserver::WillRefresh:GetSnapshot", OTHER);
|
2017-01-12 15:57:15 +03:00
|
|
|
snapshot = mOwningElement->GetSurfaceSnapshot(nullptr);
|
|
|
|
if (!snapshot) {
|
|
|
|
return;
|
|
|
|
}
|
2015-09-17 07:36:57 +03:00
|
|
|
}
|
|
|
|
|
2017-01-12 15:57:15 +03:00
|
|
|
RefPtr<DataSourceSurface> copy;
|
|
|
|
{
|
Bug 1375392 - Tweak the PROFILER_LABEL* macros. r=mstange.
This patch makes the following changes to the macros.
- Removes PROFILER_LABEL_FUNC. It's only suitable for use in functions outside
classes, due to PROFILER_FUNCTION_NAME not getting class names, and it was
mostly misused.
- Removes PROFILER_FUNCTION_NAME. It's no longer used, and __func__ is
universally available now anyway.
- Combines the first two string literal arguments of PROFILER_LABEL and
PROFILER_LABEL_DYNAMIC into a single argument. There was no good reason for
them to be separate, and it forced a '::' in the label, which isn't always
appropriate. Also, the meaning of the "name_space" argument was interpreted
in an interesting variety of ways.
- Adds an "AUTO_" prefix to PROFILER_LABEL and PROFILER_LABEL_DYNAMIC, to make
it clearer they construct RAII objects rather than just being function calls.
(I myself have screwed up the scoping because of this in the past.)
- Fills in the 'js::ProfileEntry::Category::' qualifier within the macro, so
the caller doesn't need to. This makes a *lot* more of the uses fit onto a
single line.
The patch also makes the following changes to the macro uses (beyond those
required by the changes described above).
- Fixes a bunch of labels that had gotten out of sync with the name of the
class and/or function that encloses them.
- Removes a useless PROFILER_LABEL use within a trivial scope in
EventStateManager::DispatchMouseOrPointerEvent(). It clearly wasn't serving
any useful purpose. It also serves as extra evidence that the AUTO_ prefix is
a good idea.
- Tweaks DecodePool::SyncRunIf{Preferred,Possible} so that the labelling is
done within them, instead of at their callsites, because that's a more
standard way of doing things.
--HG--
extra : rebase_source : 318d1bc6fc1425a94aacbf489dd46e4f83211de4
2017-06-22 10:08:53 +03:00
|
|
|
AUTO_PROFILER_LABEL(
|
|
|
|
"RequestedFrameRefreshObserver::WillRefresh:CopySurface", OTHER);
|
2017-08-22 09:23:41 +03:00
|
|
|
copy = CopySurface(snapshot, mReturnPlaceholderData);
|
2017-01-12 15:57:15 +03:00
|
|
|
if (!copy) {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-17 16:54:56 +03:00
|
|
|
}
|
2015-09-17 07:36:57 +03:00
|
|
|
|
2017-01-12 15:57:15 +03:00
|
|
|
{
|
Bug 1375392 - Tweak the PROFILER_LABEL* macros. r=mstange.
This patch makes the following changes to the macros.
- Removes PROFILER_LABEL_FUNC. It's only suitable for use in functions outside
classes, due to PROFILER_FUNCTION_NAME not getting class names, and it was
mostly misused.
- Removes PROFILER_FUNCTION_NAME. It's no longer used, and __func__ is
universally available now anyway.
- Combines the first two string literal arguments of PROFILER_LABEL and
PROFILER_LABEL_DYNAMIC into a single argument. There was no good reason for
them to be separate, and it forced a '::' in the label, which isn't always
appropriate. Also, the meaning of the "name_space" argument was interpreted
in an interesting variety of ways.
- Adds an "AUTO_" prefix to PROFILER_LABEL and PROFILER_LABEL_DYNAMIC, to make
it clearer they construct RAII objects rather than just being function calls.
(I myself have screwed up the scoping because of this in the past.)
- Fills in the 'js::ProfileEntry::Category::' qualifier within the macro, so
the caller doesn't need to. This makes a *lot* more of the uses fit onto a
single line.
The patch also makes the following changes to the macro uses (beyond those
required by the changes described above).
- Fixes a bunch of labels that had gotten out of sync with the name of the
class and/or function that encloses them.
- Removes a useless PROFILER_LABEL use within a trivial scope in
EventStateManager::DispatchMouseOrPointerEvent(). It clearly wasn't serving
any useful purpose. It also serves as extra evidence that the AUTO_ prefix is
a good idea.
- Tweaks DecodePool::SyncRunIf{Preferred,Possible} so that the labelling is
done within them, instead of at their callsites, because that's a more
standard way of doing things.
--HG--
extra : rebase_source : 318d1bc6fc1425a94aacbf489dd46e4f83211de4
2017-06-22 10:08:53 +03:00
|
|
|
AUTO_PROFILER_LABEL(
|
|
|
|
"RequestedFrameRefreshObserver::WillRefresh:SetFrame", OTHER);
|
2017-01-13 13:56:03 +03:00
|
|
|
mOwningElement->SetFrameCapture(copy.forget(), aTime);
|
2017-01-12 15:57:15 +03:00
|
|
|
mOwningElement->MarkContextCleanForFrameCapture();
|
|
|
|
}
|
2015-09-17 07:36:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void DetachFromRefreshDriver()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwningElement);
|
|
|
|
MOZ_ASSERT(mRefreshDriver);
|
|
|
|
|
|
|
|
Unregister();
|
|
|
|
mRefreshDriver = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Register()
|
|
|
|
{
|
|
|
|
if (mRegistered) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mRefreshDriver);
|
|
|
|
if (mRefreshDriver) {
|
2017-01-05 10:31:56 +03:00
|
|
|
mRefreshDriver->AddRefreshObserver(this, FlushType::Display);
|
2015-09-17 07:36:57 +03:00
|
|
|
mRegistered = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unregister()
|
|
|
|
{
|
|
|
|
if (!mRegistered) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mRefreshDriver);
|
|
|
|
if (mRefreshDriver) {
|
2017-01-05 10:31:56 +03:00
|
|
|
mRefreshDriver->RemoveRefreshObserver(this, FlushType::Display);
|
2015-09-17 07:36:57 +03:00
|
|
|
mRegistered = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~RequestedFrameRefreshObserver()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mRefreshDriver);
|
|
|
|
MOZ_ASSERT(!mRegistered);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mRegistered;
|
2017-08-22 09:23:41 +03:00
|
|
|
bool mReturnPlaceholderData;
|
2015-09-17 07:36:57 +03:00
|
|
|
HTMLCanvasElement* const mOwningElement;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsRefreshDriver> mRefreshDriver;
|
2015-09-17 07:36:57 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2014-04-29 12:57:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(HTMLCanvasPrintState, mCanvas,
|
|
|
|
mContext, mCallback)
|
2013-09-12 02:35:51 +04:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(HTMLCanvasPrintState, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(HTMLCanvasPrintState, Release)
|
|
|
|
|
|
|
|
HTMLCanvasPrintState::HTMLCanvasPrintState(HTMLCanvasElement* aCanvas,
|
|
|
|
nsICanvasRenderingContextInternal* aContext,
|
|
|
|
nsITimerCallback* aCallback)
|
|
|
|
: mIsDone(false), mPendingNotify(false), mCanvas(aCanvas),
|
|
|
|
mContext(aContext), mCallback(aCallback)
|
2012-08-31 21:45:45 +04:00
|
|
|
{
|
2013-09-12 02:35:51 +04:00
|
|
|
}
|
2012-08-31 21:45:45 +04:00
|
|
|
|
2013-09-12 02:35:51 +04:00
|
|
|
HTMLCanvasPrintState::~HTMLCanvasPrintState()
|
|
|
|
{
|
|
|
|
}
|
2012-08-31 21:45:45 +04:00
|
|
|
|
2013-09-12 02:35:51 +04:00
|
|
|
/* virtual */ JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
HTMLCanvasPrintState::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-09-12 02:35:51 +04:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
return MozCanvasPrintStateBinding::Wrap(aCx, this, aGivenProto);
|
2013-09-12 02:35:51 +04:00
|
|
|
}
|
2012-08-31 21:45:45 +04:00
|
|
|
|
2013-09-12 02:35:51 +04:00
|
|
|
nsISupports*
|
|
|
|
HTMLCanvasPrintState::Context() const
|
|
|
|
{
|
|
|
|
return mContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HTMLCanvasPrintState::Done()
|
|
|
|
{
|
|
|
|
if (!mPendingNotify && !mIsDone) {
|
|
|
|
// The canvas needs to be invalidated for printing reftests on linux to
|
|
|
|
// work.
|
|
|
|
if (mCanvas) {
|
|
|
|
mCanvas->InvalidateCanvas();
|
|
|
|
}
|
2017-06-12 22:34:10 +03:00
|
|
|
RefPtr<nsRunnableMethod<HTMLCanvasPrintState>> doneEvent =
|
|
|
|
NewRunnableMethod("dom::HTMLCanvasPrintState::NotifyDone",
|
|
|
|
this,
|
|
|
|
&HTMLCanvasPrintState::NotifyDone);
|
2013-09-12 02:35:51 +04:00
|
|
|
if (NS_SUCCEEDED(NS_DispatchToCurrentThread(doneEvent))) {
|
|
|
|
mPendingNotify = true;
|
2012-08-31 21:45:45 +04:00
|
|
|
}
|
|
|
|
}
|
2013-09-12 02:35:51 +04:00
|
|
|
}
|
2012-08-31 21:45:45 +04:00
|
|
|
|
2013-09-12 02:35:51 +04:00
|
|
|
void
|
|
|
|
HTMLCanvasPrintState::NotifyDone()
|
|
|
|
{
|
|
|
|
mIsDone = true;
|
|
|
|
mPendingNotify = false;
|
|
|
|
if (mCallback) {
|
|
|
|
mCallback->Notify(nullptr);
|
2012-08-31 21:45:45 +04:00
|
|
|
}
|
2013-09-12 02:35:51 +04:00
|
|
|
}
|
2012-08-31 21:45:45 +04:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
HTMLCanvasElementObserver::HTMLCanvasElementObserver(HTMLCanvasElement* aElement)
|
|
|
|
: mElement(aElement)
|
|
|
|
{
|
|
|
|
RegisterVisibilityChangeEvent();
|
|
|
|
RegisterMemoryPressureEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
HTMLCanvasElementObserver::~HTMLCanvasElementObserver()
|
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HTMLCanvasElementObserver::Destroy()
|
|
|
|
{
|
|
|
|
UnregisterMemoryPressureEvent();
|
|
|
|
UnregisterVisibilityChangeEvent();
|
|
|
|
mElement = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HTMLCanvasElementObserver::RegisterVisibilityChangeEvent()
|
|
|
|
{
|
|
|
|
if (!mElement) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDocument* document = mElement->OwnerDoc();
|
|
|
|
document->AddSystemEventListener(NS_LITERAL_STRING("visibilitychange"),
|
|
|
|
this, true, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HTMLCanvasElementObserver::UnregisterVisibilityChangeEvent()
|
|
|
|
{
|
|
|
|
if (!mElement) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDocument* document = mElement->OwnerDoc();
|
|
|
|
document->RemoveSystemEventListener(NS_LITERAL_STRING("visibilitychange"),
|
|
|
|
this, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HTMLCanvasElementObserver::RegisterMemoryPressureEvent()
|
|
|
|
{
|
|
|
|
if (!mElement) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
|
|
|
|
MOZ_ASSERT(observerService);
|
|
|
|
|
|
|
|
if (observerService)
|
|
|
|
observerService->AddObserver(this, "memory-pressure", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HTMLCanvasElementObserver::UnregisterMemoryPressureEvent()
|
|
|
|
{
|
|
|
|
if (!mElement) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
|
|
|
|
// Do not assert on observerService here. This might be triggered by
|
|
|
|
// the cycle collector at a late enough time, that XPCOM services are
|
|
|
|
// no longer available. See bug 1029504.
|
|
|
|
if (observerService)
|
|
|
|
observerService->RemoveObserver(this, "memory-pressure");
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
HTMLCanvasElementObserver::Observe(nsISupports*, const char* aTopic, const char16_t*)
|
|
|
|
{
|
|
|
|
if (!mElement || strcmp(aTopic, "memory-pressure")) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mElement->OnMemoryPressure();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
HTMLCanvasElementObserver::HandleEvent(nsIDOMEvent* aEvent)
|
|
|
|
{
|
|
|
|
nsAutoString type;
|
|
|
|
aEvent->GetType(type);
|
|
|
|
if (!mElement || !type.EqualsLiteral("visibilitychange")) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mElement->OnVisibilityChange();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(HTMLCanvasElementObserver, nsIObserver)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2014-06-20 06:01:40 +04:00
|
|
|
HTMLCanvasElement::HTMLCanvasElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
|
2013-04-13 11:03:57 +04:00
|
|
|
: nsGenericHTMLElement(aNodeInfo),
|
2015-10-12 06:21:03 +03:00
|
|
|
mResetLayer(true) ,
|
2012-08-31 21:45:45 +04:00
|
|
|
mWriteOnly(false)
|
2016-07-01 11:58:18 +03:00
|
|
|
{}
|
2005-04-18 10:33:23 +04:00
|
|
|
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::~HTMLCanvasElement()
|
2005-04-18 10:33:23 +04:00
|
|
|
{
|
2015-10-12 06:21:03 +03:00
|
|
|
if (mContextObserver) {
|
|
|
|
mContextObserver->Destroy();
|
|
|
|
mContextObserver = nullptr;
|
|
|
|
}
|
|
|
|
|
2012-08-31 21:45:45 +04:00
|
|
|
ResetPrintCallback();
|
2015-09-17 07:36:57 +03:00
|
|
|
if (mRequestedFrameRefreshObserver) {
|
|
|
|
mRequestedFrameRefreshObserver->DetachFromRefreshDriver();
|
|
|
|
}
|
2015-10-12 06:21:02 +03:00
|
|
|
|
|
|
|
if (mAsyncCanvasRenderer) {
|
|
|
|
mAsyncCanvasRenderer->mHTMLCanvasElement = nullptr;
|
|
|
|
}
|
2005-04-18 10:33:23 +04:00
|
|
|
}
|
|
|
|
|
2014-04-25 20:49:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(HTMLCanvasElement, nsGenericHTMLElement,
|
|
|
|
mCurrentContext, mPrintCallback,
|
2015-12-18 09:52:17 +03:00
|
|
|
mPrintState, mOriginalCanvas,
|
|
|
|
mOffscreenCanvas)
|
2011-06-22 01:12:50 +04:00
|
|
|
|
2017-09-28 22:17:07 +03:00
|
|
|
NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(HTMLCanvasElement, nsGenericHTMLElement)
|
2005-04-18 10:33:23 +04:00
|
|
|
|
2013-01-04 09:16:14 +04:00
|
|
|
NS_IMPL_ELEMENT_CLONE(HTMLCanvasElement)
|
2005-04-18 10:33:23 +04:00
|
|
|
|
2013-04-13 11:05:51 +04:00
|
|
|
/* virtual */ JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
HTMLCanvasElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-04-13 11:05:51 +04:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
return HTMLCanvasElementBinding::Wrap(aCx, this, aGivenProto);
|
2013-04-13 11:05:51 +04:00
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
already_AddRefed<nsICanvasRenderingContextInternal>
|
|
|
|
HTMLCanvasElement::CreateContext(CanvasContextType aContextType)
|
|
|
|
{
|
2016-11-03 17:57:33 +03:00
|
|
|
// Note that the compositor backend will be LAYERS_NONE if there is no widget.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsICanvasRenderingContextInternal> ret =
|
2016-11-03 17:57:33 +03:00
|
|
|
CreateContextHelper(aContextType, GetCompositorBackendType());
|
2015-10-12 06:21:03 +03:00
|
|
|
|
|
|
|
// Add Observer for webgl canvas.
|
|
|
|
if (aContextType == CanvasContextType::WebGL1 ||
|
|
|
|
aContextType == CanvasContextType::WebGL2) {
|
2015-10-16 05:01:53 +03:00
|
|
|
if (!mContextObserver) {
|
|
|
|
mContextObserver = new HTMLCanvasElementObserver(this);
|
|
|
|
}
|
2015-10-12 06:21:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ret->SetCanvasElement(this);
|
|
|
|
return ret.forget();
|
|
|
|
}
|
|
|
|
|
2005-05-07 07:45:06 +04:00
|
|
|
nsIntSize
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::GetWidthHeight()
|
2005-04-18 10:33:23 +04:00
|
|
|
{
|
2011-05-19 14:49:18 +04:00
|
|
|
nsIntSize size(DEFAULT_CANVAS_WIDTH, DEFAULT_CANVAS_HEIGHT);
|
2005-04-18 10:33:23 +04:00
|
|
|
const nsAttrValue* value;
|
|
|
|
|
2006-12-26 20:47:52 +03:00
|
|
|
if ((value = GetParsedAttr(nsGkAtoms::width)) &&
|
2005-05-07 07:45:06 +04:00
|
|
|
value->Type() == nsAttrValue::eInteger)
|
|
|
|
{
|
2005-04-18 10:33:23 +04:00
|
|
|
size.width = value->GetIntegerValue();
|
|
|
|
}
|
|
|
|
|
2006-12-26 20:47:52 +03:00
|
|
|
if ((value = GetParsedAttr(nsGkAtoms::height)) &&
|
2005-05-07 07:45:06 +04:00
|
|
|
value->Type() == nsAttrValue::eInteger)
|
|
|
|
{
|
2005-04-18 10:33:23 +04:00
|
|
|
size.height = value->GetIntegerValue();
|
|
|
|
}
|
|
|
|
|
2014-11-26 22:29:49 +03:00
|
|
|
MOZ_ASSERT(size.width >= 0 && size.height >= 0,
|
|
|
|
"we should've required <canvas> width/height attrs to be "
|
|
|
|
"unsigned (non-negative) values");
|
|
|
|
|
2005-05-07 07:45:06 +04:00
|
|
|
return size;
|
2005-04-18 10:33:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2017-10-03 01:05:19 +03:00
|
|
|
HTMLCanvasElement::AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
|
2017-06-07 20:28:20 +03:00
|
|
|
const nsAttrValue* aValue,
|
2017-10-10 00:33:38 +03:00
|
|
|
const nsAttrValue* aOldValue,
|
|
|
|
nsIPrincipal* aSubjectPrincipal,
|
|
|
|
bool aNotify)
|
2005-04-18 10:33:23 +04:00
|
|
|
{
|
2017-06-07 20:28:20 +03:00
|
|
|
AfterMaybeChangeAttr(aNamespaceID, aName, aNotify);
|
2005-04-18 10:33:23 +04:00
|
|
|
|
2017-06-07 20:28:20 +03:00
|
|
|
return nsGenericHTMLElement::AfterSetAttr(aNamespaceID, aName, aValue,
|
2017-10-10 00:33:38 +03:00
|
|
|
aOldValue, aSubjectPrincipal, aNotify);
|
2005-04-18 10:33:23 +04:00
|
|
|
}
|
|
|
|
|
2013-10-01 01:02:40 +04:00
|
|
|
nsresult
|
2017-10-03 01:05:19 +03:00
|
|
|
HTMLCanvasElement::OnAttrSetButNotChanged(int32_t aNamespaceID, nsAtom* aName,
|
2017-06-07 20:28:20 +03:00
|
|
|
const nsAttrValueOrString& aValue,
|
|
|
|
bool aNotify)
|
2013-10-01 01:02:40 +04:00
|
|
|
{
|
2017-06-07 20:28:20 +03:00
|
|
|
AfterMaybeChangeAttr(aNamespaceID, aName, aNotify);
|
|
|
|
|
|
|
|
return nsGenericHTMLElement::OnAttrSetButNotChanged(aNamespaceID, aName,
|
|
|
|
aValue, aNotify);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-10-03 01:05:19 +03:00
|
|
|
HTMLCanvasElement::AfterMaybeChangeAttr(int32_t aNamespaceID, nsAtom* aName,
|
2017-06-07 20:28:20 +03:00
|
|
|
bool aNotify)
|
|
|
|
{
|
|
|
|
if (mCurrentContext && aNamespaceID == kNameSpaceID_None &&
|
|
|
|
(aName == nsGkAtoms::width || aName == nsGkAtoms::height ||
|
|
|
|
aName == nsGkAtoms::moz_opaque)) {
|
2015-10-18 14:14:22 +03:00
|
|
|
ErrorResult dummy;
|
2017-06-07 20:28:20 +03:00
|
|
|
UpdateContext(nullptr, JS::NullHandleValue, dummy);
|
2013-10-01 01:02:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-31 21:45:45 +04:00
|
|
|
void
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::HandlePrintCallback(nsPresContext::nsPresContextType aType)
|
2012-08-31 21:45:45 +04:00
|
|
|
{
|
|
|
|
// Only call the print callback here if 1) we're in a print testing mode or
|
|
|
|
// print preview mode, 2) the canvas has a print callback and 3) the callback
|
|
|
|
// hasn't already been called. For real printing the callback is handled in
|
|
|
|
// nsSimplePageSequenceFrame::PrePrintNextPage.
|
|
|
|
if ((aType == nsPresContext::eContext_PageLayout ||
|
|
|
|
aType == nsPresContext::eContext_PrintPreview) &&
|
2013-09-12 02:35:51 +04:00
|
|
|
!mPrintState && GetMozPrintCallback()) {
|
2012-08-31 21:45:45 +04:00
|
|
|
DispatchPrintCallback(nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::DispatchPrintCallback(nsITimerCallback* aCallback)
|
2012-08-31 21:45:45 +04:00
|
|
|
{
|
|
|
|
// For print reftests the context may not be initialized yet, so get a context
|
|
|
|
// so mCurrentContext is set.
|
|
|
|
if (!mCurrentContext) {
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsISupports> context;
|
2013-04-13 11:05:33 +04:00
|
|
|
rv = GetContext(NS_LITERAL_STRING("2d"), getter_AddRefs(context));
|
2012-08-31 21:45:45 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2013-01-04 09:16:14 +04:00
|
|
|
mPrintState = new HTMLCanvasPrintState(this, mCurrentContext, aCallback);
|
2012-08-31 21:45:45 +04:00
|
|
|
|
2017-06-12 22:34:10 +03:00
|
|
|
RefPtr<nsRunnableMethod<HTMLCanvasElement>> renderEvent =
|
|
|
|
NewRunnableMethod("dom::HTMLCanvasElement::CallPrintCallback",
|
|
|
|
this,
|
|
|
|
&HTMLCanvasElement::CallPrintCallback);
|
2017-07-26 11:13:35 +03:00
|
|
|
return OwnerDoc()->Dispatch(TaskCategory::Other, renderEvent.forget());
|
2012-08-31 21:45:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::CallPrintCallback()
|
2012-08-31 21:45:45 +04:00
|
|
|
{
|
2013-09-12 02:35:51 +04:00
|
|
|
ErrorResult rv;
|
|
|
|
GetMozPrintCallback()->Call(*mPrintState, rv);
|
2012-08-31 21:45:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::ResetPrintCallback()
|
2012-08-31 21:45:45 +04:00
|
|
|
{
|
|
|
|
if (mPrintState) {
|
|
|
|
mPrintState = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::IsPrintCallbackDone()
|
2012-08-31 21:45:45 +04:00
|
|
|
{
|
|
|
|
if (mPrintState == nullptr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mPrintState->mIsDone;
|
|
|
|
}
|
|
|
|
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement*
|
|
|
|
HTMLCanvasElement::GetOriginalCanvas()
|
2012-08-31 21:45:45 +04:00
|
|
|
{
|
|
|
|
return mOriginalCanvas ? mOriginalCanvas.get() : this;
|
|
|
|
}
|
|
|
|
|
2009-12-11 07:02:13 +03:00
|
|
|
nsresult
|
2017-04-20 22:57:48 +03:00
|
|
|
HTMLCanvasElement::CopyInnerTo(Element* aDest,
|
|
|
|
bool aPreallocateChildren)
|
2009-12-11 07:02:13 +03:00
|
|
|
{
|
2017-04-20 22:57:48 +03:00
|
|
|
nsresult rv = nsGenericHTMLElement::CopyInnerTo(aDest, aPreallocateChildren);
|
2009-12-11 07:02:13 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-10-18 14:53:36 +04:00
|
|
|
if (aDest->OwnerDoc()->IsStaticDocument()) {
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement* dest = static_cast<HTMLCanvasElement*>(aDest);
|
2013-04-13 11:05:33 +04:00
|
|
|
dest->mOriginalCanvas = this;
|
2012-08-31 21:45:45 +04:00
|
|
|
|
2018-01-12 15:48:20 +03:00
|
|
|
// We make sure that the canvas is not zero sized since that would cause
|
|
|
|
// the DrawImage call below to return an error, which would cause printing
|
|
|
|
// to fail.
|
|
|
|
nsIntSize size = GetWidthHeight();
|
|
|
|
if (size.height > 0 && size.width > 0) {
|
|
|
|
nsCOMPtr<nsISupports> cxt;
|
|
|
|
dest->GetContext(NS_LITERAL_STRING("2d"), getter_AddRefs(cxt));
|
|
|
|
RefPtr<CanvasRenderingContext2D> context2d =
|
|
|
|
static_cast<CanvasRenderingContext2D*>(cxt.get());
|
|
|
|
if (context2d && !mPrintCallback) {
|
|
|
|
CanvasImageSource source;
|
|
|
|
source.SetAsHTMLCanvasElement() = this;
|
|
|
|
ErrorResult err;
|
|
|
|
context2d->DrawImage(source,
|
|
|
|
0.0, 0.0, err);
|
|
|
|
rv = err.StealNSResult();
|
|
|
|
}
|
2009-12-11 07:02:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-10-21 05:11:07 +03:00
|
|
|
nsresult HTMLCanvasElement::GetEventTargetParent(EventChainPreVisitor& aVisitor)
|
2014-05-20 08:54:00 +04:00
|
|
|
{
|
2014-08-04 09:28:50 +04:00
|
|
|
if (aVisitor.mEvent->mClass == eMouseEventClass) {
|
2014-05-20 08:54:00 +04:00
|
|
|
WidgetMouseEventBase* evt = (WidgetMouseEventBase*)aVisitor.mEvent;
|
|
|
|
if (mCurrentContext) {
|
|
|
|
nsIFrame *frame = GetPrimaryFrame();
|
|
|
|
if (!frame)
|
|
|
|
return NS_OK;
|
|
|
|
nsPoint ptInRoot = nsLayoutUtils::GetEventCoordinatesRelativeTo(evt, frame);
|
|
|
|
nsRect paddingRect = frame->GetContentRectRelativeToSelf();
|
|
|
|
Point hitpoint;
|
2018-02-19 23:15:23 +03:00
|
|
|
hitpoint.x = (ptInRoot.x - paddingRect.x) / AppUnitsPerCSSPixel();
|
|
|
|
hitpoint.y = (ptInRoot.y - paddingRect.y) / AppUnitsPerCSSPixel();
|
2014-05-20 08:54:00 +04:00
|
|
|
|
|
|
|
evt->region = mCurrentContext->GetHitRegion(hitpoint);
|
|
|
|
aVisitor.mCanHandle = true;
|
|
|
|
}
|
|
|
|
}
|
2016-10-21 05:11:07 +03:00
|
|
|
return nsGenericHTMLElement::GetEventTargetParent(aVisitor);
|
2014-05-20 08:54:00 +04:00
|
|
|
}
|
|
|
|
|
2005-04-18 10:33:23 +04:00
|
|
|
nsChangeHint
|
2017-10-03 01:05:19 +03:00
|
|
|
HTMLCanvasElement::GetAttributeChangeHint(const nsAtom* aAttribute,
|
2013-01-04 09:16:14 +04:00
|
|
|
int32_t aModType) const
|
2005-04-18 10:33:23 +04:00
|
|
|
{
|
|
|
|
nsChangeHint retval =
|
|
|
|
nsGenericHTMLElement::GetAttributeChangeHint(aAttribute, aModType);
|
2006-12-26 20:47:52 +03:00
|
|
|
if (aAttribute == nsGkAtoms::width ||
|
|
|
|
aAttribute == nsGkAtoms::height)
|
2005-04-18 10:33:23 +04:00
|
|
|
{
|
2016-05-23 06:26:03 +03:00
|
|
|
retval |= NS_STYLE_HINT_REFLOW;
|
2008-06-04 04:00:32 +04:00
|
|
|
} else if (aAttribute == nsGkAtoms::moz_opaque)
|
|
|
|
{
|
2016-05-23 06:26:03 +03:00
|
|
|
retval |= NS_STYLE_HINT_VISUAL;
|
2005-04-18 10:33:23 +04:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::ParseAttribute(int32_t aNamespaceID,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* aAttribute,
|
2013-01-04 09:16:14 +04:00
|
|
|
const nsAString& aValue,
|
2017-11-02 06:35:52 +03:00
|
|
|
nsIPrincipal* aMaybeScriptedPrincipal,
|
2013-01-04 09:16:14 +04:00
|
|
|
nsAttrValue& aResult)
|
2005-04-18 10:33:23 +04:00
|
|
|
{
|
2010-04-23 06:53:09 +04:00
|
|
|
if (aNamespaceID == kNameSpaceID_None &&
|
|
|
|
(aAttribute == nsGkAtoms::width || aAttribute == nsGkAtoms::height)) {
|
2011-03-25 14:40:55 +03:00
|
|
|
return aResult.ParseNonNegativeIntValue(aValue);
|
2005-04-18 10:33:23 +04:00
|
|
|
}
|
|
|
|
|
2005-11-29 19:37:15 +03:00
|
|
|
return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
|
2017-11-02 06:35:52 +03:00
|
|
|
aMaybeScriptedPrincipal, aResult);
|
2005-04-18 10:33:23 +04:00
|
|
|
}
|
|
|
|
|
2005-09-01 02:16:51 +04:00
|
|
|
|
|
|
|
|
2016-11-15 08:18:32 +03:00
|
|
|
void
|
|
|
|
HTMLCanvasElement::ToDataURL(JSContext* aCx, const nsAString& aType,
|
|
|
|
JS::Handle<JS::Value> aParams,
|
|
|
|
nsAString& aDataURL,
|
|
|
|
ErrorResult& aRv)
|
2005-09-01 02:16:51 +04:00
|
|
|
{
|
2006-10-12 18:38:35 +04:00
|
|
|
// do a trust check if this is a write-only canvas
|
2017-04-07 22:32:18 +03:00
|
|
|
if (mWriteOnly &&
|
2017-09-04 04:51:02 +03:00
|
|
|
!nsContentUtils::CallerHasPermission(aCx, nsGkAtoms::all_urlsPermission)) {
|
2016-11-15 08:18:32 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
2006-05-03 04:48:27 +04:00
|
|
|
}
|
|
|
|
|
2016-11-15 08:18:32 +03:00
|
|
|
aRv = ToDataURLImpl(aCx, aType, aParams, aDataURL);
|
2005-09-01 02:16:51 +04:00
|
|
|
}
|
|
|
|
|
2013-09-12 02:35:51 +04:00
|
|
|
void
|
|
|
|
HTMLCanvasElement::SetMozPrintCallback(PrintCallback* aCallback)
|
2012-08-31 21:45:45 +04:00
|
|
|
{
|
|
|
|
mPrintCallback = aCallback;
|
|
|
|
}
|
|
|
|
|
2013-09-12 02:35:51 +04:00
|
|
|
PrintCallback*
|
2013-04-13 11:05:40 +04:00
|
|
|
HTMLCanvasElement::GetMozPrintCallback() const
|
2012-08-31 21:45:45 +04:00
|
|
|
{
|
|
|
|
if (mOriginalCanvas) {
|
2013-04-13 11:05:40 +04:00
|
|
|
return mOriginalCanvas->GetMozPrintCallback();
|
2012-08-31 21:45:45 +04:00
|
|
|
}
|
2013-04-13 11:05:40 +04:00
|
|
|
return mPrintCallback;
|
|
|
|
}
|
|
|
|
|
2016-09-12 19:00:14 +03:00
|
|
|
class CanvasCaptureTrackSource : public MediaStreamTrackSource
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(CanvasCaptureTrackSource,
|
|
|
|
MediaStreamTrackSource)
|
|
|
|
|
|
|
|
CanvasCaptureTrackSource(nsIPrincipal* aPrincipal,
|
|
|
|
CanvasCaptureMediaStream* aCaptureStream)
|
|
|
|
: MediaStreamTrackSource(aPrincipal, nsString())
|
|
|
|
, mCaptureStream(aCaptureStream) {}
|
|
|
|
|
|
|
|
MediaSourceEnum GetMediaSource() const override
|
|
|
|
{
|
|
|
|
return MediaSourceEnum::Other;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stop() override
|
|
|
|
{
|
|
|
|
if (!mCaptureStream) {
|
|
|
|
NS_ERROR("No stream");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCaptureStream->StopCapture();
|
|
|
|
}
|
|
|
|
|
2017-11-10 17:55:42 +03:00
|
|
|
void Disable() override
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Enable() override
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-09-12 19:00:14 +03:00
|
|
|
private:
|
|
|
|
virtual ~CanvasCaptureTrackSource() {}
|
|
|
|
|
|
|
|
RefPtr<CanvasCaptureMediaStream> mCaptureStream;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(CanvasCaptureTrackSource,
|
|
|
|
MediaStreamTrackSource)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(CanvasCaptureTrackSource,
|
|
|
|
MediaStreamTrackSource)
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CanvasCaptureTrackSource)
|
2016-09-12 19:00:14 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(MediaStreamTrackSource)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(CanvasCaptureTrackSource,
|
|
|
|
MediaStreamTrackSource,
|
|
|
|
mCaptureStream)
|
|
|
|
|
2015-05-13 09:04:30 +03:00
|
|
|
already_AddRefed<CanvasCaptureMediaStream>
|
|
|
|
HTMLCanvasElement::CaptureStream(const Optional<double>& aFrameRate,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2015-05-13 09:04:41 +03:00
|
|
|
if (IsWriteOnly()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* window = OwnerDoc()->GetInnerWindow();
|
2015-05-13 09:04:41 +03:00
|
|
|
if (!window) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mCurrentContext) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_INITIALIZED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CanvasCaptureMediaStream> stream =
|
2015-05-13 09:04:41 +03:00
|
|
|
CanvasCaptureMediaStream::CreateSourceStream(window, this);
|
|
|
|
if (!stream) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
TrackID videoTrackId = 1;
|
2016-04-06 15:56:44 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> principal = NodePrincipal();
|
|
|
|
nsresult rv =
|
|
|
|
stream->Init(aFrameRate, videoTrackId, principal);
|
2015-05-13 09:04:41 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-09-17 07:36:57 +03:00
|
|
|
|
2016-08-12 14:50:41 +03:00
|
|
|
RefPtr<MediaStreamTrack> track =
|
2016-01-22 11:27:37 +03:00
|
|
|
stream->CreateDOMTrack(videoTrackId, MediaSegment::VIDEO,
|
2016-09-12 19:00:14 +03:00
|
|
|
new CanvasCaptureTrackSource(principal, stream));
|
2016-08-12 14:50:41 +03:00
|
|
|
stream->AddTrackInternal(track);
|
2015-12-22 06:29:26 +03:00
|
|
|
|
2017-08-22 09:23:41 +03:00
|
|
|
// Check site-specific permission and display prompt if appropriate.
|
|
|
|
// If no permission, arrange for the frame capture listener to return
|
|
|
|
// all-white, opaque image data.
|
|
|
|
bool usePlaceholder = !CanvasUtils::IsImageExtractionAllowed(
|
|
|
|
OwnerDoc(),
|
|
|
|
nsContentUtils::GetCurrentJSContext());
|
|
|
|
|
|
|
|
rv = RegisterFrameCaptureListener(stream->FrameCaptureListener(), usePlaceholder);
|
2015-12-22 06:29:26 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-05-13 09:04:41 +03:00
|
|
|
return stream.forget();
|
2015-05-13 09:04:30 +03:00
|
|
|
}
|
|
|
|
|
2006-05-03 04:48:27 +04:00
|
|
|
nsresult
|
2017-08-22 09:23:41 +03:00
|
|
|
HTMLCanvasElement::ExtractData(JSContext* aCx,
|
|
|
|
nsAString& aType,
|
2013-01-04 09:16:14 +04:00
|
|
|
const nsAString& aOptions,
|
2013-10-17 06:55:08 +04:00
|
|
|
nsIInputStream** aStream)
|
2005-09-01 02:16:51 +04:00
|
|
|
{
|
2017-08-22 09:23:41 +03:00
|
|
|
// Check site-specific permission and display prompt if appropriate.
|
|
|
|
// If no permission, return all-white, opaque image data.
|
|
|
|
bool usePlaceholder = !CanvasUtils::IsImageExtractionAllowed(OwnerDoc(), aCx);
|
2013-10-17 06:55:08 +04:00
|
|
|
return ImageEncoder::ExtractData(aType,
|
|
|
|
aOptions,
|
|
|
|
GetSize(),
|
2017-08-22 09:23:41 +03:00
|
|
|
usePlaceholder,
|
2013-10-17 06:55:08 +04:00
|
|
|
mCurrentContext,
|
2015-10-12 06:21:03 +03:00
|
|
|
mAsyncCanvasRenderer,
|
2013-10-17 06:55:08 +04:00
|
|
|
aStream);
|
2010-09-18 05:14:59 +04:00
|
|
|
}
|
|
|
|
|
2013-07-24 15:17:00 +04:00
|
|
|
nsresult
|
|
|
|
HTMLCanvasElement::ToDataURLImpl(JSContext* aCx,
|
|
|
|
const nsAString& aMimeType,
|
|
|
|
const JS::Value& aEncoderOptions,
|
|
|
|
nsAString& aDataURL)
|
|
|
|
{
|
|
|
|
nsIntSize size = GetWidthHeight();
|
|
|
|
if (size.height == 0 || size.width == 0) {
|
|
|
|
aDataURL = NS_LITERAL_STRING("data:,");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString type;
|
2014-08-13 15:53:32 +04:00
|
|
|
nsContentUtils::ASCIIToLower(aMimeType, type);
|
2013-07-24 15:17:00 +04:00
|
|
|
|
|
|
|
nsAutoString params;
|
|
|
|
bool usingCustomParseOptions;
|
2014-08-13 15:53:32 +04:00
|
|
|
nsresult rv =
|
|
|
|
ParseParams(aCx, type, aEncoderOptions, params, &usingCustomParseOptions);
|
2013-07-24 15:17:00 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-06-08 22:12:18 +04:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2017-08-22 09:23:41 +03:00
|
|
|
rv = ExtractData(aCx, type, params, getter_AddRefs(stream));
|
2011-08-26 00:09:05 +04:00
|
|
|
|
2013-04-13 11:03:57 +04:00
|
|
|
// If there are unrecognized custom parse options, we should fall back to
|
2011-08-26 00:09:05 +04:00
|
|
|
// the default values for the encoder without any options at all.
|
|
|
|
if (rv == NS_ERROR_INVALID_ARG && usingCustomParseOptions) {
|
2017-08-22 09:23:41 +03:00
|
|
|
rv = ExtractData(aCx, type, EmptyString(), getter_AddRefs(stream));
|
2011-08-26 00:09:05 +04:00
|
|
|
}
|
|
|
|
|
2010-09-18 05:14:59 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2005-09-01 02:16:51 +04:00
|
|
|
// build data URL string
|
2013-10-17 06:55:08 +04:00
|
|
|
aDataURL = NS_LITERAL_STRING("data:") + type + NS_LITERAL_STRING(";base64,");
|
2005-09-01 02:16:51 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint64_t count;
|
2011-06-08 22:12:18 +04:00
|
|
|
rv = stream->Available(&count);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-09-28 10:57:33 +04:00
|
|
|
NS_ENSURE_TRUE(count <= UINT32_MAX, NS_ERROR_FILE_TOO_BIG);
|
2005-09-01 02:16:51 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
return Base64EncodeInputStream(stream, aDataURL, (uint32_t)count, aDataURL.Length());
|
2005-09-01 02:16:51 +04:00
|
|
|
}
|
|
|
|
|
2013-09-12 02:35:51 +04:00
|
|
|
void
|
|
|
|
HTMLCanvasElement::ToBlob(JSContext* aCx,
|
2016-11-03 09:57:34 +03:00
|
|
|
BlobCallback& aCallback,
|
2013-07-24 15:17:00 +04:00
|
|
|
const nsAString& aType,
|
2014-02-19 19:13:38 +04:00
|
|
|
JS::Handle<JS::Value> aParams,
|
2013-09-12 02:35:51 +04:00
|
|
|
ErrorResult& aRv)
|
2012-10-16 01:15:25 +04:00
|
|
|
{
|
|
|
|
// do a trust check if this is a write-only canvas
|
2017-04-07 22:32:18 +03:00
|
|
|
if (mWriteOnly &&
|
2017-09-04 04:51:02 +03:00
|
|
|
!nsContentUtils::CallerHasPermission(aCx, nsGkAtoms::all_urlsPermission)) {
|
2013-09-12 02:35:51 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
2012-10-19 10:59:38 +04:00
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = OwnerDoc()->GetScopeObject();
|
|
|
|
MOZ_ASSERT(global);
|
2015-09-29 13:51:24 +03:00
|
|
|
|
2017-02-14 06:39:16 +03:00
|
|
|
nsIntSize elemSize = GetWidthHeight();
|
|
|
|
if (elemSize.width == 0 || elemSize.height == 0) {
|
|
|
|
// According to spec, blob should return null if either its horizontal
|
|
|
|
// dimension or its vertical dimension is zero. See link below.
|
|
|
|
// https://html.spec.whatwg.org/multipage/scripting.html#dom-canvas-toblob
|
2017-06-12 22:34:10 +03:00
|
|
|
OwnerDoc()->Dispatch(
|
|
|
|
TaskCategory::Other,
|
|
|
|
NewRunnableMethod<Blob*, const char*>(
|
|
|
|
"dom::HTMLCanvasElement::ToBlob",
|
|
|
|
&aCallback,
|
|
|
|
static_cast<void (BlobCallback::*)(Blob*, const char*)>(
|
|
|
|
&BlobCallback::Call),
|
|
|
|
nullptr,
|
|
|
|
nullptr));
|
2017-02-14 06:39:16 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-22 09:23:41 +03:00
|
|
|
// Check site-specific permission and display prompt if appropriate.
|
|
|
|
// If no permission, return all-white, opaque image data.
|
|
|
|
bool usePlaceholder = !CanvasUtils::IsImageExtractionAllowed(OwnerDoc(), aCx);
|
2015-10-12 06:21:03 +03:00
|
|
|
CanvasRenderingContextHelper::ToBlob(aCx, global, aCallback, aType,
|
2017-08-22 09:23:41 +03:00
|
|
|
aParams, usePlaceholder, aRv);
|
2015-09-29 18:57:36 +03:00
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
}
|
2013-10-17 06:55:08 +04:00
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
OffscreenCanvas*
|
|
|
|
HTMLCanvasElement::TransferControlToOffscreen(ErrorResult& aRv)
|
|
|
|
{
|
2015-10-05 18:28:25 +03:00
|
|
|
if (mCurrentContext) {
|
2015-10-12 06:21:03 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return nullptr;
|
2015-10-05 05:50:16 +03:00
|
|
|
}
|
2015-09-29 18:57:36 +03:00
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
if (!mOffscreenCanvas) {
|
|
|
|
nsIntSize sz = GetWidthHeight();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AsyncCanvasRenderer> renderer = GetAsyncCanvasRenderer();
|
2015-10-12 06:21:03 +03:00
|
|
|
renderer->SetWidth(sz.width);
|
|
|
|
renderer->SetHeight(sz.height);
|
2015-10-05 18:28:25 +03:00
|
|
|
|
2018-02-23 01:28:39 +03:00
|
|
|
nsPIDOMWindowInner* win = OwnerDoc()->GetInnerWindow();
|
|
|
|
if (!win) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
mOffscreenCanvas = new OffscreenCanvas(win->AsGlobal(),
|
2015-12-18 09:52:17 +03:00
|
|
|
sz.width,
|
2015-10-12 06:21:03 +03:00
|
|
|
sz.height,
|
|
|
|
GetCompositorBackendType(),
|
|
|
|
renderer);
|
2015-12-18 09:52:17 +03:00
|
|
|
if (mWriteOnly) {
|
|
|
|
mOffscreenCanvas->SetWriteOnly();
|
|
|
|
}
|
|
|
|
|
2015-10-16 05:01:53 +03:00
|
|
|
if (!mContextObserver) {
|
|
|
|
mContextObserver = new HTMLCanvasElementObserver(this);
|
|
|
|
}
|
2015-10-12 06:21:03 +03:00
|
|
|
} else {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
}
|
2015-10-05 18:28:25 +03:00
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
return mOffscreenCanvas;
|
2012-10-16 01:15:25 +04:00
|
|
|
}
|
|
|
|
|
2014-10-08 20:15:23 +04:00
|
|
|
already_AddRefed<File>
|
2013-04-13 11:05:40 +04:00
|
|
|
HTMLCanvasElement::MozGetAsFile(const nsAString& aName,
|
|
|
|
const nsAString& aType,
|
2016-11-15 08:18:32 +03:00
|
|
|
CallerType aCallerType,
|
2013-04-13 11:05:40 +04:00
|
|
|
ErrorResult& aRv)
|
2010-09-18 05:14:59 +04:00
|
|
|
{
|
2013-08-12 21:26:31 +04:00
|
|
|
OwnerDoc()->WarnOnceAbout(nsIDocument::eMozGetAsFile);
|
|
|
|
|
2010-09-18 05:14:59 +04:00
|
|
|
// do a trust check if this is a write-only canvas
|
2016-11-15 08:18:32 +03:00
|
|
|
if (mWriteOnly && aCallerType != CallerType::System) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return nullptr;
|
2010-09-18 05:14:59 +04:00
|
|
|
}
|
|
|
|
|
2016-11-15 08:18:32 +03:00
|
|
|
|
|
|
|
RefPtr<File> file;
|
|
|
|
aRv = MozGetAsFileImpl(aName, aType, getter_AddRefs(file));
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return file.forget();
|
2010-09-18 05:14:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-11-15 08:18:32 +03:00
|
|
|
HTMLCanvasElement::MozGetAsFileImpl(const nsAString& aName,
|
2013-01-04 09:16:14 +04:00
|
|
|
const nsAString& aType,
|
2016-11-15 08:18:32 +03:00
|
|
|
File** aResult)
|
2010-09-18 05:14:59 +04:00
|
|
|
{
|
2011-06-08 22:12:18 +04:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2013-10-08 23:00:17 +04:00
|
|
|
nsAutoString type(aType);
|
2017-08-22 09:23:41 +03:00
|
|
|
nsresult rv = ExtractData(nsContentUtils::GetCurrentJSContext(),
|
|
|
|
type, EmptyString(), getter_AddRefs(stream));
|
2013-10-17 06:55:08 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-10-08 23:00:17 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint64_t imgSize;
|
2012-07-30 18:20:58 +04:00
|
|
|
void* imgData = nullptr;
|
2017-11-09 13:18:09 +03:00
|
|
|
rv = NS_ReadInputStreamToBuffer(stream, &imgData, -1, &imgSize);
|
2011-06-08 22:12:18 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> win = do_QueryInterface(OwnerDoc()->GetScopeObject());
|
2014-10-08 20:15:22 +04:00
|
|
|
|
2014-10-08 20:15:23 +04:00
|
|
|
// The File takes ownership of the buffer
|
2016-11-15 08:18:32 +03:00
|
|
|
RefPtr<File> file =
|
2017-11-09 13:18:09 +03:00
|
|
|
File::CreateMemoryFile(win, imgData, imgSize, aName, type, PR_Now());
|
2010-09-18 05:14:59 +04:00
|
|
|
|
2011-11-16 11:50:19 +04:00
|
|
|
file.forget(aResult);
|
|
|
|
return NS_OK;
|
2010-09-18 05:14:59 +04:00
|
|
|
}
|
|
|
|
|
2013-04-13 11:05:33 +04:00
|
|
|
nsresult
|
|
|
|
HTMLCanvasElement::GetContext(const nsAString& aContextId,
|
|
|
|
nsISupports** aContext)
|
|
|
|
{
|
2013-06-07 22:42:12 +04:00
|
|
|
ErrorResult rv;
|
2014-11-11 02:16:50 +03:00
|
|
|
*aContext = GetContext(nullptr, aContextId, JS::NullHandleValue, rv).take();
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2013-04-13 11:05:33 +04:00
|
|
|
}
|
|
|
|
|
2013-06-07 22:42:12 +04:00
|
|
|
already_AddRefed<nsISupports>
|
|
|
|
HTMLCanvasElement::GetContext(JSContext* aCx,
|
|
|
|
const nsAString& aContextId,
|
|
|
|
JS::Handle<JS::Value> aContextOptions,
|
2015-10-12 06:21:03 +03:00
|
|
|
ErrorResult& aRv)
|
2005-04-18 10:33:23 +04:00
|
|
|
{
|
2015-10-12 06:21:03 +03:00
|
|
|
if (mOffscreenCanvas) {
|
2014-11-11 02:16:50 +03:00
|
|
|
return nullptr;
|
2010-03-24 13:47:18 +03:00
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
return CanvasRenderingContextHelper::GetContext(aCx, aContextId,
|
2016-02-05 23:21:09 +03:00
|
|
|
aContextOptions.isObject() ? aContextOptions : JS::NullHandleValue,
|
|
|
|
aRv);
|
2010-03-24 13:47:18 +03:00
|
|
|
}
|
|
|
|
|
2016-11-15 08:18:32 +03:00
|
|
|
already_AddRefed<nsISupports>
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::MozGetIPCContext(const nsAString& aContextId,
|
2016-11-15 08:18:32 +03:00
|
|
|
ErrorResult& aRv)
|
2010-03-24 13:47:18 +03:00
|
|
|
{
|
2016-11-15 08:18:32 +03:00
|
|
|
// Note that we're a [ChromeOnly] method, so from JS we can only be called by
|
|
|
|
// system code.
|
2010-03-24 13:47:18 +03:00
|
|
|
|
|
|
|
// We only support 2d shmem contexts for now.
|
2016-11-15 08:18:32 +03:00
|
|
|
if (!aContextId.EqualsLiteral("2d")) {
|
|
|
|
aRv.Throw(NS_ERROR_INVALID_ARG);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2010-03-24 13:47:18 +03:00
|
|
|
|
2014-11-11 02:16:50 +03:00
|
|
|
CanvasContextType contextType = CanvasContextType::Canvas2D;
|
|
|
|
|
|
|
|
if (!mCurrentContext) {
|
|
|
|
// This canvas doesn't have a context yet.
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsICanvasRenderingContextInternal> context;
|
2015-10-12 06:21:03 +03:00
|
|
|
context = CreateContext(contextType);
|
2014-11-11 02:16:50 +03:00
|
|
|
if (!context) {
|
2016-11-15 08:18:32 +03:00
|
|
|
return nullptr;
|
2010-07-16 00:39:31 +04:00
|
|
|
}
|
2010-03-24 13:47:18 +03:00
|
|
|
|
2014-11-11 02:16:50 +03:00
|
|
|
mCurrentContext = context;
|
2011-10-17 18:59:28 +04:00
|
|
|
mCurrentContext->SetIsIPC(true);
|
2014-11-11 02:16:50 +03:00
|
|
|
mCurrentContextType = contextType;
|
2010-03-24 13:47:18 +03:00
|
|
|
|
2015-10-18 14:14:22 +03:00
|
|
|
ErrorResult dummy;
|
|
|
|
nsresult rv = UpdateContext(nullptr, JS::NullHandleValue, dummy);
|
2016-11-15 08:18:32 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-11-11 02:16:50 +03:00
|
|
|
} else {
|
|
|
|
// We already have a context of some type.
|
2016-11-15 08:18:32 +03:00
|
|
|
if (contextType != mCurrentContextType) {
|
|
|
|
aRv.Throw(NS_ERROR_INVALID_ARG);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2005-04-18 10:33:23 +04:00
|
|
|
}
|
|
|
|
|
2016-11-15 08:18:32 +03:00
|
|
|
nsCOMPtr<nsISupports> context(mCurrentContext);
|
|
|
|
return context.forget();
|
2005-04-18 10:33:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-18 08:04:22 +04:00
|
|
|
nsIntSize
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::GetSize()
|
2005-04-18 10:33:23 +04:00
|
|
|
{
|
2010-05-18 08:04:22 +04:00
|
|
|
return GetWidthHeight();
|
2005-05-13 01:34:56 +04:00
|
|
|
}
|
2006-03-02 03:44:01 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::IsWriteOnly()
|
2006-03-02 03:44:01 +03:00
|
|
|
{
|
|
|
|
return mWriteOnly;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::SetWriteOnly()
|
2006-03-02 03:44:01 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mWriteOnly = true;
|
2006-03-02 03:44:01 +03:00
|
|
|
}
|
2007-09-26 00:46:08 +04:00
|
|
|
|
2010-05-18 08:04:22 +04:00
|
|
|
void
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::InvalidateCanvasContent(const gfx::Rect* damageRect)
|
2007-09-26 00:46:08 +04:00
|
|
|
{
|
2009-07-10 01:09:43 +04:00
|
|
|
// We don't need to flush anything here; if there's no frame or if
|
|
|
|
// we plan to reframe we don't need to invalidate it anyway.
|
2009-12-25 00:20:05 +03:00
|
|
|
nsIFrame *frame = GetPrimaryFrame();
|
2010-05-18 08:04:22 +04:00
|
|
|
if (!frame)
|
|
|
|
return;
|
2007-09-26 00:46:08 +04:00
|
|
|
|
2013-09-04 15:30:57 +04:00
|
|
|
ActiveLayerTracker::NotifyContentChange(frame);
|
2011-03-28 03:59:47 +04:00
|
|
|
|
2017-08-03 08:55:15 +03:00
|
|
|
// When using layers-free WebRender, we cannot invalidate the layer (because there isn't one).
|
|
|
|
// Instead, we mark the CanvasRenderer dirty and scheduling an empty transaction
|
|
|
|
// which is effectively equivalent.
|
|
|
|
CanvasRenderer* renderer = nullptr;
|
2018-03-16 00:35:56 +03:00
|
|
|
RefPtr<WebRenderCanvasData> data = GetWebRenderUserData<WebRenderCanvasData>(frame, static_cast<uint32_t>(DisplayItemType::TYPE_CANVAS));
|
|
|
|
if (data) {
|
|
|
|
renderer = data->GetCanvasRenderer();
|
2011-03-28 03:59:47 +04:00
|
|
|
}
|
2017-08-03 08:55:15 +03:00
|
|
|
|
|
|
|
if (renderer) {
|
|
|
|
renderer->SetDirty();
|
|
|
|
frame->SchedulePaint(nsIFrame::PAINT_COMPOSITE_ONLY);
|
|
|
|
} else {
|
|
|
|
Layer* layer = nullptr;
|
|
|
|
if (damageRect) {
|
|
|
|
nsIntSize size = GetWidthHeight();
|
|
|
|
if (size.width != 0 && size.height != 0) {
|
|
|
|
gfx::IntRect invalRect = gfx::IntRect::Truncate(*damageRect);
|
2017-08-07 07:07:43 +03:00
|
|
|
layer = frame->InvalidateLayer(DisplayItemType::TYPE_CANVAS, &invalRect);
|
2017-08-03 08:55:15 +03:00
|
|
|
}
|
|
|
|
} else {
|
2017-08-07 07:07:43 +03:00
|
|
|
layer = frame->InvalidateLayer(DisplayItemType::TYPE_CANVAS);
|
2017-08-03 08:55:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (layer) {
|
|
|
|
static_cast<CanvasLayer*>(layer)->Updated();
|
|
|
|
}
|
2007-09-26 00:46:08 +04:00
|
|
|
}
|
2012-06-30 01:35:06 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Treat canvas invalidations as animation activity for JS. Frequently
|
|
|
|
* invalidating a canvas will feed into heuristics and cause JIT code to be
|
|
|
|
* kept around longer, for smoother animations.
|
|
|
|
*/
|
2018-02-23 01:28:39 +03:00
|
|
|
nsPIDOMWindowInner* win = OwnerDoc()->GetInnerWindow();
|
2013-06-21 18:25:20 +04:00
|
|
|
|
2018-02-23 01:28:39 +03:00
|
|
|
if (win) {
|
|
|
|
if (JSObject *obj = win->AsGlobal()->GetGlobalJSObject()) {
|
2012-06-30 01:35:06 +04:00
|
|
|
js::NotifyAnimationActivity(obj);
|
|
|
|
}
|
|
|
|
}
|
2007-09-26 00:46:08 +04:00
|
|
|
}
|
2008-04-30 21:04:46 +04:00
|
|
|
|
2011-03-28 03:59:47 +04:00
|
|
|
void
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::InvalidateCanvas()
|
2011-03-28 03:59:47 +04:00
|
|
|
{
|
|
|
|
// We don't need to flush anything here; if there's no frame or if
|
|
|
|
// we plan to reframe we don't need to invalidate it anyway.
|
|
|
|
nsIFrame *frame = GetPrimaryFrame();
|
|
|
|
if (!frame)
|
|
|
|
return;
|
|
|
|
|
2012-08-29 09:39:31 +04:00
|
|
|
frame->InvalidateFrame();
|
2011-03-28 03:59:47 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::CountContexts()
|
2008-04-30 21:04:46 +04:00
|
|
|
{
|
|
|
|
if (mCurrentContext)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsICanvasRenderingContextInternal *
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::GetContextAtIndex(int32_t index)
|
2008-04-30 21:04:46 +04:00
|
|
|
{
|
|
|
|
if (mCurrentContext && index == 0)
|
2011-11-16 11:50:19 +04:00
|
|
|
return mCurrentContext;
|
2008-04-30 21:04:46 +04:00
|
|
|
|
2013-04-03 05:14:24 +04:00
|
|
|
return nullptr;
|
2008-04-30 21:04:46 +04:00
|
|
|
}
|
2009-03-05 16:43:45 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::GetIsOpaque()
|
2009-03-05 16:43:45 +03:00
|
|
|
{
|
2014-03-16 23:34:14 +04:00
|
|
|
if (mCurrentContext) {
|
|
|
|
return mCurrentContext->GetIsOpaque();
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
return GetOpaqueAttr();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
HTMLCanvasElement::GetOpaqueAttr()
|
|
|
|
{
|
2009-03-05 16:43:45 +03:00
|
|
|
return HasAttr(kNameSpaceID_None, nsGkAtoms::moz_opaque);
|
|
|
|
}
|
2010-05-18 08:04:22 +04:00
|
|
|
|
2015-12-18 09:52:17 +03:00
|
|
|
already_AddRefed<Layer>
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::GetCanvasLayer(nsDisplayListBuilder* aBuilder,
|
2015-12-18 09:52:17 +03:00
|
|
|
Layer *aOldLayer,
|
2013-01-04 09:16:14 +04:00
|
|
|
LayerManager *aManager)
|
2010-05-18 08:04:22 +04:00
|
|
|
{
|
2015-10-12 06:21:03 +03:00
|
|
|
// The address of sOffscreenCanvasLayerUserDataDummy is used as the user
|
|
|
|
// data key for retained LayerManagers managed by FrameLayerBuilder.
|
|
|
|
// We don't much care about what value in it, so just assign a dummy
|
|
|
|
// value for it.
|
|
|
|
static uint8_t sOffscreenCanvasLayerUserDataDummy = 0;
|
|
|
|
|
|
|
|
if (mCurrentContext) {
|
2017-07-22 03:30:34 +03:00
|
|
|
return mCurrentContext->GetCanvasLayer(aBuilder, aOldLayer, aManager);
|
2015-10-12 06:21:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mOffscreenCanvas) {
|
2015-10-12 06:21:03 +03:00
|
|
|
if (!mResetLayer &&
|
|
|
|
aOldLayer && aOldLayer->HasUserData(&sOffscreenCanvasLayerUserDataDummy)) {
|
2015-12-18 09:52:17 +03:00
|
|
|
RefPtr<Layer> ret = aOldLayer;
|
2015-10-12 06:21:03 +03:00
|
|
|
return ret.forget();
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CanvasLayer> layer = aManager->CreateCanvasLayer();
|
2015-10-12 06:21:03 +03:00
|
|
|
if (!layer) {
|
|
|
|
NS_WARNING("CreateCanvasLayer failed!");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-10-05 05:50:16 +03:00
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
LayerUserData* userData = nullptr;
|
|
|
|
layer->SetUserData(&sOffscreenCanvasLayerUserDataDummy, userData);
|
2015-10-12 06:21:03 +03:00
|
|
|
|
2017-08-03 08:55:14 +03:00
|
|
|
CanvasRenderer* canvasRenderer = layer->CreateOrGetCanvasRenderer();
|
2017-08-18 10:13:29 +03:00
|
|
|
|
|
|
|
if (!InitializeCanvasRenderer(aBuilder, canvasRenderer)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-10-12 06:21:03 +03:00
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
layer->Updated();
|
|
|
|
return layer.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2010-05-18 08:04:22 +04:00
|
|
|
}
|
|
|
|
|
2017-10-28 13:07:27 +03:00
|
|
|
bool
|
|
|
|
HTMLCanvasElement::UpdateWebRenderCanvasData(nsDisplayListBuilder* aBuilder,
|
|
|
|
WebRenderCanvasData* aCanvasData)
|
|
|
|
{
|
|
|
|
if (mCurrentContext) {
|
|
|
|
return mCurrentContext->UpdateWebRenderCanvasData(aBuilder, aCanvasData);
|
|
|
|
}
|
|
|
|
if (mOffscreenCanvas) {
|
|
|
|
CanvasRenderer* renderer = aCanvasData->GetCanvasRenderer();
|
|
|
|
|
|
|
|
if(!mResetLayer && renderer) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderer = aCanvasData->CreateCanvasRenderer();
|
|
|
|
if (!InitializeCanvasRenderer(aBuilder, renderer)) {
|
|
|
|
// Clear CanvasRenderer of WebRenderCanvasData
|
|
|
|
aCanvasData->ClearCanvasRenderer();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(renderer);
|
|
|
|
mResetLayer = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear CanvasRenderer of WebRenderCanvasData
|
|
|
|
aCanvasData->ClearCanvasRenderer();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-18 10:13:29 +03:00
|
|
|
bool
|
2017-08-03 08:55:14 +03:00
|
|
|
HTMLCanvasElement::InitializeCanvasRenderer(nsDisplayListBuilder* aBuilder,
|
|
|
|
CanvasRenderer* aRenderer)
|
|
|
|
{
|
|
|
|
if (mCurrentContext) {
|
2017-08-18 10:13:29 +03:00
|
|
|
return mCurrentContext->InitializeCanvasRenderer(aBuilder, aRenderer);
|
2017-08-03 08:55:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mOffscreenCanvas) {
|
|
|
|
CanvasInitializeData data;
|
|
|
|
data.mRenderer = GetAsyncCanvasRenderer();
|
|
|
|
data.mSize = GetWidthHeight();
|
|
|
|
aRenderer->Initialize(data);
|
2017-08-18 10:13:29 +03:00
|
|
|
return true;
|
2017-08-03 08:55:14 +03:00
|
|
|
}
|
2017-08-18 10:13:29 +03:00
|
|
|
|
2017-10-28 13:07:27 +03:00
|
|
|
return false;
|
2017-08-03 08:55:14 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2015-10-12 06:21:03 +03:00
|
|
|
HTMLCanvasElement::ShouldForceInactiveLayer(LayerManager* aManager)
|
2011-07-08 14:42:21 +04:00
|
|
|
{
|
2015-10-12 06:21:03 +03:00
|
|
|
if (mCurrentContext) {
|
|
|
|
return mCurrentContext->ShouldForceInactiveLayer(aManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mOffscreenCanvas) {
|
|
|
|
// TODO: We should handle offscreen canvas case.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2011-07-08 14:42:21 +04:00
|
|
|
}
|
|
|
|
|
2010-05-18 08:04:22 +04:00
|
|
|
void
|
2013-01-04 09:16:14 +04:00
|
|
|
HTMLCanvasElement::MarkContextClean()
|
2010-05-18 08:04:22 +04:00
|
|
|
{
|
|
|
|
if (!mCurrentContext)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mCurrentContext->MarkContextClean();
|
|
|
|
}
|
|
|
|
|
2015-09-17 06:37:05 +03:00
|
|
|
void
|
|
|
|
HTMLCanvasElement::MarkContextCleanForFrameCapture()
|
|
|
|
{
|
|
|
|
if (!mCurrentContext)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mCurrentContext->MarkContextCleanForFrameCapture();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
HTMLCanvasElement::IsContextCleanForFrameCapture()
|
|
|
|
{
|
|
|
|
return mCurrentContext && mCurrentContext->IsContextCleanForFrameCapture();
|
|
|
|
}
|
|
|
|
|
2015-12-22 06:29:26 +03:00
|
|
|
nsresult
|
2017-08-22 09:23:41 +03:00
|
|
|
HTMLCanvasElement::RegisterFrameCaptureListener(FrameCaptureListener* aListener,
|
|
|
|
bool aReturnPlaceholderData)
|
2015-09-17 07:36:57 +03:00
|
|
|
{
|
|
|
|
WeakPtr<FrameCaptureListener> listener = aListener;
|
|
|
|
|
|
|
|
if (mRequestedFrameListeners.Contains(listener)) {
|
2015-12-22 06:29:26 +03:00
|
|
|
return NS_OK;
|
2015-09-17 07:36:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mRequestedFrameRefreshObserver) {
|
|
|
|
nsIDocument* doc = OwnerDoc();
|
2015-12-22 06:29:26 +03:00
|
|
|
if (!doc) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2015-09-17 07:36:57 +03:00
|
|
|
|
2015-12-22 06:29:26 +03:00
|
|
|
while (doc->GetParentDocument()) {
|
|
|
|
doc = doc->GetParentDocument();
|
|
|
|
}
|
|
|
|
|
2018-02-21 01:00:10 +03:00
|
|
|
nsPresContext* context = doc->GetPresContext();
|
2015-12-22 06:29:26 +03:00
|
|
|
if (!context) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2015-09-17 07:36:57 +03:00
|
|
|
|
|
|
|
context = context->GetRootPresContext();
|
2015-12-22 06:29:26 +03:00
|
|
|
if (!context) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2015-09-17 07:36:57 +03:00
|
|
|
|
|
|
|
nsRefreshDriver* driver = context->RefreshDriver();
|
2015-12-22 06:29:26 +03:00
|
|
|
if (!driver) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2015-09-17 07:36:57 +03:00
|
|
|
|
|
|
|
mRequestedFrameRefreshObserver =
|
2017-08-22 09:23:41 +03:00
|
|
|
new RequestedFrameRefreshObserver(this, driver, aReturnPlaceholderData);
|
|
|
|
} else {
|
|
|
|
mRequestedFrameRefreshObserver->SetReturnPlaceholderData(aReturnPlaceholderData);
|
2015-09-17 07:36:57 +03:00
|
|
|
}
|
|
|
|
|
2015-12-22 06:29:26 +03:00
|
|
|
mRequestedFrameListeners.AppendElement(listener);
|
2015-09-17 07:36:57 +03:00
|
|
|
mRequestedFrameRefreshObserver->Register();
|
2015-12-22 06:29:26 +03:00
|
|
|
return NS_OK;
|
2015-09-17 07:36:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
HTMLCanvasElement::IsFrameCaptureRequested() const
|
|
|
|
{
|
|
|
|
for (WeakPtr<FrameCaptureListener> listener : mRequestedFrameListeners) {
|
|
|
|
if (!listener) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listener->FrameCaptureRequested()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-09-12 18:59:38 +03:00
|
|
|
HTMLCanvasElement::ProcessDestroyedFrameListeners()
|
2015-09-17 07:36:57 +03:00
|
|
|
{
|
|
|
|
// Loop backwards to allow removing elements in the loop.
|
|
|
|
for (int i = mRequestedFrameListeners.Length() - 1; i >= 0; --i) {
|
|
|
|
WeakPtr<FrameCaptureListener> listener = mRequestedFrameListeners[i];
|
|
|
|
if (!listener) {
|
|
|
|
// listener was destroyed. Remove it from the list.
|
|
|
|
mRequestedFrameListeners.RemoveElementAt(i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mRequestedFrameListeners.IsEmpty()) {
|
|
|
|
mRequestedFrameRefreshObserver->Unregister();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-12 18:59:38 +03:00
|
|
|
void
|
2017-01-13 13:56:03 +03:00
|
|
|
HTMLCanvasElement::SetFrameCapture(already_AddRefed<SourceSurface> aSurface,
|
|
|
|
const TimeStamp& aTime)
|
2016-09-12 18:59:38 +03:00
|
|
|
{
|
|
|
|
RefPtr<SourceSurface> surface = aSurface;
|
|
|
|
RefPtr<SourceSurfaceImage> image = new SourceSurfaceImage(surface->GetSize(), surface);
|
|
|
|
|
|
|
|
for (WeakPtr<FrameCaptureListener> listener : mRequestedFrameListeners) {
|
|
|
|
if (!listener) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Image> imageRefCopy = image.get();
|
2017-01-13 13:56:03 +03:00
|
|
|
listener->NewFrame(imageRefCopy.forget(), aTime);
|
2016-09-12 18:59:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<SourceSurface>
|
2017-04-12 12:55:23 +03:00
|
|
|
HTMLCanvasElement::GetSurfaceSnapshot(gfxAlphaType* const aOutAlphaType)
|
2014-04-17 09:29:52 +04:00
|
|
|
{
|
|
|
|
if (!mCurrentContext)
|
|
|
|
return nullptr;
|
|
|
|
|
2017-04-12 12:55:23 +03:00
|
|
|
return mCurrentContext->GetSurfaceSnapshot(aOutAlphaType);
|
2014-04-17 09:29:52 +04:00
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:02 +03:00
|
|
|
AsyncCanvasRenderer*
|
|
|
|
HTMLCanvasElement::GetAsyncCanvasRenderer()
|
|
|
|
{
|
|
|
|
if (!mAsyncCanvasRenderer) {
|
|
|
|
mAsyncCanvasRenderer = new AsyncCanvasRenderer();
|
|
|
|
mAsyncCanvasRenderer->mHTMLCanvasElement = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mAsyncCanvasRenderer;
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
layers::LayersBackend
|
|
|
|
HTMLCanvasElement::GetCompositorBackendType() const
|
|
|
|
{
|
|
|
|
nsIWidget* docWidget = nsContentUtils::WidgetForDocument(OwnerDoc());
|
|
|
|
if (docWidget) {
|
|
|
|
layers::LayerManager* layerManager = docWidget->GetLayerManager();
|
2016-11-07 21:06:01 +03:00
|
|
|
if (layerManager) {
|
|
|
|
return layerManager->GetCompositorBackendType();
|
|
|
|
}
|
2015-10-12 06:21:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return LayersBackend::LAYERS_NONE;
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
void
|
|
|
|
HTMLCanvasElement::OnVisibilityChange()
|
|
|
|
{
|
|
|
|
if (OwnerDoc()->Hidden()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mOffscreenCanvas) {
|
2016-04-11 21:40:06 +03:00
|
|
|
class Runnable final : public CancelableRunnable
|
2015-10-12 06:21:03 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit Runnable(AsyncCanvasRenderer* aRenderer)
|
2017-06-12 22:34:10 +03:00
|
|
|
: mozilla::CancelableRunnable("Runnable")
|
|
|
|
, mRenderer(aRenderer)
|
2015-10-12 06:21:03 +03:00
|
|
|
{}
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2015-10-12 06:21:03 +03:00
|
|
|
{
|
|
|
|
if (mRenderer && mRenderer->mContext) {
|
|
|
|
mRenderer->mContext->OnVisibilityChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AsyncCanvasRenderer> mRenderer;
|
2015-10-12 06:21:03 +03:00
|
|
|
};
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIRunnable> runnable = new Runnable(mAsyncCanvasRenderer);
|
2017-06-01 23:43:00 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> activeTarget = mAsyncCanvasRenderer->GetActiveEventTarget();
|
2017-06-01 23:42:05 +03:00
|
|
|
if (activeTarget) {
|
|
|
|
activeTarget->Dispatch(runnable, nsIThread::DISPATCH_NORMAL);
|
2015-10-12 06:21:03 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mCurrentContext) {
|
|
|
|
mCurrentContext->OnVisibilityChange();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HTMLCanvasElement::OnMemoryPressure()
|
|
|
|
{
|
|
|
|
if (mOffscreenCanvas) {
|
2016-04-11 21:40:06 +03:00
|
|
|
class Runnable final : public CancelableRunnable
|
2015-10-12 06:21:03 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit Runnable(AsyncCanvasRenderer* aRenderer)
|
2017-06-12 22:34:10 +03:00
|
|
|
: mozilla::CancelableRunnable("Runnable")
|
|
|
|
, mRenderer(aRenderer)
|
2015-10-12 06:21:03 +03:00
|
|
|
{}
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2015-10-12 06:21:03 +03:00
|
|
|
{
|
|
|
|
if (mRenderer && mRenderer->mContext) {
|
|
|
|
mRenderer->mContext->OnMemoryPressure();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AsyncCanvasRenderer> mRenderer;
|
2015-10-12 06:21:03 +03:00
|
|
|
};
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIRunnable> runnable = new Runnable(mAsyncCanvasRenderer);
|
2017-06-01 23:43:00 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> activeTarget = mAsyncCanvasRenderer->GetActiveEventTarget();
|
2017-06-01 23:42:05 +03:00
|
|
|
if (activeTarget) {
|
|
|
|
activeTarget->Dispatch(runnable, nsIThread::DISPATCH_NORMAL);
|
2015-10-12 06:21:03 +03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mCurrentContext) {
|
|
|
|
mCurrentContext->OnMemoryPressure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:02 +03:00
|
|
|
/* static */ void
|
|
|
|
HTMLCanvasElement::SetAttrFromAsyncCanvasRenderer(AsyncCanvasRenderer *aRenderer)
|
|
|
|
{
|
|
|
|
HTMLCanvasElement *element = aRenderer->mHTMLCanvasElement;
|
|
|
|
if (!element) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
if (element->GetWidthHeight() == aRenderer->GetSize()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:21:02 +03:00
|
|
|
gfx::IntSize asyncCanvasSize = aRenderer->GetSize();
|
|
|
|
|
|
|
|
ErrorResult rv;
|
2016-05-05 21:29:54 +03:00
|
|
|
element->SetUnsignedIntAttr(nsGkAtoms::width, asyncCanvasSize.width,
|
|
|
|
DEFAULT_CANVAS_WIDTH, rv);
|
2015-10-12 06:21:02 +03:00
|
|
|
if (rv.Failed()) {
|
|
|
|
NS_WARNING("Failed to set width attribute to a canvas element asynchronously.");
|
|
|
|
}
|
|
|
|
|
2016-05-05 21:29:54 +03:00
|
|
|
element->SetUnsignedIntAttr(nsGkAtoms::height, asyncCanvasSize.height,
|
|
|
|
DEFAULT_CANVAS_HEIGHT, rv);
|
2015-10-12 06:21:02 +03:00
|
|
|
if (rv.Failed()) {
|
|
|
|
NS_WARNING("Failed to set height attribute to a canvas element asynchronously.");
|
|
|
|
}
|
2015-10-12 06:21:03 +03:00
|
|
|
|
|
|
|
element->mResetLayer = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
HTMLCanvasElement::InvalidateFromAsyncCanvasRenderer(AsyncCanvasRenderer *aRenderer)
|
|
|
|
{
|
|
|
|
HTMLCanvasElement *element = aRenderer->mHTMLCanvasElement;
|
|
|
|
if (!element) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
element->InvalidateCanvasContent(nullptr);
|
2015-10-12 06:21:02 +03:00
|
|
|
}
|
|
|
|
|
2016-02-25 02:54:50 +03:00
|
|
|
already_AddRefed<layers::SharedSurfaceTextureClient>
|
|
|
|
HTMLCanvasElement::GetVRFrame()
|
|
|
|
{
|
|
|
|
if (GetCurrentContextType() != CanvasContextType::WebGL1 &&
|
|
|
|
GetCurrentContextType() != CanvasContextType::WebGL2) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
WebGLContext* webgl = static_cast<WebGLContext*>(GetContextAtIndex(0));
|
|
|
|
if (!webgl) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return webgl->GetVRFrame();
|
|
|
|
}
|
|
|
|
|
2013-01-04 09:16:14 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|