gfx/2d/DataSurfaceHelpers.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/2d/DataSurfaceHelpers.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,171 @@
     1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "2D.h"
    1.10 +#include "DataSurfaceHelpers.h"
    1.11 +#include "Logging.h"
    1.12 +#include "mozilla/CheckedInt.h"
    1.13 +#include "mozilla/MathAlgorithms.h"
    1.14 +
    1.15 +namespace mozilla {
    1.16 +namespace gfx {
    1.17 +
    1.18 +void
    1.19 +ConvertBGRXToBGRA(uint8_t* aData, const IntSize &aSize, int32_t aStride)
    1.20 +{
    1.21 +  uint32_t* pixel = reinterpret_cast<uint32_t*>(aData);
    1.22 +
    1.23 +  for (int row = 0; row < aSize.height; ++row) {
    1.24 +    for (int column = 0; column < aSize.width; ++column) {
    1.25 +#ifdef IS_BIG_ENDIAN
    1.26 +      pixel[column] |= 0x000000FF;
    1.27 +#else
    1.28 +      pixel[column] |= 0xFF000000;
    1.29 +#endif
    1.30 +    }
    1.31 +    pixel += (aStride/4);
    1.32 +  }
    1.33 +}
    1.34 +
    1.35 +void
    1.36 +CopySurfaceDataToPackedArray(uint8_t* aSrc, uint8_t* aDst, IntSize aSrcSize,
    1.37 +                             int32_t aSrcStride, int32_t aBytesPerPixel)
    1.38 +{
    1.39 +  MOZ_ASSERT(aBytesPerPixel > 0,
    1.40 +             "Negative stride for aDst not currently supported");
    1.41 +  MOZ_ASSERT(BufferSizeFromStrideAndHeight(aSrcStride, aSrcSize.height) > 0,
    1.42 +             "How did we end up with a surface with such a big buffer?");
    1.43 +
    1.44 +  int packedStride = aSrcSize.width * aBytesPerPixel;
    1.45 +
    1.46 +  if (aSrcStride == packedStride) {
    1.47 +    // aSrc is already packed, so we can copy with a single memcpy.
    1.48 +    memcpy(aDst, aSrc, packedStride * aSrcSize.height);
    1.49 +  } else {
    1.50 +    // memcpy one row at a time.
    1.51 +    for (int row = 0; row < aSrcSize.height; ++row) {
    1.52 +      memcpy(aDst, aSrc, packedStride);
    1.53 +      aSrc += aSrcStride;
    1.54 +      aDst += packedStride;
    1.55 +    }
    1.56 +  }
    1.57 +}
    1.58 +
    1.59 +void
    1.60 +CopyBGRXSurfaceDataToPackedBGRArray(uint8_t* aSrc, uint8_t* aDst,
    1.61 +                                    IntSize aSrcSize, int32_t aSrcStride)
    1.62 +{
    1.63 +  int packedStride = aSrcSize.width * 3;
    1.64 +
    1.65 +  uint8_t* srcPx = aSrc;
    1.66 +  uint8_t* dstPx = aDst;
    1.67 +
    1.68 +  for (int row = 0; row < aSrcSize.height; ++row) {
    1.69 +    for (int col = 0; col < aSrcSize.height; ++col) {
    1.70 +      dstPx[0] = srcPx[0];
    1.71 +      dstPx[1] = srcPx[1];
    1.72 +      dstPx[2] = srcPx[2];
    1.73 +      // srcPx[3] (unused or alpha component) dropped on floor
    1.74 +      srcPx += 4;
    1.75 +      dstPx += 3;
    1.76 +    }
    1.77 +    srcPx = aSrc += aSrcStride;
    1.78 +    dstPx = aDst += packedStride;
    1.79 +  }
    1.80 +}
    1.81 +
    1.82 +uint8_t*
    1.83 +SurfaceToPackedBGRA(DataSourceSurface *aSurface)
    1.84 +{
    1.85 +  SurfaceFormat format = aSurface->GetFormat();
    1.86 +  if (format != SurfaceFormat::B8G8R8A8 && format != SurfaceFormat::B8G8R8X8) {
    1.87 +    return nullptr;
    1.88 +  }
    1.89 +
    1.90 +  IntSize size = aSurface->GetSize();
    1.91 +
    1.92 +  uint8_t* imageBuffer = new (std::nothrow) uint8_t[size.width * size.height * sizeof(uint32_t)];
    1.93 +  if (!imageBuffer) {
    1.94 +    return nullptr;
    1.95 +  }
    1.96 +
    1.97 +  DataSourceSurface::MappedSurface map;
    1.98 +  if (!aSurface->Map(DataSourceSurface::MapType::READ, &map)) {
    1.99 +    delete [] imageBuffer;
   1.100 +    return nullptr;
   1.101 +  }
   1.102 +
   1.103 +  CopySurfaceDataToPackedArray(map.mData, imageBuffer, size,
   1.104 +                               map.mStride, 4 * sizeof(uint8_t));
   1.105 +
   1.106 +  aSurface->Unmap();
   1.107 +
   1.108 +  if (format == SurfaceFormat::B8G8R8X8) {
   1.109 +    // Convert BGRX to BGRA by setting a to 255.
   1.110 +    ConvertBGRXToBGRA(reinterpret_cast<uint8_t *>(imageBuffer), size, size.width * sizeof(uint32_t));
   1.111 +  }
   1.112 +
   1.113 +  return imageBuffer;
   1.114 +}
   1.115 +
   1.116 +uint8_t*
   1.117 +SurfaceToPackedBGR(DataSourceSurface *aSurface)
   1.118 +{
   1.119 +  SurfaceFormat format = aSurface->GetFormat();
   1.120 +  MOZ_ASSERT(format == SurfaceFormat::B8G8R8X8, "Format not supported");
   1.121 +
   1.122 +  if (format != SurfaceFormat::B8G8R8X8) {
   1.123 +    // To support B8G8R8A8 we'd need to un-pre-multiply alpha
   1.124 +    return nullptr;
   1.125 +  }
   1.126 +
   1.127 +  IntSize size = aSurface->GetSize();
   1.128 +
   1.129 +  uint8_t* imageBuffer = new (std::nothrow) uint8_t[size.width * size.height * 3 * sizeof(uint8_t)];
   1.130 +  if (!imageBuffer) {
   1.131 +    return nullptr;
   1.132 +  }
   1.133 +
   1.134 +  DataSourceSurface::MappedSurface map;
   1.135 +  if (!aSurface->Map(DataSourceSurface::MapType::READ, &map)) {
   1.136 +    delete [] imageBuffer;
   1.137 +    return nullptr;
   1.138 +  }
   1.139 +
   1.140 +  CopyBGRXSurfaceDataToPackedBGRArray(map.mData, imageBuffer, size,
   1.141 +                                      map.mStride);
   1.142 +
   1.143 +  aSurface->Unmap();
   1.144 +
   1.145 +  return imageBuffer;
   1.146 +}
   1.147 +
   1.148 +size_t
   1.149 +BufferSizeFromStrideAndHeight(int32_t aStride,
   1.150 +                              int32_t aHeight,
   1.151 +                              int32_t aExtraBytes)
   1.152 +{
   1.153 +  if (MOZ_UNLIKELY(aHeight <= 0)) {
   1.154 +    return 0;
   1.155 +  }
   1.156 +
   1.157 +  // We limit the length returned to values that can be represented by int32_t
   1.158 +  // because we don't want to allocate buffers any bigger than that. This
   1.159 +  // allows for a buffer size of over 2 GiB which is already rediculously
   1.160 +  // large and will make the process janky. (Note the choice of the signed type
   1.161 +  // is deliberate because we specifically don't want the returned value to
   1.162 +  // overflow if someone stores the buffer length in an int32_t variable.)
   1.163 +
   1.164 +  CheckedInt32 requiredBytes =
   1.165 +    CheckedInt32(aStride) * CheckedInt32(aHeight) + CheckedInt32(aExtraBytes);
   1.166 +  if (MOZ_UNLIKELY(!requiredBytes.isValid())) {
   1.167 +    gfxWarning() << "Buffer size too big; returning zero";
   1.168 +    return 0;
   1.169 +  }
   1.170 +  return requiredBytes.value();
   1.171 +}
   1.172 +
   1.173 +}
   1.174 +}

mercurial