CanvasRenderer.cpp (3938B)
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/. */ 6 7 #include "CanvasRenderer.h" 8 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" 16 17 #ifdef MOZ_WIDGET_GTK 18 # include "mozilla/widget/DMABufSurface.h" 19 # include "mozilla/widget/DMABufDevice.h" 20 #endif 21 22 namespace mozilla { 23 namespace layers { 24 25 CanvasRendererData::CanvasRendererData() = default; 26 CanvasRendererData::~CanvasRendererData() = default; 27 28 // - 29 30 BorrowedSourceSurface::BorrowedSourceSurface( 31 PersistentBufferProvider* const returnTo, 32 const RefPtr<gfx::SourceSurface> surf) 33 : mReturnTo(returnTo), mSurf(surf) {} 34 35 BorrowedSourceSurface::~BorrowedSourceSurface() { 36 if (mReturnTo) { 37 auto forgettable = mSurf; 38 mReturnTo->ReturnSnapshot(forgettable.forget()); 39 } 40 } 41 42 // - 43 44 CanvasRenderer::CanvasRenderer() { MOZ_COUNT_CTOR(CanvasRenderer); } 45 46 CanvasRenderer::~CanvasRenderer() { MOZ_COUNT_DTOR(CanvasRenderer); } 47 48 void CanvasRenderer::Initialize(const CanvasRendererData& aData) { 49 mData = aData; 50 } 51 52 bool CanvasRenderer::IsDataValid(const CanvasRendererData& aData) const { 53 return mData.GetContext() == aData.GetContext(); 54 } 55 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(); 61 62 RefPtr<gfx::SourceSurface> ss; 63 64 if (provider) { 65 ss = provider->BorrowSnapshot(); 66 } 67 if (!ss) { 68 provider = nullptr; 69 ss = context->GetFrontBufferSnapshot(requireAlphaPremult); 70 } 71 if (!ss) return nullptr; 72 73 return std::make_shared<BorrowedSourceSurface>(provider, ss); 74 } 75 76 void CanvasRenderer::FirePreTransactionCallback() const { 77 if (!mData.mDoPaintCallbacks) return; 78 const auto context = mData.GetContext(); 79 if (!context) return; 80 context->OnBeforePaintTransaction(); 81 } 82 83 void CanvasRenderer::FireDidTransactionCallback() const { 84 if (!mData.mDoPaintCallbacks) return; 85 const auto context = mData.GetContext(); 86 if (!context) return; 87 context->OnDidPaintTransaction(); 88 } 89 90 TextureType TexTypeForWebgl(KnowsCompositor* const knowsCompositor, 91 bool aIsWebglOop) { 92 if (!knowsCompositor) return TextureType::Unknown; 93 const auto layersBackend = knowsCompositor->GetCompositorBackendType(); 94 95 switch (layersBackend) { 96 case LayersBackend::LAYERS_LAST: 97 MOZ_CRASH("Unexpected LayersBackend::LAYERS_LAST"); 98 99 case LayersBackend::LAYERS_NONE: 100 return TextureType::Unknown; 101 102 case LayersBackend::LAYERS_WR: 103 break; 104 } 105 106 if (kIsWindows) { 107 if (knowsCompositor->SupportsD3D11()) { 108 return TextureType::D3D11; 109 } 110 } 111 if (kIsMacOS) { 112 return TextureType::MacIOSurface; 113 } 114 115 #ifdef MOZ_WIDGET_GTK 116 if (kIsLinux) { 117 if (!knowsCompositor->UsingSoftwareWebRender() && 118 widget::DMABufDevice::IsDMABufWebGLEnabled()) { 119 return TextureType::DMABUF; 120 } 121 } 122 #endif 123 124 if (kIsAndroid) { 125 // EGLimages cannot be shared cross-process, so only use if webgl is 126 // out-of-process. 127 if (aIsWebglOop && StaticPrefs::webgl_enable_egl_image()) { 128 return TextureType::EGLImage; 129 } 130 if (gfx::gfxVars::UseAHardwareBufferSharedSurfaceWebglOop()) { 131 return TextureType::AndroidHardwareBuffer; 132 } 133 if (StaticPrefs::webgl_enable_surface_texture()) { 134 return TextureType::AndroidNativeWindow; 135 } 136 } 137 138 return TextureType::Unknown; 139 } 140 141 } // namespace layers 142 } // namespace mozilla