2019-10-02 19:46:03 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/WebGPUBinding.h"
|
|
|
|
#include "CanvasContext.h"
|
2020-04-07 01:29:18 +03:00
|
|
|
#include "SwapChain.h"
|
|
|
|
#include "nsDisplayList.h"
|
|
|
|
#include "LayerUserData.h"
|
|
|
|
#include "mozilla/dom/HTMLCanvasElement.h"
|
|
|
|
#include "mozilla/layers/CompositorManagerChild.h"
|
|
|
|
#include "mozilla/layers/RenderRootStateManager.h"
|
|
|
|
#include "mozilla/layers/WebRenderBridgeChild.h"
|
2019-10-02 19:46:03 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace webgpu {
|
|
|
|
|
2020-04-06 23:46:51 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(CanvasContext)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(CanvasContext)
|
2020-04-06 23:10:03 +03:00
|
|
|
|
2020-04-07 01:29:18 +03:00
|
|
|
GPU_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(CanvasContext, mSwapChain,
|
|
|
|
mCanvasElement, mOffscreenCanvas)
|
2020-04-06 23:10:03 +03:00
|
|
|
|
2020-04-06 23:46:51 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CanvasContext)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsICanvasRenderingContextInternal)
|
2020-04-07 01:29:18 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2020-04-06 23:46:51 +03:00
|
|
|
NS_INTERFACE_MAP_END
|
2020-04-06 23:10:03 +03:00
|
|
|
|
2020-04-07 01:29:18 +03:00
|
|
|
CanvasContext::CanvasContext()
|
|
|
|
: mExternalImageId(layers::CompositorManagerChild::GetInstance()
|
2020-04-21 02:50:04 +03:00
|
|
|
->GetNextExternalImageId()) {}
|
2020-04-07 01:29:18 +03:00
|
|
|
|
2020-04-28 18:03:41 +03:00
|
|
|
CanvasContext::~CanvasContext() {
|
|
|
|
Cleanup();
|
|
|
|
RemovePostRefreshObserver();
|
|
|
|
}
|
2020-04-07 01:29:18 +03:00
|
|
|
|
|
|
|
void CanvasContext::Cleanup() {
|
|
|
|
if (mSwapChain) {
|
|
|
|
mSwapChain->Destroy(mExternalImageId);
|
|
|
|
mSwapChain = nullptr;
|
|
|
|
}
|
|
|
|
if (mRenderRootStateManager && mImageKey) {
|
|
|
|
mRenderRootStateManager->AddImageKeyForDiscard(mImageKey.value());
|
|
|
|
mRenderRootStateManager = nullptr;
|
|
|
|
mImageKey.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* CanvasContext::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
|
|
|
return dom::GPUCanvasContext_Binding::Wrap(aCx, this, aGivenProto);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<layers::Layer> CanvasContext::GetCanvasLayer(
|
|
|
|
nsDisplayListBuilder* aBuilder, layers::Layer* aOldLayer,
|
|
|
|
layers::LayerManager* aManager) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CanvasContext::UpdateWebRenderCanvasData(
|
|
|
|
nsDisplayListBuilder* aBuilder, WebRenderCanvasData* aCanvasData) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<SwapChain> CanvasContext::ConfigureSwapChain(
|
|
|
|
const dom::GPUSwapChainDescriptor& aDesc, ErrorResult& aRv) {
|
|
|
|
Cleanup();
|
|
|
|
|
|
|
|
gfx::SurfaceFormat format;
|
|
|
|
switch (aDesc.mFormat) {
|
|
|
|
case dom::GPUTextureFormat::Rgba8unorm:
|
|
|
|
format = gfx::SurfaceFormat::R8G8B8A8;
|
|
|
|
break;
|
|
|
|
case dom::GPUTextureFormat::Bgra8unorm:
|
|
|
|
format = gfx::SurfaceFormat::B8G8R8A8;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
dom::GPUExtent3DDict extent;
|
|
|
|
extent.mWidth = mWidth;
|
|
|
|
extent.mHeight = mHeight;
|
|
|
|
extent.mDepth = 1;
|
|
|
|
mSwapChain = new SwapChain(aDesc, extent, mExternalImageId, format);
|
2020-04-21 02:50:04 +03:00
|
|
|
|
|
|
|
// Force a new frame to be built, which will execute the
|
|
|
|
// `CanvasContextType::WebGPU` switch case in `CreateWebRenderCommands` and
|
|
|
|
// populate the WR user data.
|
|
|
|
mCanvasElement->InvalidateCanvas();
|
|
|
|
|
|
|
|
mSwapChain->GetCurrentTexture()->mTargetCanvasElement = mCanvasElement;
|
2020-04-07 01:29:18 +03:00
|
|
|
return mSwapChain;
|
|
|
|
}
|
|
|
|
|
|
|
|
Maybe<wr::ImageKey> CanvasContext::GetImageKey() const { return mImageKey; }
|
|
|
|
|
|
|
|
wr::ImageKey CanvasContext::CreateImageKey(
|
|
|
|
layers::RenderRootStateManager* aManager) {
|
|
|
|
const auto key = aManager->WrBridge()->GetNextImageKey();
|
|
|
|
mRenderRootStateManager = aManager;
|
|
|
|
mImageKey = Some(key);
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CanvasContext::UpdateWebRenderLocalCanvasData(
|
|
|
|
layers::WebRenderLocalCanvasData* aCanvasData) {
|
|
|
|
if (!mSwapChain || !mSwapChain->GetGpuBridge()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aCanvasData->mGpuBridge = mSwapChain->GetGpuBridge();
|
|
|
|
aCanvasData->mGpuTextureId = mSwapChain->GetCurrentTexture()->mId;
|
|
|
|
aCanvasData->mExternalImageId = mExternalImageId;
|
|
|
|
aCanvasData->mFormat = mSwapChain->mFormat;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-02 19:46:03 +03:00
|
|
|
} // namespace webgpu
|
|
|
|
} // namespace mozilla
|