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"
32 class nsImageLoadingContent
;
41 } // namespace mozilla
43 class nsImageListener final
: public imgINotificationObserver
{
45 virtual ~nsImageListener();
48 explicit nsImageListener(nsImageFrame
* aFrame
);
51 NS_DECL_IMGINOTIFICATIONOBSERVER
53 void SetFrame(nsImageFrame
* frame
) { mFrame
= frame
; }
59 class nsImageFrame
: public nsAtomicContainerFrame
, public nsIReflowCallback
{
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
)
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 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;
115 mozilla::a11y::AccType
AccessibleType() override
;
118 #ifdef DEBUG_FRAME_DUMP
119 nsresult
GetFrameName(nsAString
& aResult
) const override
;
120 void List(FILE* out
= stderr
, const char* aPrefix
= "",
121 ListFlags aFlags
= ListFlags()) const final
;
124 LogicalSides
GetLogicalSkipSides() const final
;
126 static void ReleaseGlobals();
128 already_AddRefed
<imgIRequest
> GetCurrentRequest() const;
129 void Notify(imgIRequest
*, int32_t aType
, const nsIntRect
* aData
);
132 * Returns whether we should replace an element with an image corresponding to
133 * its 'content' CSS property.
135 static bool ShouldCreateImageFrameForContentProperty(
136 const mozilla::dom::Element
&, const ComputedStyle
&);
139 * Function to test whether given an element and its style, that element
140 * should get an image frame, and if so, which kind of image frame (for
141 * `content`, or for the element itself).
143 enum class ImageFrameType
{
148 static ImageFrameType
ImageFrameTypeFor(const mozilla::dom::Element
&,
149 const ComputedStyle
&);
151 ImgDrawResult
DisplayAltFeedback(gfxContext
& aRenderingContext
,
152 const nsRect
& aDirtyRect
, nsPoint aPt
,
155 ImgDrawResult
DisplayAltFeedbackWithoutLayer(
156 nsDisplayItem
*, mozilla::wr::DisplayListBuilder
&,
157 mozilla::wr::IpcResourceUpdateQueue
&,
158 const mozilla::layers::StackingContextHelper
&,
159 mozilla::layers::RenderRootStateManager
*, nsDisplayListBuilder
*,
160 nsPoint aPt
, uint32_t aFlags
);
163 * Return a map element associated with this image.
165 mozilla::dom::Element
* GetMapElement() const;
168 * Return true if the image has associated image map.
170 bool HasImageMap() const { return mImageMap
|| GetMapElement(); }
172 nsImageMap
* GetImageMap();
173 nsImageMap
* GetExistingImageMap() const { return mImageMap
; }
175 void AddInlineMinISize(gfxContext
* aRenderingContext
,
176 InlineMinISizeData
* aData
) final
;
178 void DisconnectMap();
181 bool ReflowFinished() final
;
182 void ReflowCallbackCanceled() final
;
184 // The kind of image frame we are.
185 enum class Kind
: uint8_t {
186 // For an nsImageLoadingContent.
188 // For a <xul:image> element.
190 // For css 'content: url(..)' on non-generated content.
192 // For a child of a ::before / ::after pseudo-element that had an url() item
193 // for the content property.
194 ContentPropertyAtIndex
,
195 // For a list-style-image ::marker.
199 // Creates a suitable continuing frame for this frame.
200 nsImageFrame
* CreateContinuingFrame(mozilla::PresShell
*,
201 ComputedStyle
*) const;
204 friend nsIFrame
* NS_NewImageFrame(mozilla::PresShell
*, ComputedStyle
*);
205 friend nsIFrame
* NS_NewXULImageFrame(mozilla::PresShell
*, ComputedStyle
*);
206 friend nsIFrame
* NS_NewImageFrameForContentProperty(mozilla::PresShell
*,
208 friend nsIFrame
* NS_NewImageFrameForGeneratedContentIndex(mozilla::PresShell
*,
210 friend nsIFrame
* NS_NewImageFrameForListStyleImage(mozilla::PresShell
*,
213 nsImageFrame(ComputedStyle
* aStyle
, nsPresContext
* aPresContext
, Kind aKind
)
214 : nsImageFrame(aStyle
, aPresContext
, kClassID
, aKind
) {}
216 nsImageFrame(ComputedStyle
*, nsPresContext
* aPresContext
, ClassID
, Kind
);
218 void ReflowChildren(nsPresContext
*, const ReflowInput
&,
219 const mozilla::LogicalSize
& aImageSize
);
221 void UpdateIntrinsicSizeAndRatio();
224 nsImageFrame(ComputedStyle
* aStyle
, nsPresContext
* aPresContext
, ClassID aID
)
225 : nsImageFrame(aStyle
, aPresContext
, aID
, Kind::ImageLoadingContent
) {}
227 ~nsImageFrame() override
;
229 void EnsureIntrinsicSizeAndRatio();
231 bool GotInitialReflow() const {
232 return !HasAnyStateBits(NS_FRAME_FIRST_REFLOW
);
235 SizeComputationResult
ComputeSize(
236 gfxContext
* aRenderingContext
, mozilla::WritingMode aWM
,
237 const mozilla::LogicalSize
& aCBSize
, nscoord aAvailableISize
,
238 const mozilla::LogicalSize
& aMargin
,
239 const mozilla::LogicalSize
& aBorderPadding
,
240 const mozilla::StyleSizeOverrides
& aSizeOverrides
,
241 mozilla::ComputeSizeFlags aFlags
) final
;
243 bool IsServerImageMap();
245 // Translate a point that is relative to our frame into a localized CSS pixel
246 // coordinate that is relative to the content area of this frame (inside the
248 mozilla::CSSIntPoint
TranslateEventCoords(const nsPoint
& aPoint
);
250 bool GetAnchorHREFTargetAndNode(nsIURI
** aHref
, nsString
& aTarget
,
253 * Computes the width of the string that fits into the available space
255 * @param in aLength total length of the string in PRUnichars
256 * @param in aMaxWidth width not to be exceeded
257 * @param out aMaxFit length of the string that fits within aMaxWidth
259 * @return width of the string that fits within aMaxWidth
261 nscoord
MeasureString(const char16_t
* aString
, int32_t aLength
,
262 nscoord aMaxWidth
, uint32_t& aMaxFit
,
263 gfxContext
& aContext
, nsFontMetrics
& aFontMetrics
);
265 void DisplayAltText(nsPresContext
* aPresContext
,
266 gfxContext
& aRenderingContext
, const nsString
& aAltText
,
267 const nsRect
& aRect
);
269 ImgDrawResult
PaintImage(gfxContext
& aRenderingContext
, nsPoint aPt
,
270 const nsRect
& aDirtyRect
, imgIContainer
* aImage
,
274 * If we're ready to decode - that is, if our current request's image is
275 * available and our decoding heuristics are satisfied - then trigger a decode
276 * for our image at the size we predict it will be drawn next time it's
279 void MaybeDecodeForPredictedSize();
282 * Is this frame part of a ::marker pseudo?
284 bool IsForMarkerPseudo() const;
287 friend class nsImageListener
;
288 friend class nsImageLoadingContent
;
289 friend class mozilla::PresShell
;
291 void OnSizeAvailable(imgIRequest
* aRequest
, imgIContainer
* aImage
);
292 void OnFrameUpdate(imgIRequest
* aRequest
, const nsIntRect
* aRect
);
293 void OnLoadComplete(imgIRequest
* aRequest
, nsresult aStatus
);
296 * Notification that aRequest will now be the current request.
298 void NotifyNewCurrentRequest(imgIRequest
* aRequest
, nsresult aStatus
);
300 /// Always sync decode our image when painting if @aForce is true.
301 void SetForceSyncDecoding(bool aForce
) { mForceSyncDecoding
= aForce
; }
303 void AssertSyncDecodingHintIsInSync() const
311 * Computes the predicted dest rect that we'll draw into, in app units, based
312 * upon the provided frame content box. (The content box is what
313 * nsDisplayImage::GetBounds() returns.)
314 * The result is not necessarily contained in the frame content box.
316 nsRect
PredictedDestRect(const nsRect
& aFrameContentBox
);
319 nscoord
GetContinuationOffset() const;
320 bool ShouldDisplaySelection();
322 // Whether the image frame should use the mapped aspect ratio from width=""
324 bool ShouldUseMappedAspectRatio() const;
326 // Recalculate mIntrinsicSize from the image.
327 bool UpdateIntrinsicSize();
329 // Recalculate mIntrinsicRatio from the image.
330 bool UpdateIntrinsicRatio();
333 * This function calculates the transform for converting between
334 * source space & destination space. May fail if our image has a
335 * percent-valued or zero-valued height or width.
337 * @param aTransform The transform object to populate.
339 * @return whether we succeeded in creating the transform.
341 bool GetSourceToDestTransform(nsTransform2D
& aTransform
);
344 * Helper function to check whether the request corresponds to a load we don't
345 * care about. Most of the decoder observer methods will bail early if this
348 bool IsPendingLoad(imgIRequest
*) const;
351 * Updates mImage based on the current image request, and the image passed in
352 * (both can be null), and invalidate layout and paint as needed.
354 void UpdateImage(imgIRequest
*, imgIContainer
*);
357 * Function to convert a dirty rect in the source image to a dirty
358 * rect for the image frame.
360 nsRect
SourceRectToDest(const nsIntRect
& aRect
);
363 * Triggers invalidation for both our image display item and, if appropriate,
364 * our alt-feedback display item.
366 * @param aLayerInvalidRect The area to invalidate in layer space. If null,
367 * the entire layer will be invalidated.
368 * @param aFrameInvalidRect The area to invalidate in frame space. If null,
369 * the entire frame will be invalidated.
371 void InvalidateSelf(const nsIntRect
* aLayerInvalidRect
,
372 const nsRect
* aFrameInvalidRect
);
374 void MaybeSendIntrinsicSizeAndRatioToEmbedder();
375 void MaybeSendIntrinsicSizeAndRatioToEmbedder(Maybe
<mozilla::IntrinsicSize
>,
376 Maybe
<mozilla::AspectRatio
>);
378 RefPtr
<nsImageMap
> mImageMap
;
380 RefPtr
<nsImageListener
> mListener
;
382 // An image request created for content: url(..), list-style-image, or
384 RefPtr
<imgRequestProxy
> mOwnedRequest
;
386 nsCOMPtr
<imgIContainer
> mImage
;
387 nsCOMPtr
<imgIContainer
> mPrevImage
;
389 // The content-box size as if we are not fragmented, cached in the most recent
391 nsSize mComputedSize
;
393 mozilla::IntrinsicSize mIntrinsicSize
;
395 // Stores mImage's intrinsic ratio, or a default AspectRatio if there's no
397 mozilla::AspectRatio mIntrinsicRatio
;
400 bool mOwnedRequestRegistered
= false;
401 bool mDisplayingIcon
= false;
402 bool mFirstFrameComplete
= false;
403 bool mReflowCallbackPosted
= false;
404 bool mForceSyncDecoding
= false;
405 bool mIsInObjectOrEmbed
= false;
408 friend class mozilla::nsDisplayImage
;
409 friend class nsDisplayGradient
;
414 * Note that nsDisplayImage does not receive events. However, an image element
415 * is replaced content so its background will be z-adjacent to the
416 * image itself, and hence receive events just as if the image itself
419 class nsDisplayImage final
: public nsPaintedDisplayItem
{
421 typedef mozilla::layers::LayerManager LayerManager
;
423 nsDisplayImage(nsDisplayListBuilder
* aBuilder
, nsImageFrame
* aFrame
,
424 imgIContainer
* aImage
, imgIContainer
* aPrevImage
)
425 : nsPaintedDisplayItem(aBuilder
, aFrame
),
427 mPrevImage(aPrevImage
) {
428 MOZ_COUNT_CTOR(nsDisplayImage
);
430 ~nsDisplayImage() final
{ MOZ_COUNT_DTOR(nsDisplayImage
); }
432 void Paint(nsDisplayListBuilder
*, gfxContext
* aCtx
) final
;
435 * @return The dest rect we'll use when drawing this image, in app units.
436 * Not necessarily contained in this item's bounds.
438 nsRect
GetDestRect() const;
440 nsRect
GetBounds(bool* aSnap
) const {
442 return Frame()->GetContentRectRelativeToSelf() + ToReferenceFrame();
445 nsRect
GetBounds(nsDisplayListBuilder
*, bool* aSnap
) const final
{
446 return GetBounds(aSnap
);
449 nsRegion
GetOpaqueRegion(nsDisplayListBuilder
*, bool* aSnap
) const final
;
451 bool CreateWebRenderCommands(mozilla::wr::DisplayListBuilder
&,
452 mozilla::wr::IpcResourceUpdateQueue
&,
453 const StackingContextHelper
&,
454 mozilla::layers::RenderRootStateManager
*,
455 nsDisplayListBuilder
*) final
;
457 NS_DISPLAY_DECL_NAME("Image", TYPE_IMAGE
)
459 nsCOMPtr
<imgIContainer
> mImage
;
460 nsCOMPtr
<imgIContainer
> mPrevImage
;
463 } // namespace mozilla
465 #endif /* nsImageFrame_h___ */