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 "RenderBufferTextureHost.h"
9 #include "mozilla/gfx/Logging.h"
10 #include "mozilla/layers/ImageDataSerializer.h"
15 RenderBufferTextureHost::RenderBufferTextureHost(
16 uint8_t* aBuffer
, const layers::BufferDescriptor
& aDescriptor
)
18 mDescriptor(aDescriptor
),
24 MOZ_COUNT_CTOR_INHERITED(RenderBufferTextureHost
, RenderTextureHost
);
26 switch (mDescriptor
.type()) {
27 case layers::BufferDescriptor::TYCbCrDescriptor
: {
28 const layers::YCbCrDescriptor
& ycbcr
= mDescriptor
.get_YCbCrDescriptor();
29 mSize
= ycbcr
.display().Size();
30 mFormat
= gfx::SurfaceFormat::YUV
;
33 case layers::BufferDescriptor::TRGBDescriptor
: {
34 const layers::RGBDescriptor
& rgb
= mDescriptor
.get_RGBDescriptor();
36 mFormat
= rgb
.format();
40 gfxCriticalError() << "Bad buffer host descriptor "
41 << (int)mDescriptor
.type();
42 MOZ_CRASH("GFX: Bad descriptor");
46 RenderBufferTextureHost::~RenderBufferTextureHost() {
47 MOZ_COUNT_DTOR_INHERITED(RenderBufferTextureHost
, RenderTextureHost
);
50 wr::WrExternalImage
RenderBufferTextureHost::Lock(
51 uint8_t aChannelIndex
, gl::GLContext
* aGL
, wr::ImageRendering aRendering
) {
54 // We hit some problems to get the shmem.
55 gfxCriticalNote
<< "GetBuffer Failed";
56 return InvalidToWrExternalImage();
58 if (mFormat
!= gfx::SurfaceFormat::YUV
) {
59 mSurface
= gfx::Factory::CreateWrappingDataSourceSurface(
61 layers::ImageDataSerializer::GetRGBStride(
62 mDescriptor
.get_RGBDescriptor()),
64 if (NS_WARN_IF(!mSurface
)) {
65 gfxCriticalNote
<< "DataSourceSurface is null";
66 return InvalidToWrExternalImage();
68 if (NS_WARN_IF(!mSurface
->Map(gfx::DataSourceSurface::MapType::READ_WRITE
,
71 gfxCriticalNote
<< "Failed to map Surface";
72 return InvalidToWrExternalImage();
75 const layers::YCbCrDescriptor
& desc
= mDescriptor
.get_YCbCrDescriptor();
77 mYSurface
= gfx::Factory::CreateWrappingDataSourceSurface(
78 layers::ImageDataSerializer::GetYChannel(GetBuffer(), desc
),
79 desc
.yStride(), desc
.ySize(), gfx::SurfaceFormat::A8
);
80 mCbSurface
= gfx::Factory::CreateWrappingDataSourceSurface(
81 layers::ImageDataSerializer::GetCbChannel(GetBuffer(), desc
),
82 desc
.cbCrStride(), desc
.cbCrSize(), gfx::SurfaceFormat::A8
);
83 mCrSurface
= gfx::Factory::CreateWrappingDataSourceSurface(
84 layers::ImageDataSerializer::GetCrChannel(GetBuffer(), desc
),
85 desc
.cbCrStride(), desc
.cbCrSize(), gfx::SurfaceFormat::A8
);
86 if (NS_WARN_IF(!mYSurface
|| !mCbSurface
|| !mCrSurface
)) {
87 mYSurface
= mCbSurface
= mCrSurface
= nullptr;
88 gfxCriticalNote
<< "YCbCr Surface is null";
89 return InvalidToWrExternalImage();
92 !mYSurface
->Map(gfx::DataSourceSurface::MapType::READ_WRITE
,
94 !mCbSurface
->Map(gfx::DataSourceSurface::MapType::READ_WRITE
,
96 !mCrSurface
->Map(gfx::DataSourceSurface::MapType::READ_WRITE
,
98 mYSurface
= mCbSurface
= mCrSurface
= nullptr;
99 gfxCriticalNote
<< "Failed to map YCbCr Surface";
100 return InvalidToWrExternalImage();
106 RenderBufferData data
= GetBufferDataForRender(aChannelIndex
);
107 return RawDataToWrExternalImage(data
.mData
, data
.mBufferSize
);
110 void RenderBufferTextureHost::Unlock() {
115 } else if (mYSurface
) {
119 mYSurface
= mCbSurface
= mCrSurface
= nullptr;
125 RenderBufferTextureHost::RenderBufferData
126 RenderBufferTextureHost::GetBufferDataForRender(uint8_t aChannelIndex
) {
127 MOZ_ASSERT(mFormat
!= gfx::SurfaceFormat::YUV
|| aChannelIndex
< 3);
128 MOZ_ASSERT(mFormat
== gfx::SurfaceFormat::YUV
|| aChannelIndex
< 1);
131 if (mFormat
!= gfx::SurfaceFormat::YUV
) {
132 MOZ_ASSERT(mSurface
);
134 return RenderBufferData(mMap
.mData
,
135 mMap
.mStride
* mSurface
->GetSize().height
);
137 MOZ_ASSERT(mYSurface
&& mCbSurface
&& mCrSurface
);
139 switch (aChannelIndex
) {
141 return RenderBufferData(mYMap
.mData
,
142 mYMap
.mStride
* mYSurface
->GetSize().height
);
145 return RenderBufferData(mCbMap
.mData
,
146 mCbMap
.mStride
* mCbSurface
->GetSize().height
);
149 return RenderBufferData(mCrMap
.mData
,
150 mCrMap
.mStride
* mCrSurface
->GetSize().height
);
153 MOZ_ASSERT_UNREACHABLE("unexpected to be called");
154 return RenderBufferData(nullptr, 0);
160 } // namespace mozilla