Bug 1918529 - fix some subpixel misalignment issues with gfx.webrender.svg-filter...
[gecko.git] / gfx / webrender_bindings / RenderTextureHostSWGL.cpp
bloba4b7ec235874123db9f80cad0e0d25bc320575f8
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"
13 namespace mozilla {
14 namespace wr {
16 bool RenderTextureHostSWGL::UpdatePlanes(RenderCompositor* aCompositor) {
17 wr_swgl_make_current(mContext);
18 size_t planeCount = GetPlaneCount();
19 bool texInit = false;
20 if (mPlanes.size() < planeCount) {
21 mPlanes.reserve(planeCount);
22 while (mPlanes.size() < planeCount) {
23 mPlanes.push_back(PlaneInfo(wr_swgl_gen_texture(mContext)));
25 texInit = true;
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)) {
32 if (i > 0) {
33 UnmapPlanes();
35 return false;
37 GLenum internalFormat = 0;
38 switch (format) {
39 case gfx::SurfaceFormat::B8G8R8A8:
40 case gfx::SurfaceFormat::B8G8R8X8:
41 MOZ_ASSERT(colorDepth == gfx::ColorDepth::COLOR_8);
42 internalFormat = LOCAL_GL_RGBA8;
43 break;
44 case gfx::SurfaceFormat::YUV420:
45 switch (colorDepth) {
46 case gfx::ColorDepth::COLOR_8:
47 internalFormat = LOCAL_GL_R8;
48 break;
49 case gfx::ColorDepth::COLOR_10:
50 case gfx::ColorDepth::COLOR_12:
51 case gfx::ColorDepth::COLOR_16:
52 internalFormat = LOCAL_GL_R16;
53 break;
55 break;
56 case gfx::SurfaceFormat::NV12:
57 switch (colorDepth) {
58 case gfx::ColorDepth::COLOR_8:
59 internalFormat = i > 0 ? LOCAL_GL_RG8 : LOCAL_GL_R8;
60 break;
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;
65 break;
67 break;
68 case gfx::SurfaceFormat::P010:
69 MOZ_ASSERT(colorDepth == gfx::ColorDepth::COLOR_10);
70 internalFormat = i > 0 ? LOCAL_GL_RG16 : LOCAL_GL_R16;
71 break;
72 case gfx::SurfaceFormat::YUY2:
73 MOZ_ASSERT(colorDepth == gfx::ColorDepth::COLOR_8);
74 internalFormat = LOCAL_GL_RGB_RAW_422_APPLE;
75 break;
76 default:
77 MOZ_RELEASE_ASSERT(false, "Unhandled external image format");
78 break;
80 wr_swgl_set_texture_buffer(mContext, plane.mTexture, internalFormat,
81 plane.mSize.width, plane.mSize.height,
82 plane.mStride, plane.mData, 0, 0);
84 if (texInit) {
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,
89 LOCAL_GL_LINEAR);
90 wr_swgl_set_texture_parameter(mContext, plane.mTexture,
91 LOCAL_GL_TEXTURE_MAG_FILTER,
92 LOCAL_GL_LINEAR);
95 return true;
98 bool RenderTextureHostSWGL::SetContext(void* aContext) {
99 if (mContext != aContext) {
100 CleanupPlanes();
101 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();
112 if (!mLocked) {
113 if (!UpdatePlanes(aCompositor)) {
114 return InvalidToWrExternalImage();
116 mLocked = true;
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() {
137 if (mLocked) {
138 mLocked = false;
139 UnmapPlanes();
143 void RenderTextureHostSWGL::CleanupPlanes() {
144 if (!mContext) {
145 return;
147 if (!mPlanes.empty()) {
148 wr_swgl_make_current(mContext);
149 for (const auto& plane : mPlanes) {
150 wr_swgl_delete_texture(mContext, plane.mTexture);
152 mPlanes.clear();
154 wr_swgl_destroy_context(mContext);
155 mContext = nullptr;
158 RenderTextureHostSWGL::~RenderTextureHostSWGL() { CleanupPlanes(); }
160 bool RenderTextureHostSWGL::LockSWGLCompositeSurface(
161 void* aContext, wr::SWGLCompositeSurfaceInfo* aInfo) {
162 if (!SetContext(aContext)) {
163 return false;
165 if (!mLocked) {
166 if (!UpdatePlanes(nullptr)) {
167 return false;
169 mLocked = true;
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);
185 break;
187 case gfx::SurfaceFormat::B8G8R8A8:
188 case gfx::SurfaceFormat::B8G8R8X8:
189 break;
190 default:
191 gfxCriticalNote << "Unhandled external image format: " << GetFormat();
192 MOZ_RELEASE_ASSERT(false, "Unhandled external image format");
193 break;
195 aInfo->size.width = mPlanes[0].mSize.width;
196 aInfo->size.height = mPlanes[0].mSize.height;
197 return true;
200 bool wr_swgl_lock_composite_surface(void* aContext, wr::ExternalImageId aId,
201 wr::SWGLCompositeSurfaceInfo* aInfo) {
202 RenderTextureHost* texture = RenderThread::Get()->GetRenderTexture(aId);
203 if (!texture) {
204 return false;
206 RenderTextureHostSWGL* swglTex = texture->AsRenderTextureHostSWGL();
207 if (!swglTex) {
208 return false;
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);
215 if (!texture) {
216 return;
218 RenderTextureHostSWGL* swglTex = texture->AsRenderTextureHostSWGL();
219 if (!swglTex) {
220 return;
222 swglTex->UnlockSWGL();
225 } // namespace wr
226 } // namespace mozilla