Helpers.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_2D_HELPERS_H_
  6. #define MOZILLA_GFX_2D_HELPERS_H_
  7. #include "2D.h"
  8. namespace mozilla {
  9. namespace gfx {
  10. class AutoRestoreTransform
  11. {
  12. public:
  13. AutoRestoreTransform()
  14. {
  15. }
  16. explicit AutoRestoreTransform(DrawTarget *aTarget)
  17. : mDrawTarget(aTarget),
  18. mOldTransform(aTarget->GetTransform())
  19. {
  20. }
  21. void Init(DrawTarget *aTarget)
  22. {
  23. MOZ_ASSERT(!mDrawTarget || aTarget == mDrawTarget);
  24. if (!mDrawTarget) {
  25. mDrawTarget = aTarget;
  26. mOldTransform = aTarget->GetTransform();
  27. }
  28. }
  29. ~AutoRestoreTransform()
  30. {
  31. if (mDrawTarget) {
  32. mDrawTarget->SetTransform(mOldTransform);
  33. }
  34. }
  35. private:
  36. RefPtr<DrawTarget> mDrawTarget;
  37. Matrix mOldTransform;
  38. };
  39. class AutoPopClips
  40. {
  41. public:
  42. explicit AutoPopClips(DrawTarget *aTarget)
  43. : mDrawTarget(aTarget)
  44. , mPushCount(0)
  45. {
  46. MOZ_ASSERT(mDrawTarget);
  47. }
  48. ~AutoPopClips()
  49. {
  50. PopAll();
  51. }
  52. void PushClip(const Path *aPath)
  53. {
  54. mDrawTarget->PushClip(aPath);
  55. ++mPushCount;
  56. }
  57. void PushClipRect(const Rect &aRect)
  58. {
  59. mDrawTarget->PushClipRect(aRect);
  60. ++mPushCount;
  61. }
  62. void PopClip()
  63. {
  64. MOZ_ASSERT(mPushCount > 0);
  65. mDrawTarget->PopClip();
  66. --mPushCount;
  67. }
  68. void PopAll()
  69. {
  70. while (mPushCount-- > 0) {
  71. mDrawTarget->PopClip();
  72. }
  73. }
  74. private:
  75. RefPtr<DrawTarget> mDrawTarget;
  76. int32_t mPushCount;
  77. };
  78. } // namespace gfx
  79. } // namespace mozilla
  80. #endif // MOZILLA_GFX_2D_HELPERS_H_