Bug 1821144 - remove old windows worker definitions. r=aryx
[gecko.git] / image / ClippedImage.cpp
blob901aa4185df85212bbb9ff29c4c220f7fda3d4e9
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 std::max;
29 namespace image {
31 class ClippedImageCachedSurface {
32 public:
33 ClippedImageCachedSurface(already_AddRefed<SourceSurface> aSurface,
34 const nsIntSize& aSize,
35 const SVGImageContext& aSVGContext, float aFrame,
36 uint32_t aFlags, ImgDrawResult aDrawResult)
37 : mSurface(aSurface),
38 mSize(aSize),
39 mSVGContext(aSVGContext),
40 mFrame(aFrame),
41 mFlags(aFlags),
42 mDrawResult(aDrawResult) {
43 MOZ_ASSERT(mSurface, "Must have a valid surface");
46 bool Matches(const nsIntSize& aSize, const SVGImageContext& aSVGContext,
47 float aFrame, uint32_t aFlags) const {
48 return mSize == aSize && mSVGContext == aSVGContext && mFrame == aFrame &&
49 mFlags == aFlags;
52 already_AddRefed<SourceSurface> Surface() const {
53 RefPtr<SourceSurface> surf(mSurface);
54 return surf.forget();
57 ImgDrawResult GetDrawResult() const { return mDrawResult; }
59 bool NeedsRedraw() const {
60 return mDrawResult != ImgDrawResult::SUCCESS &&
61 mDrawResult != ImgDrawResult::BAD_IMAGE;
64 private:
65 RefPtr<SourceSurface> mSurface;
66 const nsIntSize mSize;
67 SVGImageContext mSVGContext;
68 const float mFrame;
69 const uint32_t mFlags;
70 const ImgDrawResult mDrawResult;
73 class DrawSingleTileCallback : public gfxDrawingCallback {
74 public:
75 DrawSingleTileCallback(ClippedImage* aImage, const nsIntSize& aSize,
76 const SVGImageContext& aSVGContext,
77 uint32_t aWhichFrame, uint32_t aFlags, float aOpacity)
78 : mImage(aImage),
79 mSize(aSize),
80 mSVGContext(aSVGContext),
81 mWhichFrame(aWhichFrame),
82 mFlags(aFlags),
83 mDrawResult(ImgDrawResult::NOT_READY),
84 mOpacity(aOpacity) {
85 MOZ_ASSERT(mImage, "Must have an image to clip");
88 virtual bool operator()(gfxContext* aContext, const gfxRect& aFillRect,
89 const SamplingFilter aSamplingFilter,
90 const gfxMatrix& aTransform) override {
91 MOZ_ASSERT(aTransform.IsIdentity(),
92 "Caller is probably CreateSamplingRestrictedDrawable, "
93 "which should not happen");
95 // Draw the image. |gfxCallbackDrawable| always calls this function with
96 // arguments that guarantee we never tile.
97 mDrawResult = mImage->DrawSingleTile(
98 aContext, mSize, ImageRegion::Create(aFillRect), mWhichFrame,
99 aSamplingFilter, mSVGContext, mFlags, mOpacity);
101 return true;
104 ImgDrawResult GetDrawResult() { return mDrawResult; }
106 private:
107 RefPtr<ClippedImage> mImage;
108 const nsIntSize mSize;
109 const SVGImageContext& mSVGContext;
110 const uint32_t mWhichFrame;
111 const uint32_t mFlags;
112 ImgDrawResult mDrawResult;
113 float mOpacity;
116 ClippedImage::ClippedImage(Image* aImage, nsIntRect aClip,
117 const Maybe<nsSize>& aSVGViewportSize)
118 : ImageWrapper(aImage), mClip(aClip) {
119 MOZ_ASSERT(aImage != nullptr, "ClippedImage requires an existing Image");
120 MOZ_ASSERT_IF(aSVGViewportSize,
121 aImage->GetType() == imgIContainer::TYPE_VECTOR);
122 if (aSVGViewportSize) {
123 mSVGViewportSize =
124 Some(aSVGViewportSize->ToNearestPixels(AppUnitsPerCSSPixel()));
128 ClippedImage::~ClippedImage() {}
130 bool ClippedImage::ShouldClip() {
131 // We need to evaluate the clipping region against the image's width and
132 // height once they're available to determine if it's valid and whether we
133 // actually need to do any work. We may fail if the image's width and height
134 // aren't available yet, in which case we'll try again later.
135 if (mShouldClip.isNothing()) {
136 int32_t width, height;
137 RefPtr<ProgressTracker> progressTracker =
138 InnerImage()->GetProgressTracker();
139 if (InnerImage()->HasError()) {
140 // If there's a problem with the inner image we'll let it handle
141 // everything.
142 mShouldClip.emplace(false);
143 } else if (mSVGViewportSize && !mSVGViewportSize->IsEmpty()) {
144 // Clamp the clipping region to the size of the SVG viewport.
145 nsIntRect svgViewportRect(nsIntPoint(0, 0), *mSVGViewportSize);
147 mClip = mClip.Intersect(svgViewportRect);
149 // If the clipping region is the same size as the SVG viewport size
150 // we don't have to do anything.
151 mShouldClip.emplace(!mClip.IsEqualInterior(svgViewportRect));
152 } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&width)) && width > 0 &&
153 NS_SUCCEEDED(InnerImage()->GetHeight(&height)) && height > 0) {
154 // Clamp the clipping region to the size of the underlying image.
155 mClip = mClip.Intersect(nsIntRect(0, 0, width, height));
157 // If the clipping region is the same size as the underlying image we
158 // don't have to do anything.
159 mShouldClip.emplace(
160 !mClip.IsEqualInterior(nsIntRect(0, 0, width, height)));
161 } else if (progressTracker &&
162 !(progressTracker->GetProgress() & FLAG_LOAD_COMPLETE)) {
163 // The image just hasn't finished loading yet. We don't yet know whether
164 // clipping with be needed or not for now. Just return without memorizing
165 // anything.
166 return false;
167 } else {
168 // We have a fully loaded image without a clearly defined width and
169 // height. This can happen with SVG images.
170 mShouldClip.emplace(false);
174 MOZ_ASSERT(mShouldClip.isSome(), "Should have computed a result");
175 return *mShouldClip;
178 NS_IMETHODIMP
179 ClippedImage::GetWidth(int32_t* aWidth) {
180 if (!ShouldClip()) {
181 return InnerImage()->GetWidth(aWidth);
184 *aWidth = mClip.Width();
185 return NS_OK;
188 NS_IMETHODIMP
189 ClippedImage::GetHeight(int32_t* aHeight) {
190 if (!ShouldClip()) {
191 return InnerImage()->GetHeight(aHeight);
194 *aHeight = mClip.Height();
195 return NS_OK;
198 NS_IMETHODIMP
199 ClippedImage::GetIntrinsicSize(nsSize* aSize) {
200 if (!ShouldClip()) {
201 return InnerImage()->GetIntrinsicSize(aSize);
204 *aSize = nsSize(mClip.Width(), mClip.Height());
205 return NS_OK;
208 Maybe<AspectRatio> ClippedImage::GetIntrinsicRatio() {
209 if (!ShouldClip()) {
210 return InnerImage()->GetIntrinsicRatio();
213 return Some(AspectRatio::FromSize(mClip.Width(), mClip.Height()));
216 NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
217 ClippedImage::GetFrame(uint32_t aWhichFrame, uint32_t aFlags) {
218 ImgDrawResult result;
219 RefPtr<SourceSurface> surface;
220 Tie(result, surface) = GetFrameInternal(mClip.Size(), SVGImageContext(),
221 Nothing(), aWhichFrame, aFlags, 1.0);
222 return surface.forget();
225 NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
226 ClippedImage::GetFrameAtSize(const IntSize& aSize, uint32_t aWhichFrame,
227 uint32_t aFlags) {
228 // XXX(seth): It'd be nice to support downscale-during-decode for this case,
229 // but right now we just fall back to the intrinsic size.
230 return GetFrame(aWhichFrame, aFlags);
233 std::pair<ImgDrawResult, RefPtr<SourceSurface>> ClippedImage::GetFrameInternal(
234 const nsIntSize& aSize, const SVGImageContext& aSVGContext,
235 const Maybe<ImageIntRegion>& aRegion, uint32_t aWhichFrame, uint32_t aFlags,
236 float aOpacity) {
237 if (!ShouldClip()) {
238 RefPtr<SourceSurface> surface = InnerImage()->GetFrame(aWhichFrame, aFlags);
239 return std::make_pair(
240 surface ? ImgDrawResult::SUCCESS : ImgDrawResult::NOT_READY,
241 std::move(surface));
244 float frameToDraw = InnerImage()->GetFrameIndex(aWhichFrame);
245 if (!mCachedSurface ||
246 !mCachedSurface->Matches(aSize, aSVGContext, frameToDraw, aFlags) ||
247 mCachedSurface->NeedsRedraw()) {
248 // Create a surface to draw into.
249 RefPtr<DrawTarget> target =
250 gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
251 IntSize(aSize.width, aSize.height), SurfaceFormat::OS_RGBA);
252 if (!target || !target->IsValid()) {
253 NS_ERROR("Could not create a DrawTarget");
254 return std::make_pair(ImgDrawResult::TEMPORARY_ERROR,
255 RefPtr<SourceSurface>());
258 gfxContext ctx(target);
260 // Create our callback.
261 RefPtr<DrawSingleTileCallback> drawTileCallback =
262 new DrawSingleTileCallback(this, aSize, aSVGContext, aWhichFrame,
263 aFlags, aOpacity);
264 RefPtr<gfxDrawable> drawable =
265 new gfxCallbackDrawable(drawTileCallback, aSize);
267 // Actually draw. The callback will end up invoking DrawSingleTile.
268 gfxUtils::DrawPixelSnapped(&ctx, drawable, SizeDouble(aSize),
269 ImageRegion::Create(aSize),
270 SurfaceFormat::OS_RGBA, SamplingFilter::LINEAR,
271 imgIContainer::FLAG_CLAMP);
273 // Cache the resulting surface.
274 mCachedSurface = MakeUnique<ClippedImageCachedSurface>(
275 target->Snapshot(), aSize, aSVGContext, frameToDraw, aFlags,
276 drawTileCallback->GetDrawResult());
279 MOZ_ASSERT(mCachedSurface, "Should have a cached surface now");
280 RefPtr<SourceSurface> surface = mCachedSurface->Surface();
281 return std::make_pair(mCachedSurface->GetDrawResult(), std::move(surface));
284 NS_IMETHODIMP_(bool)
285 ClippedImage::IsImageContainerAvailable(WindowRenderer* aRenderer,
286 uint32_t aFlags) {
287 if (!ShouldClip()) {
288 return InnerImage()->IsImageContainerAvailable(aRenderer, aFlags);
290 return false;
293 NS_IMETHODIMP_(ImgDrawResult)
294 ClippedImage::GetImageProvider(WindowRenderer* aRenderer,
295 const gfx::IntSize& aSize,
296 const SVGImageContext& aSVGContext,
297 const Maybe<ImageIntRegion>& aRegion,
298 uint32_t aFlags,
299 WebRenderImageProvider** aProvider) {
300 // XXX(seth): We currently don't have a way of clipping the result of
301 // GetImageContainer. We work around this by always returning null, but if it
302 // ever turns out that ClippedImage is widely used on codepaths that can
303 // actually benefit from GetImageContainer, it would be a good idea to fix
304 // that method for performance reasons.
306 if (!ShouldClip()) {
307 return InnerImage()->GetImageProvider(aRenderer, aSize, aSVGContext,
308 aRegion, aFlags, aProvider);
311 return ImgDrawResult::NOT_SUPPORTED;
314 static bool MustCreateSurface(gfxContext* aContext, const nsIntSize& aSize,
315 const ImageRegion& aRegion,
316 const uint32_t aFlags) {
317 gfxRect imageRect(0, 0, aSize.width, aSize.height);
318 bool willTile = !imageRect.Contains(aRegion.Rect()) &&
319 !(aFlags & imgIContainer::FLAG_CLAMP);
320 bool willResample = aContext->CurrentMatrix().HasNonIntegerTranslation() &&
321 (willTile || !aRegion.RestrictionContains(imageRect));
322 return willTile || willResample;
325 NS_IMETHODIMP_(ImgDrawResult)
326 ClippedImage::Draw(gfxContext* aContext, const nsIntSize& aSize,
327 const ImageRegion& aRegion, uint32_t aWhichFrame,
328 SamplingFilter aSamplingFilter,
329 const SVGImageContext& aSVGContext, uint32_t aFlags,
330 float aOpacity) {
331 if (!ShouldClip()) {
332 return InnerImage()->Draw(aContext, aSize, aRegion, aWhichFrame,
333 aSamplingFilter, aSVGContext, aFlags, aOpacity);
336 // Check for tiling. If we need to tile then we need to create a
337 // gfxCallbackDrawable to handle drawing for us.
338 if (MustCreateSurface(aContext, aSize, aRegion, aFlags)) {
339 // Create a temporary surface containing a single tile of this image.
340 // GetFrame will call DrawSingleTile internally.
341 ImgDrawResult result;
342 RefPtr<SourceSurface> surface;
343 Tie(result, surface) = GetFrameInternal(aSize, aSVGContext, Nothing(),
344 aWhichFrame, aFlags, aOpacity);
345 if (!surface) {
346 MOZ_ASSERT(result != ImgDrawResult::SUCCESS);
347 return result;
350 // Create a drawable from that surface.
351 RefPtr<gfxSurfaceDrawable> drawable =
352 new gfxSurfaceDrawable(surface, aSize);
354 // Draw.
355 gfxUtils::DrawPixelSnapped(aContext, drawable, SizeDouble(aSize), aRegion,
356 SurfaceFormat::OS_RGBA, aSamplingFilter,
357 aOpacity);
359 return result;
362 return DrawSingleTile(aContext, aSize, aRegion, aWhichFrame, aSamplingFilter,
363 aSVGContext, aFlags, aOpacity);
366 ImgDrawResult ClippedImage::DrawSingleTile(
367 gfxContext* aContext, const nsIntSize& aSize, const ImageRegion& aRegion,
368 uint32_t aWhichFrame, SamplingFilter aSamplingFilter,
369 const SVGImageContext& aSVGContext, uint32_t aFlags, float aOpacity) {
370 MOZ_ASSERT(!MustCreateSurface(aContext, aSize, aRegion, aFlags),
371 "Shouldn't need to create a surface");
373 gfxRect clip(mClip.X(), mClip.Y(), mClip.Width(), mClip.Height());
374 nsIntSize size(aSize), innerSize(aSize);
375 bool needScale = false;
376 if (mSVGViewportSize && !mSVGViewportSize->IsEmpty()) {
377 innerSize = *mSVGViewportSize;
378 needScale = true;
379 } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&innerSize.width)) &&
380 NS_SUCCEEDED(InnerImage()->GetHeight(&innerSize.height))) {
381 needScale = true;
382 } else {
383 MOZ_ASSERT_UNREACHABLE(
384 "If ShouldClip() led us to draw then we should never get here");
387 if (needScale) {
388 double scaleX = aSize.width / clip.Width();
389 double scaleY = aSize.height / clip.Height();
391 // Map the clip and size to the scale requested by the caller.
392 clip.Scale(scaleX, scaleY);
393 size = innerSize;
394 size.Scale(scaleX, scaleY);
397 // We restrict our drawing to only the clipping region, and translate so that
398 // the clipping region is placed at the position the caller expects.
399 ImageRegion region(aRegion);
400 region.MoveBy(clip.X(), clip.Y());
401 region = region.Intersect(clip);
403 gfxContextMatrixAutoSaveRestore saveMatrix(aContext);
404 aContext->Multiply(gfxMatrix::Translation(-clip.X(), -clip.Y()));
406 auto unclipViewport = [&](const SVGImageContext& aOldContext) {
407 // Map the viewport to the inner image. Note that we don't take the aSize
408 // parameter of imgIContainer::Draw into account, just the clipping region.
409 // The size in pixels at which the output will ultimately be drawn is
410 // irrelevant here since the purpose of the SVG viewport size is to
411 // determine what *region* of the SVG document will be drawn.
412 SVGImageContext context(aOldContext);
413 auto oldViewport = aOldContext.GetViewportSize();
414 if (oldViewport) {
415 CSSIntSize newViewport;
416 newViewport.width =
417 ceil(oldViewport->width * double(innerSize.width) / mClip.Width());
418 newViewport.height =
419 ceil(oldViewport->height * double(innerSize.height) / mClip.Height());
420 context.SetViewportSize(Some(newViewport));
422 return context;
425 return InnerImage()->Draw(aContext, size, region, aWhichFrame,
426 aSamplingFilter, unclipViewport(aSVGContext),
427 aFlags, aOpacity);
430 NS_IMETHODIMP
431 ClippedImage::RequestDiscard() {
432 // We're very aggressive about discarding.
433 mCachedSurface = nullptr;
435 return InnerImage()->RequestDiscard();
438 NS_IMETHODIMP_(Orientation)
439 ClippedImage::GetOrientation() {
440 // XXX(seth): This should not actually be here; this is just to work around a
441 // what appears to be a bug in MSVC's linker.
442 return InnerImage()->GetOrientation();
445 nsIntSize ClippedImage::OptimalImageSizeForDest(const gfxSize& aDest,
446 uint32_t aWhichFrame,
447 SamplingFilter aSamplingFilter,
448 uint32_t aFlags) {
449 if (!ShouldClip()) {
450 return InnerImage()->OptimalImageSizeForDest(aDest, aWhichFrame,
451 aSamplingFilter, aFlags);
454 int32_t imgWidth, imgHeight;
455 bool needScale = false;
456 bool forceUniformScaling = false;
457 if (mSVGViewportSize && !mSVGViewportSize->IsEmpty()) {
458 imgWidth = mSVGViewportSize->width;
459 imgHeight = mSVGViewportSize->height;
460 needScale = true;
461 forceUniformScaling = (aFlags & imgIContainer::FLAG_FORCE_UNIFORM_SCALING);
462 } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&imgWidth)) &&
463 NS_SUCCEEDED(InnerImage()->GetHeight(&imgHeight))) {
464 needScale = true;
467 if (needScale) {
468 // To avoid ugly sampling artifacts, ClippedImage needs the image size to
469 // be chosen such that the clipping region lies on pixel boundaries.
471 // First, we select a scale that's good for ClippedImage. An integer
472 // multiple of the size of the clipping region is always fine.
473 IntSize scale = IntSize::Ceil(aDest.width / mClip.Width(),
474 aDest.height / mClip.Height());
476 if (forceUniformScaling) {
477 scale.width = scale.height = max(scale.height, scale.width);
480 // Determine the size we'd prefer to render the inner image at, and ask the
481 // inner image what size we should actually use.
482 gfxSize desiredSize(double(imgWidth) * scale.width,
483 double(imgHeight) * scale.height);
484 nsIntSize innerDesiredSize = InnerImage()->OptimalImageSizeForDest(
485 desiredSize, aWhichFrame, aSamplingFilter, aFlags);
487 // To get our final result, we take the inner image's desired size and
488 // determine how large the clipped region would be at that scale. (Again, we
489 // ensure an integer multiple of the size of the clipping region.)
490 IntSize finalScale =
491 IntSize::Ceil(double(innerDesiredSize.width) / imgWidth,
492 double(innerDesiredSize.height) / imgHeight);
493 return mClip.Size() * finalScale;
496 MOZ_ASSERT(false,
497 "If ShouldClip() led us to draw then we should never get here");
498 return InnerImage()->OptimalImageSizeForDest(aDest, aWhichFrame,
499 aSamplingFilter, aFlags);
502 NS_IMETHODIMP_(nsIntRect)
503 ClippedImage::GetImageSpaceInvalidationRect(const nsIntRect& aRect) {
504 if (!ShouldClip()) {
505 return InnerImage()->GetImageSpaceInvalidationRect(aRect);
508 nsIntRect rect(InnerImage()->GetImageSpaceInvalidationRect(aRect));
509 rect = rect.Intersect(mClip);
510 rect.MoveBy(-mClip.X(), -mClip.Y());
511 return rect;
514 } // namespace image
515 } // namespace mozilla