gfx/2d/SourceSurfaceCG.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_GFX_SOURCESURFACECG_H
     7 #define _MOZILLA_GFX_SOURCESURFACECG_H
     9 #include <ApplicationServices/ApplicationServices.h>
    11 #include "2D.h"
    13 class MacIOSurface;
    15 namespace mozilla {
    16 namespace gfx {
    18 CGImageRef
    19 CreateCGImage(void *aInfo,
    20               const void *aData,
    21               const IntSize &aSize,
    22               int32_t aStride,
    23               SurfaceFormat aFormat);
    25 class DrawTargetCG;
    27 class SourceSurfaceCG : public SourceSurface
    28 {
    29 public:
    30   MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(SourceSurfaceCG)
    31   SourceSurfaceCG() {}
    32   SourceSurfaceCG(CGImageRef aImage) : mImage(aImage) {}
    33   ~SourceSurfaceCG();
    35   virtual SurfaceType GetType() const { return SurfaceType::COREGRAPHICS_IMAGE; }
    36   virtual IntSize GetSize() const;
    37   virtual SurfaceFormat GetFormat() const;
    38   virtual TemporaryRef<DataSourceSurface> GetDataSurface();
    40   CGImageRef GetImage() { return mImage; }
    42   bool InitFromData(unsigned char *aData,
    43                     const IntSize &aSize,
    44                     int32_t aStride,
    45                     SurfaceFormat aFormat);
    47 private:
    48   CGImageRef mImage;
    50   /* It might be better to just use the bitmap info from the CGImageRef to
    51    * deduce the format to save space in SourceSurfaceCG,
    52    * for now we just store it in mFormat */
    53   SurfaceFormat mFormat;
    54 };
    56 class DataSourceSurfaceCG : public DataSourceSurface
    57 {
    58 public:
    59   MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceCG)
    60   DataSourceSurfaceCG() {}
    61   DataSourceSurfaceCG(CGImageRef aImage);
    62   ~DataSourceSurfaceCG();
    64   virtual SurfaceType GetType() const { return SurfaceType::DATA; }
    65   virtual IntSize GetSize() const;
    66   virtual SurfaceFormat GetFormat() const { return mFormat; }
    68   CGImageRef GetImage() { return mImage; }
    70   bool InitFromData(unsigned char *aData,
    71                     const IntSize &aSize,
    72                     int32_t aStride,
    73                     SurfaceFormat aFormat);
    75   virtual unsigned char *GetData();
    77   virtual int32_t Stride() { return CGImageGetBytesPerRow(mImage); }
    80 private:
    81   CGContextRef mCg;
    82   CGImageRef mImage;
    83   SurfaceFormat mFormat;
    84   //XXX: we don't need to store mData we can just get it from the CGContext
    85   void *mData;
    86   /* It might be better to just use the bitmap info from the CGImageRef to
    87    * deduce the format to save space in SourceSurfaceCG,
    88    * for now we just store it in mFormat */
    89 };
    91 class SourceSurfaceCGContext : public DataSourceSurface
    92 {
    93 public:
    94   MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceCGContext)
    95   virtual void DrawTargetWillChange() = 0;
    96   virtual CGImageRef GetImage() = 0;
    97 };
    99 class SourceSurfaceCGBitmapContext : public SourceSurfaceCGContext
   100 {
   101 public:
   102   MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceCGBitmapContext)
   103   SourceSurfaceCGBitmapContext(DrawTargetCG *);
   104   ~SourceSurfaceCGBitmapContext();
   106   virtual SurfaceType GetType() const { return SurfaceType::COREGRAPHICS_CGCONTEXT; }
   107   virtual IntSize GetSize() const;
   108   virtual SurfaceFormat GetFormat() const { return mFormat; }
   109   virtual TemporaryRef<DataSourceSurface> GetDataSurface()
   110   {
   111     // This call to DrawTargetWillChange() is needed to make a local copy of
   112     // the data from mDrawTarget.  If we don't do that, the data can end up
   113     // getting deleted before the CGImageRef it belongs to.
   114     //
   115     // Another reason we need a local copy of the data is that the data in
   116     // mDrawTarget could change when someone touches the original DrawTargetCG
   117     // object.  But a SourceSurface object should be immutable.
   118     //
   119     // For more information see bug 925448.
   120     DrawTargetWillChange();
   121     return this;
   122   }
   124   CGImageRef GetImage() { EnsureImage(); return mImage; }
   126   virtual unsigned char *GetData() { return static_cast<unsigned char*>(mData); }
   128   virtual int32_t Stride() { return mStride; }
   130 private:
   131   //XXX: do the other backends friend their DrawTarget?
   132   friend class DrawTargetCG;
   133   virtual void DrawTargetWillChange();
   134   void EnsureImage() const;
   136   // We hold a weak reference to these two objects.
   137   // The cycle is broken by DrawTargetWillChange
   138   DrawTargetCG *mDrawTarget;
   139   CGContextRef mCg;
   140   SurfaceFormat mFormat;
   142   mutable CGImageRef mImage;
   144   // mData can be owned by three different things:
   145   // mImage, mCg or SourceSurfaceCGBitmapContext
   146   void *mData;
   148   // The image buffer, if the buffer is owned by this class.
   149   AlignedArray<uint8_t> mDataHolder;
   151   int32_t mStride;
   152   IntSize mSize;
   153 };
   155 class SourceSurfaceCGIOSurfaceContext : public SourceSurfaceCGContext
   156 {
   157 public:
   158   MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceCGIOSurfaceContext)
   159   SourceSurfaceCGIOSurfaceContext(DrawTargetCG *);
   160   ~SourceSurfaceCGIOSurfaceContext();
   162   virtual SurfaceType GetType() const { return SurfaceType::COREGRAPHICS_CGCONTEXT; }
   163   virtual IntSize GetSize() const;
   164   virtual SurfaceFormat GetFormat() const { return mFormat; }
   166   CGImageRef GetImage() { EnsureImage(); return mImage; }
   168   virtual unsigned char *GetData();
   170   virtual int32_t Stride() { return mStride; }
   172 private:
   173   //XXX: do the other backends friend their DrawTarget?
   174   friend class DrawTargetCG;
   175   virtual void DrawTargetWillChange();
   176   void EnsureImage() const;
   178   SurfaceFormat mFormat;
   179   mutable CGImageRef mImage;
   180   MacIOSurface* mIOSurface;
   182   void *mData;
   183   int32_t mStride;
   185   IntSize mSize;
   186 };
   189 }
   190 }
   192 #endif // _MOZILLA_GFX_SOURCESURFACECG_H

mercurial