Bug 1728955: part 8) Refactor `DisplayErrCode` in Windows' `nsClipboard`. r=masayuki
[gecko.git] / image / ClippedImage.cpp
blob2434c85984137fa5bcb2117cf4aa8ddd08531e6e
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "ClippedImage.h"
8 #include <algorithm>
9 #include <cmath>
10 #include <new> // Workaround for bug in VS10; see bug 981264.
11 #include <utility>
13 #include "ImageRegion.h"
14 #include "Orientation.h"
15 #include "gfxContext.h"
16 #include "gfxDrawable.h"
17 #include "gfxPlatform.h"
18 #include "gfxUtils.h"
19 #include "mozilla/RefPtr.h"
20 #include "mozilla/SVGImageContext.h"
21 #include "mozilla/Tuple.h"
22 #include "mozilla/gfx/2D.h"
24 namespace mozilla {
26 using namespace gfx;
27 using layers::ImageContainer;
28 using std::make_pair;
29 using std::max;
30 using std::modf;
31 using std::pair;
33 namespace image {
35 class ClippedImageCachedSurface {
36 public:
37 ClippedImageCachedSurface(already_AddRefed<SourceSurface> aSurface,
38 const nsIntSize& aSize,
39 const Maybe<SVGImageContext>& aSVGContext,
40 float aFrame, uint32_t aFlags,
41 ImgDrawResult aDrawResult)
42 : mSurface(aSurface),
43 mSize(aSize),
44 mSVGContext(aSVGContext),
45 mFrame(aFrame),
46 mFlags(aFlags),
47 mDrawResult(aDrawResult) {
48 MOZ_ASSERT(mSurface, "Must have a valid surface");
51 bool Matches(const nsIntSize& aSize,
52 const Maybe<SVGImageContext>& aSVGContext, float aFrame,
53 uint32_t aFlags) const {
54 return mSize == aSize && mSVGContext == aSVGContext && mFrame == aFrame &&
55 mFlags == aFlags;
58 already_AddRefed<SourceSurface> Surface() const {
59 RefPtr<SourceSurface> surf(mSurface);
60 return surf.forget();
63 ImgDrawResult GetDrawResult() const { return mDrawResult; }
65 bool NeedsRedraw() const {
66 return mDrawResult != ImgDrawResult::SUCCESS &&
67 mDrawResult != ImgDrawResult::BAD_IMAGE;
70 private:
71 RefPtr<SourceSurface> mSurface;
72 const nsIntSize mSize;
73 Maybe<SVGImageContext> mSVGContext;
74 const float mFrame;
75 const uint32_t mFlags;
76 const ImgDrawResult mDrawResult;
79 class DrawSingleTileCallback : public gfxDrawingCallback {
80 public:
81 DrawSingleTileCallback(ClippedImage* aImage, const nsIntSize& aSize,
82 const Maybe<SVGImageContext>& aSVGContext,
83 uint32_t aWhichFrame, uint32_t aFlags, float aOpacity)
84 : mImage(aImage),
85 mSize(aSize),
86 mSVGContext(aSVGContext),
87 mWhichFrame(aWhichFrame),
88 mFlags(aFlags),
89 mDrawResult(ImgDrawResult::NOT_READY),
90 mOpacity(aOpacity) {
91 MOZ_ASSERT(mImage, "Must have an image to clip");
94 virtual bool operator()(gfxContext* aContext, const gfxRect& aFillRect,
95 const SamplingFilter aSamplingFilter,
96 const gfxMatrix& aTransform) override {
97 MOZ_ASSERT(aTransform.IsIdentity(),
98 "Caller is probably CreateSamplingRestrictedDrawable, "
99 "which should not happen");
101 // Draw the image. |gfxCallbackDrawable| always calls this function with
102 // arguments that guarantee we never tile.
103 mDrawResult = mImage->DrawSingleTile(
104 aContext, mSize, ImageRegion::Create(aFillRect), mWhichFrame,
105 aSamplingFilter, mSVGContext, mFlags, mOpacity);
107 return true;
110 ImgDrawResult GetDrawResult() { return mDrawResult; }
112 private:
113 RefPtr<ClippedImage> mImage;
114 const nsIntSize mSize;
115 const Maybe<SVGImageContext>& mSVGContext;
116 const uint32_t mWhichFrame;
117 const uint32_t mFlags;
118 ImgDrawResult mDrawResult;
119 float mOpacity;
122 ClippedImage::ClippedImage(Image* aImage, nsIntRect aClip,
123 const Maybe<nsSize>& aSVGViewportSize)
124 : ImageWrapper(aImage), mClip(aClip) {
125 MOZ_ASSERT(aImage != nullptr, "ClippedImage requires an existing Image");
126 MOZ_ASSERT_IF(aSVGViewportSize,
127 aImage->GetType() == imgIContainer::TYPE_VECTOR);
128 if (aSVGViewportSize) {
129 mSVGViewportSize =
130 Some(aSVGViewportSize->ToNearestPixels(AppUnitsPerCSSPixel()));
134 ClippedImage::~ClippedImage() {}
136 bool ClippedImage::ShouldClip() {
137 // We need to evaluate the clipping region against the image's width and
138 // height once they're available to determine if it's valid and whether we
139 // actually need to do any work. We may fail if the image's width and height
140 // aren't available yet, in which case we'll try again later.
141 if (mShouldClip.isNothing()) {
142 int32_t width, height;
143 RefPtr<ProgressTracker> progressTracker =
144 InnerImage()->GetProgressTracker();
145 if (InnerImage()->HasError()) {
146 // If there's a problem with the inner image we'll let it handle
147 // everything.
148 mShouldClip.emplace(false);
149 } else if (mSVGViewportSize && !mSVGViewportSize->IsEmpty()) {
150 // Clamp the clipping region to the size of the SVG viewport.
151 nsIntRect svgViewportRect(nsIntPoint(0, 0), *mSVGViewportSize);
153 mClip = mClip.Intersect(svgViewportRect);
155 // If the clipping region is the same size as the SVG viewport size
156 // we don't have to do anything.
157 mShouldClip.emplace(!mClip.IsEqualInterior(svgViewportRect));
158 } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&width)) && width > 0 &&
159 NS_SUCCEEDED(InnerImage()->GetHeight(&height)) && height > 0) {
160 // Clamp the clipping region to the size of the underlying image.
161 mClip = mClip.Intersect(nsIntRect(0, 0, width, height));
163 // If the clipping region is the same size as the underlying image we
164 // don't have to do anything.
165 mShouldClip.emplace(
166 !mClip.IsEqualInterior(nsIntRect(0, 0, width, height)));
167 } else if (progressTracker &&
168 !(progressTracker->GetProgress() & FLAG_LOAD_COMPLETE)) {
169 // The image just hasn't finished loading yet. We don't yet know whether
170 // clipping with be needed or not for now. Just return without memorizing
171 // anything.
172 return false;
173 } else {
174 // We have a fully loaded image without a clearly defined width and
175 // height. This can happen with SVG images.
176 mShouldClip.emplace(false);
180 MOZ_ASSERT(mShouldClip.isSome(), "Should have computed a result");
181 return *mShouldClip;
184 NS_IMETHODIMP
185 ClippedImage::GetWidth(int32_t* aWidth) {
186 if (!ShouldClip()) {
187 return InnerImage()->GetWidth(aWidth);
190 *aWidth = mClip.Width();
191 return NS_OK;
194 NS_IMETHODIMP
195 ClippedImage::GetHeight(int32_t* aHeight) {
196 if (!ShouldClip()) {
197 return InnerImage()->GetHeight(aHeight);
200 *aHeight = mClip.Height();
201 return NS_OK;
204 NS_IMETHODIMP
205 ClippedImage::GetIntrinsicSize(nsSize* aSize) {
206 if (!ShouldClip()) {
207 return InnerImage()->GetIntrinsicSize(aSize);
210 *aSize = nsSize(mClip.Width(), mClip.Height());
211 return NS_OK;
214 Maybe<AspectRatio> ClippedImage::GetIntrinsicRatio() {
215 if (!ShouldClip()) {
216 return InnerImage()->GetIntrinsicRatio();
219 return Some(AspectRatio::FromSize(mClip.Width(), mClip.Height()));
222 NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
223 ClippedImage::GetFrame(uint32_t aWhichFrame, uint32_t aFlags) {
224 ImgDrawResult result;
225 RefPtr<SourceSurface> surface;
226 Tie(result, surface) = GetFrameInternal(mClip.Size(), Nothing(), Nothing(),
227 aWhichFrame, aFlags, 1.0);
228 return surface.forget();
231 NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
232 ClippedImage::GetFrameAtSize(const IntSize& aSize, uint32_t aWhichFrame,
233 uint32_t aFlags) {
234 // XXX(seth): It'd be nice to support downscale-during-decode for this case,
235 // but right now we just fall back to the intrinsic size.
236 return GetFrame(aWhichFrame, aFlags);
239 std::pair<ImgDrawResult, RefPtr<SourceSurface>> ClippedImage::GetFrameInternal(
240 const nsIntSize& aSize, const Maybe<SVGImageContext>& aSVGContext,
241 const Maybe<ImageIntRegion>& aRegion, uint32_t aWhichFrame, uint32_t aFlags,
242 float aOpacity) {
243 if (!ShouldClip()) {
244 RefPtr<SourceSurface> surface = InnerImage()->GetFrame(aWhichFrame, aFlags);
245 return std::make_pair(
246 surface ? ImgDrawResult::SUCCESS : ImgDrawResult::NOT_READY,
247 std::move(surface));
250 float frameToDraw = InnerImage()->GetFrameIndex(aWhichFrame);
251 if (!mCachedSurface ||
252 !mCachedSurface->Matches(aSize, aSVGContext, frameToDraw, aFlags) ||
253 mCachedSurface->NeedsRedraw()) {
254 // Create a surface to draw into.
255 RefPtr<DrawTarget> target =
256 gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
257 IntSize(aSize.width, aSize.height), SurfaceFormat::OS_RGBA);
258 if (!target || !target->IsValid()) {
259 NS_ERROR("Could not create a DrawTarget");
260 return std::make_pair(ImgDrawResult::TEMPORARY_ERROR,
261 RefPtr<SourceSurface>());
264 RefPtr<gfxContext> ctx = gfxContext::CreateOrNull(target);
265 MOZ_ASSERT(ctx); // already checked the draw target above
267 // Create our callback.
268 RefPtr<DrawSingleTileCallback> drawTileCallback =
269 new DrawSingleTileCallback(this, aSize, aSVGContext, aWhichFrame,
270 aFlags, aOpacity);
271 RefPtr<gfxDrawable> drawable =
272 new gfxCallbackDrawable(drawTileCallback, aSize);
274 // Actually draw. The callback will end up invoking DrawSingleTile.
275 gfxUtils::DrawPixelSnapped(ctx, drawable, SizeDouble(aSize),
276 ImageRegion::Create(aSize),
277 SurfaceFormat::OS_RGBA, SamplingFilter::LINEAR,
278 imgIContainer::FLAG_CLAMP);
280 // Cache the resulting surface.
281 mCachedSurface = MakeUnique<ClippedImageCachedSurface>(
282 target->Snapshot(), aSize, aSVGContext, frameToDraw, aFlags,
283 drawTileCallback->GetDrawResult());
286 MOZ_ASSERT(mCachedSurface, "Should have a cached surface now");
287 RefPtr<SourceSurface> surface = mCachedSurface->Surface();
288 return std::make_pair(mCachedSurface->GetDrawResult(), std::move(surface));
291 NS_IMETHODIMP_(bool)
292 ClippedImage::IsImageContainerAvailable(WindowRenderer* aRenderer,
293 uint32_t aFlags) {
294 if (!ShouldClip()) {
295 return InnerImage()->IsImageContainerAvailable(aRenderer, aFlags);
297 return false;
300 NS_IMETHODIMP_(ImgDrawResult)
301 ClippedImage::GetImageContainerAtSize(WindowRenderer* aRenderer,
302 const gfx::IntSize& aSize,
303 const Maybe<SVGImageContext>& aSVGContext,
304 const Maybe<ImageIntRegion>& aRegion,
305 uint32_t aFlags,
306 layers::ImageContainer** aOutContainer) {
307 // XXX(seth): We currently don't have a way of clipping the result of
308 // GetImageContainer. We work around this by always returning null, but if it
309 // ever turns out that ClippedImage is widely used on codepaths that can
310 // actually benefit from GetImageContainer, it would be a good idea to fix
311 // that method for performance reasons.
313 if (!ShouldClip()) {
314 return InnerImage()->GetImageContainerAtSize(
315 aRenderer, aSize, aSVGContext, aRegion, aFlags, aOutContainer);
318 return ImgDrawResult::NOT_SUPPORTED;
321 static bool MustCreateSurface(gfxContext* aContext, const nsIntSize& aSize,
322 const ImageRegion& aRegion,
323 const uint32_t aFlags) {
324 gfxRect imageRect(0, 0, aSize.width, aSize.height);
325 bool willTile = !imageRect.Contains(aRegion.Rect()) &&
326 !(aFlags & imgIContainer::FLAG_CLAMP);
327 bool willResample = aContext->CurrentMatrix().HasNonIntegerTranslation() &&
328 (willTile || !aRegion.RestrictionContains(imageRect));
329 return willTile || willResample;
332 NS_IMETHODIMP_(ImgDrawResult)
333 ClippedImage::Draw(gfxContext* aContext, const nsIntSize& aSize,
334 const ImageRegion& aRegion, uint32_t aWhichFrame,
335 SamplingFilter aSamplingFilter,
336 const Maybe<SVGImageContext>& aSVGContext, uint32_t aFlags,
337 float aOpacity) {
338 if (!ShouldClip()) {
339 return InnerImage()->Draw(aContext, aSize, aRegion, aWhichFrame,
340 aSamplingFilter, aSVGContext, aFlags, aOpacity);
343 // Check for tiling. If we need to tile then we need to create a
344 // gfxCallbackDrawable to handle drawing for us.
345 if (MustCreateSurface(aContext, aSize, aRegion, aFlags)) {
346 // Create a temporary surface containing a single tile of this image.
347 // GetFrame will call DrawSingleTile internally.
348 ImgDrawResult result;
349 RefPtr<SourceSurface> surface;
350 Tie(result, surface) = GetFrameInternal(aSize, aSVGContext, Nothing(),
351 aWhichFrame, aFlags, aOpacity);
352 if (!surface) {
353 MOZ_ASSERT(result != ImgDrawResult::SUCCESS);
354 return result;
357 // Create a drawable from that surface.
358 RefPtr<gfxSurfaceDrawable> drawable =
359 new gfxSurfaceDrawable(surface, aSize);
361 // Draw.
362 gfxUtils::DrawPixelSnapped(aContext, drawable, SizeDouble(aSize), aRegion,
363 SurfaceFormat::OS_RGBA, aSamplingFilter,
364 aOpacity);
366 return result;
369 return DrawSingleTile(aContext, aSize, aRegion, aWhichFrame, aSamplingFilter,
370 aSVGContext, aFlags, aOpacity);
373 ImgDrawResult ClippedImage::DrawSingleTile(
374 gfxContext* aContext, const nsIntSize& aSize, const ImageRegion& aRegion,
375 uint32_t aWhichFrame, SamplingFilter aSamplingFilter,
376 const Maybe<SVGImageContext>& aSVGContext, uint32_t aFlags,
377 float aOpacity) {
378 MOZ_ASSERT(!MustCreateSurface(aContext, aSize, aRegion, aFlags),
379 "Shouldn't need to create a surface");
381 gfxRect clip(mClip.X(), mClip.Y(), mClip.Width(), mClip.Height());
382 nsIntSize size(aSize), innerSize(aSize);
383 bool needScale = false;
384 if (mSVGViewportSize && !mSVGViewportSize->IsEmpty()) {
385 innerSize = *mSVGViewportSize;
386 needScale = true;
387 } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&innerSize.width)) &&
388 NS_SUCCEEDED(InnerImage()->GetHeight(&innerSize.height))) {
389 needScale = true;
390 } else {
391 MOZ_ASSERT_UNREACHABLE(
392 "If ShouldClip() led us to draw then we should never get here");
395 if (needScale) {
396 double scaleX = aSize.width / clip.Width();
397 double scaleY = aSize.height / clip.Height();
399 // Map the clip and size to the scale requested by the caller.
400 clip.Scale(scaleX, scaleY);
401 size = innerSize;
402 size.Scale(scaleX, scaleY);
405 // We restrict our drawing to only the clipping region, and translate so that
406 // the clipping region is placed at the position the caller expects.
407 ImageRegion region(aRegion);
408 region.MoveBy(clip.X(), clip.Y());
409 region = region.Intersect(clip);
411 gfxContextMatrixAutoSaveRestore saveMatrix(aContext);
412 aContext->Multiply(gfxMatrix::Translation(-clip.X(), -clip.Y()));
414 auto unclipViewport = [&](const SVGImageContext& aOldContext) {
415 // Map the viewport to the inner image. Note that we don't take the aSize
416 // parameter of imgIContainer::Draw into account, just the clipping region.
417 // The size in pixels at which the output will ultimately be drawn is
418 // irrelevant here since the purpose of the SVG viewport size is to
419 // determine what *region* of the SVG document will be drawn.
420 SVGImageContext context(aOldContext);
421 auto oldViewport = aOldContext.GetViewportSize();
422 if (oldViewport) {
423 CSSIntSize newViewport;
424 newViewport.width =
425 ceil(oldViewport->width * double(innerSize.width) / mClip.Width());
426 newViewport.height =
427 ceil(oldViewport->height * double(innerSize.height) / mClip.Height());
428 context.SetViewportSize(Some(newViewport));
430 return context;
433 return InnerImage()->Draw(aContext, size, region, aWhichFrame,
434 aSamplingFilter, aSVGContext.map(unclipViewport),
435 aFlags, aOpacity);
438 NS_IMETHODIMP
439 ClippedImage::RequestDiscard() {
440 // We're very aggressive about discarding.
441 mCachedSurface = nullptr;
443 return InnerImage()->RequestDiscard();
446 NS_IMETHODIMP_(Orientation)
447 ClippedImage::GetOrientation() {
448 // XXX(seth): This should not actually be here; this is just to work around a
449 // what appears to be a bug in MSVC's linker.
450 return InnerImage()->GetOrientation();
453 nsIntSize ClippedImage::OptimalImageSizeForDest(const gfxSize& aDest,
454 uint32_t aWhichFrame,
455 SamplingFilter aSamplingFilter,
456 uint32_t aFlags) {
457 if (!ShouldClip()) {
458 return InnerImage()->OptimalImageSizeForDest(aDest, aWhichFrame,
459 aSamplingFilter, aFlags);
462 int32_t imgWidth, imgHeight;
463 bool needScale = false;
464 bool forceUniformScaling = false;
465 if (mSVGViewportSize && !mSVGViewportSize->IsEmpty()) {
466 imgWidth = mSVGViewportSize->width;
467 imgHeight = mSVGViewportSize->height;
468 needScale = true;
469 forceUniformScaling = (aFlags & imgIContainer::FLAG_FORCE_UNIFORM_SCALING);
470 } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&imgWidth)) &&
471 NS_SUCCEEDED(InnerImage()->GetHeight(&imgHeight))) {
472 needScale = true;
475 if (needScale) {
476 // To avoid ugly sampling artifacts, ClippedImage needs the image size to
477 // be chosen such that the clipping region lies on pixel boundaries.
479 // First, we select a scale that's good for ClippedImage. An integer
480 // multiple of the size of the clipping region is always fine.
481 IntSize scale = IntSize::Ceil(aDest.width / mClip.Width(),
482 aDest.height / mClip.Height());
484 if (forceUniformScaling) {
485 scale.width = scale.height = max(scale.height, scale.width);
488 // Determine the size we'd prefer to render the inner image at, and ask the
489 // inner image what size we should actually use.
490 gfxSize desiredSize(double(imgWidth) * scale.width,
491 double(imgHeight) * scale.height);
492 nsIntSize innerDesiredSize = InnerImage()->OptimalImageSizeForDest(
493 desiredSize, aWhichFrame, aSamplingFilter, aFlags);
495 // To get our final result, we take the inner image's desired size and
496 // determine how large the clipped region would be at that scale. (Again, we
497 // ensure an integer multiple of the size of the clipping region.)
498 IntSize finalScale =
499 IntSize::Ceil(double(innerDesiredSize.width) / imgWidth,
500 double(innerDesiredSize.height) / imgHeight);
501 return mClip.Size() * finalScale;
504 MOZ_ASSERT(false,
505 "If ShouldClip() led us to draw then we should never get here");
506 return InnerImage()->OptimalImageSizeForDest(aDest, aWhichFrame,
507 aSamplingFilter, aFlags);
510 NS_IMETHODIMP_(nsIntRect)
511 ClippedImage::GetImageSpaceInvalidationRect(const nsIntRect& aRect) {
512 if (!ShouldClip()) {
513 return InnerImage()->GetImageSpaceInvalidationRect(aRect);
516 nsIntRect rect(InnerImage()->GetImageSpaceInvalidationRect(aRect));
517 rect = rect.Intersect(mClip);
518 rect.MoveBy(-mClip.X(), -mClip.Y());
519 return rect;
522 } // namespace image
523 } // namespace mozilla