Bug 1815313 - Add test for invalid schemes in onLoadRequest. r=jonalmeida
[gecko.git] / image / ClippedImage.cpp
blob12e38bdde848a0b02b91387e1f31cfae5be23abf
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 RefPtr<gfxContext> ctx = gfxContext::CreateOrNull(target);
259 MOZ_ASSERT(ctx); // already checked the draw target above
261 // Create our callback.
262 RefPtr<DrawSingleTileCallback> drawTileCallback =
263 new DrawSingleTileCallback(this, aSize, aSVGContext, aWhichFrame,
264 aFlags, aOpacity);
265 RefPtr<gfxDrawable> drawable =
266 new gfxCallbackDrawable(drawTileCallback, aSize);
268 // Actually draw. The callback will end up invoking DrawSingleTile.
269 gfxUtils::DrawPixelSnapped(ctx, drawable, SizeDouble(aSize),
270 ImageRegion::Create(aSize),
271 SurfaceFormat::OS_RGBA, SamplingFilter::LINEAR,
272 imgIContainer::FLAG_CLAMP);
274 // Cache the resulting surface.
275 mCachedSurface = MakeUnique<ClippedImageCachedSurface>(
276 target->Snapshot(), aSize, aSVGContext, frameToDraw, aFlags,
277 drawTileCallback->GetDrawResult());
280 MOZ_ASSERT(mCachedSurface, "Should have a cached surface now");
281 RefPtr<SourceSurface> surface = mCachedSurface->Surface();
282 return std::make_pair(mCachedSurface->GetDrawResult(), std::move(surface));
285 NS_IMETHODIMP_(bool)
286 ClippedImage::IsImageContainerAvailable(WindowRenderer* aRenderer,
287 uint32_t aFlags) {
288 if (!ShouldClip()) {
289 return InnerImage()->IsImageContainerAvailable(aRenderer, aFlags);
291 return false;
294 NS_IMETHODIMP_(ImgDrawResult)
295 ClippedImage::GetImageProvider(WindowRenderer* aRenderer,
296 const gfx::IntSize& aSize,
297 const SVGImageContext& aSVGContext,
298 const Maybe<ImageIntRegion>& aRegion,
299 uint32_t aFlags,
300 WebRenderImageProvider** aProvider) {
301 // XXX(seth): We currently don't have a way of clipping the result of
302 // GetImageContainer. We work around this by always returning null, but if it
303 // ever turns out that ClippedImage is widely used on codepaths that can
304 // actually benefit from GetImageContainer, it would be a good idea to fix
305 // that method for performance reasons.
307 if (!ShouldClip()) {
308 return InnerImage()->GetImageProvider(aRenderer, aSize, aSVGContext,
309 aRegion, aFlags, aProvider);
312 return ImgDrawResult::NOT_SUPPORTED;
315 static bool MustCreateSurface(gfxContext* aContext, const nsIntSize& aSize,
316 const ImageRegion& aRegion,
317 const uint32_t aFlags) {
318 gfxRect imageRect(0, 0, aSize.width, aSize.height);
319 bool willTile = !imageRect.Contains(aRegion.Rect()) &&
320 !(aFlags & imgIContainer::FLAG_CLAMP);
321 bool willResample = aContext->CurrentMatrix().HasNonIntegerTranslation() &&
322 (willTile || !aRegion.RestrictionContains(imageRect));
323 return willTile || willResample;
326 NS_IMETHODIMP_(ImgDrawResult)
327 ClippedImage::Draw(gfxContext* aContext, const nsIntSize& aSize,
328 const ImageRegion& aRegion, uint32_t aWhichFrame,
329 SamplingFilter aSamplingFilter,
330 const SVGImageContext& aSVGContext, uint32_t aFlags,
331 float aOpacity) {
332 if (!ShouldClip()) {
333 return InnerImage()->Draw(aContext, aSize, aRegion, aWhichFrame,
334 aSamplingFilter, aSVGContext, aFlags, aOpacity);
337 // Check for tiling. If we need to tile then we need to create a
338 // gfxCallbackDrawable to handle drawing for us.
339 if (MustCreateSurface(aContext, aSize, aRegion, aFlags)) {
340 // Create a temporary surface containing a single tile of this image.
341 // GetFrame will call DrawSingleTile internally.
342 ImgDrawResult result;
343 RefPtr<SourceSurface> surface;
344 Tie(result, surface) = GetFrameInternal(aSize, aSVGContext, Nothing(),
345 aWhichFrame, aFlags, aOpacity);
346 if (!surface) {
347 MOZ_ASSERT(result != ImgDrawResult::SUCCESS);
348 return result;
351 // Create a drawable from that surface.
352 RefPtr<gfxSurfaceDrawable> drawable =
353 new gfxSurfaceDrawable(surface, aSize);
355 // Draw.
356 gfxUtils::DrawPixelSnapped(aContext, drawable, SizeDouble(aSize), aRegion,
357 SurfaceFormat::OS_RGBA, aSamplingFilter,
358 aOpacity);
360 return result;
363 return DrawSingleTile(aContext, aSize, aRegion, aWhichFrame, aSamplingFilter,
364 aSVGContext, aFlags, aOpacity);
367 ImgDrawResult ClippedImage::DrawSingleTile(
368 gfxContext* aContext, const nsIntSize& aSize, const ImageRegion& aRegion,
369 uint32_t aWhichFrame, SamplingFilter aSamplingFilter,
370 const SVGImageContext& aSVGContext, uint32_t aFlags, float aOpacity) {
371 MOZ_ASSERT(!MustCreateSurface(aContext, aSize, aRegion, aFlags),
372 "Shouldn't need to create a surface");
374 gfxRect clip(mClip.X(), mClip.Y(), mClip.Width(), mClip.Height());
375 nsIntSize size(aSize), innerSize(aSize);
376 bool needScale = false;
377 if (mSVGViewportSize && !mSVGViewportSize->IsEmpty()) {
378 innerSize = *mSVGViewportSize;
379 needScale = true;
380 } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&innerSize.width)) &&
381 NS_SUCCEEDED(InnerImage()->GetHeight(&innerSize.height))) {
382 needScale = true;
383 } else {
384 MOZ_ASSERT_UNREACHABLE(
385 "If ShouldClip() led us to draw then we should never get here");
388 if (needScale) {
389 double scaleX = aSize.width / clip.Width();
390 double scaleY = aSize.height / clip.Height();
392 // Map the clip and size to the scale requested by the caller.
393 clip.Scale(scaleX, scaleY);
394 size = innerSize;
395 size.Scale(scaleX, scaleY);
398 // We restrict our drawing to only the clipping region, and translate so that
399 // the clipping region is placed at the position the caller expects.
400 ImageRegion region(aRegion);
401 region.MoveBy(clip.X(), clip.Y());
402 region = region.Intersect(clip);
404 gfxContextMatrixAutoSaveRestore saveMatrix(aContext);
405 aContext->Multiply(gfxMatrix::Translation(-clip.X(), -clip.Y()));
407 auto unclipViewport = [&](const SVGImageContext& aOldContext) {
408 // Map the viewport to the inner image. Note that we don't take the aSize
409 // parameter of imgIContainer::Draw into account, just the clipping region.
410 // The size in pixels at which the output will ultimately be drawn is
411 // irrelevant here since the purpose of the SVG viewport size is to
412 // determine what *region* of the SVG document will be drawn.
413 SVGImageContext context(aOldContext);
414 auto oldViewport = aOldContext.GetViewportSize();
415 if (oldViewport) {
416 CSSIntSize newViewport;
417 newViewport.width =
418 ceil(oldViewport->width * double(innerSize.width) / mClip.Width());
419 newViewport.height =
420 ceil(oldViewport->height * double(innerSize.height) / mClip.Height());
421 context.SetViewportSize(Some(newViewport));
423 return context;
426 return InnerImage()->Draw(aContext, size, region, aWhichFrame,
427 aSamplingFilter, unclipViewport(aSVGContext),
428 aFlags, aOpacity);
431 NS_IMETHODIMP
432 ClippedImage::RequestDiscard() {
433 // We're very aggressive about discarding.
434 mCachedSurface = nullptr;
436 return InnerImage()->RequestDiscard();
439 NS_IMETHODIMP_(Orientation)
440 ClippedImage::GetOrientation() {
441 // XXX(seth): This should not actually be here; this is just to work around a
442 // what appears to be a bug in MSVC's linker.
443 return InnerImage()->GetOrientation();
446 nsIntSize ClippedImage::OptimalImageSizeForDest(const gfxSize& aDest,
447 uint32_t aWhichFrame,
448 SamplingFilter aSamplingFilter,
449 uint32_t aFlags) {
450 if (!ShouldClip()) {
451 return InnerImage()->OptimalImageSizeForDest(aDest, aWhichFrame,
452 aSamplingFilter, aFlags);
455 int32_t imgWidth, imgHeight;
456 bool needScale = false;
457 bool forceUniformScaling = false;
458 if (mSVGViewportSize && !mSVGViewportSize->IsEmpty()) {
459 imgWidth = mSVGViewportSize->width;
460 imgHeight = mSVGViewportSize->height;
461 needScale = true;
462 forceUniformScaling = (aFlags & imgIContainer::FLAG_FORCE_UNIFORM_SCALING);
463 } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&imgWidth)) &&
464 NS_SUCCEEDED(InnerImage()->GetHeight(&imgHeight))) {
465 needScale = true;
468 if (needScale) {
469 // To avoid ugly sampling artifacts, ClippedImage needs the image size to
470 // be chosen such that the clipping region lies on pixel boundaries.
472 // First, we select a scale that's good for ClippedImage. An integer
473 // multiple of the size of the clipping region is always fine.
474 IntSize scale = IntSize::Ceil(aDest.width / mClip.Width(),
475 aDest.height / mClip.Height());
477 if (forceUniformScaling) {
478 scale.width = scale.height = max(scale.height, scale.width);
481 // Determine the size we'd prefer to render the inner image at, and ask the
482 // inner image what size we should actually use.
483 gfxSize desiredSize(double(imgWidth) * scale.width,
484 double(imgHeight) * scale.height);
485 nsIntSize innerDesiredSize = InnerImage()->OptimalImageSizeForDest(
486 desiredSize, aWhichFrame, aSamplingFilter, aFlags);
488 // To get our final result, we take the inner image's desired size and
489 // determine how large the clipped region would be at that scale. (Again, we
490 // ensure an integer multiple of the size of the clipping region.)
491 IntSize finalScale =
492 IntSize::Ceil(double(innerDesiredSize.width) / imgWidth,
493 double(innerDesiredSize.height) / imgHeight);
494 return mClip.Size() * finalScale;
497 MOZ_ASSERT(false,
498 "If ShouldClip() led us to draw then we should never get here");
499 return InnerImage()->OptimalImageSizeForDest(aDest, aWhichFrame,
500 aSamplingFilter, aFlags);
503 NS_IMETHODIMP_(nsIntRect)
504 ClippedImage::GetImageSpaceInvalidationRect(const nsIntRect& aRect) {
505 if (!ShouldClip()) {
506 return InnerImage()->GetImageSpaceInvalidationRect(aRect);
509 nsIntRect rect(InnerImage()->GetImageSpaceInvalidationRect(aRect));
510 rect = rect.Intersect(mClip);
511 rect.MoveBy(-mClip.X(), -mClip.Y());
512 return rect;
515 } // namespace image
516 } // namespace mozilla