DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (2f44d750d784)

VCS Links

Line Code
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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 "ImageUtils.h"
#include "ImageContainer.h"
#include "mozilla/AlreadyAddRefed.h"
#include "mozilla/dom/ImageBitmapBinding.h"
#include "mozilla/ErrorResult.h"

using namespace mozilla::layers;
using namespace mozilla::gfx;

namespace mozilla {
namespace dom {

static ImageBitmapFormat GetImageBitmapFormatFromSurfaceFromat(
    SurfaceFormat aSurfaceFormat) {
  switch (aSurfaceFormat) {
    case SurfaceFormat::B8G8R8A8:
    case SurfaceFormat::B8G8R8X8:
      return ImageBitmapFormat::BGRA32;
    case SurfaceFormat::R8G8B8A8:
    case SurfaceFormat::R8G8B8X8:
      return ImageBitmapFormat::RGBA32;
    case SurfaceFormat::R8G8B8:
      return ImageBitmapFormat::RGB24;
    case SurfaceFormat::B8G8R8:
      return ImageBitmapFormat::BGR24;
    case SurfaceFormat::HSV:
      return ImageBitmapFormat::HSV;
    case SurfaceFormat::Lab:
      return ImageBitmapFormat::Lab;
    case SurfaceFormat::Depth:
      return ImageBitmapFormat::DEPTH;
    case SurfaceFormat::A8:
      return ImageBitmapFormat::GRAY8;
    case SurfaceFormat::R5G6B5_UINT16:
    case SurfaceFormat::YUV:
    case SurfaceFormat::NV12:
    case SurfaceFormat::P010:
    case SurfaceFormat::P016:
    case SurfaceFormat::UNKNOWN:
    default:
      return ImageBitmapFormat::EndGuard_;
  }
}

static ImageBitmapFormat GetImageBitmapFormatFromPlanarYCbCrData(
    layers::PlanarYCbCrData const* aData) {
  MOZ_ASSERT(aData);

  if (aData->mYSkip == 0 && aData->mCbSkip == 0 &&
      aData->mCrSkip == 0) {  // Possibly three planes.
    if (aData->mCbChannel >=
            aData->mYChannel + aData->mYSize.height * aData->mYStride &&
        aData->mCrChannel >=
            aData->mCbChannel + aData->mCbCrSize.height *
                                    aData->mCbCrStride) {  // Three planes.
      if (aData->mYSize.height == aData->mCbCrSize.height) {
        if (aData->mYSize.width == aData->mCbCrSize.width) {
          return ImageBitmapFormat::YUV444P;
        }
        if (((aData->mYSize.width + 1) / 2) == aData->mCbCrSize.width) {
          return ImageBitmapFormat::YUV422P;
        }
      } else if (((aData->mYSize.height + 1) / 2) == aData->mCbCrSize.height) {
        if (((aData->mYSize.width + 1) / 2) == aData->mCbCrSize.width) {
          return ImageBitmapFormat::YUV420P;
        }
      }
    }
  } else if (aData->mYSkip == 0 && aData->mCbSkip == 1 &&
             aData->mCrSkip == 1) {  // Possibly two planes.
    if (aData->mCbChannel >=
            aData->mYChannel + aData->mYSize.height * aData->mYStride &&
        aData->mCbChannel == aData->mCrChannel - 1) {  // Two planes.
      if (((aData->mYSize.height + 1) / 2) == aData->mCbCrSize.height &&
          ((aData->mYSize.width + 1) / 2) == aData->mCbCrSize.width) {
        return ImageBitmapFormat::YUV420SP_NV12;  // Y-Cb-Cr
      }
    } else if (aData->mCrChannel >=
                   aData->mYChannel + aData->mYSize.height * aData->mYStride &&
               aData->mCrChannel == aData->mCbChannel - 1) {  // Two planes.
      if (((aData->mYSize.height + 1) / 2) == aData->mCbCrSize.height &&
          ((aData->mYSize.width + 1) / 2) == aData->mCbCrSize.width) {
        return ImageBitmapFormat::YUV420SP_NV21;  // Y-Cr-Cb
      }
    }
  }

  return ImageBitmapFormat::EndGuard_;
}

// ImageUtils::Impl implements the _generic_ algorithm which always readback
// data in RGBA format into CPU memory.
// Since layers::CairoImage is just a warpper to a DataSourceSurface, the
// implementation of CairoSurfaceImpl is nothing different to the generic
// version.
class ImageUtils::Impl {
 public:
  explicit Impl(layers::Image* aImage) : mImage(aImage), mSurface(nullptr) {}

  virtual ~Impl() = default;

  virtual ImageBitmapFormat GetFormat() const {
    return GetImageBitmapFormatFromSurfaceFromat(Surface()->GetFormat());
  }

  virtual uint32_t GetBufferLength() const {
    DataSourceSurface::ScopedMap map(Surface(), DataSourceSurface::READ);
    const uint32_t stride = map.GetStride();
    const IntSize size = Surface()->GetSize();
    return (uint32_t)(size.height * stride);
  }

 protected:
  Impl() {}

  DataSourceSurface* Surface() const {
    if (!mSurface) {
      RefPtr<SourceSurface> surface = mImage->GetAsSourceSurface();
      MOZ_ASSERT(surface);

      mSurface = surface->GetDataSurface();
      MOZ_ASSERT(mSurface);
    }

    return mSurface.get();
  }

  RefPtr<layers::Image> mImage;
  mutable RefPtr<DataSourceSurface> mSurface;
};

// YUVImpl is optimized for the layers::PlanarYCbCrImage and layers::NVImage.
// This implementation does not readback data in RGBA format but keep it in YUV
// format family.
class YUVImpl final : public ImageUtils::Impl {
 public:
  explicit YUVImpl(layers::Image* aImage) : Impl(aImage) {
    MOZ_ASSERT(aImage->GetFormat() == ImageFormat::PLANAR_YCBCR ||
               aImage->GetFormat() == ImageFormat::NV_IMAGE);
  }

  ImageBitmapFormat GetFormat() const override {
    return GetImageBitmapFormatFromPlanarYCbCrData(GetPlanarYCbCrData());
  }

  uint32_t GetBufferLength() const override {
    if (mImage->GetFormat() == ImageFormat::PLANAR_YCBCR) {
      return mImage->AsPlanarYCbCrImage()->GetDataSize();
    }
    return mImage->AsNVImage()->GetBufferSize();
  }

 private:
  const PlanarYCbCrData* GetPlanarYCbCrData() const {
    if (mImage->GetFormat() == ImageFormat::PLANAR_YCBCR) {
      return mImage->AsPlanarYCbCrImage()->GetData();
    }
    return mImage->AsNVImage()->GetData();
  }
};

// TODO: optimize for other platforms.
// For Windows: implement D3D9RGB32TextureImpl and D3D11ShareHandleTextureImpl.
// Others: SharedBGRImpl, MACIOSrufaceImpl, GLImageImpl, SurfaceTextureImpl
//         EGLImageImpl and OverlayImegImpl.

ImageUtils::ImageUtils(layers::Image* aImage) : mImpl(nullptr) {
  MOZ_ASSERT(aImage, "Create ImageUtils with nullptr.");
  switch (aImage->GetFormat()) {
    case mozilla::ImageFormat::PLANAR_YCBCR:
    case mozilla::ImageFormat::NV_IMAGE:
      mImpl = new YUVImpl(aImage);
      break;
    case mozilla::ImageFormat::CAIRO_SURFACE:
    default:
      mImpl = new Impl(aImage);
  }
}

ImageUtils::~ImageUtils() {
  if (mImpl) {
    delete mImpl;
    mImpl = nullptr;
  }
}

ImageBitmapFormat ImageUtils::GetFormat() const {
  MOZ_ASSERT(mImpl);
  return mImpl->GetFormat();
}

uint32_t ImageUtils::GetBufferLength() const {
  MOZ_ASSERT(mImpl);
  return mImpl->GetBufferLength();
}

}  // namespace dom
}  // namespace mozilla