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/. */
5 
6 #ifndef mozilla_image_SurfaceFlags_h
7 #define mozilla_image_SurfaceFlags_h
8 
9 #include "imgIContainer.h"
10 #include "mozilla/TypedEnumBits.h"
11 
12 namespace mozilla {
13 namespace image {
14 
15 /**
16  * Flags that change the output a decoder generates. Because different
17  * combinations of these flags result in logically different surfaces, these
18  * flags must be taken into account in SurfaceCache lookups.
19  */
20 enum class SurfaceFlags : uint8_t {
21   NO_PREMULTIPLY_ALPHA = 1 << 0,
22   NO_COLORSPACE_CONVERSION = 1 << 1,
23   TO_SRGB_COLORSPACE = 2 << 1,
24 };
MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(SurfaceFlags)25 MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(SurfaceFlags)
26 
27 /**
28  * @return the default set of surface flags.
29  */
30 inline SurfaceFlags DefaultSurfaceFlags() { return SurfaceFlags(); }
31 
32 /**
33  * Given a set of imgIContainer FLAG_* flags, returns a set of SurfaceFlags with
34  * the corresponding flags set.
35  */
ToSurfaceFlags(uint32_t aFlags)36 inline SurfaceFlags ToSurfaceFlags(uint32_t aFlags) {
37   SurfaceFlags flags = DefaultSurfaceFlags();
38   if (aFlags & imgIContainer::FLAG_DECODE_NO_PREMULTIPLY_ALPHA) {
39     flags |= SurfaceFlags::NO_PREMULTIPLY_ALPHA;
40   }
41   if (aFlags & imgIContainer::FLAG_DECODE_NO_COLORSPACE_CONVERSION) {
42     flags |= SurfaceFlags::NO_COLORSPACE_CONVERSION;
43   }
44   if (aFlags & imgIContainer::FLAG_DECODE_TO_SRGB_COLORSPACE) {
45     flags |= SurfaceFlags::TO_SRGB_COLORSPACE;
46   }
47   return flags;
48 }
49 
50 /**
51  * Given a set of SurfaceFlags, returns a set of imgIContainer FLAG_* flags with
52  * the corresponding flags set.
53  */
FromSurfaceFlags(SurfaceFlags aFlags)54 inline uint32_t FromSurfaceFlags(SurfaceFlags aFlags) {
55   uint32_t flags = imgIContainer::DECODE_FLAGS_DEFAULT;
56   if (aFlags & SurfaceFlags::NO_PREMULTIPLY_ALPHA) {
57     flags |= imgIContainer::FLAG_DECODE_NO_PREMULTIPLY_ALPHA;
58   }
59   if (aFlags & SurfaceFlags::NO_COLORSPACE_CONVERSION) {
60     flags |= imgIContainer::FLAG_DECODE_NO_COLORSPACE_CONVERSION;
61   }
62   if (aFlags & SurfaceFlags::TO_SRGB_COLORSPACE) {
63     flags |= imgIContainer::FLAG_DECODE_TO_SRGB_COLORSPACE;
64   }
65   return flags;
66 }
67 
68 }  // namespace image
69 }  // namespace mozilla
70 
71 #endif  // mozilla_image_SurfaceFlags_h
72