1 /** -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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 #include
"nsISupports.idl"
12 #include
"ImgDrawResult.h"
13 #include
"gfxContext.h"
14 #include
"gfxMatrix.h"
16 #include
"mozilla/gfx/2D.h"
17 #include
"mozilla/AspectRatio.h"
18 #include
"mozilla/Maybe.h"
19 #include
"mozilla/RefPtr.h"
35 class SVGImageContext
;
49 native MaybeAspectRatio
(mozilla
::Maybe<mozilla
::AspectRatio
>);
50 native ImgDrawResult
(mozilla
::image
::ImgDrawResult
);
51 [ptr] native gfxContext
(gfxContext
);
52 [ref] native gfxMatrix
(gfxMatrix
);
53 [ref] native gfxRect
(gfxRect
);
54 [ref] native gfxSize
(gfxSize
);
55 native SamplingFilter
(mozilla
::gfx
::SamplingFilter
);
56 [ref] native nsIntRect
(nsIntRect
);
57 native nsIntRectByVal
(nsIntRect
);
58 [ref] native nsIntSize
(nsIntSize
);
59 native nsSize
(nsSize
);
60 [ptr] native nsIFrame
(nsIFrame
);
61 native TempRefImageContainer
(already_AddRefed
<mozilla
::layers
::ImageContainer
>);
62 [ptr] native ImageContainer
(mozilla
::layers
::ImageContainer
);
63 [ref] native ImageRegion
(mozilla
::image
::ImageRegion
);
64 [ptr] native LayerManager
(mozilla
::layers
::LayerManager
);
65 native Orientation
(mozilla
::image
::Orientation
);
66 [ref] native TimeStamp
(mozilla
::TimeStamp
);
67 [ref] native MaybeSVGImageContext
(mozilla
::Maybe<mozilla
::SVGImageContext
>);
68 native TempRefSourceSurface
(already_AddRefed
<mozilla
::gfx
::SourceSurface
>);
69 native TempRefImgIContainer
(already_AddRefed
<imgIContainer
>);
70 native nsIntSizeByVal
(nsIntSize
);
74 * imgIContainer is the interface that represents an image. It allows
75 * access to frames as Thebes surfaces. It also allows drawing of images
76 * onto Thebes contexts.
78 * Internally, imgIContainer also manages animation of images.
80 [scriptable
, builtinclass
, uuid(a8dbee24
-ff86
-4755-b40e
-51175caf31af
)]
81 interface imgIContainer
: nsISupports
84 * The width of the container rectangle. In the case of any error,
85 * zero is returned, and an exception will be thrown.
87 readonly attribute int32_t width
;
90 * The height of the container rectangle. In the case of any error,
91 * zero is returned, and an exception will be thrown.
93 readonly attribute int32_t height
;
96 * The intrinsic size of this image in appunits. If the image has no intrinsic
97 * size in a dimension, -1 will be returned for that dimension. In the case of
98 * any error, an exception will be thrown.
100 [noscript
] readonly attribute nsSize intrinsicSize
;
103 * The (dimensionless) intrinsic ratio of this image. In the case of any
104 * error, Nothing() will be returned.
106 [notxpcom
, nostdcall
] readonly attribute MaybeAspectRatio intrinsicRatio
;
109 * Given a size at which this image will be displayed, and the drawing
110 * parameters affecting how it will be drawn, returns the image size which
111 * should be used to draw to produce the highest quality result. This is the
112 * appropriate size, for example, to use as an input to the pixel snapping
115 * For best results the size returned by this method should not be cached. It
116 * can change over time due to changes in the internal state of the image.
118 * @param aDest The size of the destination rect into which this image will be
119 * drawn, in device pixels.
120 * @param aWhichFrame Frame specifier of the FRAME_* variety.
121 * @param aSamplingFilter The filter to be used if we're scaling the image.
122 * @param aFlags Flags of the FLAG_* variety
124 [notxpcom
, nostdcall
] nsIntSizeByVal
125 optimalImageSizeForDest
([const] in gfxSize aDest
, in uint32_t aWhichFrame
,
126 in SamplingFilter aSamplingFilter
, in uint32_t aFlags
);
129 * Enumerated values for the 'type' attribute (below).
131 const unsigned short TYPE_RASTER
= 0;
132 const unsigned short TYPE_VECTOR
= 1;
135 * The type of this image (one of the TYPE_* values above).
137 [infallible
] readonly attribute
unsigned short type
;
140 * Whether this image is animated. You can only be guaranteed that querying
141 * this will not throw if STATUS_DECODE_COMPLETE is set on the imgIRequest.
143 * @throws NS_ERROR_NOT_AVAILABLE if the animated state cannot be determined.
145 readonly attribute
boolean animated
;
148 * Producer ID for image containers created by this image.
150 [infallible
] readonly attribute
unsigned long producerId
;
153 * Flags for imgIContainer operations.
157 * FLAG_NONE: Lack of flags.
159 * FLAG_SYNC_DECODE: Forces synchronous/non-progressive decode of all
160 * available data before the call returns.
162 * FLAG_SYNC_DECODE_IF_FAST: Like FLAG_SYNC_DECODE, but requests a sync decode
163 * be performed only if ImageLib estimates it can be completed very quickly.
165 * FLAG_ASYNC_NOTIFY: Send notifications asynchronously, even if we decode
166 * synchronously beause of FLAG_SYNC_DECODE or FLAG_SYNC_DECODE_IF_FAST.
168 * FLAG_DECODE_NO_PREMULTIPLY_ALPHA: Do not premultiply alpha if
169 * it's not already premultiplied in the image data.
171 * FLAG_DECODE_NO_COLORSPACE_CONVERSION: Do not do any colorspace conversion;
172 * ignore any embedded profiles, and don't convert to any particular
175 * FLAG_CLAMP: Extend the image to the fill area by clamping image sample
176 * coordinates instead of by tiling. This only affects 'draw'.
178 * FLAG_HIGH_QUALITY_SCALING: A hint as to whether this image should be
179 * scaled using the high quality scaler. Do not set this if not drawing to
180 * a window or not listening to invalidations. Passing this flag will do two
181 * things: 1) request a decode of the image at the size asked for by the
182 * caller if one isn't already started or complete, and 2) allows a decoded
183 * frame of any size (it could be neither the requested size, nor the
184 * intrinsic size) to be substituted.
186 * FLAG_WANT_DATA_SURFACE: Can be passed to GetFrame when the caller wants a
187 * DataSourceSurface instead of a hardware accelerated surface. This can be
188 * important for performance (by avoiding an upload to/readback from the GPU)
189 * when the caller knows they want a SourceSurface of type DATA.
191 * FLAG_BYPASS_SURFACE_CACHE: Forces drawing to happen rather than taking
192 * cached rendering from the surface cache. This is used when we are printing,
193 * for example, where we want the vector commands from VectorImages to end up
194 * in the PDF output rather than a cached rendering at screen resolution.
196 * FLAG_FORCE_PRESERVEASPECTRATIO_NONE: Force scaling this image
197 * non-uniformly if necessary. This flag is for vector image only. A raster
198 * image should ignore this flag. While drawing a vector image with this
199 * flag, do not force uniform scaling even if its root <svg> node has a
200 * preserveAspectRatio attribute that would otherwise require uniform
201 * scaling , such as xMinYMin/ xMidYMin. Always scale the graphic content of
202 * the given image non-uniformly if necessary such that the image's
203 * viewBox (if specified or implied by height/width attributes) exactly
204 * matches the viewport rectangle.
206 * FLAG_FORCE_UNIFORM_SCALING: Signal to ClippedImage::OptimalSizeForDest that
207 * its returned size can only scale the image's size *uniformly* (by the same
208 * factor in each dimension). We need this flag when painting border-image
209 * section with SVG image source-data, if the SVG image has no viewBox and no
210 * intrinsic size. In such a case, we synthesize a viewport for the SVG image
211 * (a "window into SVG space") based on the border image area, and we need to
212 * be sure we don't subsequently scale that viewport in a way that distorts
213 * its contents by stretching them more in one dimension than the other.
215 * FLAG_AVOID_REDECODE_FOR_SIZE: If there is already a raster surface
216 * available for this image, but it is not the same size as requested, skip
217 * starting a new decode for said size.
219 const unsigned long FLAG_NONE
= 0x0;
220 const unsigned long FLAG_SYNC_DECODE
= 0x1;
221 const unsigned long FLAG_SYNC_DECODE_IF_FAST
= 0x2;
222 const unsigned long FLAG_ASYNC_NOTIFY
= 0x4;
223 const unsigned long FLAG_DECODE_NO_PREMULTIPLY_ALPHA
= 0x8;
224 const unsigned long FLAG_DECODE_NO_COLORSPACE_CONVERSION
= 0x10;
225 const unsigned long FLAG_CLAMP
= 0x20;
226 const unsigned long FLAG_HIGH_QUALITY_SCALING
= 0x40;
227 const unsigned long FLAG_WANT_DATA_SURFACE
= 0x80;
228 const unsigned long FLAG_BYPASS_SURFACE_CACHE
= 0x100;
229 const unsigned long FLAG_FORCE_PRESERVEASPECTRATIO_NONE
= 0x200;
230 const unsigned long FLAG_FORCE_UNIFORM_SCALING
= 0x400;
231 const unsigned long FLAG_AVOID_REDECODE_FOR_SIZE
= 0x800;
234 * A constant specifying the default set of decode flags (i.e., the default
235 * values for FLAG_DECODE_*).
237 const unsigned long DECODE_FLAGS_DEFAULT
= 0;
240 * Constants for specifying various "special" frames.
242 * FRAME_FIRST: The first frame
243 * FRAME_CURRENT: The current frame
245 * FRAME_MAX_VALUE should be set to the value of the maximum constant above,
246 * as it is used for ensuring that a valid value was passed in.
248 const unsigned long FRAME_FIRST
= 0;
249 const unsigned long FRAME_CURRENT
= 1;
250 const unsigned long FRAME_MAX_VALUE
= 1;
253 * Get a surface for the given frame. This may be a platform-native,
254 * optimized surface, so you cannot inspect its pixel data. If you
255 * need that, use SourceSurface::GetDataSurface.
257 * @param aWhichFrame Frame specifier of the FRAME_* variety.
258 * @param aFlags Flags of the FLAG_* variety
260 [noscript
, notxpcom
] TempRefSourceSurface getFrame
(in uint32_t aWhichFrame
,
264 * Get a surface for the given frame at the specified size. Matching the
265 * requested size is best effort; it's not guaranteed that the surface you get
266 * will be a perfect match. (Some reasons you may get a surface of a different
267 * size include: if you requested upscaling, if downscale-during-decode is
268 * disabled, or if you didn't request the first frame.)
270 * @param aSize The desired size.
271 * @param aWhichFrame Frame specifier of the FRAME_* variety.
272 * @param aFlags Flags of the FLAG_* variety
274 [noscript
, notxpcom
] TempRefSourceSurface getFrameAtSize
([const] in nsIntSize aSize
,
275 in uint32_t aWhichFrame
,
279 * Returns true if this image will draw opaquely right now if asked to draw
280 * with FLAG_HIGH_QUALITY_SCALING and otherwise default flags. If this image
281 * (when decoded) is opaque but no decoded frames are available then
282 * willDrawOpaqueNow will return false.
284 [noscript
, notxpcom
] boolean willDrawOpaqueNow
();
287 * @return true if getImageContainer() is expected to return a valid
288 * ImageContainer when passed the given @Manager and @Flags
291 [noscript
, notxpcom
] boolean isImageContainerAvailable
(in LayerManager aManager
,
294 * Attempts to create an ImageContainer (and Image) containing the current
295 * frame at its native size.
297 * Avoid calling this unless you're actually going to layerize this image.
299 * @param aManager The LayerManager which will be used to create the
301 * @param aFlags Decoding / drawing flags (in other words, FLAG_* flags).
302 * Currently only FLAG_SYNC_DECODE and FLAG_SYNC_DECODE_IF_FAST
304 * @return An ImageContainer for the current frame, or nullptr if one could
307 [noscript
, notxpcom
] TempRefImageContainer getImageContainer
(in LayerManager aManager
,
311 * Attempts to create an ImageContainer (and Image) containing the current
312 * frame at the given size. Match the requested size is best effort; it's
313 * not guaranteed that the surface you get will be a perfect match. (Some
314 * reasons you may get a surface of a different size include: if you
315 * requested upscaling, or if downscale-during-decode is disabled.)
317 * Avoid calling this unless you're actually going to layerize this image.
319 * @param aManager The LayerManager which will be used to create the
321 * @param aSVGContext If specified, SVG-related rendering context, such as
322 * overridden attributes on the image document's root <svg>
323 * node, and the size of the viewport that the full image
324 * would occupy. Ignored for raster images.
325 * @param aFlags Decoding / drawing flags (in other words, FLAG_* flags).
326 * Currently only FLAG_SYNC_DECODE and FLAG_SYNC_DECODE_IF_FAST
328 * @param aContainer Return value for ImageContainer for the current frame.
329 * May be null depending on the draw result.
330 * @return The draw result for the current frame.
332 [noscript
, notxpcom
] ImgDrawResult getImageContainerAtSize
(in LayerManager aManager
,
333 [const] in nsIntSize aSize
,
334 [const] in MaybeSVGImageContext aSVGContext
,
336 out ImageContainer aOutContainer
);
339 * @return true if getImageContainer() is expected to return a valid
340 * ImageContainer when passed the given @Manager, @Size and @Flags
343 [noscript
, notxpcom
] boolean isImageContainerAvailableAtSize
(in LayerManager aManager
,
344 [const] in nsIntSize aSize
,
348 * Draw the requested frame of this image onto the context specified.
350 * Drawing an image involves scaling it to a certain size (which may be
351 * implemented as a "smart" scale by substituting an HQ-scaled frame or
352 * rendering at a high DPI), and then selecting a region of that image to
353 * draw. That region is drawn onto the graphics context and in the process
354 * transformed by the context matrix, which determines the final area that is
355 * filled. The basic process looks like this:
357 * +------------------+
360 * | intrinsic width |
362 * | intrinsic height |
363 * +------------------+
366 * / (scale to aSize) \
368 * +----------------------------+
371 * | aSize.width X aSize.height |
376 * +-------(---------(----------+
383 * +---------------------+
387 * +---------------------+
389 * The region may extend outside of the scaled image's boundaries. It's
390 * actually a region in tiled image space, which is formed by tiling the
391 * scaled image infinitely in every direction. Drawing with a region larger
392 * than the scaled image thus causes the filled area to contain multiple tiled
393 * copies of the image, which looks like this:
395 * ....................................................
397 * : Tile : Tile : Tile :
398 * : +------------[aRegion]------------+ :
399 * :........|.......:................:........|.......:
401 * : Ti|le : Scaled Image : Ti|le :
403 * :........|.......:................:........|.......:
404 * : +---------------------------------+ :
405 * : Ti|le : Tile : Ti|le :
407 * :......(.........:................:..........).....:
410 * | (transform by aContext matrix) |
412 * +---------------------------------------------+
414 * |.....:.................................:.....|
418 * |.....:.................................:.....|
420 * +---------------------------------------------+
423 * @param aContext The Thebes context to draw the image to.
424 * @param aSize The size to which the image should be scaled before drawing.
425 * This requirement may be satisfied using HQ scaled frames,
426 * selecting from different resolution layers, drawing at a
427 * higher DPI, or just performing additional scaling on the
428 * graphics context. Callers can use optimalImageSizeForDest()
429 * to determine the best choice for this parameter if they have
430 * no special size requirements.
431 * @param aRegion The region in tiled image space which will be drawn onto the
432 * graphics context. aRegion is in the coordinate space of the
433 * image after it has been scaled to aSize - that is, the image
434 * is scaled first, and then aRegion is applied. When aFlags
435 * includes FLAG_CLAMP, the image will be extended to this area
436 * by clamping image sample coordinates. Otherwise, the image
437 * will be automatically tiled as necessary. aRegion can also
438 * optionally contain a second region which restricts the set
439 * of pixels we're allowed to sample from when drawing; this
440 * is only of use to callers which need to draw with pixel
442 * @param aWhichFrame Frame specifier of the FRAME_* variety.
443 * @param aSamplingFilter The filter to be used if we're scaling the image.
444 * @param aSVGContext If specified, SVG-related rendering context, such as
445 * overridden attributes on the image document's root <svg>
446 * node, and the size of the viewport that the full image
447 * would occupy. Ignored for raster images.
448 * @param aFlags Flags of the FLAG_* variety
449 * @return A ImgDrawResult value indicating whether and to what degree the
450 * drawing operation was successful.
452 [noscript
, notxpcom
] ImgDrawResult
453 draw
(in gfxContext aContext
,
454 [const] in nsIntSize aSize
,
455 [const] in ImageRegion aRegion
,
456 in uint32_t aWhichFrame
,
457 in SamplingFilter aSamplingFilter
,
458 [const] in MaybeSVGImageContext aSVGContext
,
463 * Ensures that an image is decoding. Calling this function guarantees that
464 * the image will at some point fire off decode notifications. Images that
465 * can be decoded "quickly" according to some heuristic will be decoded
468 * @param aFlags Flags of the FLAG_* variety. Only FLAG_ASYNC_NOTIFY
469 * is accepted; all others are ignored.
470 * @param aWhichFrame Frame specifier of the FRAME_* variety.
472 [noscript
] void startDecoding
(in uint32_t aFlags
, in uint32_t aWhichFrame
);
475 nsresult StartDecoding
(uint32_t aFlags
) {
476 return StartDecoding
(aFlags
, FRAME_CURRENT
);
481 * Exactly like startDecoding above except returns whether the current frame
482 * of the image is complete or not.
484 * @param aFlags Flags of the FLAG_* variety. Only FLAG_ASYNC_NOTIFY
485 * is accepted; all others are ignored.
486 * @param aWhichFrame Frame specifier of the FRAME_* variety.
488 [noscript
, notxpcom
] boolean startDecodingWithResult
(in uint32_t aFlags
, in uint32_t aWhichFrame
);
491 bool StartDecodingWithResult
(uint32_t aFlags
) {
492 return StartDecodingWithResult
(aFlags
, FRAME_CURRENT
);
497 * This method triggers decoding for an image, but unlike startDecoding() it
498 * enables the caller to provide more detailed information about the decode
501 * @param aFlags Flags of the FLAG_* variety.
502 * @param aWhichFrame Frame specifier of the FRAME_* variety.
503 * @return True there is a surface that satisfies the request and it is
504 * fully decoded, else false.
506 [noscript
, notxpcom
] boolean requestDecodeWithResult
(in uint32_t aFlags
, in uint32_t aWhichFrame
);
509 bool RequestDecodeWithResult
(uint32_t aFlags
) {
510 return RequestDecodeWithResult
(aFlags
, FRAME_CURRENT
);
515 * This method triggers decoding for an image, but unlike startDecoding() it
516 * enables the caller to provide more detailed information about the decode
519 * @param aSize The size to which the image should be scaled while decoding,
520 * if possible. If the image cannot be scaled to this size while
521 * being decoded, it will be decoded at its intrinsic size.
522 * @param aFlags Flags of the FLAG_* variety.
523 * @param aWhichFrame Frame specifier of the FRAME_* variety.
525 [noscript
] void requestDecodeForSize
([const] in nsIntSize aSize
,
527 in uint32_t aWhichFrame
);
530 nsresult RequestDecodeForSize
(const nsIntSize
& aSize
, uint32_t aFlags
) {
531 return RequestDecodeForSize
(aSize
, aFlags
, FRAME_CURRENT
);
536 * Increments the lock count on the image. An image will not be discarded
537 * as long as the lock count is nonzero. Note that it is still possible for
538 * the image to be undecoded if decode-on-draw is enabled and the image
541 * Upon instantiation images have a lock count of zero.
546 * Decreases the lock count on the image. If the lock count drops to zero,
547 * the image is allowed to discard its frame data to save memory.
549 * Upon instantiation images have a lock count of zero. It is an error to
550 * call this method without first having made a matching lockImage() call.
551 * In other words, the lock count is not allowed to be negative.
556 * If this image is unlocked, discard its decoded data. If the image is
557 * locked or has already been discarded, do nothing.
559 void requestDiscard
();
562 * Indicates that this imgIContainer has been triggered to update
563 * its internal animation state. Likely this should only be called
564 * from within nsImageFrame or objects of similar type.
566 [notxpcom
] void requestRefresh
([const] in TimeStamp aTime
);
569 * Animation mode Constants
574 const short kNormalAnimMode
= 0;
575 const short kDontAnimMode
= 1;
576 const short kLoopOnceAnimMode
= 2;
578 attribute
unsigned short animationMode
;
580 /* Methods to control animation */
581 void resetAnimation
();
584 * Returns an index for the requested animation frame (either FRAME_FIRST or
587 * The units of the index aren't specified, and may vary between different
588 * types of images. What you can rely on is that on all occasions when
589 * getFrameIndex(FRAME_CURRENT) returns a certain value,
590 * draw(..FRAME_CURRENT..) will draw the same frame. The same holds for
591 * FRAME_FIRST as well.
593 * @param aWhichFrame Frame specifier of the FRAME_* variety.
595 [notxpcom
] float getFrameIndex
(in uint32_t aWhichFrame
);
598 * Returns the inherent orientation of the image, as described in the image's
599 * metadata (e.g. EXIF).
601 [notxpcom
] Orientation getOrientation
();
604 * Returns the delay, in ms, between the first and second frame. If this
605 * returns 0, there is no delay between first and second frame (i.e., this
606 * image could render differently whenever it draws).
608 * If this image is not animated, or not known to be animated (see attribute
609 * animated), returns -1.
611 [notxpcom
] int32_t getFirstFrameDelay
();
614 * If this is an animated image that hasn't started animating already, this
615 * sets the animation's start time to the indicated time.
617 * This has no effect if the image isn't animated or it has started animating
618 * already; it also has no effect if the image format doesn't care about
619 * animation start time.
621 * In all cases, animation does not actually begin until startAnimation(),
622 * resetAnimation(), or requestRefresh() is called for the first time.
624 [notxpcom
] void setAnimationStartTime
([const] in TimeStamp aTime
);
627 * Given an invalidation rect in the coordinate system used by the decoder,
628 * returns an invalidation rect in image space.
630 * This is the identity transformation in most cases, but the result can
631 * differ if the image is wrapped by an ImageWrapper that changes its size
634 [notxpcom
] nsIntRectByVal
635 getImageSpaceInvalidationRect
([const] in nsIntRect aRect
);
638 * Removes any ImageWrappers and returns the unwrapped base image.
640 [notxpcom
, nostdcall
] TempRefImgIContainer unwrap
();
643 * Propagate the use counters (if any) from this container to the passed in
646 [noscript
, notxpcom
] void propagateUseCounters
(in Document aDocument
);
650 * Get the set of sizes the image can decode to natively.
652 virtual nsresult GetNativeSizes
(nsTArray
<nsIntSize
>& aNativeSizes
) const = 0;
654 virtual size_t GetNativeSizesLength
() const = 0;