fune/dom/webgpu/CanvasContext.h
sotaro 954c16acec Bug 1805209 - Use RemoteTexture for WebGPU r=gfx-reviewers,lsalzman
WebGPU uses CompositableInProcessManager to push TextureHost directly from WebGPUParent to WebRender. But CompositableInProcessManager plumbing has a problem and caused Bug 1805209.

gecko already has a similar mechanism, called RemoteTextureMap. It is used in oop WebGL. If WebGPU uses RemoteTextureMap instead of CompositableInProcessManager, both WebGPU and oop WebGL use same mechanism.

WebGPUParent pushes a new texture to RemoteTextureMap. The RemoteTextureMap notifies the pushed texture to WebRenderImageHost.

Before the change, only one TextureHost is used for one swap chain. With the change, multiple TextureHosts are used for one swap chain with recycling.

The changes are followings.

- Use RemoteTextureMap instead of CompositableInProcessManager.
- Use RemoteTextureOwnerId instead of CompositableHandle.
- Use WebRenderCanvasData instead of WebRenderInProcessImageData.
- Add remote texture pushed callback functionality to RemoteTextureMap. With it, RemoteTextureMap notifies a new pushed remote texture to WebRenderImageHost.
- Remove CompositableInProcessManager.

Differential Revision: https://phabricator.services.mozilla.com/D164890
2022-12-23 20:41:02 +00:00

109 lines
3.4 KiB
C++

/* -*- 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/. */
#ifndef GPU_CanvasContext_H_
#define GPU_CanvasContext_H_
#include "nsICanvasRenderingContextInternal.h"
#include "nsWrapperCache.h"
#include "ObjectModel.h"
#include "mozilla/layers/LayersTypes.h"
#include "mozilla/webrender/WebRenderAPI.h"
namespace mozilla {
namespace dom {
class Promise;
struct GPUCanvasConfiguration;
enum class GPUTextureFormat : uint8_t;
} // namespace dom
namespace webgpu {
class Adapter;
class Texture;
class CanvasContext final : public nsICanvasRenderingContextInternal,
public nsWrapperCache {
private:
virtual ~CanvasContext();
void Cleanup();
public:
// nsISupports interface + CC
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(CanvasContext)
CanvasContext();
JSObject* WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) override;
public: // nsICanvasRenderingContextInternal
int32_t GetWidth() override { return mWidth; }
int32_t GetHeight() override { return mHeight; }
NS_IMETHOD SetDimensions(int32_t aWidth, int32_t aHeight) override {
mWidth = aWidth;
mHeight = aHeight;
return NS_OK;
}
NS_IMETHOD InitializeWithDrawTarget(
nsIDocShell* aShell, NotNull<gfx::DrawTarget*> aTarget) override {
return NS_OK;
}
bool UpdateWebRenderCanvasData(mozilla::nsDisplayListBuilder* aBuilder,
WebRenderCanvasData* aCanvasData) override;
bool InitializeCanvasRenderer(nsDisplayListBuilder* aBuilder,
layers::CanvasRenderer* aRenderer) override;
mozilla::UniquePtr<uint8_t[]> GetImageBuffer(int32_t* aFormat) override;
NS_IMETHOD GetInputStream(const char* aMimeType,
const nsAString& aEncoderOptions,
nsIInputStream** aStream) override;
already_AddRefed<mozilla::gfx::SourceSurface> GetSurfaceSnapshot(
gfxAlphaType* aOutAlphaType) override;
void SetOpaqueValueFromOpaqueAttr(bool aOpaqueAttrValue) override {}
bool GetIsOpaque() override { return true; }
void ResetBitmap() override { Unconfigure(); }
void MarkContextClean() override {}
NS_IMETHOD Redraw(const gfxRect& aDirty) override { return NS_OK; }
void DidRefresh() override {}
void MarkContextCleanForFrameCapture() override {}
Watchable<FrameCaptureState>* GetFrameCaptureState() override {
return nullptr;
}
public:
void Configure(const dom::GPUCanvasConfiguration& aDesc);
void Unconfigure();
dom::GPUTextureFormat GetPreferredFormat(Adapter& aAdapter) const;
RefPtr<Texture> GetCurrentTexture(ErrorResult& aRv);
void MaybeQueueSwapChainPresent();
void SwapChainPresent();
void ForceNewFrame();
private:
uint32_t mWidth = 0, mHeight = 0;
bool mPendingSwapChainPresent = false;
RefPtr<WebGPUChild> mBridge;
RefPtr<Texture> mTexture;
gfx::SurfaceFormat mGfxFormat = gfx::SurfaceFormat::R8G8B8A8;
gfx::IntSize mGfxSize;
Maybe<layers::RemoteTextureId> mLastRemoteTextureId;
Maybe<layers::RemoteTextureOwnerId> mRemoteTextureOwnerId;
};
} // namespace webgpu
} // namespace mozilla
#endif // GPU_CanvasContext_H_