Update configs. IGNORE BROKEN CHANGESETS CLOSED TREE NO BUG a=release ba=release
[gecko.git] / gfx / layers / CanvasRenderer.cpp
blobd69f60e36cadfc6b5dbc6f06d610faf9c1d92f29
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "CanvasRenderer.h"
9 #include "BuildConstants.h"
10 #include "ipc/KnowsCompositor.h"
11 #include "mozilla/gfx/gfxVars.h"
12 #include "mozilla/StaticPrefs_webgl.h"
13 #include "nsICanvasRenderingContextInternal.h"
14 #include "PersistentBufferProvider.h"
15 #include "WebGLTypes.h"
17 #ifdef MOZ_WIDGET_GTK
18 # include "mozilla/widget/DMABufSurface.h"
19 # include "mozilla/widget/DMABufLibWrapper.h"
20 #endif
22 namespace mozilla {
23 namespace layers {
25 CanvasRendererData::CanvasRendererData() = default;
26 CanvasRendererData::~CanvasRendererData() = default;
28 // -
30 BorrowedSourceSurface::BorrowedSourceSurface(
31 PersistentBufferProvider* const returnTo,
32 const RefPtr<gfx::SourceSurface> surf)
33 : mReturnTo(returnTo), mSurf(surf) {}
35 BorrowedSourceSurface::~BorrowedSourceSurface() {
36 if (mReturnTo) {
37 auto forgettable = mSurf;
38 mReturnTo->ReturnSnapshot(forgettable.forget());
42 // -
44 CanvasRenderer::CanvasRenderer() { MOZ_COUNT_CTOR(CanvasRenderer); }
46 CanvasRenderer::~CanvasRenderer() { MOZ_COUNT_DTOR(CanvasRenderer); }
48 void CanvasRenderer::Initialize(const CanvasRendererData& aData) {
49 mData = aData;
52 bool CanvasRenderer::IsDataValid(const CanvasRendererData& aData) const {
53 return mData.GetContext() == aData.GetContext();
56 std::shared_ptr<BorrowedSourceSurface> CanvasRenderer::BorrowSnapshot(
57 const bool requireAlphaPremult) const {
58 const auto context = mData.GetContext();
59 if (!context) return nullptr;
60 RefPtr<PersistentBufferProvider> provider = context->GetBufferProvider();
62 RefPtr<gfx::SourceSurface> ss;
64 if (provider) {
65 ss = provider->BorrowSnapshot();
67 if (!ss) {
68 provider = nullptr;
69 ss = context->GetFrontBufferSnapshot(requireAlphaPremult);
71 if (!ss) return nullptr;
73 return std::make_shared<BorrowedSourceSurface>(provider, ss);
76 void CanvasRenderer::FirePreTransactionCallback() const {
77 if (!mData.mDoPaintCallbacks) return;
78 const auto context = mData.GetContext();
79 if (!context) return;
80 context->OnBeforePaintTransaction();
83 void CanvasRenderer::FireDidTransactionCallback() const {
84 if (!mData.mDoPaintCallbacks) return;
85 const auto context = mData.GetContext();
86 if (!context) return;
87 context->OnDidPaintTransaction();
90 TextureType TexTypeForWebgl(KnowsCompositor* const knowsCompositor) {
91 if (!knowsCompositor) return TextureType::Unknown;
92 const auto layersBackend = knowsCompositor->GetCompositorBackendType();
94 switch (layersBackend) {
95 case LayersBackend::LAYERS_LAST:
96 MOZ_CRASH("Unexpected LayersBackend::LAYERS_LAST");
98 case LayersBackend::LAYERS_NONE:
99 return TextureType::Unknown;
101 case LayersBackend::LAYERS_WR:
102 break;
105 if (kIsWindows) {
106 if (knowsCompositor->SupportsD3D11()) {
107 return TextureType::D3D11;
110 if (kIsMacOS) {
111 return TextureType::MacIOSurface;
114 #ifdef MOZ_WIDGET_GTK
115 if (kIsLinux) {
116 if (!knowsCompositor->UsingSoftwareWebRender() &&
117 widget::DMABufDevice::IsDMABufWebGLEnabled()) {
118 return TextureType::DMABUF;
121 #endif
123 if (kIsAndroid) {
124 if (gfx::gfxVars::UseAHardwareBufferSharedSurfaceWebglOop()) {
125 return TextureType::AndroidHardwareBuffer;
127 if (StaticPrefs::webgl_enable_surface_texture()) {
128 return TextureType::AndroidNativeWindow;
132 return TextureType::Unknown;
135 } // namespace layers
136 } // namespace mozilla