SourceSurfaceD2D1.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/. */
  5. #ifndef MOZILLA_GFX_SOURCESURFACED2D1_H_
  6. #define MOZILLA_GFX_SOURCESURFACED2D1_H_
  7. #include "2D.h"
  8. #include "HelpersD2D.h"
  9. #include <vector>
  10. #include <d3d11.h>
  11. #include <d2d1_1.h>
  12. namespace mozilla {
  13. namespace gfx {
  14. class DrawTargetD2D1;
  15. class SourceSurfaceD2D1 : public SourceSurface
  16. {
  17. public:
  18. MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(SourceSurfaceD2D1)
  19. SourceSurfaceD2D1(ID2D1Image* aImage, ID2D1DeviceContext *aDC,
  20. SurfaceFormat aFormat, const IntSize &aSize,
  21. DrawTargetD2D1 *aDT = nullptr);
  22. ~SourceSurfaceD2D1();
  23. virtual SurfaceType GetType() const { return SurfaceType::D2D1_1_IMAGE; }
  24. virtual IntSize GetSize() const { return mSize; }
  25. virtual SurfaceFormat GetFormat() const { return mFormat; }
  26. virtual bool IsValid() const;
  27. virtual already_AddRefed<DataSourceSurface> GetDataSurface();
  28. ID2D1Image *GetImage() { return mImage; }
  29. void EnsureIndependent() { if (!mDrawTarget) return; DrawTargetWillChange(); }
  30. private:
  31. friend class DrawTargetD2D1;
  32. bool EnsureRealizedBitmap();
  33. // This function is called by the draw target this texture belongs to when
  34. // it is about to be changed. The texture will be required to make a copy
  35. // of itself when this happens.
  36. void DrawTargetWillChange();
  37. // This will mark the surface as no longer depending on its drawtarget,
  38. // this may happen on destruction or copying.
  39. void MarkIndependent();
  40. RefPtr<ID2D1Image> mImage;
  41. // This may be null if we were created for a non-bitmap image and have not
  42. // had a reason yet to realize ourselves.
  43. RefPtr<ID2D1Bitmap1> mRealizedBitmap;
  44. RefPtr<ID2D1DeviceContext> mDC;
  45. // Keep this around to verify whether out image is still valid in the future.
  46. RefPtr<ID2D1Device> mDevice;
  47. SurfaceFormat mFormat;
  48. IntSize mSize;
  49. DrawTargetD2D1* mDrawTarget;
  50. };
  51. class DataSourceSurfaceD2D1 : public DataSourceSurface
  52. {
  53. public:
  54. MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceD2D1)
  55. DataSourceSurfaceD2D1(ID2D1Bitmap1 *aMappableBitmap, SurfaceFormat aFormat);
  56. ~DataSourceSurfaceD2D1();
  57. virtual SurfaceType GetType() const { return SurfaceType::DATA; }
  58. virtual IntSize GetSize() const;
  59. virtual SurfaceFormat GetFormat() const { return mFormat; }
  60. virtual bool IsValid() const { return !!mBitmap; }
  61. virtual uint8_t *GetData();
  62. virtual int32_t Stride();
  63. virtual bool Map(MapType, MappedSurface *aMappedSurface);
  64. virtual void Unmap();
  65. private:
  66. friend class SourceSurfaceD2DTarget;
  67. void EnsureMapped();
  68. mutable RefPtr<ID2D1Bitmap1> mBitmap;
  69. SurfaceFormat mFormat;
  70. D2D1_MAPPED_RECT mMap;
  71. bool mMapped;
  72. };
  73. }
  74. }
  75. #endif /* MOZILLA_GFX_SOURCESURFACED2D2TARGET_H_ */