Bug 1758813 [wpt PR 33142] - Implement RP sign out, a=testonly
[gecko.git] / layout / generic / nsImageFrame.h
blob2012cbfb5f1a48bdf707b7044d749690de955039
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 /* rendering object for replaced elements with image data */
9 #ifndef nsImageFrame_h___
10 #define nsImageFrame_h___
12 #include "nsAtomicContainerFrame.h"
13 #include "nsIObserver.h"
15 #include "imgINotificationObserver.h"
17 #include "nsDisplayList.h"
18 #include "imgIContainer.h"
19 #include "mozilla/Attributes.h"
20 #include "mozilla/DebugOnly.h"
21 #include "mozilla/StaticPtr.h"
22 #include "nsIReflowCallback.h"
23 #include "nsTObserverArray.h"
25 class nsFontMetrics;
26 class nsImageMap;
27 class nsIURI;
28 class nsILoadGroup;
29 class nsPresContext;
30 class nsImageFrame;
31 class nsTransform2D;
32 class nsImageLoadingContent;
34 namespace mozilla {
35 class nsDisplayImage;
36 class PresShell;
37 namespace layers {
38 class ImageContainer;
39 class LayerManager;
40 } // namespace layers
41 } // namespace mozilla
43 class nsImageListener final : public imgINotificationObserver {
44 protected:
45 virtual ~nsImageListener();
47 public:
48 explicit nsImageListener(nsImageFrame* aFrame);
50 NS_DECL_ISUPPORTS
51 NS_DECL_IMGINOTIFICATIONOBSERVER
53 void SetFrame(nsImageFrame* frame) { mFrame = frame; }
55 private:
56 nsImageFrame* mFrame;
59 class nsImageFrame : public nsAtomicContainerFrame, public nsIReflowCallback {
60 public:
61 template <typename T>
62 using Maybe = mozilla::Maybe<T>;
63 using Nothing = mozilla::Nothing;
64 using Visibility = mozilla::Visibility;
66 typedef mozilla::image::ImgDrawResult ImgDrawResult;
67 typedef mozilla::layers::ImageContainer ImageContainer;
68 typedef mozilla::layers::LayerManager LayerManager;
70 NS_DECL_FRAMEARENA_HELPERS(nsImageFrame)
71 NS_DECL_QUERYFRAME
73 void DestroyFrom(nsIFrame* aDestructRoot, PostDestroyData&) override;
74 void DidSetComputedStyle(ComputedStyle* aOldStyle) final;
76 void Init(nsIContent* aContent, nsContainerFrame* aParent,
77 nsIFrame* aPrevInFlow) override;
78 void BuildDisplayList(nsDisplayListBuilder*, const nsDisplayListSet&) final;
79 nscoord GetMinISize(gfxContext* aRenderingContext) final;
80 nscoord GetPrefISize(gfxContext* aRenderingContext) final;
81 mozilla::IntrinsicSize GetIntrinsicSize() final { return mIntrinsicSize; }
82 mozilla::AspectRatio GetIntrinsicRatio() const final {
83 return mIntrinsicRatio;
85 void Reflow(nsPresContext*, ReflowOutput&, const ReflowInput&,
86 nsReflowStatus&) override;
87 bool IsLeafDynamic() const override;
89 nsresult GetContentForEvent(mozilla::WidgetEvent*,
90 nsIContent** aContent) final;
91 nsresult HandleEvent(nsPresContext*, mozilla::WidgetGUIEvent*,
92 nsEventStatus*) override;
93 mozilla::Maybe<Cursor> GetCursor(const nsPoint&) override;
94 nsresult AttributeChanged(int32_t aNameSpaceID, nsAtom* aAttribute,
95 int32_t aModType) final;
97 void OnVisibilityChange(
98 Visibility aNewVisibility,
99 const Maybe<OnNonvisible>& aNonvisibleAction = Nothing()) final;
101 void ResponsiveContentDensityChanged();
102 void SetupForContentURLRequest();
103 bool ShouldShowBrokenImageIcon() const;
105 const mozilla::StyleImage* GetImageFromStyle() const;
107 #ifdef ACCESSIBILITY
108 mozilla::a11y::AccType AccessibleType() override;
109 #endif
111 bool IsFrameOfType(uint32_t aFlags) const final {
112 return nsAtomicContainerFrame::IsFrameOfType(
113 aFlags & ~(nsIFrame::eReplaced | nsIFrame::eReplacedSizing));
116 #ifdef DEBUG_FRAME_DUMP
117 nsresult GetFrameName(nsAString& aResult) const override;
118 void List(FILE* out = stderr, const char* aPrefix = "",
119 ListFlags aFlags = ListFlags()) const final;
120 #endif
122 LogicalSides GetLogicalSkipSides() const final;
124 static void ReleaseGlobals() {
125 if (gIconLoad) {
126 gIconLoad->Shutdown();
127 gIconLoad = nullptr;
131 nsresult RestartAnimation();
132 nsresult StopAnimation();
134 already_AddRefed<imgIRequest> GetCurrentRequest() const;
135 void Notify(imgIRequest*, int32_t aType, const nsIntRect* aData);
138 * Returns whether we should replace an element with an image corresponding to
139 * its 'content' CSS property.
141 static bool ShouldCreateImageFrameForContent(const mozilla::dom::Element&,
142 const ComputedStyle&);
145 * Function to test whether given an element and its style, that element
146 * should get an image frame. Note that this method is only used by the
147 * frame constructor; it's only here because it uses gIconLoad for now.
149 static bool ShouldCreateImageFrameFor(const mozilla::dom::Element&,
150 const ComputedStyle&);
152 ImgDrawResult DisplayAltFeedback(gfxContext& aRenderingContext,
153 const nsRect& aDirtyRect, nsPoint aPt,
154 uint32_t aFlags);
156 ImgDrawResult DisplayAltFeedbackWithoutLayer(
157 nsDisplayItem*, mozilla::wr::DisplayListBuilder&,
158 mozilla::wr::IpcResourceUpdateQueue&,
159 const mozilla::layers::StackingContextHelper&,
160 mozilla::layers::RenderRootStateManager*, nsDisplayListBuilder*,
161 nsPoint aPt, uint32_t aFlags);
164 * Return a map element associated with this image.
166 mozilla::dom::Element* GetMapElement() const;
169 * Return true if the image has associated image map.
171 bool HasImageMap() const { return mImageMap || GetMapElement(); }
173 nsImageMap* GetImageMap();
174 nsImageMap* GetExistingImageMap() const { return mImageMap; }
176 void AddInlineMinISize(gfxContext* aRenderingContext,
177 InlineMinISizeData* aData) final;
179 void DisconnectMap();
181 // nsIReflowCallback
182 bool ReflowFinished() final;
183 void ReflowCallbackCanceled() final;
185 // The kind of image frame we are.
186 enum class Kind : uint8_t {
187 // For an nsImageLoadingContent.
188 ImageElement,
189 // For css 'content: url(..)' on non-generated content.
190 ContentProperty,
191 // For a child of a ::before / ::after pseudo-element that had an url() item
192 // for the content property.
193 ContentPropertyAtIndex,
194 // For a list-style-image ::marker.
195 ListStyleImage,
198 // Creates a suitable continuing frame for this frame.
199 nsImageFrame* CreateContinuingFrame(mozilla::PresShell*,
200 ComputedStyle*) const;
202 private:
203 friend nsIFrame* NS_NewImageFrame(mozilla::PresShell*, ComputedStyle*);
204 friend nsIFrame* NS_NewImageFrameForContentProperty(mozilla::PresShell*,
205 ComputedStyle*);
206 friend nsIFrame* NS_NewImageFrameForGeneratedContentIndex(mozilla::PresShell*,
207 ComputedStyle*);
208 friend nsIFrame* NS_NewImageFrameForListStyleImage(mozilla::PresShell*,
209 ComputedStyle*);
211 nsImageFrame(ComputedStyle* aStyle, nsPresContext* aPresContext, Kind aKind)
212 : nsImageFrame(aStyle, aPresContext, kClassID, aKind) {}
214 nsImageFrame(ComputedStyle*, nsPresContext* aPresContext, ClassID, Kind);
216 void ReflowChildren(nsPresContext*, const ReflowInput&,
217 const mozilla::LogicalSize& aImageSize);
219 protected:
220 nsImageFrame(ComputedStyle* aStyle, nsPresContext* aPresContext, ClassID aID)
221 : nsImageFrame(aStyle, aPresContext, aID, Kind::ImageElement) {}
223 ~nsImageFrame() override;
225 void EnsureIntrinsicSizeAndRatio();
227 bool GotInitialReflow() const {
228 return !HasAnyStateBits(NS_FRAME_FIRST_REFLOW);
231 SizeComputationResult ComputeSize(
232 gfxContext* aRenderingContext, mozilla::WritingMode aWM,
233 const mozilla::LogicalSize& aCBSize, nscoord aAvailableISize,
234 const mozilla::LogicalSize& aMargin,
235 const mozilla::LogicalSize& aBorderPadding,
236 const mozilla::StyleSizeOverrides& aSizeOverrides,
237 mozilla::ComputeSizeFlags aFlags) final;
239 bool IsServerImageMap();
241 // Translate a point that is relative to our frame into a localized CSS pixel
242 // coordinate that is relative to the content area of this frame (inside the
243 // border+padding).
244 mozilla::CSSIntPoint TranslateEventCoords(const nsPoint& aPoint);
246 bool GetAnchorHREFTargetAndNode(nsIURI** aHref, nsString& aTarget,
247 nsIContent** aNode);
249 * Computes the width of the string that fits into the available space
251 * @param in aLength total length of the string in PRUnichars
252 * @param in aMaxWidth width not to be exceeded
253 * @param out aMaxFit length of the string that fits within aMaxWidth
254 * in PRUnichars
255 * @return width of the string that fits within aMaxWidth
257 nscoord MeasureString(const char16_t* aString, int32_t aLength,
258 nscoord aMaxWidth, uint32_t& aMaxFit,
259 gfxContext& aContext, nsFontMetrics& aFontMetrics);
261 void DisplayAltText(nsPresContext* aPresContext,
262 gfxContext& aRenderingContext, const nsString& aAltText,
263 const nsRect& aRect);
265 ImgDrawResult PaintImage(gfxContext& aRenderingContext, nsPoint aPt,
266 const nsRect& aDirtyRect, imgIContainer* aImage,
267 uint32_t aFlags);
270 * If we're ready to decode - that is, if our current request's image is
271 * available and our decoding heuristics are satisfied - then trigger a decode
272 * for our image at the size we predict it will be drawn next time it's
273 * painted.
275 void MaybeDecodeForPredictedSize();
278 * Is this frame part of a ::marker pseudo?
280 bool IsForMarkerPseudo() const;
282 protected:
283 friend class nsImageListener;
284 friend class nsImageLoadingContent;
285 friend class mozilla::PresShell;
287 void OnSizeAvailable(imgIRequest* aRequest, imgIContainer* aImage);
288 void OnFrameUpdate(imgIRequest* aRequest, const nsIntRect* aRect);
289 void OnLoadComplete(imgIRequest* aRequest, nsresult aStatus);
292 * Notification that aRequest will now be the current request.
294 void NotifyNewCurrentRequest(imgIRequest* aRequest, nsresult aStatus);
296 /// Always sync decode our image when painting if @aForce is true.
297 void SetForceSyncDecoding(bool aForce) { mForceSyncDecoding = aForce; }
300 * Computes the predicted dest rect that we'll draw into, in app units, based
301 * upon the provided frame content box. (The content box is what
302 * nsDisplayImage::GetBounds() returns.)
303 * The result is not necessarily contained in the frame content box.
305 nsRect PredictedDestRect(const nsRect& aFrameContentBox);
307 private:
308 void MaybeRecordContentUrlOnImageTelemetry();
310 // random helpers
311 inline void SpecToURI(const nsAString& aSpec, nsIURI** aURI);
313 inline void GetLoadGroup(nsPresContext* aPresContext,
314 nsILoadGroup** aLoadGroup);
315 nscoord GetContinuationOffset() const;
316 void GetDocumentCharacterSet(nsACString& aCharset) const;
317 bool ShouldDisplaySelection();
319 // Whether the image frame should use the mapped aspect ratio from width=""
320 // and height="".
321 bool ShouldUseMappedAspectRatio() const;
323 // Recalculate mIntrinsicSize from the image.
324 bool UpdateIntrinsicSize();
326 // Recalculate mIntrinsicRatio from the image.
327 bool UpdateIntrinsicRatio();
330 * This function calculates the transform for converting between
331 * source space & destination space. May fail if our image has a
332 * percent-valued or zero-valued height or width.
334 * @param aTransform The transform object to populate.
336 * @return whether we succeeded in creating the transform.
338 bool GetSourceToDestTransform(nsTransform2D& aTransform);
341 * Helper function to check whether the request corresponds to a load we don't
342 * care about. Most of the decoder observer methods will bail early if this
343 * returns true.
345 bool IsPendingLoad(imgIRequest*) const;
348 * Updates mImage based on the current image request (cannot be null), and the
349 * image passed in (can be null), and invalidate layout and paint as needed.
351 void UpdateImage(imgIRequest*, imgIContainer*);
354 * Function to convert a dirty rect in the source image to a dirty
355 * rect for the image frame.
357 nsRect SourceRectToDest(const nsIntRect& aRect);
360 * Triggers invalidation for both our image display item and, if appropriate,
361 * our alt-feedback display item.
363 * @param aLayerInvalidRect The area to invalidate in layer space. If null,
364 * the entire layer will be invalidated.
365 * @param aFrameInvalidRect The area to invalidate in frame space. If null,
366 * the entire frame will be invalidated.
368 void InvalidateSelf(const nsIntRect* aLayerInvalidRect,
369 const nsRect* aFrameInvalidRect);
371 RefPtr<nsImageMap> mImageMap;
373 RefPtr<nsImageListener> mListener;
375 // An image request created for content: url(..) or list-style-image.
376 RefPtr<imgRequestProxy> mContentURLRequest;
378 nsCOMPtr<imgIContainer> mImage;
379 nsCOMPtr<imgIContainer> mPrevImage;
381 // The content-box size as if we are not fragmented, cached in the most recent
382 // reflow.
383 nsSize mComputedSize;
385 mozilla::IntrinsicSize mIntrinsicSize;
387 // Stores mImage's intrinsic ratio, or a default AspectRatio if there's no
388 // intrinsic ratio.
389 mozilla::AspectRatio mIntrinsicRatio;
391 const Kind mKind;
392 bool mContentURLRequestRegistered;
393 bool mDisplayingIcon;
394 bool mFirstFrameComplete;
395 bool mReflowCallbackPosted;
396 bool mForceSyncDecoding;
398 /* loading / broken image icon support */
400 // XXXbz this should be handled by the prescontext, I think; that
401 // way we would have a single iconload per mozilla session instead
402 // of one per document...
404 // LoadIcons: initiate the loading of the static icons used to show
405 // loading / broken images
406 nsresult LoadIcons(nsPresContext* aPresContext);
407 nsresult LoadIcon(const nsAString& aSpec, nsPresContext* aPresContext,
408 imgRequestProxy** aRequest);
410 class IconLoad final : public nsIObserver, public imgINotificationObserver {
411 // private class that wraps the data and logic needed for
412 // broken image and loading image icons
413 public:
414 IconLoad();
416 void Shutdown();
418 NS_DECL_ISUPPORTS
419 NS_DECL_NSIOBSERVER
420 NS_DECL_IMGINOTIFICATIONOBSERVER
422 void AddIconObserver(nsImageFrame* frame) {
423 MOZ_ASSERT(!mIconObservers.Contains(frame),
424 "Observer shouldn't aleady be in array");
425 mIconObservers.AppendElement(frame);
428 void RemoveIconObserver(nsImageFrame* frame) {
429 mozilla::DebugOnly<bool> didRemove = mIconObservers.RemoveElement(frame);
430 MOZ_ASSERT(didRemove, "Observer not in array");
433 private:
434 ~IconLoad() = default;
436 void GetPrefs();
437 nsTObserverArray<nsImageFrame*> mIconObservers;
439 public:
440 RefPtr<imgRequestProxy> mLoadingImage;
441 RefPtr<imgRequestProxy> mBrokenImage;
442 bool mPrefForceInlineAltText;
443 bool mPrefShowPlaceholders;
444 bool mPrefShowLoadingPlaceholder;
447 public:
448 // singleton pattern: one LoadIcons instance is used
449 static mozilla::StaticRefPtr<IconLoad> gIconLoad;
451 friend class mozilla::nsDisplayImage;
452 friend class nsDisplayGradient;
455 namespace mozilla {
457 * Note that nsDisplayImage does not receive events. However, an image element
458 * is replaced content so its background will be z-adjacent to the
459 * image itself, and hence receive events just as if the image itself
460 * received events.
462 class nsDisplayImage final : public nsPaintedDisplayItem {
463 public:
464 typedef mozilla::layers::LayerManager LayerManager;
466 nsDisplayImage(nsDisplayListBuilder* aBuilder, nsImageFrame* aFrame,
467 imgIContainer* aImage, imgIContainer* aPrevImage)
468 : nsPaintedDisplayItem(aBuilder, aFrame),
469 mImage(aImage),
470 mPrevImage(aPrevImage) {
471 MOZ_COUNT_CTOR(nsDisplayImage);
473 ~nsDisplayImage() final { MOZ_COUNT_DTOR(nsDisplayImage); }
475 nsDisplayItemGeometry* AllocateGeometry(nsDisplayListBuilder*) final;
476 void ComputeInvalidationRegion(nsDisplayListBuilder*,
477 const nsDisplayItemGeometry*,
478 nsRegion* aInvalidRegion) const final;
479 void Paint(nsDisplayListBuilder*, gfxContext* aCtx) final;
482 * @return The dest rect we'll use when drawing this image, in app units.
483 * Not necessarily contained in this item's bounds.
485 nsRect GetDestRect() const;
487 nsRect GetBounds(bool* aSnap) const {
488 *aSnap = true;
489 return Frame()->GetContentRectRelativeToSelf() + ToReferenceFrame();
492 nsRect GetBounds(nsDisplayListBuilder*, bool* aSnap) const final {
493 return GetBounds(aSnap);
496 nsRegion GetOpaqueRegion(nsDisplayListBuilder*, bool* aSnap) const final;
498 bool CreateWebRenderCommands(mozilla::wr::DisplayListBuilder&,
499 mozilla::wr::IpcResourceUpdateQueue&,
500 const StackingContextHelper&,
501 mozilla::layers::RenderRootStateManager*,
502 nsDisplayListBuilder*) final;
504 NS_DISPLAY_DECL_NAME("Image", TYPE_IMAGE)
505 private:
506 nsCOMPtr<imgIContainer> mImage;
507 nsCOMPtr<imgIContainer> mPrevImage;
510 } // namespace mozilla
512 #endif /* nsImageFrame_h___ */