michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef GFX_IMAGESURFACE_H michael@0: #define GFX_IMAGESURFACE_H michael@0: michael@0: #include "mozilla/MemoryReporting.h" michael@0: #include "mozilla/RefPtr.h" michael@0: #include "gfxASurface.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsSize.h" michael@0: michael@0: // ARGB -- raw buffer.. wont be changed.. good for storing data. michael@0: michael@0: class gfxSubimageSurface; michael@0: michael@0: namespace mozilla { michael@0: namespace gfx { michael@0: class DataSourceSurface; michael@0: class SourceSurface; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * A raw image buffer. The format can be set in the constructor. Its main michael@0: * purpose is for storing read-only images and using it as a source surface, michael@0: * but it can also be drawn to. michael@0: */ michael@0: class gfxImageSurface : public gfxASurface { michael@0: public: michael@0: /** michael@0: * Construct an image surface around an existing buffer of image data. michael@0: * @param aData A buffer containing the image data michael@0: * @param aSize The size of the buffer michael@0: * @param aStride The stride of the buffer michael@0: * @param format Format of the data michael@0: * michael@0: * @see gfxImageFormat michael@0: */ michael@0: gfxImageSurface(unsigned char *aData, const gfxIntSize& aSize, michael@0: long aStride, gfxImageFormat aFormat); michael@0: michael@0: /** michael@0: * Construct an image surface. michael@0: * @param aSize The size of the buffer michael@0: * @param format Format of the data michael@0: * michael@0: * @see gfxImageFormat michael@0: */ michael@0: gfxImageSurface(const gfxIntSize& size, gfxImageFormat format, bool aClear = true); michael@0: michael@0: /** michael@0: * Construct an image surface, with a specified stride and allowing the michael@0: * allocation of more memory than required for the storage of the surface michael@0: * itself. When aStride and aMinimalAllocation are <=0, this constructor michael@0: * is the equivalent of the preceeding one. michael@0: * michael@0: * @param format Format of the data michael@0: * @param aSize The size of the buffer michael@0: * @param aStride The stride of the buffer - if <=0, use ComputeStride() michael@0: * @param aMinimalAllocation Allocate at least this many bytes. If smaller michael@0: * than width * stride, or width*stride <=0, this value is ignored. michael@0: * @param aClear michael@0: * michael@0: * @see gfxImageFormat michael@0: */ michael@0: gfxImageSurface(const gfxIntSize& aSize, gfxImageFormat aFormat, michael@0: long aStride, int32_t aMinimalAllocation, bool aClear); michael@0: michael@0: gfxImageSurface(cairo_surface_t *csurf); michael@0: michael@0: virtual ~gfxImageSurface(); michael@0: michael@0: // ImageSurface methods michael@0: gfxImageFormat Format() const { return mFormat; } michael@0: michael@0: virtual const gfxIntSize GetSize() const { return mSize; } michael@0: int32_t Width() const { return mSize.width; } michael@0: int32_t Height() const { return mSize.height; } michael@0: michael@0: /** michael@0: * Distance in bytes between the start of a line and the start of the michael@0: * next line. michael@0: */ michael@0: int32_t Stride() const { return mStride; } michael@0: /** michael@0: * Returns a pointer for the image data. Users of this function can michael@0: * write to it, but must not attempt to free the buffer. michael@0: */ michael@0: unsigned char* Data() const { return mData; } // delete this data under us and die. michael@0: /** michael@0: * Returns the total size of the image data. michael@0: */ michael@0: int32_t GetDataSize() const { return mStride*mSize.height; } michael@0: michael@0: /* Fast copy from another image surface; returns TRUE if successful, FALSE otherwise */ michael@0: bool CopyFrom (gfxImageSurface *other); michael@0: michael@0: /** michael@0: * Fast copy from a source surface; returns TRUE if successful, FALSE otherwise michael@0: * Assumes that the format of this surface is compatable with aSurface michael@0: */ michael@0: bool CopyFrom (mozilla::gfx::SourceSurface *aSurface); michael@0: michael@0: /** michael@0: * Fast copy to a source surface; returns TRUE if successful, FALSE otherwise michael@0: * Assumes that the format of this surface is compatible with aSurface michael@0: */ michael@0: bool CopyTo (mozilla::gfx::SourceSurface *aSurface); michael@0: michael@0: /** michael@0: * Copy to a Moz2D DataSourceSurface. michael@0: * Marked as virtual so that browsercomps can access this method. michael@0: */ michael@0: virtual mozilla::TemporaryRef CopyToB8G8R8A8DataSourceSurface(); michael@0: michael@0: /* return new Subimage with pointing to original image starting from aRect.pos michael@0: * and size of aRect.size. New subimage keeping current image reference michael@0: */ michael@0: already_AddRefed GetSubimage(const gfxRect& aRect); michael@0: michael@0: virtual already_AddRefed GetAsImageSurface(); michael@0: michael@0: /** See gfxASurface.h. */ michael@0: virtual void MovePixels(const nsIntRect& aSourceRect, michael@0: const nsIntPoint& aDestTopLeft) MOZ_OVERRIDE; michael@0: michael@0: static long ComputeStride(const gfxIntSize&, gfxImageFormat); michael@0: michael@0: virtual size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const michael@0: MOZ_OVERRIDE; michael@0: virtual size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const michael@0: MOZ_OVERRIDE; michael@0: virtual bool SizeOfIsMeasured() const MOZ_OVERRIDE; michael@0: michael@0: protected: michael@0: gfxImageSurface(); michael@0: void InitWithData(unsigned char *aData, const gfxIntSize& aSize, michael@0: long aStride, gfxImageFormat aFormat); michael@0: /** michael@0: * See the parameters to the matching constructor. This should only michael@0: * be called once, in the constructor, which has already set mSize michael@0: * and mFormat. michael@0: */ michael@0: void AllocateAndInit(long aStride, int32_t aMinimalAllocation, bool aClear); michael@0: void InitFromSurface(cairo_surface_t *csurf); michael@0: michael@0: long ComputeStride() const { return ComputeStride(mSize, mFormat); } michael@0: michael@0: michael@0: void MakeInvalid(); michael@0: michael@0: gfxIntSize mSize; michael@0: bool mOwnsData; michael@0: unsigned char *mData; michael@0: gfxImageFormat mFormat; michael@0: long mStride; michael@0: }; michael@0: michael@0: class gfxSubimageSurface : public gfxImageSurface { michael@0: protected: michael@0: friend class gfxImageSurface; michael@0: gfxSubimageSurface(gfxImageSurface* aParent, michael@0: unsigned char* aData, michael@0: const gfxIntSize& aSize, michael@0: gfxImageFormat aFormat); michael@0: private: michael@0: nsRefPtr mParent; michael@0: }; michael@0: michael@0: #endif /* GFX_IMAGESURFACE_H */