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 "RenderDMABUFTextureHost.h"
9 #include "GLContextEGL.h"
10 #include "mozilla/gfx/Logging.h"
11 #include "ScopedGLHelpers.h"
13 namespace mozilla::wr
{
15 RenderDMABUFTextureHost::RenderDMABUFTextureHost(DMABufSurface
* aSurface
)
16 : mSurface(aSurface
) {
17 MOZ_COUNT_CTOR_INHERITED(RenderDMABUFTextureHost
, RenderTextureHost
);
20 RenderDMABUFTextureHost::~RenderDMABUFTextureHost() {
21 MOZ_COUNT_DTOR_INHERITED(RenderDMABUFTextureHost
, RenderTextureHost
);
22 DeleteTextureHandle();
25 wr::WrExternalImage
RenderDMABUFTextureHost::Lock(uint8_t aChannelIndex
,
27 if (mGL
.get() != aGL
) {
29 // This should not happen. EGLImage is created only in
31 MOZ_ASSERT_UNREACHABLE("Unexpected GL context");
32 return InvalidToWrExternalImage();
37 if (!mGL
|| !mGL
->MakeCurrent()) {
38 return InvalidToWrExternalImage();
41 if (!mSurface
->GetTexture(aChannelIndex
)) {
42 if (!mSurface
->CreateTexture(mGL
, aChannelIndex
)) {
43 return InvalidToWrExternalImage();
45 ActivateBindAndTexParameteri(mGL
, LOCAL_GL_TEXTURE0
, LOCAL_GL_TEXTURE_2D
,
46 mSurface
->GetTexture(aChannelIndex
));
49 if (auto texture
= mSurface
->GetTexture(aChannelIndex
)) {
50 mSurface
->MaybeSemaphoreWait(texture
);
53 const gfx::IntSize
size(mSurface
->GetWidth(aChannelIndex
),
54 mSurface
->GetHeight(aChannelIndex
));
55 return NativeTextureToWrExternalImage(
56 mSurface
->GetTexture(aChannelIndex
), 0.0, 0.0,
57 static_cast<float>(size
.width
), static_cast<float>(size
.height
));
60 void RenderDMABUFTextureHost::Unlock() {}
62 void RenderDMABUFTextureHost::DeleteTextureHandle() {
63 mSurface
->ReleaseTextures();
66 void RenderDMABUFTextureHost::ClearCachedResources() {
67 DeleteTextureHandle();
71 gfx::SurfaceFormat
RenderDMABUFTextureHost::GetFormat() const {
72 return mSurface
->GetFormat();
75 bool RenderDMABUFTextureHost::MapPlane(RenderCompositor
* aCompositor
,
76 uint8_t aChannelIndex
,
77 PlaneInfo
& aPlaneInfo
) {
78 if (mSurface
->GetAsDMABufSurfaceYUV()) {
79 // DMABufSurfaceYUV is not supported.
83 const RefPtr
<gfx::SourceSurface
> surface
= mSurface
->GetAsSourceSurface();
88 const RefPtr
<gfx::DataSourceSurface
> dataSurface
= surface
->GetDataSurface();
93 gfx::DataSourceSurface::MappedSurface map
;
94 if (!dataSurface
->Map(gfx::DataSourceSurface::MapType::READ
, &map
)) {
98 mReadback
= dataSurface
;
99 aPlaneInfo
.mSize
= gfx::IntSize(mSurface
->GetWidth(), mSurface
->GetHeight());
100 aPlaneInfo
.mStride
= map
.mStride
;
101 aPlaneInfo
.mData
= map
.mData
;
106 void RenderDMABUFTextureHost::UnmapPlanes() {
113 } // namespace mozilla::wr