gfx/layers/YCbCrImageDataSerializer.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* -*- Mode: C++; tab-width: 20; 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/. */
     6 #ifndef MOZILLA_LAYERS_BLOBYCBCRSURFACE_H
     7 #define MOZILLA_LAYERS_BLOBYCBCRSURFACE_H
     9 #include <stddef.h>                     // for size_t
    10 #include <stdint.h>                     // for uint8_t, uint32_t
    11 #include "ImageTypes.h"                 // for StereoMode
    12 #include "mozilla/Attributes.h"         // for MOZ_STACK_CLASS
    13 #include "mozilla/RefPtr.h"             // for TemporaryRef
    14 #include "mozilla/gfx/Point.h"          // for IntSize
    16 namespace mozilla {
    17 namespace gfx {
    18 class DataSourceSurface;
    19 }
    21 namespace layers {
    23 class Image;
    25 /**
    26  * Convenience class to share code between YCbCrImageDataSerializer
    27  * and YCbCrImageDataDeserializer.
    28  * Do not use it.
    29  */
    30 class YCbCrImageDataDeserializerBase
    31 {
    32 public:
    33   bool IsValid() const { return mIsValid; }
    35   /**
    36    * Returns the Y channel data pointer.
    37    */
    38   uint8_t* GetYData();
    39   /**
    40    * Returns the Cb channel data pointer.
    41    */
    42   uint8_t* GetCbData();
    43   /**
    44    * Returns the Cr channel data pointer.
    45    */
    46   uint8_t* GetCrData();
    48   /**
    49    * Returns the Y channel stride.
    50    */
    51   uint32_t GetYStride();
    52   /**
    53    * Returns the stride of the Cb and Cr channels.
    54    */
    55   uint32_t GetCbCrStride();
    57   /**
    58    * Returns the dimensions of the Y Channel.
    59    */
    60   gfx::IntSize GetYSize();
    62   /**
    63    * Returns the dimensions of the Cb and Cr Channel.
    64    */
    65   gfx::IntSize GetCbCrSize();
    67   /**
    68    * Stereo mode for the image.
    69    */
    70   StereoMode GetStereoMode();
    72   /**
    73    * Return a pointer to the begining of the data buffer.
    74    */
    75   uint8_t* GetData();
    77   /**
    78    * This function is meant as a helper to know how much shared memory we need
    79    * to allocate in a shmem in order to place a shared YCbCr image blob of
    80    * given dimensions.
    81    */
    82   static size_t ComputeMinBufferSize(const gfx::IntSize& aYSize,
    83                                      uint32_t aYStride,
    84                                      const gfx::IntSize& aCbCrSize,
    85                                      uint32_t aCbCrStride);
    86   static size_t ComputeMinBufferSize(const gfx::IntSize& aYSize,
    87                                      const gfx::IntSize& aCbCrSize);
    88   static size_t ComputeMinBufferSize(uint32_t aSize);
    90 protected:
    91   YCbCrImageDataDeserializerBase(uint8_t* aData, size_t aDataSize)
    92     : mData (aData)
    93     , mDataSize(aDataSize)
    94     , mIsValid(false)
    95   {}
    97   void Validate();
    99   uint8_t* mData;
   100   size_t mDataSize;
   101   bool mIsValid;
   102 };
   104 /**
   105  * A view on a YCbCr image stored with its metadata in a blob of memory.
   106  * It is only meant as a convenience to access the image data, and does not own
   107  * the data. The instance can live on the stack and used as follows:
   108  *
   109  * const YCbCrImage& yuv = sharedImage.get_YCbCrImage();
   110  * YCbCrImageDataDeserializer deserializer(yuv.data().get<uint8_t>());
   111  * if (!deserializer.IsValid()) {
   112  *   // handle error
   113  * }
   114  * size = deserializer.GetYSize(); // work with the data, etc...
   115  */
   116 class MOZ_STACK_CLASS YCbCrImageDataSerializer : public YCbCrImageDataDeserializerBase
   117 {
   118 public:
   119   YCbCrImageDataSerializer(uint8_t* aData, size_t aDataSize)
   120     : YCbCrImageDataDeserializerBase(aData, aDataSize)
   121   {
   122     // a serializer needs to be usable before correct buffer info has been written to it
   123     mIsValid = !!mData;
   124   }
   126   /**
   127    * Write the image informations in the buffer for given dimensions.
   128    * The provided pointer should point to the beginning of the (chunk of)
   129    * buffer on which we want to store the image.
   130    */
   131   void InitializeBufferInfo(uint32_t aYOffset,
   132                             uint32_t aCbOffset,
   133                             uint32_t aCrOffset,
   134                             uint32_t aYStride,
   135                             uint32_t aCbCrStride,
   136                             const gfx::IntSize& aYSize,
   137                             const gfx::IntSize& aCbCrSize,
   138                             StereoMode aStereoMode);
   139   void InitializeBufferInfo(uint32_t aYStride,
   140                             uint32_t aCbCrStride,
   141                             const gfx::IntSize& aYSize,
   142                             const gfx::IntSize& aCbCrSize,
   143                             StereoMode aStereoMode);
   144   void InitializeBufferInfo(const gfx::IntSize& aYSize,
   145                             const gfx::IntSize& aCbCrSize,
   146                             StereoMode aStereoMode);
   147   bool CopyData(const uint8_t* aYData,
   148                 const uint8_t* aCbData, const uint8_t* aCrData,
   149                 gfx::IntSize aYSize, uint32_t aYStride,
   150                 gfx::IntSize aCbCrSize, uint32_t aCbCrStride,
   151                 uint32_t aYSkip, uint32_t aCbCrSkip);
   152 };
   154 /**
   155  * A view on a YCbCr image stored with its metadata in a blob of memory.
   156  * It is only meant as a convenience to access the image data, and does not own
   157  * the data. The instance can live on the stack and used as follows:
   158  *
   159  * const YCbCrImage& yuv = sharedImage.get_YCbCrImage();
   160  * YCbCrImageDataDeserializer deserializer(yuv.data().get<uint8_t>());
   161  * if (!deserializer.IsValid()) {
   162  *   // handle error
   163  * }
   164  * size = deserializer.GetYSize(); // work with the data, etc...
   165  */
   166 class MOZ_STACK_CLASS YCbCrImageDataDeserializer : public YCbCrImageDataDeserializerBase
   167 {
   168 public:
   169   YCbCrImageDataDeserializer(uint8_t* aData, size_t aDataSize)
   170     : YCbCrImageDataDeserializerBase(aData, aDataSize)
   171   {
   172     Validate();
   173   }
   175   /**
   176    * Convert the YCbCr data into RGB and return a DataSourceSurface.
   177    * This is a costly operation, so use it only when YCbCr compositing is
   178    * not supported.
   179    */
   180   TemporaryRef<gfx::DataSourceSurface> ToDataSourceSurface();
   181 };
   183 } // namespace
   184 } // namespace
   186 #endif

mercurial