summaryrefslogtreecommitdiffstats
path: root/dom/webgpu/CanvasContext.cpp
blob: a7a8b8ed9b492aea81bcc4c4bdc9e031dad6a0ea (plain)
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
/* -*- 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"
#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"
#include "ipc/WebGPUChild.h"

namespace mozilla {
namespace webgpu {

NS_IMPL_CYCLE_COLLECTING_ADDREF(CanvasContext)
NS_IMPL_CYCLE_COLLECTING_RELEASE(CanvasContext)

GPU_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(CanvasContext, mSwapChain,
                                       mCanvasElement, mOffscreenCanvas)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CanvasContext)
  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  NS_INTERFACE_MAP_ENTRY(nsICanvasRenderingContextInternal)
  NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END

CanvasContext::CanvasContext()
    : mExternalImageId(layers::CompositorManagerChild::GetInstance()
                           ->GetNextExternalImageId()) {}

CanvasContext::~CanvasContext() {
  Cleanup();
  RemovePostRefreshObserver();
}

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);

  // 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;
  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;
}

}  // namespace webgpu
}  // namespace mozilla