Bug 1686855 [wpt PR 27197] - PlzDedicatedWorker: WPT for clients.matchAll() with...
[gecko.git] / image / ImageRegion.h
blob0acc6e007b0ede9059e0c970764d8bfca27b9a64
1 /* -*- Mode: C++; tab-width: 2; 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/. */
6 #ifndef mozilla_image_ImageRegion_h
7 #define mozilla_image_ImageRegion_h
9 #include "gfxMatrix.h"
10 #include "gfxPoint.h"
11 #include "gfxRect.h"
12 #include "gfxTypes.h"
13 #include "mozilla/gfx/Matrix.h"
14 #include "mozilla/gfx/Types.h"
15 #include "nsSize.h"
17 namespace mozilla {
18 namespace image {
20 /**
21 * An axis-aligned rectangle in tiled image space, with an optional sampling
22 * restriction rect. The drawing code ensures that if a sampling restriction
23 * rect is present, any pixels sampled during the drawing process are found
24 * within that rect.
26 * The sampling restriction rect exists primarily for callers which perform
27 * pixel snapping. Other callers should generally use one of the Create()
28 * overloads.
30 class ImageRegion {
31 typedef mozilla::gfx::ExtendMode ExtendMode;
33 public:
34 static ImageRegion Empty() {
35 return ImageRegion(gfxRect(), ExtendMode::CLAMP);
38 static ImageRegion Create(const gfxRect& aRect,
39 ExtendMode aExtendMode = ExtendMode::CLAMP) {
40 return ImageRegion(aRect, aExtendMode);
43 static ImageRegion Create(const gfxSize& aSize,
44 ExtendMode aExtendMode = ExtendMode::CLAMP) {
45 return ImageRegion(gfxRect(0, 0, aSize.width, aSize.height), aExtendMode);
48 static ImageRegion Create(const nsIntSize& aSize,
49 ExtendMode aExtendMode = ExtendMode::CLAMP) {
50 return ImageRegion(gfxRect(0, 0, aSize.width, aSize.height), aExtendMode);
53 static ImageRegion CreateWithSamplingRestriction(
54 const gfxRect& aRect, const gfxRect& aRestriction,
55 ExtendMode aExtendMode = ExtendMode::CLAMP) {
56 return ImageRegion(aRect, aRestriction, aExtendMode);
59 bool IsRestricted() const { return mIsRestricted; }
60 const gfxRect& Rect() const { return mRect; }
62 const gfxRect& Restriction() const {
63 MOZ_ASSERT(mIsRestricted);
64 return mRestriction;
67 bool RestrictionContains(const gfxRect& aRect) const {
68 if (!mIsRestricted) {
69 return true;
71 return mRestriction.Contains(aRect);
74 ImageRegion Intersect(const gfxRect& aRect) const {
75 if (mIsRestricted) {
76 return CreateWithSamplingRestriction(aRect.Intersect(mRect),
77 aRect.Intersect(mRestriction));
79 return Create(aRect.Intersect(mRect));
82 gfxRect IntersectAndRestrict(const gfxRect& aRect) const {
83 gfxRect intersection = mRect.Intersect(aRect);
84 if (mIsRestricted) {
85 intersection = mRestriction.Intersect(intersection);
87 return intersection;
90 void MoveBy(gfxFloat dx, gfxFloat dy) {
91 mRect.MoveBy(dx, dy);
92 if (mIsRestricted) {
93 mRestriction.MoveBy(dx, dy);
97 void Scale(gfxFloat sx, gfxFloat sy) {
98 mRect.Scale(sx, sy);
99 if (mIsRestricted) {
100 mRestriction.Scale(sx, sy);
104 void TransformBy(const gfxMatrix& aMatrix) {
105 mRect = aMatrix.TransformRect(mRect);
106 if (mIsRestricted) {
107 mRestriction = aMatrix.TransformRect(mRestriction);
111 void TransformBoundsBy(const gfxMatrix& aMatrix) {
112 mRect = aMatrix.TransformBounds(mRect);
113 if (mIsRestricted) {
114 mRestriction = aMatrix.TransformBounds(mRestriction);
118 ImageRegion operator-(const gfxPoint& aPt) const {
119 if (mIsRestricted) {
120 return CreateWithSamplingRestriction(mRect - aPt, mRestriction - aPt);
122 return Create(mRect - aPt);
125 ImageRegion operator+(const gfxPoint& aPt) const {
126 if (mIsRestricted) {
127 return CreateWithSamplingRestriction(mRect + aPt, mRestriction + aPt);
129 return Create(mRect + aPt);
132 gfx::ExtendMode GetExtendMode() const { return mExtendMode; }
134 /* ImageRegion() : mIsRestricted(false) { } */
136 private:
137 explicit ImageRegion(const gfxRect& aRect, ExtendMode aExtendMode)
138 : mRect(aRect), mExtendMode(aExtendMode), mIsRestricted(false) {}
140 ImageRegion(const gfxRect& aRect, const gfxRect& aRestriction,
141 ExtendMode aExtendMode)
142 : mRect(aRect),
143 mRestriction(aRestriction),
144 mExtendMode(aExtendMode),
145 mIsRestricted(true) {}
147 gfxRect mRect;
148 gfxRect mRestriction;
149 ExtendMode mExtendMode;
150 bool mIsRestricted;
153 } // namespace image
154 } // namespace mozilla
156 #endif // mozilla_image_ImageRegion_h