1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
// Copyright (c) 2011-2024 Intel Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "mfx_common.h"
#ifndef _LIBMFX_ALLOCATOR_VAAPI_H_
#define _LIBMFX_ALLOCATOR_VAAPI_H_
#include <va/va.h>
#include <unistd.h>
#include "mfxvideo++int.h"
#include "libmfx_allocator.h"
// VAAPI Allocator internal Mem ID
struct vaapiMemIdInt
{
VASurfaceID* m_surface;
VAImage m_image;
unsigned int m_fourcc;
};
// Internal Allocators
namespace mfxDefaultAllocatorVAAPI
{
mfxStatus AllocFramesHW(mfxHDL pthis, mfxFrameAllocRequest *request, mfxFrameAllocResponse *response);
mfxStatus LockFrameHW(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr);
mfxStatus GetHDLHW(mfxHDL pthis, mfxMemId mid, mfxHDL *handle);
mfxStatus UnlockFrameHW(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr = nullptr);
mfxStatus FreeFramesHW(mfxHDL pthis, mfxFrameAllocResponse *response);
mfxStatus ReallocFrameHW(mfxHDL pthis, mfxFrameSurface1 *surf, VASurfaceID *va_surf);
mfxStatus SetFrameData(const VAImage &va_image, mfxU32 mfx_fourcc, mfxU8* p_buffer, mfxFrameData& frame_data);
class mfxWideHWFrameAllocator : public mfxBaseWideFrameAllocator
{
public:
mfxWideHWFrameAllocator(mfxU16 type, mfxHDL handle);
virtual ~mfxWideHWFrameAllocator(void){};
VADisplay* m_pVADisplay;
mfxU32 m_DecId;
std::vector<VASurfaceID> m_allocatedSurfaces;
std::vector<vaapiMemIdInt> m_allocatedMids;
};
} // namespace mfxDefaultAllocatorVAAPI
class SurfaceScopedLock
{
public:
SurfaceScopedLock(VADisplay disp, VASurfaceID& surface_id)
: m_display(disp)
, m_surface_id(surface_id)
{}
~SurfaceScopedLock()
{
if (m_mapped) std::ignore = MFX_STS_TRACE(Unmap());
if (m_image_created) std::ignore = MFX_STS_TRACE(DestroyImage());
}
mfxStatus DeriveImage()
{
MFX_CHECK(!m_image_created, MFX_ERR_UNDEFINED_BEHAVIOR);
{
PERF_UTILITY_AUTO("vaDeriveImage", PERF_LEVEL_DDI);
VAStatus va_sts = vaDeriveImage(m_display, m_surface_id, &m_image);
MFX_CHECK(VA_STATUS_SUCCESS == va_sts, MFX_ERR_DEVICE_FAILED);
}
m_image_created = true;
return MFX_ERR_NONE;
}
// add map type when vaMapBuffer will support it
mfxStatus Map(mfxU8* & ptr/*, MAP_TYPE*/)
{
ptr = nullptr;
MFX_CHECK(m_image_created, MFX_ERR_LOCK_MEMORY);
{
MFX_AUTO_LTRACE(MFX_TRACE_LEVEL_EXTCALL, "vaMapBuffer");
PERF_UTILITY_AUTO("vaMapBuffer", PERF_LEVEL_DDI);
VAStatus va_sts = vaMapBuffer(m_display, m_image.buf, (void **)&ptr);
MFX_CHECK(VA_STATUS_SUCCESS == va_sts, MFX_ERR_DEVICE_FAILED);
}
m_mapped = true;
return MFX_ERR_NONE;
}
mfxStatus Unmap()
{
MFX_CHECK(m_image_created, MFX_ERR_NOT_INITIALIZED);
MFX_CHECK(m_mapped, MFX_ERR_UNDEFINED_BEHAVIOR);
{
MFX_AUTO_LTRACE(MFX_TRACE_LEVEL_EXTCALL, "vaUnmapBuffer");
PERF_UTILITY_AUTO("vaUnmapBuffer", PERF_LEVEL_DDI);
VAStatus va_sts = vaUnmapBuffer(m_display, m_image.buf);
MFX_CHECK(VA_STATUS_SUCCESS == va_sts, MFX_ERR_DEVICE_FAILED);
}
m_mapped = false;
return MFX_ERR_NONE;
}
mfxStatus DestroyImage()
{
MFX_CHECK(m_image_created, MFX_ERR_NOT_INITIALIZED);
MFX_CHECK(!m_mapped, MFX_ERR_UNKNOWN);
{
PERF_UTILITY_AUTO("vaUnmapBuffer", PERF_LEVEL_DDI);
VAStatus va_sts = vaDestroyImage(m_display, m_image.image_id);
MFX_CHECK(VA_STATUS_SUCCESS == va_sts, MFX_ERR_DEVICE_FAILED);
}
m_image_created = false;
return MFX_ERR_NONE;
}
VAImage m_image = {};
private:
bool m_image_created = false;
bool m_mapped = false;
VADisplay m_display;
VASurfaceID& m_surface_id;
};
class VADisplayWrapper : public std::enable_shared_from_this<VADisplayWrapper>
{
public:
VADisplayWrapper(VADisplay dpy, int fdDRM = -1)
: m_display(dpy)
, m_fdDRM(fdDRM)
{}
~VADisplayWrapper()
{
if (m_fdDRM != -1)
{
std::ignore = MFX_STS_TRACE(vaTerminate(m_display));
std::ignore = MFX_STS_TRACE(close(m_fdDRM));
}
}
VADisplayWrapper(const VADisplayWrapper&) = delete;
VADisplayWrapper(VADisplayWrapper&& other) = default;
VADisplayWrapper& operator= (const VADisplayWrapper&) = delete;
VADisplayWrapper& operator= (VADisplayWrapper&& other) = default;
operator VADisplay() const { return m_display; }
private:
VADisplay m_display;
int m_fdDRM = -1;
};
class vaapi_resource_wrapper
{
public:
vaapi_resource_wrapper(VADisplayWrapper& display)
: m_resource_id(VA_INVALID_ID), m_pVADisplay(display.shared_from_this())
{}
virtual mfxStatus Lock(mfxFrameData& frame_data, mfxU32 flags) = 0;
virtual mfxStatus Unlock() = 0;
virtual ~vaapi_resource_wrapper() {};
virtual mfxStatus Export(const mfxSurfaceHeader& /*export_header*/, mfxSurfaceBase*& /*exported_surface*/,
mfxFrameSurfaceInterfaceImpl* /*p_base_surface*/)
{
MFX_RETURN(MFX_ERR_UNSUPPORTED);
}
VAGenericID* GetHandle() { return &m_resource_id; }
VADisplayWrapper& GetDevice() { return *m_pVADisplay; }
protected:
VAGenericID m_resource_id;
std::shared_ptr<VADisplayWrapper> m_pVADisplay;
};
class vaapi_buffer_wrapper : public vaapi_resource_wrapper
{
public:
vaapi_buffer_wrapper(const mfxFrameInfo &info, VADisplayWrapper& display, mfxU32 context);
~vaapi_buffer_wrapper();
virtual mfxStatus Lock(mfxFrameData& frame_data, mfxU32 flags) override;
virtual mfxStatus Unlock() override;
private:
bool m_bIsSegmap;
mfxU32 m_pitch;
};
class vaapi_surface_wrapper : public vaapi_resource_wrapper
{
public:
vaapi_surface_wrapper(const mfxFrameInfo &info, mfxU16 type, VADisplayWrapper& display, mfxSurfaceHeader* import_surface);
~vaapi_surface_wrapper();
virtual mfxStatus Lock(mfxFrameData& frame_data, mfxU32 flags) override;
virtual mfxStatus Unlock() override;
virtual mfxStatus Export(const mfxSurfaceHeader& export_header, mfxSurfaceBase*& exported_surface, mfxFrameSurfaceInterfaceImpl* p_base_surface) override;
std::shared_ptr<ImportExportHelper>& GetCreateHelper()
{
std::lock_guard<std::mutex> guard(m_mutex);
if (!m_import_export_helper)
m_import_export_helper.reset(new ImportExportHelper());
return m_import_export_helper;
}
private:
std::pair<mfxStatus, bool> TryImportSurface (const mfxFrameInfo& info, mfxSurfaceHeader* import_surface);
std::pair<mfxStatus, bool> TryImportSurfaceVAAPI(const mfxFrameInfo& info, mfxSurfaceVAAPI& import_surface);
mfxStatus CopyImportSurface (const mfxFrameInfo& info, mfxSurfaceHeader* import_surface);
mfxStatus CopyImportSurfaceVAAPI(const mfxFrameInfo& info, mfxSurfaceVAAPI& import_surface);
SurfaceScopedLock m_surface_lock;
// If m_imported == true we will not delete vaapi surface in destructor
bool m_imported = false;
mfxU16 m_type;
mfxU32 m_fourcc;
std::mutex m_mutex;
std::shared_ptr<ImportExportHelper> m_import_export_helper;
};
struct mfxFrameSurface1_hw_vaapi : public RWAcessSurface
{
static mfxFrameSurface1_hw_vaapi* Create(const mfxFrameInfo& info, mfxU16 type, mfxMemId mid, std::shared_ptr<staging_adapter_stub>& stg_adapter, mfxHDL display, mfxU32 context, FrameAllocatorBase& allocator,
mfxSurfaceHeader* import_surface)
{
auto surface = new mfxFrameSurface1_hw_vaapi(info, type, mid, stg_adapter, display, context, allocator, import_surface);
surface->AddRef();
return surface;
}
~mfxFrameSurface1_hw_vaapi()
{
// Unmap surface if it is still mapped
while (Locked())
{
if (MFX_FAILED(Unlock()))
break;
}
}
mfxStatus Lock(mfxU32 flags) override;
mfxStatus Unlock() override;
std::pair<mfxHDL, mfxResourceType> GetNativeHandle() const override;
std::pair<mfxHDL, mfxHandleType> GetDeviceHandle() const override;
mfxStatus CreateExportSurface(const mfxSurfaceHeader& export_header, mfxSurfaceBase*& exported_surface) override
{
//mfxFrameSurfaceInterfaceImpl
switch (export_header.SurfaceType)
{
case MFX_SURFACE_TYPE_VAAPI:
MFX_CHECK_HDL(m_resource_wrapper);
MFX_RETURN(m_resource_wrapper->Export(export_header, exported_surface, this));
default:
MFX_RETURN(MFX_ERR_UNSUPPORTED);
}
}
mfxStatus GetHDL(mfxHDL& handle) const;
mfxStatus Realloc(const mfxFrameInfo & info);
static mfxU16 AdjustType(mfxU16 type) { return mfxFrameSurface1_sw::AdjustType(type); }
private:
mfxFrameSurface1_hw_vaapi(const mfxFrameInfo& info, mfxU16 type, mfxMemId mid, std::shared_ptr<staging_adapter_stub>& stg_adapter, mfxHDL display, mfxU32 context, FrameAllocatorBase& allocator,
mfxSurfaceHeader* import_surface);
mutable std::shared_timed_mutex m_hdl_mutex;
mfxU16 m_type;
VAContextID m_context;
std::unique_ptr<vaapi_resource_wrapper> m_resource_wrapper;
};
using FlexibleFrameAllocatorHW_VAAPI = FlexibleFrameAllocator<mfxFrameSurface1_hw_vaapi, staging_adapter_stub>;
class mfxSurfaceVAAPIImpl
: public mfxSurfaceImpl<mfxSurfaceVAAPI>
{
public:
static mfxSurfaceVAAPIImpl* Create(const mfxSurfaceHeader& export_header, mfxFrameSurfaceInterfaceImpl* p_base_surface, std::shared_ptr<VADisplayWrapper>& display, VASurfaceID surface_id)
{
auto surface = new mfxSurfaceVAAPIImpl(export_header, p_base_surface, display, surface_id);
surface->AddRef();
return surface;
}
~mfxSurfaceVAAPIImpl();
private:
mfxSurfaceVAAPIImpl(const mfxSurfaceHeader& export_header, mfxFrameSurfaceInterfaceImpl* p_base_surface, std::shared_ptr<VADisplayWrapper>& display, VASurfaceID surface_id);
VASurfaceID m_surface_id = VA_INVALID_ID;
std::shared_ptr<VADisplayWrapper> m_pVADisplay;
};
#endif // LIBMFX_ALLOCATOR_VAAPI_H_
/* EOF */
|