/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "D3D11TextureWrapper.h"

#include "FFmpegLibWrapper.h"
#include "FFmpegLog.h"
#include "libavutil/frame.h"
#include "mozilla/Assertions.h"
#include "mozilla/Logging.h"
#include "mozilla/gfx/gfxVars.h"

struct ID3D11Texture2D;

extern mozilla::LazyLogModule sFFmpegVideoLog;

#define LOG(...) \
  MOZ_LOG_FMT(sFFmpegVideoLog, mozilla::LogLevel::Verbose, __VA_ARGS__)

namespace mozilla {

D3D11TextureWrapper::D3D11TextureWrapper(AVFrame* aAVFrame,
                                         const FFmpegLibWrapper* aLib,
                                         ID3D11Texture2D* aTexture,
                                         const gfx::SurfaceFormat aFormat,
                                         const unsigned int aArrayIdx,
                                         std::function<void()>&& aReleaseMethod)
    : mFormat(aFormat),
      mArrayIdx(aArrayIdx),
      mReleaseMethod(std::move(aReleaseMethod)),
      mLib(aLib),
      mTexture(aTexture) {
  MOZ_ASSERT(XRE_IsGPUProcess());
  MOZ_ASSERT(gfx::gfxVars::HwDecodedVideoZeroCopy());
  MOZ_ASSERT(mLib);
  MOZ_ASSERT(aAVFrame);
  MOZ_ASSERT(aTexture);
  mHWAVBuffer = aLib->av_buffer_ref(aAVFrame->buf[0]);
  MOZ_ASSERT(mHWAVBuffer);
  LOG("Locked D3D11 texture {} on index {}", fmt::ptr(mTexture), mArrayIdx);
}

D3D11TextureWrapper::~D3D11TextureWrapper() {
  MOZ_ASSERT(XRE_IsGPUProcess());
  MOZ_ASSERT(mLib);
  MOZ_ASSERT(mHWAVBuffer);
  mLib->av_buffer_unref(&mHWAVBuffer);
  mLib = nullptr;
  mReleaseMethod();
  LOG("Unlocked D3D11 texture {} on index {}", fmt::ptr(mTexture), mArrayIdx);
}

}  // namespace mozilla

#undef LOG
