Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef _MOZILLA_GFX_SOURCESURFACECG_H |
michael@0 | 7 | #define _MOZILLA_GFX_SOURCESURFACECG_H |
michael@0 | 8 | |
michael@0 | 9 | #include <ApplicationServices/ApplicationServices.h> |
michael@0 | 10 | |
michael@0 | 11 | #include "2D.h" |
michael@0 | 12 | |
michael@0 | 13 | class MacIOSurface; |
michael@0 | 14 | |
michael@0 | 15 | namespace mozilla { |
michael@0 | 16 | namespace gfx { |
michael@0 | 17 | |
michael@0 | 18 | CGImageRef |
michael@0 | 19 | CreateCGImage(void *aInfo, |
michael@0 | 20 | const void *aData, |
michael@0 | 21 | const IntSize &aSize, |
michael@0 | 22 | int32_t aStride, |
michael@0 | 23 | SurfaceFormat aFormat); |
michael@0 | 24 | |
michael@0 | 25 | class DrawTargetCG; |
michael@0 | 26 | |
michael@0 | 27 | class SourceSurfaceCG : public SourceSurface |
michael@0 | 28 | { |
michael@0 | 29 | public: |
michael@0 | 30 | MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(SourceSurfaceCG) |
michael@0 | 31 | SourceSurfaceCG() {} |
michael@0 | 32 | SourceSurfaceCG(CGImageRef aImage) : mImage(aImage) {} |
michael@0 | 33 | ~SourceSurfaceCG(); |
michael@0 | 34 | |
michael@0 | 35 | virtual SurfaceType GetType() const { return SurfaceType::COREGRAPHICS_IMAGE; } |
michael@0 | 36 | virtual IntSize GetSize() const; |
michael@0 | 37 | virtual SurfaceFormat GetFormat() const; |
michael@0 | 38 | virtual TemporaryRef<DataSourceSurface> GetDataSurface(); |
michael@0 | 39 | |
michael@0 | 40 | CGImageRef GetImage() { return mImage; } |
michael@0 | 41 | |
michael@0 | 42 | bool InitFromData(unsigned char *aData, |
michael@0 | 43 | const IntSize &aSize, |
michael@0 | 44 | int32_t aStride, |
michael@0 | 45 | SurfaceFormat aFormat); |
michael@0 | 46 | |
michael@0 | 47 | private: |
michael@0 | 48 | CGImageRef mImage; |
michael@0 | 49 | |
michael@0 | 50 | /* It might be better to just use the bitmap info from the CGImageRef to |
michael@0 | 51 | * deduce the format to save space in SourceSurfaceCG, |
michael@0 | 52 | * for now we just store it in mFormat */ |
michael@0 | 53 | SurfaceFormat mFormat; |
michael@0 | 54 | }; |
michael@0 | 55 | |
michael@0 | 56 | class DataSourceSurfaceCG : public DataSourceSurface |
michael@0 | 57 | { |
michael@0 | 58 | public: |
michael@0 | 59 | MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceCG) |
michael@0 | 60 | DataSourceSurfaceCG() {} |
michael@0 | 61 | DataSourceSurfaceCG(CGImageRef aImage); |
michael@0 | 62 | ~DataSourceSurfaceCG(); |
michael@0 | 63 | |
michael@0 | 64 | virtual SurfaceType GetType() const { return SurfaceType::DATA; } |
michael@0 | 65 | virtual IntSize GetSize() const; |
michael@0 | 66 | virtual SurfaceFormat GetFormat() const { return mFormat; } |
michael@0 | 67 | |
michael@0 | 68 | CGImageRef GetImage() { return mImage; } |
michael@0 | 69 | |
michael@0 | 70 | bool InitFromData(unsigned char *aData, |
michael@0 | 71 | const IntSize &aSize, |
michael@0 | 72 | int32_t aStride, |
michael@0 | 73 | SurfaceFormat aFormat); |
michael@0 | 74 | |
michael@0 | 75 | virtual unsigned char *GetData(); |
michael@0 | 76 | |
michael@0 | 77 | virtual int32_t Stride() { return CGImageGetBytesPerRow(mImage); } |
michael@0 | 78 | |
michael@0 | 79 | |
michael@0 | 80 | private: |
michael@0 | 81 | CGContextRef mCg; |
michael@0 | 82 | CGImageRef mImage; |
michael@0 | 83 | SurfaceFormat mFormat; |
michael@0 | 84 | //XXX: we don't need to store mData we can just get it from the CGContext |
michael@0 | 85 | void *mData; |
michael@0 | 86 | /* It might be better to just use the bitmap info from the CGImageRef to |
michael@0 | 87 | * deduce the format to save space in SourceSurfaceCG, |
michael@0 | 88 | * for now we just store it in mFormat */ |
michael@0 | 89 | }; |
michael@0 | 90 | |
michael@0 | 91 | class SourceSurfaceCGContext : public DataSourceSurface |
michael@0 | 92 | { |
michael@0 | 93 | public: |
michael@0 | 94 | MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceCGContext) |
michael@0 | 95 | virtual void DrawTargetWillChange() = 0; |
michael@0 | 96 | virtual CGImageRef GetImage() = 0; |
michael@0 | 97 | }; |
michael@0 | 98 | |
michael@0 | 99 | class SourceSurfaceCGBitmapContext : public SourceSurfaceCGContext |
michael@0 | 100 | { |
michael@0 | 101 | public: |
michael@0 | 102 | MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceCGBitmapContext) |
michael@0 | 103 | SourceSurfaceCGBitmapContext(DrawTargetCG *); |
michael@0 | 104 | ~SourceSurfaceCGBitmapContext(); |
michael@0 | 105 | |
michael@0 | 106 | virtual SurfaceType GetType() const { return SurfaceType::COREGRAPHICS_CGCONTEXT; } |
michael@0 | 107 | virtual IntSize GetSize() const; |
michael@0 | 108 | virtual SurfaceFormat GetFormat() const { return mFormat; } |
michael@0 | 109 | virtual TemporaryRef<DataSourceSurface> GetDataSurface() |
michael@0 | 110 | { |
michael@0 | 111 | // This call to DrawTargetWillChange() is needed to make a local copy of |
michael@0 | 112 | // the data from mDrawTarget. If we don't do that, the data can end up |
michael@0 | 113 | // getting deleted before the CGImageRef it belongs to. |
michael@0 | 114 | // |
michael@0 | 115 | // Another reason we need a local copy of the data is that the data in |
michael@0 | 116 | // mDrawTarget could change when someone touches the original DrawTargetCG |
michael@0 | 117 | // object. But a SourceSurface object should be immutable. |
michael@0 | 118 | // |
michael@0 | 119 | // For more information see bug 925448. |
michael@0 | 120 | DrawTargetWillChange(); |
michael@0 | 121 | return this; |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | CGImageRef GetImage() { EnsureImage(); return mImage; } |
michael@0 | 125 | |
michael@0 | 126 | virtual unsigned char *GetData() { return static_cast<unsigned char*>(mData); } |
michael@0 | 127 | |
michael@0 | 128 | virtual int32_t Stride() { return mStride; } |
michael@0 | 129 | |
michael@0 | 130 | private: |
michael@0 | 131 | //XXX: do the other backends friend their DrawTarget? |
michael@0 | 132 | friend class DrawTargetCG; |
michael@0 | 133 | virtual void DrawTargetWillChange(); |
michael@0 | 134 | void EnsureImage() const; |
michael@0 | 135 | |
michael@0 | 136 | // We hold a weak reference to these two objects. |
michael@0 | 137 | // The cycle is broken by DrawTargetWillChange |
michael@0 | 138 | DrawTargetCG *mDrawTarget; |
michael@0 | 139 | CGContextRef mCg; |
michael@0 | 140 | SurfaceFormat mFormat; |
michael@0 | 141 | |
michael@0 | 142 | mutable CGImageRef mImage; |
michael@0 | 143 | |
michael@0 | 144 | // mData can be owned by three different things: |
michael@0 | 145 | // mImage, mCg or SourceSurfaceCGBitmapContext |
michael@0 | 146 | void *mData; |
michael@0 | 147 | |
michael@0 | 148 | // The image buffer, if the buffer is owned by this class. |
michael@0 | 149 | AlignedArray<uint8_t> mDataHolder; |
michael@0 | 150 | |
michael@0 | 151 | int32_t mStride; |
michael@0 | 152 | IntSize mSize; |
michael@0 | 153 | }; |
michael@0 | 154 | |
michael@0 | 155 | class SourceSurfaceCGIOSurfaceContext : public SourceSurfaceCGContext |
michael@0 | 156 | { |
michael@0 | 157 | public: |
michael@0 | 158 | MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceCGIOSurfaceContext) |
michael@0 | 159 | SourceSurfaceCGIOSurfaceContext(DrawTargetCG *); |
michael@0 | 160 | ~SourceSurfaceCGIOSurfaceContext(); |
michael@0 | 161 | |
michael@0 | 162 | virtual SurfaceType GetType() const { return SurfaceType::COREGRAPHICS_CGCONTEXT; } |
michael@0 | 163 | virtual IntSize GetSize() const; |
michael@0 | 164 | virtual SurfaceFormat GetFormat() const { return mFormat; } |
michael@0 | 165 | |
michael@0 | 166 | CGImageRef GetImage() { EnsureImage(); return mImage; } |
michael@0 | 167 | |
michael@0 | 168 | virtual unsigned char *GetData(); |
michael@0 | 169 | |
michael@0 | 170 | virtual int32_t Stride() { return mStride; } |
michael@0 | 171 | |
michael@0 | 172 | private: |
michael@0 | 173 | //XXX: do the other backends friend their DrawTarget? |
michael@0 | 174 | friend class DrawTargetCG; |
michael@0 | 175 | virtual void DrawTargetWillChange(); |
michael@0 | 176 | void EnsureImage() const; |
michael@0 | 177 | |
michael@0 | 178 | SurfaceFormat mFormat; |
michael@0 | 179 | mutable CGImageRef mImage; |
michael@0 | 180 | MacIOSurface* mIOSurface; |
michael@0 | 181 | |
michael@0 | 182 | void *mData; |
michael@0 | 183 | int32_t mStride; |
michael@0 | 184 | |
michael@0 | 185 | IntSize mSize; |
michael@0 | 186 | }; |
michael@0 | 187 | |
michael@0 | 188 | |
michael@0 | 189 | } |
michael@0 | 190 | } |
michael@0 | 191 | |
michael@0 | 192 | #endif // _MOZILLA_GFX_SOURCESURFACECG_H |