Bug 1839315: part 4) Link from `SheetLoadData::mWasAlternate` to spec. r=emilio DONTBUILD
[gecko.git] / layout / generic / nsImageFrame.h
blobbe14cb82c651b7cc8142cb222a0a801d86276150
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 Destroy(DestroyContext&) 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(const 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 ElementStateChanged(mozilla::dom::ElementState) override;
103 void SetupOwnedRequest();
104 void DeinitOwnedRequest();
105 bool ShouldShowBrokenImageIcon() const;
107 bool IsForImageLoadingContent() const {
108 return mKind == Kind::ImageLoadingContent;
111 void UpdateXULImage();
112 const mozilla::StyleImage* GetImageFromStyle() const;
114 #ifdef ACCESSIBILITY
115 mozilla::a11y::AccType AccessibleType() override;
116 #endif
118 bool IsFrameOfType(uint32_t aFlags) const final {
119 return nsAtomicContainerFrame::IsFrameOfType(
120 aFlags & ~(nsIFrame::eReplaced | nsIFrame::eReplacedSizing));
123 #ifdef DEBUG_FRAME_DUMP
124 nsresult GetFrameName(nsAString& aResult) const override;
125 void List(FILE* out = stderr, const char* aPrefix = "",
126 ListFlags aFlags = ListFlags()) const final;
127 #endif
129 LogicalSides GetLogicalSkipSides() const final;
131 static void ReleaseGlobals();
133 already_AddRefed<imgIRequest> GetCurrentRequest() const;
134 void Notify(imgIRequest*, int32_t aType, const nsIntRect* aData);
137 * Returns whether we should replace an element with an image corresponding to
138 * its 'content' CSS property.
140 static bool ShouldCreateImageFrameForContentProperty(
141 const mozilla::dom::Element&, const ComputedStyle&);
144 * Function to test whether given an element and its style, that element
145 * should get an image frame, and if so, which kind of image frame (for
146 * `content`, or for the element itself).
148 enum class ImageFrameType {
149 ForContentProperty,
150 ForElementRequest,
151 None,
153 static ImageFrameType ImageFrameTypeFor(const mozilla::dom::Element&,
154 const ComputedStyle&);
156 ImgDrawResult DisplayAltFeedback(gfxContext& aRenderingContext,
157 const nsRect& aDirtyRect, nsPoint aPt,
158 uint32_t aFlags);
160 ImgDrawResult DisplayAltFeedbackWithoutLayer(
161 nsDisplayItem*, mozilla::wr::DisplayListBuilder&,
162 mozilla::wr::IpcResourceUpdateQueue&,
163 const mozilla::layers::StackingContextHelper&,
164 mozilla::layers::RenderRootStateManager*, nsDisplayListBuilder*,
165 nsPoint aPt, uint32_t aFlags);
168 * Return a map element associated with this image.
170 mozilla::dom::Element* GetMapElement() const;
173 * Return true if the image has associated image map.
175 bool HasImageMap() const { return mImageMap || GetMapElement(); }
177 nsImageMap* GetImageMap();
178 nsImageMap* GetExistingImageMap() const { return mImageMap; }
180 void AddInlineMinISize(gfxContext* aRenderingContext,
181 InlineMinISizeData* aData) final;
183 void DisconnectMap();
185 // nsIReflowCallback
186 bool ReflowFinished() final;
187 void ReflowCallbackCanceled() final;
189 // The kind of image frame we are.
190 enum class Kind : uint8_t {
191 // For an nsImageLoadingContent.
192 ImageLoadingContent,
193 // For a <xul:image> element.
194 XULImage,
195 // For css 'content: url(..)' on non-generated content.
196 ContentProperty,
197 // For a child of a ::before / ::after pseudo-element that had an url() item
198 // for the content property.
199 ContentPropertyAtIndex,
200 // For a list-style-image ::marker.
201 ListStyleImage,
204 // Creates a suitable continuing frame for this frame.
205 nsImageFrame* CreateContinuingFrame(mozilla::PresShell*,
206 ComputedStyle*) const;
208 private:
209 friend nsIFrame* NS_NewImageFrame(mozilla::PresShell*, ComputedStyle*);
210 friend nsIFrame* NS_NewXULImageFrame(mozilla::PresShell*, ComputedStyle*);
211 friend nsIFrame* NS_NewImageFrameForContentProperty(mozilla::PresShell*,
212 ComputedStyle*);
213 friend nsIFrame* NS_NewImageFrameForGeneratedContentIndex(mozilla::PresShell*,
214 ComputedStyle*);
215 friend nsIFrame* NS_NewImageFrameForListStyleImage(mozilla::PresShell*,
216 ComputedStyle*);
218 nsImageFrame(ComputedStyle* aStyle, nsPresContext* aPresContext, Kind aKind)
219 : nsImageFrame(aStyle, aPresContext, kClassID, aKind) {}
221 nsImageFrame(ComputedStyle*, nsPresContext* aPresContext, ClassID, Kind);
223 void ReflowChildren(nsPresContext*, const ReflowInput&,
224 const mozilla::LogicalSize& aImageSize);
226 void UpdateIntrinsicSizeAndRatio();
228 protected:
229 nsImageFrame(ComputedStyle* aStyle, nsPresContext* aPresContext, ClassID aID)
230 : nsImageFrame(aStyle, aPresContext, aID, Kind::ImageLoadingContent) {}
232 ~nsImageFrame() override;
234 void EnsureIntrinsicSizeAndRatio();
236 bool GotInitialReflow() const {
237 return !HasAnyStateBits(NS_FRAME_FIRST_REFLOW);
240 SizeComputationResult ComputeSize(
241 gfxContext* aRenderingContext, mozilla::WritingMode aWM,
242 const mozilla::LogicalSize& aCBSize, nscoord aAvailableISize,
243 const mozilla::LogicalSize& aMargin,
244 const mozilla::LogicalSize& aBorderPadding,
245 const mozilla::StyleSizeOverrides& aSizeOverrides,
246 mozilla::ComputeSizeFlags aFlags) final;
248 bool IsServerImageMap();
250 // Translate a point that is relative to our frame into a localized CSS pixel
251 // coordinate that is relative to the content area of this frame (inside the
252 // border+padding).
253 mozilla::CSSIntPoint TranslateEventCoords(const nsPoint& aPoint);
255 bool GetAnchorHREFTargetAndNode(nsIURI** aHref, nsString& aTarget,
256 nsIContent** aNode);
258 * Computes the width of the string that fits into the available space
260 * @param in aLength total length of the string in PRUnichars
261 * @param in aMaxWidth width not to be exceeded
262 * @param out aMaxFit length of the string that fits within aMaxWidth
263 * in PRUnichars
264 * @return width of the string that fits within aMaxWidth
266 nscoord MeasureString(const char16_t* aString, int32_t aLength,
267 nscoord aMaxWidth, uint32_t& aMaxFit,
268 gfxContext& aContext, nsFontMetrics& aFontMetrics);
270 void DisplayAltText(nsPresContext* aPresContext,
271 gfxContext& aRenderingContext, const nsString& aAltText,
272 const nsRect& aRect);
274 ImgDrawResult PaintImage(gfxContext& aRenderingContext, nsPoint aPt,
275 const nsRect& aDirtyRect, imgIContainer* aImage,
276 uint32_t aFlags);
279 * If we're ready to decode - that is, if our current request's image is
280 * available and our decoding heuristics are satisfied - then trigger a decode
281 * for our image at the size we predict it will be drawn next time it's
282 * painted.
284 void MaybeDecodeForPredictedSize();
287 * Is this frame part of a ::marker pseudo?
289 bool IsForMarkerPseudo() const;
291 protected:
292 friend class nsImageListener;
293 friend class nsImageLoadingContent;
294 friend class mozilla::PresShell;
296 void OnSizeAvailable(imgIRequest* aRequest, imgIContainer* aImage);
297 void OnFrameUpdate(imgIRequest* aRequest, const nsIntRect* aRect);
298 void OnLoadComplete(imgIRequest* aRequest, nsresult aStatus);
301 * Notification that aRequest will now be the current request.
303 void NotifyNewCurrentRequest(imgIRequest* aRequest, nsresult aStatus);
305 /// Always sync decode our image when painting if @aForce is true.
306 void SetForceSyncDecoding(bool aForce) { mForceSyncDecoding = aForce; }
308 void AssertSyncDecodingHintIsInSync() const
309 #ifndef DEBUG
311 #else
313 #endif
316 * Computes the predicted dest rect that we'll draw into, in app units, based
317 * upon the provided frame content box. (The content box is what
318 * nsDisplayImage::GetBounds() returns.)
319 * The result is not necessarily contained in the frame content box.
321 nsRect PredictedDestRect(const nsRect& aFrameContentBox);
323 private:
324 nscoord GetContinuationOffset() const;
325 bool ShouldDisplaySelection();
327 // Whether the image frame should use the mapped aspect ratio from width=""
328 // and height="".
329 bool ShouldUseMappedAspectRatio() const;
331 // Recalculate mIntrinsicSize from the image.
332 bool UpdateIntrinsicSize();
334 // Recalculate mIntrinsicRatio from the image.
335 bool UpdateIntrinsicRatio();
338 * This function calculates the transform for converting between
339 * source space & destination space. May fail if our image has a
340 * percent-valued or zero-valued height or width.
342 * @param aTransform The transform object to populate.
344 * @return whether we succeeded in creating the transform.
346 bool GetSourceToDestTransform(nsTransform2D& aTransform);
349 * Helper function to check whether the request corresponds to a load we don't
350 * care about. Most of the decoder observer methods will bail early if this
351 * returns true.
353 bool IsPendingLoad(imgIRequest*) const;
356 * Updates mImage based on the current image request, and the image passed in
357 * (both can be null), and invalidate layout and paint as needed.
359 void UpdateImage(imgIRequest*, imgIContainer*);
362 * Function to convert a dirty rect in the source image to a dirty
363 * rect for the image frame.
365 nsRect SourceRectToDest(const nsIntRect& aRect);
368 * Triggers invalidation for both our image display item and, if appropriate,
369 * our alt-feedback display item.
371 * @param aLayerInvalidRect The area to invalidate in layer space. If null,
372 * the entire layer will be invalidated.
373 * @param aFrameInvalidRect The area to invalidate in frame space. If null,
374 * the entire frame will be invalidated.
376 void InvalidateSelf(const nsIntRect* aLayerInvalidRect,
377 const nsRect* aFrameInvalidRect);
379 void MaybeSendIntrinsicSizeAndRatioToEmbedder();
380 void MaybeSendIntrinsicSizeAndRatioToEmbedder(Maybe<mozilla::IntrinsicSize>,
381 Maybe<mozilla::AspectRatio>);
383 RefPtr<nsImageMap> mImageMap;
385 RefPtr<nsImageListener> mListener;
387 // An image request created for content: url(..), list-style-image, or
388 // <xul:image>.
389 RefPtr<imgRequestProxy> mOwnedRequest;
391 nsCOMPtr<imgIContainer> mImage;
392 nsCOMPtr<imgIContainer> mPrevImage;
394 // The content-box size as if we are not fragmented, cached in the most recent
395 // reflow.
396 nsSize mComputedSize;
398 mozilla::IntrinsicSize mIntrinsicSize;
400 // Stores mImage's intrinsic ratio, or a default AspectRatio if there's no
401 // intrinsic ratio.
402 mozilla::AspectRatio mIntrinsicRatio;
404 const Kind mKind;
405 bool mOwnedRequestRegistered = false;
406 bool mDisplayingIcon = false;
407 bool mFirstFrameComplete = false;
408 bool mReflowCallbackPosted = false;
409 bool mForceSyncDecoding = false;
410 bool mIsInObjectOrEmbed = false;
412 public:
413 friend class mozilla::nsDisplayImage;
414 friend class nsDisplayGradient;
417 namespace mozilla {
419 * Note that nsDisplayImage does not receive events. However, an image element
420 * is replaced content so its background will be z-adjacent to the
421 * image itself, and hence receive events just as if the image itself
422 * received events.
424 class nsDisplayImage final : public nsPaintedDisplayItem {
425 public:
426 typedef mozilla::layers::LayerManager LayerManager;
428 nsDisplayImage(nsDisplayListBuilder* aBuilder, nsImageFrame* aFrame,
429 imgIContainer* aImage, imgIContainer* aPrevImage)
430 : nsPaintedDisplayItem(aBuilder, aFrame),
431 mImage(aImage),
432 mPrevImage(aPrevImage) {
433 MOZ_COUNT_CTOR(nsDisplayImage);
435 ~nsDisplayImage() final { MOZ_COUNT_DTOR(nsDisplayImage); }
437 void Paint(nsDisplayListBuilder*, gfxContext* aCtx) final;
440 * @return The dest rect we'll use when drawing this image, in app units.
441 * Not necessarily contained in this item's bounds.
443 nsRect GetDestRect() const;
445 nsRect GetBounds(bool* aSnap) const {
446 *aSnap = true;
447 return Frame()->GetContentRectRelativeToSelf() + ToReferenceFrame();
450 nsRect GetBounds(nsDisplayListBuilder*, bool* aSnap) const final {
451 return GetBounds(aSnap);
454 nsRegion GetOpaqueRegion(nsDisplayListBuilder*, bool* aSnap) const final;
456 bool CreateWebRenderCommands(mozilla::wr::DisplayListBuilder&,
457 mozilla::wr::IpcResourceUpdateQueue&,
458 const StackingContextHelper&,
459 mozilla::layers::RenderRootStateManager*,
460 nsDisplayListBuilder*) final;
462 NS_DISPLAY_DECL_NAME("Image", TYPE_IMAGE)
463 private:
464 nsCOMPtr<imgIContainer> mImage;
465 nsCOMPtr<imgIContainer> mPrevImage;
468 } // namespace mozilla
470 #endif /* nsImageFrame_h___ */