Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
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 GFX_IMAGESURFACE_H |
michael@0 | 7 | #define GFX_IMAGESURFACE_H |
michael@0 | 8 | |
michael@0 | 9 | #include "mozilla/MemoryReporting.h" |
michael@0 | 10 | #include "mozilla/RefPtr.h" |
michael@0 | 11 | #include "gfxASurface.h" |
michael@0 | 12 | #include "nsAutoPtr.h" |
michael@0 | 13 | #include "nsSize.h" |
michael@0 | 14 | |
michael@0 | 15 | // ARGB -- raw buffer.. wont be changed.. good for storing data. |
michael@0 | 16 | |
michael@0 | 17 | class gfxSubimageSurface; |
michael@0 | 18 | |
michael@0 | 19 | namespace mozilla { |
michael@0 | 20 | namespace gfx { |
michael@0 | 21 | class DataSourceSurface; |
michael@0 | 22 | class SourceSurface; |
michael@0 | 23 | } |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | /** |
michael@0 | 27 | * A raw image buffer. The format can be set in the constructor. Its main |
michael@0 | 28 | * purpose is for storing read-only images and using it as a source surface, |
michael@0 | 29 | * but it can also be drawn to. |
michael@0 | 30 | */ |
michael@0 | 31 | class gfxImageSurface : public gfxASurface { |
michael@0 | 32 | public: |
michael@0 | 33 | /** |
michael@0 | 34 | * Construct an image surface around an existing buffer of image data. |
michael@0 | 35 | * @param aData A buffer containing the image data |
michael@0 | 36 | * @param aSize The size of the buffer |
michael@0 | 37 | * @param aStride The stride of the buffer |
michael@0 | 38 | * @param format Format of the data |
michael@0 | 39 | * |
michael@0 | 40 | * @see gfxImageFormat |
michael@0 | 41 | */ |
michael@0 | 42 | gfxImageSurface(unsigned char *aData, const gfxIntSize& aSize, |
michael@0 | 43 | long aStride, gfxImageFormat aFormat); |
michael@0 | 44 | |
michael@0 | 45 | /** |
michael@0 | 46 | * Construct an image surface. |
michael@0 | 47 | * @param aSize The size of the buffer |
michael@0 | 48 | * @param format Format of the data |
michael@0 | 49 | * |
michael@0 | 50 | * @see gfxImageFormat |
michael@0 | 51 | */ |
michael@0 | 52 | gfxImageSurface(const gfxIntSize& size, gfxImageFormat format, bool aClear = true); |
michael@0 | 53 | |
michael@0 | 54 | /** |
michael@0 | 55 | * Construct an image surface, with a specified stride and allowing the |
michael@0 | 56 | * allocation of more memory than required for the storage of the surface |
michael@0 | 57 | * itself. When aStride and aMinimalAllocation are <=0, this constructor |
michael@0 | 58 | * is the equivalent of the preceeding one. |
michael@0 | 59 | * |
michael@0 | 60 | * @param format Format of the data |
michael@0 | 61 | * @param aSize The size of the buffer |
michael@0 | 62 | * @param aStride The stride of the buffer - if <=0, use ComputeStride() |
michael@0 | 63 | * @param aMinimalAllocation Allocate at least this many bytes. If smaller |
michael@0 | 64 | * than width * stride, or width*stride <=0, this value is ignored. |
michael@0 | 65 | * @param aClear |
michael@0 | 66 | * |
michael@0 | 67 | * @see gfxImageFormat |
michael@0 | 68 | */ |
michael@0 | 69 | gfxImageSurface(const gfxIntSize& aSize, gfxImageFormat aFormat, |
michael@0 | 70 | long aStride, int32_t aMinimalAllocation, bool aClear); |
michael@0 | 71 | |
michael@0 | 72 | gfxImageSurface(cairo_surface_t *csurf); |
michael@0 | 73 | |
michael@0 | 74 | virtual ~gfxImageSurface(); |
michael@0 | 75 | |
michael@0 | 76 | // ImageSurface methods |
michael@0 | 77 | gfxImageFormat Format() const { return mFormat; } |
michael@0 | 78 | |
michael@0 | 79 | virtual const gfxIntSize GetSize() const { return mSize; } |
michael@0 | 80 | int32_t Width() const { return mSize.width; } |
michael@0 | 81 | int32_t Height() const { return mSize.height; } |
michael@0 | 82 | |
michael@0 | 83 | /** |
michael@0 | 84 | * Distance in bytes between the start of a line and the start of the |
michael@0 | 85 | * next line. |
michael@0 | 86 | */ |
michael@0 | 87 | int32_t Stride() const { return mStride; } |
michael@0 | 88 | /** |
michael@0 | 89 | * Returns a pointer for the image data. Users of this function can |
michael@0 | 90 | * write to it, but must not attempt to free the buffer. |
michael@0 | 91 | */ |
michael@0 | 92 | unsigned char* Data() const { return mData; } // delete this data under us and die. |
michael@0 | 93 | /** |
michael@0 | 94 | * Returns the total size of the image data. |
michael@0 | 95 | */ |
michael@0 | 96 | int32_t GetDataSize() const { return mStride*mSize.height; } |
michael@0 | 97 | |
michael@0 | 98 | /* Fast copy from another image surface; returns TRUE if successful, FALSE otherwise */ |
michael@0 | 99 | bool CopyFrom (gfxImageSurface *other); |
michael@0 | 100 | |
michael@0 | 101 | /** |
michael@0 | 102 | * Fast copy from a source surface; returns TRUE if successful, FALSE otherwise |
michael@0 | 103 | * Assumes that the format of this surface is compatable with aSurface |
michael@0 | 104 | */ |
michael@0 | 105 | bool CopyFrom (mozilla::gfx::SourceSurface *aSurface); |
michael@0 | 106 | |
michael@0 | 107 | /** |
michael@0 | 108 | * Fast copy to a source surface; returns TRUE if successful, FALSE otherwise |
michael@0 | 109 | * Assumes that the format of this surface is compatible with aSurface |
michael@0 | 110 | */ |
michael@0 | 111 | bool CopyTo (mozilla::gfx::SourceSurface *aSurface); |
michael@0 | 112 | |
michael@0 | 113 | /** |
michael@0 | 114 | * Copy to a Moz2D DataSourceSurface. |
michael@0 | 115 | * Marked as virtual so that browsercomps can access this method. |
michael@0 | 116 | */ |
michael@0 | 117 | virtual mozilla::TemporaryRef<mozilla::gfx::DataSourceSurface> CopyToB8G8R8A8DataSourceSurface(); |
michael@0 | 118 | |
michael@0 | 119 | /* return new Subimage with pointing to original image starting from aRect.pos |
michael@0 | 120 | * and size of aRect.size. New subimage keeping current image reference |
michael@0 | 121 | */ |
michael@0 | 122 | already_AddRefed<gfxSubimageSurface> GetSubimage(const gfxRect& aRect); |
michael@0 | 123 | |
michael@0 | 124 | virtual already_AddRefed<gfxImageSurface> GetAsImageSurface(); |
michael@0 | 125 | |
michael@0 | 126 | /** See gfxASurface.h. */ |
michael@0 | 127 | virtual void MovePixels(const nsIntRect& aSourceRect, |
michael@0 | 128 | const nsIntPoint& aDestTopLeft) MOZ_OVERRIDE; |
michael@0 | 129 | |
michael@0 | 130 | static long ComputeStride(const gfxIntSize&, gfxImageFormat); |
michael@0 | 131 | |
michael@0 | 132 | virtual size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const |
michael@0 | 133 | MOZ_OVERRIDE; |
michael@0 | 134 | virtual size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const |
michael@0 | 135 | MOZ_OVERRIDE; |
michael@0 | 136 | virtual bool SizeOfIsMeasured() const MOZ_OVERRIDE; |
michael@0 | 137 | |
michael@0 | 138 | protected: |
michael@0 | 139 | gfxImageSurface(); |
michael@0 | 140 | void InitWithData(unsigned char *aData, const gfxIntSize& aSize, |
michael@0 | 141 | long aStride, gfxImageFormat aFormat); |
michael@0 | 142 | /** |
michael@0 | 143 | * See the parameters to the matching constructor. This should only |
michael@0 | 144 | * be called once, in the constructor, which has already set mSize |
michael@0 | 145 | * and mFormat. |
michael@0 | 146 | */ |
michael@0 | 147 | void AllocateAndInit(long aStride, int32_t aMinimalAllocation, bool aClear); |
michael@0 | 148 | void InitFromSurface(cairo_surface_t *csurf); |
michael@0 | 149 | |
michael@0 | 150 | long ComputeStride() const { return ComputeStride(mSize, mFormat); } |
michael@0 | 151 | |
michael@0 | 152 | |
michael@0 | 153 | void MakeInvalid(); |
michael@0 | 154 | |
michael@0 | 155 | gfxIntSize mSize; |
michael@0 | 156 | bool mOwnsData; |
michael@0 | 157 | unsigned char *mData; |
michael@0 | 158 | gfxImageFormat mFormat; |
michael@0 | 159 | long mStride; |
michael@0 | 160 | }; |
michael@0 | 161 | |
michael@0 | 162 | class gfxSubimageSurface : public gfxImageSurface { |
michael@0 | 163 | protected: |
michael@0 | 164 | friend class gfxImageSurface; |
michael@0 | 165 | gfxSubimageSurface(gfxImageSurface* aParent, |
michael@0 | 166 | unsigned char* aData, |
michael@0 | 167 | const gfxIntSize& aSize, |
michael@0 | 168 | gfxImageFormat aFormat); |
michael@0 | 169 | private: |
michael@0 | 170 | nsRefPtr<gfxImageSurface> mParent; |
michael@0 | 171 | }; |
michael@0 | 172 | |
michael@0 | 173 | #endif /* GFX_IMAGESURFACE_H */ |