1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef MOZILLA_GFX_2D_HELPERS_H_
8 #define MOZILLA_GFX_2D_HELPERS_H_
9 
10 #include "2D.h"
11 
12 namespace mozilla {
13 namespace gfx {
14 
15 class AutoRestoreTransform final {
16  public:
17   AutoRestoreTransform() = default;
18 
AutoRestoreTransform(DrawTarget * aTarget)19   explicit AutoRestoreTransform(DrawTarget* aTarget)
20       : mDrawTarget(aTarget), mOldTransform(aTarget->GetTransform()) {}
21 
Init(DrawTarget * aTarget)22   void Init(DrawTarget* aTarget) {
23     MOZ_ASSERT(!mDrawTarget || aTarget == mDrawTarget);
24     if (!mDrawTarget) {
25       mDrawTarget = aTarget;
26       mOldTransform = aTarget->GetTransform();
27     }
28   }
29 
~AutoRestoreTransform()30   ~AutoRestoreTransform() {
31     if (mDrawTarget) {
32       mDrawTarget->SetTransform(mOldTransform);
33     }
34   }
35 
36  private:
37   RefPtr<DrawTarget> mDrawTarget;
38   Matrix mOldTransform;
39 };
40 
41 class AutoPopClips final {
42  public:
AutoPopClips(DrawTarget * aTarget)43   explicit AutoPopClips(DrawTarget* aTarget)
44       : mDrawTarget(aTarget), mPushCount(0) {
45     MOZ_ASSERT(mDrawTarget);
46   }
47 
~AutoPopClips()48   ~AutoPopClips() { PopAll(); }
49 
PushClip(const Path * aPath)50   void PushClip(const Path* aPath) {
51     mDrawTarget->PushClip(aPath);
52     ++mPushCount;
53   }
54 
PushClipRect(const Rect & aRect)55   void PushClipRect(const Rect& aRect) {
56     mDrawTarget->PushClipRect(aRect);
57     ++mPushCount;
58   }
59 
PopClip()60   void PopClip() {
61     MOZ_ASSERT(mPushCount > 0);
62     mDrawTarget->PopClip();
63     --mPushCount;
64   }
65 
PopAll()66   void PopAll() {
67     while (mPushCount-- > 0) {
68       mDrawTarget->PopClip();
69     }
70   }
71 
72  private:
73   RefPtr<DrawTarget> mDrawTarget;
74   int32_t mPushCount;
75 };
76 
77 }  // namespace gfx
78 }  // namespace mozilla
79 
80 #endif  // MOZILLA_GFX_2D_HELPERS_H_
81