gfxImageSurface.h 6.1 KB

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