Bug 1891710: part 2) Enable <Element-outerHTML.html> WPT for Trusted Types. r=smaug
[gecko.git] / gfx / layers / LayersTypes.h
blobc02851d81f8e2ac3b75684bb40b7ae5bd85d1c73
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/. */
7 #ifndef GFX_LAYERSTYPES_H
8 #define GFX_LAYERSTYPES_H
10 #include <iosfwd> // for ostream
11 #include <stdint.h> // for uint32_t
12 #include <stdio.h> // FILE
13 #include <tuple>
15 #include "Units.h"
16 #include "mozilla/DefineEnum.h" // for MOZ_DEFINE_ENUM_CLASS_WITH_BASE
17 #include "mozilla/Maybe.h"
18 #include "mozilla/TimeStamp.h" // for TimeStamp
19 #include "nsRegion.h"
20 #include "mozilla/EnumSet.h"
22 #ifndef MOZ_LAYERS_HAVE_LOG
23 # define MOZ_LAYERS_HAVE_LOG
24 #endif
25 #define MOZ_LAYERS_LOG(_args) \
26 MOZ_LOG(LayerManager::GetLog(), LogLevel::Debug, _args)
27 #define MOZ_LAYERS_LOG_IF_SHADOWABLE(layer, _args)
29 #define INVALID_OVERLAY -1
31 // #define ENABLE_FRAME_LATENCY_LOG
33 namespace IPC {
34 template <typename T>
35 struct ParamTraits;
36 } // namespace IPC
38 namespace mozilla {
40 enum class StyleBorderStyle : uint8_t;
42 namespace layers {
44 class TextureHost;
46 #undef NONE
47 #undef OPAQUE
49 struct LayersId final {
50 uint64_t mId = 0;
52 auto MutTiedFields() { return std::tie(mId); }
54 bool IsValid() const { return mId != 0; }
56 // Allow explicit cast to a uint64_t for now
57 explicit operator uint64_t() const { return mId; }
59 // Implement some operators so this class can be used as a key in
60 // stdlib classes.
61 bool operator<(const LayersId& aOther) const { return mId < aOther.mId; }
63 bool operator==(const LayersId& aOther) const { return mId == aOther.mId; }
65 bool operator!=(const LayersId& aOther) const { return !(*this == aOther); }
67 friend std::ostream& operator<<(std::ostream& aStream, const LayersId& aId);
69 // Helper struct that allow this class to be used as a key in
70 // std::unordered_map like so:
71 // std::unordered_map<LayersId, ValueType, LayersId::HashFn> myMap;
72 struct HashFn {
73 std::size_t operator()(const LayersId& aKey) const {
74 return std::hash<uint64_t>{}(aKey.mId);
79 template <typename T>
80 struct BaseTransactionId final {
81 uint64_t mId = 0;
83 auto MutTiedFields() { return std::tie(mId); }
85 bool IsValid() const { return mId != 0; }
87 [[nodiscard]] BaseTransactionId<T> Next() const {
88 return BaseTransactionId<T>{mId + 1};
91 [[nodiscard]] BaseTransactionId<T> Prev() const {
92 return BaseTransactionId<T>{mId - 1};
95 int64_t operator-(const BaseTransactionId<T>& aOther) const {
96 return mId - aOther.mId;
99 // Allow explicit cast to a uint64_t for now
100 explicit operator uint64_t() const { return mId; }
102 bool operator<(const BaseTransactionId<T>& aOther) const {
103 return mId < aOther.mId;
106 bool operator<=(const BaseTransactionId<T>& aOther) const {
107 return mId <= aOther.mId;
110 bool operator>(const BaseTransactionId<T>& aOther) const {
111 return mId > aOther.mId;
114 bool operator>=(const BaseTransactionId<T>& aOther) const {
115 return mId >= aOther.mId;
118 bool operator==(const BaseTransactionId<T>& aOther) const {
119 return mId == aOther.mId;
122 bool operator!=(const BaseTransactionId<T>& aOther) const {
123 return mId != aOther.mId;
127 class TransactionIdType {};
128 typedef BaseTransactionId<TransactionIdType> TransactionId;
130 // CompositionOpportunityId is a counter that goes up every time we have an
131 // opportunity to composite. It increments even on no-op composites (if nothing
132 // has changed) and while compositing is paused. It does not skip values if a
133 // composite is delayed. It is meaningful per window.
134 // This counter is used to differentiate intentionally-skipped video frames from
135 // unintentionally-skipped video frames: If CompositionOpportunityIds are
136 // observed by the video in +1 increments, then the video was onscreen the
137 // entire time and compositing was not paused. But if gaps in
138 // CompositionOpportunityIds are observed, that must mean that the video was not
139 // considered during some composition opportunities, because compositing was
140 // paused or because the video was not part of the on-screen scene.
141 class CompositionOpportunityType {};
142 typedef BaseTransactionId<CompositionOpportunityType> CompositionOpportunityId;
144 /// We make different decisions about resource allocation sizes in WebRender
145 /// depending on whether we are going to render web pages or simpler
146 /// content in the window.
147 enum class WindowKind : int8_t { MAIN = 0, SECONDARY, LAST };
149 enum class LayersBackend : int8_t { LAYERS_NONE = 0, LAYERS_WR, LAYERS_LAST };
151 enum class WebRenderBackend : int8_t { HARDWARE = 0, SOFTWARE, LAST };
153 enum class WebRenderCompositor : int8_t {
154 DRAW = 0,
155 DIRECT_COMPOSITION,
156 CORE_ANIMATION,
157 SOFTWARE,
158 D3D11,
159 OPENGL,
160 WAYLAND,
161 LAST
164 const char* GetLayersBackendName(LayersBackend aBackend);
166 enum class TextureType : int8_t {
167 Unknown = 0,
168 D3D11,
169 MacIOSurface,
170 AndroidNativeWindow,
171 AndroidHardwareBuffer,
172 DMABUF,
173 EGLImage,
174 Last
177 enum class BufferMode : int8_t { BUFFER_NONE, BUFFERED };
179 enum class DrawRegionClip : int8_t { DRAW, NONE };
181 enum class SurfaceMode : int8_t {
182 SURFACE_NONE = 0,
183 SURFACE_OPAQUE,
184 SURFACE_SINGLE_CHANNEL_ALPHA,
185 SURFACE_COMPONENT_ALPHA
188 // clang-format off
189 MOZ_DEFINE_ENUM_CLASS_WITH_BASE(
190 ScaleMode, int8_t, (
191 SCALE_NONE,
192 STRETCH
193 // Unimplemented - PRESERVE_ASPECT_RATIO_CONTAIN
195 // clang-format on
197 // Bit flags that go on a RefLayer and override the
198 // event regions in the entire subtree below. This is needed for propagating
199 // various flags across processes since the child-process layout code doesn't
200 // know about parent-process listeners or CSS rules.
201 enum EventRegionsOverride {
202 // The default, no flags set
203 NoOverride = 0,
204 // Treat all hit regions in the subtree as dispatch-to-content
205 ForceDispatchToContent = (1 << 0),
206 // Treat all hit regions in the subtree as empty
207 ForceEmptyHitRegion = (1 << 1),
208 // OR union of all valid bit flags, for use in BitFlagsEnumSerializer
209 ALL_BITS = (1 << 2) - 1
212 MOZ_ALWAYS_INLINE EventRegionsOverride operator|(EventRegionsOverride a,
213 EventRegionsOverride b) {
214 return (EventRegionsOverride)((int)a | (int)b);
217 MOZ_ALWAYS_INLINE EventRegionsOverride& operator|=(EventRegionsOverride& a,
218 EventRegionsOverride b) {
219 a = a | b;
220 return a;
223 // Flags used as an argument to functions that dump textures.
224 enum TextureDumpMode {
225 Compress, // dump texture with LZ4 compression
226 DoNotCompress // dump texture uncompressed
229 // Corresponding bit masks for allowed touch behaviors
230 // are defined in AllowedTouchBehavior
231 typedef uint32_t TouchBehaviorFlags;
233 // Some specialized typedefs of Matrix4x4Typed.
234 typedef gfx::Matrix4x4Typed<LayerPixel, CSSTransformedLayerPixel>
235 CSSTransformMatrix;
236 // Several different async transforms can contribute to a layer's transform
237 // (specifically, an async animation can contribute a transform, and each APZC
238 // that scrolls a layer can contribute async scroll/zoom and overscroll
239 // transforms).
240 // To try to model this with typed units, we represent individual async
241 // transforms as ParentLayer -> ParentLayer transforms (aliased as
242 // AsyncTransformComponentMatrix), and we represent the product of all of them
243 // as a CSSTransformLayer -> ParentLayer transform (aliased as
244 // AsyncTransformMatrix). To create an AsyncTransformMatrix from component
245 // matrices, a ViewAs operation is needed. A MultipleAsyncTransforms
246 // PixelCastJustification is provided for this purpose.
247 typedef gfx::Matrix4x4Typed<ParentLayerPixel, ParentLayerPixel>
248 AsyncTransformComponentMatrix;
249 typedef gfx::Matrix4x4Typed<CSSTransformedLayerPixel, ParentLayerPixel>
250 AsyncTransformMatrix;
252 typedef Array<gfx::DeviceColor, 4> BorderColors;
253 typedef Array<LayerSize, 4> BorderCorners;
254 typedef Array<LayerCoord, 4> BorderWidths;
255 typedef Array<StyleBorderStyle, 4> BorderStyles;
257 typedef Maybe<LayerRect> MaybeLayerRect;
259 // This is used to communicate Layers across IPC channels. The Handle is valid
260 // for layers in the same PLayerTransaction. Handles are created by
261 // ClientLayerManager, and are cached in LayerTransactionParent on first use.
262 class LayerHandle final {
263 friend struct IPC::ParamTraits<mozilla::layers::LayerHandle>;
265 public:
266 LayerHandle() : mHandle(0) {}
267 LayerHandle(const LayerHandle& aOther) = default;
268 explicit LayerHandle(uint64_t aHandle) : mHandle(aHandle) {}
269 bool IsValid() const { return mHandle != 0; }
270 explicit operator bool() const { return IsValid(); }
271 bool operator==(const LayerHandle& aOther) const {
272 return mHandle == aOther.mHandle;
274 uint64_t Value() const { return mHandle; }
276 private:
277 uint64_t mHandle;
280 // This is used to communicate Compositables across IPC channels. The Handle is
281 // valid for layers in the same PLayerTransaction or PImageBridge. Handles are
282 // created by ClientLayerManager or ImageBridgeChild, and are cached in the
283 // parent side on first use.
284 class CompositableHandle final {
285 friend struct IPC::ParamTraits<mozilla::layers::CompositableHandle>;
287 public:
288 static CompositableHandle GetNext();
290 CompositableHandle() : mHandle(0) {}
291 CompositableHandle(const CompositableHandle& aOther) = default;
292 explicit CompositableHandle(uint64_t aHandle) : mHandle(aHandle) {}
293 bool IsValid() const { return mHandle != 0; }
294 explicit operator bool() const { return IsValid(); }
295 explicit operator uint64_t() const { return mHandle; }
296 bool operator==(const CompositableHandle& aOther) const {
297 return mHandle == aOther.mHandle;
299 bool operator!=(const CompositableHandle& aOther) const {
300 return !(*this == aOther);
302 uint64_t Value() const { return mHandle; }
304 private:
305 uint64_t mHandle;
308 enum class CompositableHandleOwner : uint8_t {
309 WebRenderBridge,
310 ImageBridge,
313 struct RemoteTextureId {
314 uint64_t mId = 0;
316 auto MutTiedFields() { return std::tie(mId); }
318 static RemoteTextureId GetNext();
320 static constexpr RemoteTextureId Max() { return RemoteTextureId{UINT64_MAX}; }
322 bool IsValid() const { return mId != 0; }
324 // Allow explicit cast to a uint64_t for now
325 explicit operator uint64_t() const { return mId; }
327 // Implement some operators so this class can be used as a key in
328 // stdlib classes.
329 bool operator<(const RemoteTextureId& aOther) const {
330 return mId < aOther.mId;
333 bool operator>(const RemoteTextureId& aOther) const {
334 return mId > aOther.mId;
337 bool operator==(const RemoteTextureId& aOther) const {
338 return mId == aOther.mId;
341 bool operator!=(const RemoteTextureId& aOther) const {
342 return !(*this == aOther);
345 bool operator>=(const RemoteTextureId& aOther) const {
346 return mId >= aOther.mId;
349 // Helper struct that allow this class to be used as a key in
350 // std::unordered_map like so:
351 // std::unordered_map<RemoteTextureId, ValueType, RemoteTextureId::HashFn>
352 // myMap;
353 struct HashFn {
354 std::size_t operator()(const RemoteTextureId aKey) const {
355 return std::hash<uint64_t>{}(aKey.mId);
360 struct RemoteTextureOwnerId {
361 uint64_t mId = 0;
363 auto MutTiedFields() { return std::tie(mId); }
365 static RemoteTextureOwnerId GetNext();
367 bool IsValid() const { return mId != 0; }
369 // Allow explicit cast to a uint64_t for now
370 explicit operator uint64_t() const { return mId; }
372 // Implement some operators so this class can be used as a key in
373 // stdlib classes.
374 bool operator<(const RemoteTextureOwnerId& aOther) const {
375 return mId < aOther.mId;
378 bool operator==(const RemoteTextureOwnerId& aOther) const {
379 return mId == aOther.mId;
382 bool operator!=(const RemoteTextureOwnerId& aOther) const {
383 return !(*this == aOther);
386 // Helper struct that allow this class to be used as a key in
387 // std::unordered_map like so:
388 // std::unordered_map<RemoteTextureOwnerId, ValueType,
389 // RemoteTextureOwnerId::HashFn> myMap;
390 struct HashFn {
391 std::size_t operator()(const RemoteTextureOwnerId aKey) const {
392 return std::hash<uint64_t>{}(aKey.mId);
397 typedef uint32_t RemoteTextureTxnType;
398 typedef uint64_t RemoteTextureTxnId;
400 // TextureId allocated in GPU process
401 struct GpuProcessTextureId {
402 uint64_t mId = 0;
404 static GpuProcessTextureId GetNext();
406 bool IsValid() const { return mId != 0; }
408 // Allow explicit cast to a uint64_t for now
409 explicit operator uint64_t() const { return mId; }
411 bool operator==(const GpuProcessTextureId& aOther) const {
412 return mId == aOther.mId;
415 bool operator!=(const GpuProcessTextureId& aOther) const {
416 return !(*this == aOther);
419 // Helper struct that allow this class to be used as a key in
420 // std::unordered_map like so:
421 // std::unordered_map<GpuProcessTextureId, ValueType,
422 // GpuProcessTextureId::HashFn> myMap;
423 struct HashFn {
424 std::size_t operator()(const GpuProcessTextureId aKey) const {
425 return std::hash<uint64_t>{}(aKey.mId);
430 // QueryId allocated in GPU process
431 struct GpuProcessQueryId {
432 uint64_t mId = 0;
434 static GpuProcessQueryId GetNext();
436 bool IsValid() const { return mId != 0; }
438 // Allow explicit cast to a uint64_t for now
439 explicit operator uint64_t() const { return mId; }
441 bool operator==(const GpuProcessQueryId& aOther) const {
442 return mId == aOther.mId;
445 bool operator!=(const GpuProcessQueryId& aOther) const {
446 return !(*this == aOther);
449 // Helper struct that allow this class to be used as a key in
450 // std::unordered_map like so:
451 // std::unordered_map<GpuProcessQueryId, ValueType,
452 // GpuProcessQueryId::HashFn> myMap;
453 struct HashFn {
454 std::size_t operator()(const GpuProcessQueryId aKey) const {
455 return std::hash<uint64_t>{}(aKey.mId);
460 // clang-format off
461 MOZ_DEFINE_ENUM_CLASS_WITH_BASE(ScrollDirection, uint8_t, (
462 eVertical,
463 eHorizontal
466 using ScrollDirections = EnumSet<ScrollDirection, uint8_t>;
468 constexpr ScrollDirections EitherScrollDirection(ScrollDirection::eVertical,ScrollDirection::eHorizontal);
469 constexpr ScrollDirections HorizontalScrollDirection(ScrollDirection::eHorizontal);
470 constexpr ScrollDirections VerticalScrollDirection(ScrollDirection::eVertical);
472 // Return the scroll directions which have a nonzero component in |aDelta|.
473 template <typename Point>
474 ScrollDirections DirectionsInDelta(const Point& aDelta) {
475 ScrollDirections result;
476 if (aDelta.x != 0) {
477 result += ScrollDirection::eHorizontal;
479 if (aDelta.y != 0) {
480 result += ScrollDirection::eVertical;
482 return result;
485 MOZ_DEFINE_ENUM_CLASS_WITH_BASE(CompositionPayloadType, uint8_t, (
487 * A |CompositionPayload| with this type indicates a key press happened
488 * before composition and will be used to determine latency between key press
489 * and presentation in |mozilla::Telemetry::KEYPRESS_PRESENT_LATENCY|
491 eKeyPress,
494 * A |CompositionPayload| with this type indicates that an APZ scroll event
495 * occurred that will be included in the composition.
497 eAPZScroll,
500 * A |CompositionPayload| with this type indicates that an APZ pinch-to-zoom
501 * event occurred that will be included in the composition.
503 eAPZPinchZoom,
506 * A |CompositionPayload| with this type indicates that content was painted
507 * that will be included in the composition.
509 eContentPaint,
512 * A |CompositionPayload| with this type indicates a mouse up (which caused
513 * a click to happen) happened before composition and will be used to determine latency
514 * between mouse up and presentation in
515 * |mozilla::Telemetry::MOUSEUP_FOLLOWED_BY_CLICK_PRESENT_LATENCY|
517 eMouseUpFollowedByClick
519 // clang-format on
521 extern const char* kCompositionPayloadTypeNames[kCompositionPayloadTypeCount];
523 struct CompositionPayload {
524 bool operator==(const CompositionPayload& aOther) const {
525 return mType == aOther.mType && mTimeStamp == aOther.mTimeStamp;
527 /* The type of payload that is in this composition */
528 CompositionPayloadType mType;
529 /* When this payload was generated */
530 TimeStamp mTimeStamp;
533 } // namespace layers
534 } // namespace mozilla
536 #endif /* GFX_LAYERSTYPES_H */