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 "RenderTextureHostSWGL.h"
9 #include "mozilla/gfx/Logging.h"
10 #include "mozilla/layers/TextureHost.h"
11 #include "RenderThread.h"
16 bool RenderTextureHostSWGL::UpdatePlanes(RenderCompositor
* aCompositor
) {
17 wr_swgl_make_current(mContext
);
18 size_t planeCount
= GetPlaneCount();
20 if (mPlanes
.size() < planeCount
) {
21 mPlanes
.reserve(planeCount
);
22 while (mPlanes
.size() < planeCount
) {
23 mPlanes
.push_back(PlaneInfo(wr_swgl_gen_texture(mContext
)));
27 gfx::SurfaceFormat format
= GetFormat();
28 gfx::ColorDepth colorDepth
= GetColorDepth();
29 for (size_t i
= 0; i
< planeCount
; i
++) {
30 PlaneInfo
& plane
= mPlanes
[i
];
31 if (!MapPlane(aCompositor
, i
, plane
)) {
37 GLenum internalFormat
= 0;
39 case gfx::SurfaceFormat::B8G8R8A8
:
40 case gfx::SurfaceFormat::B8G8R8X8
:
41 MOZ_ASSERT(colorDepth
== gfx::ColorDepth::COLOR_8
);
42 internalFormat
= LOCAL_GL_RGBA8
;
44 case gfx::SurfaceFormat::YUV420
:
46 case gfx::ColorDepth::COLOR_8
:
47 internalFormat
= LOCAL_GL_R8
;
49 case gfx::ColorDepth::COLOR_10
:
50 case gfx::ColorDepth::COLOR_12
:
51 case gfx::ColorDepth::COLOR_16
:
52 internalFormat
= LOCAL_GL_R16
;
56 case gfx::SurfaceFormat::NV12
:
58 case gfx::ColorDepth::COLOR_8
:
59 internalFormat
= i
> 0 ? LOCAL_GL_RG8
: LOCAL_GL_R8
;
61 case gfx::ColorDepth::COLOR_10
:
62 case gfx::ColorDepth::COLOR_12
:
63 case gfx::ColorDepth::COLOR_16
:
64 internalFormat
= i
> 0 ? LOCAL_GL_RG16
: LOCAL_GL_R16
;
68 case gfx::SurfaceFormat::P010
:
69 MOZ_ASSERT(colorDepth
== gfx::ColorDepth::COLOR_10
);
70 internalFormat
= i
> 0 ? LOCAL_GL_RG16
: LOCAL_GL_R16
;
72 case gfx::SurfaceFormat::YUY2
:
73 MOZ_ASSERT(colorDepth
== gfx::ColorDepth::COLOR_8
);
74 internalFormat
= LOCAL_GL_RGB_RAW_422_APPLE
;
77 MOZ_RELEASE_ASSERT(false, "Unhandled external image format");
80 wr_swgl_set_texture_buffer(mContext
, plane
.mTexture
, internalFormat
,
81 plane
.mSize
.width
, plane
.mSize
.height
,
82 plane
.mStride
, plane
.mData
, 0, 0);
85 // Initialize the mip filters to linear by default.
86 for (const auto& plane
: mPlanes
) {
87 wr_swgl_set_texture_parameter(mContext
, plane
.mTexture
,
88 LOCAL_GL_TEXTURE_MIN_FILTER
,
90 wr_swgl_set_texture_parameter(mContext
, plane
.mTexture
,
91 LOCAL_GL_TEXTURE_MAG_FILTER
,
98 bool RenderTextureHostSWGL::SetContext(void* aContext
) {
99 if (mContext
!= aContext
) {
102 wr_swgl_reference_context(mContext
);
104 return mContext
!= nullptr;
107 wr::WrExternalImage
RenderTextureHostSWGL::LockSWGL(
108 uint8_t aChannelIndex
, void* aContext
, RenderCompositor
* aCompositor
) {
109 if (!SetContext(aContext
)) {
110 return InvalidToWrExternalImage();
113 if (!UpdatePlanes(aCompositor
)) {
114 return InvalidToWrExternalImage();
118 if (aChannelIndex
>= mPlanes
.size()) {
119 return InvalidToWrExternalImage();
121 const PlaneInfo
& plane
= mPlanes
[aChannelIndex
];
123 // Prefer native textures, unless our backend forbids it.
124 layers::TextureHost::NativeTexturePolicy policy
=
125 layers::TextureHost::BackendNativeTexturePolicy(
126 layers::WebRenderBackend::SOFTWARE
, plane
.mSize
);
127 return policy
== layers::TextureHost::NativeTexturePolicy::FORBID
128 ? RawDataToWrExternalImage((uint8_t*)plane
.mData
,
129 plane
.mStride
* plane
.mSize
.height
)
130 : NativeTextureToWrExternalImage(
131 plane
.mTexture
, 0.0, 0.0,
132 static_cast<float>(plane
.mSize
.width
),
133 static_cast<float>(plane
.mSize
.height
));
136 void RenderTextureHostSWGL::UnlockSWGL() {
143 void RenderTextureHostSWGL::CleanupPlanes() {
147 if (!mPlanes
.empty()) {
148 wr_swgl_make_current(mContext
);
149 for (const auto& plane
: mPlanes
) {
150 wr_swgl_delete_texture(mContext
, plane
.mTexture
);
154 wr_swgl_destroy_context(mContext
);
158 RenderTextureHostSWGL::~RenderTextureHostSWGL() { CleanupPlanes(); }
160 bool RenderTextureHostSWGL::LockSWGLCompositeSurface(
161 void* aContext
, wr::SWGLCompositeSurfaceInfo
* aInfo
) {
162 if (!SetContext(aContext
)) {
166 if (!UpdatePlanes(nullptr)) {
171 MOZ_ASSERT(mPlanes
.size() <= 3);
172 for (size_t i
= 0; i
< mPlanes
.size(); i
++) {
173 aInfo
->textures
[i
] = mPlanes
[i
].mTexture
;
175 switch (GetFormat()) {
176 case gfx::SurfaceFormat::YUV420
:
177 case gfx::SurfaceFormat::NV12
:
178 case gfx::SurfaceFormat::P010
:
179 case gfx::SurfaceFormat::YUY2
: {
180 aInfo
->yuv_planes
= mPlanes
.size();
181 auto colorSpace
= GetYUVColorSpace();
182 aInfo
->color_space
= ToWrYuvRangedColorSpace(colorSpace
);
183 auto colorDepth
= GetColorDepth();
184 aInfo
->color_depth
= ToWrColorDepth(colorDepth
);
187 case gfx::SurfaceFormat::B8G8R8A8
:
188 case gfx::SurfaceFormat::B8G8R8X8
:
191 gfxCriticalNote
<< "Unhandled external image format: " << GetFormat();
192 MOZ_RELEASE_ASSERT(false, "Unhandled external image format");
195 aInfo
->size
.width
= mPlanes
[0].mSize
.width
;
196 aInfo
->size
.height
= mPlanes
[0].mSize
.height
;
200 bool wr_swgl_lock_composite_surface(void* aContext
, wr::ExternalImageId aId
,
201 wr::SWGLCompositeSurfaceInfo
* aInfo
) {
202 RenderTextureHost
* texture
= RenderThread::Get()->GetRenderTexture(aId
);
206 RenderTextureHostSWGL
* swglTex
= texture
->AsRenderTextureHostSWGL();
210 return swglTex
->LockSWGLCompositeSurface(aContext
, aInfo
);
213 void wr_swgl_unlock_composite_surface(void* aContext
, wr::ExternalImageId aId
) {
214 RenderTextureHost
* texture
= RenderThread::Get()->GetRenderTexture(aId
);
218 RenderTextureHostSWGL
* swglTex
= texture
->AsRenderTextureHostSWGL();
222 swglTex
->UnlockSWGL();
226 } // namespace mozilla