tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

GMPVideoPlaneImpl.cpp (3130B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #include "GMPVideoPlaneImpl.h"
      7 
      8 #include <algorithm>
      9 
     10 #include "GMPSharedMemManager.h"
     11 #include "GMPVideoHost.h"
     12 #include "mozilla/gmp/GMPTypes.h"
     13 
     14 namespace mozilla::gmp {
     15 
     16 GMPPlaneImpl::GMPPlaneImpl(nsTArray<uint8_t>&& aArrayBuffer,
     17                           const GMPPlaneData& aPlaneData)
     18    : mArrayBuffer(std::move(aArrayBuffer)),
     19      mSize(aPlaneData.mSize()),
     20      mStride(aPlaneData.mStride()) {
     21  MOZ_ASSERT(aPlaneData.mOffset() == 0);
     22 }
     23 
     24 bool GMPPlaneImpl::InitPlaneData(nsTArray<uint8_t>& aArrayBuffer,
     25                                 GMPPlaneData& aPlaneData) {
     26  aArrayBuffer = std::move(mArrayBuffer);
     27  aPlaneData.mSize() = mSize;
     28  aPlaneData.mStride() = mStride;
     29 
     30  return true;
     31 }
     32 
     33 GMPErr GMPPlaneImpl::MaybeResize(int32_t aNewSize) {
     34  if (!mArrayBuffer.SetLength(aNewSize, fallible)) {
     35    return GMPAllocErr;
     36  }
     37 
     38  return GMPNoErr;
     39 }
     40 
     41 GMPErr GMPPlaneImpl::CreateEmptyPlane(int32_t aAllocatedSize, int32_t aStride,
     42                                      int32_t aPlaneSize) {
     43  if (aAllocatedSize < 1 || aStride < 1 || aPlaneSize < 1) {
     44    return GMPGenericErr;
     45  }
     46 
     47  GMPErr err = MaybeResize(aAllocatedSize);
     48  if (err != GMPNoErr) {
     49    return err;
     50  }
     51 
     52  mSize = aPlaneSize;
     53  mStride = aStride;
     54 
     55  return GMPNoErr;
     56 }
     57 
     58 GMPErr GMPPlaneImpl::Copy(const GMPPlane& aPlane) {
     59  auto& planeimpl = static_cast<const GMPPlaneImpl&>(aPlane);
     60 
     61  GMPErr err = MaybeResize(planeimpl.mSize);
     62  if (err != GMPNoErr) {
     63    return err;
     64  }
     65 
     66  if (planeimpl.Buffer() && planeimpl.mSize > 0) {
     67    memcpy(Buffer(), planeimpl.Buffer(), std::min(mSize, planeimpl.mSize));
     68  }
     69 
     70  mSize = planeimpl.mSize;
     71  mStride = planeimpl.mStride;
     72 
     73  return GMPNoErr;
     74 }
     75 
     76 GMPErr GMPPlaneImpl::Copy(int32_t aSize, int32_t aStride,
     77                          const uint8_t* aBuffer) {
     78  GMPErr err = MaybeResize(aSize);
     79  if (err != GMPNoErr) {
     80    return err;
     81  }
     82 
     83  if (aBuffer && aSize > 0) {
     84    memcpy(Buffer(), aBuffer, aSize);
     85  }
     86 
     87  mSize = aSize;
     88  mStride = aStride;
     89 
     90  return GMPNoErr;
     91 }
     92 
     93 void GMPPlaneImpl::Swap(GMPPlane& aPlane) {
     94  auto& planeimpl = static_cast<GMPPlaneImpl&>(aPlane);
     95 
     96  std::swap(mStride, planeimpl.mStride);
     97  std::swap(mSize, planeimpl.mSize);
     98  mArrayBuffer.SwapElements(planeimpl.mArrayBuffer);
     99 }
    100 
    101 int32_t GMPPlaneImpl::AllocatedSize() const {
    102  return static_cast<int32_t>(mArrayBuffer.Length());
    103 }
    104 
    105 void GMPPlaneImpl::ResetSize() { mSize = 0; }
    106 
    107 bool GMPPlaneImpl::IsZeroSize() const { return (mSize == 0); }
    108 
    109 int32_t GMPPlaneImpl::Stride() const { return mStride; }
    110 
    111 const uint8_t* GMPPlaneImpl::Buffer() const {
    112  if (!mArrayBuffer.IsEmpty()) {
    113    return mArrayBuffer.Elements();
    114  }
    115  return nullptr;
    116 }
    117 uint8_t* GMPPlaneImpl::Buffer() {
    118  if (!mArrayBuffer.IsEmpty()) {
    119    return mArrayBuffer.Elements();
    120  }
    121  return nullptr;
    122 }
    123 
    124 void GMPPlaneImpl::Destroy() { delete this; }
    125 
    126 }  // namespace mozilla::gmp