image/src/ClippedImage.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "gfxDrawable.h"
michael@0 7 #include "gfxPlatform.h"
michael@0 8 #include "gfxUtils.h"
michael@0 9 #include "mozilla/gfx/2D.h"
michael@0 10 #include "mozilla/RefPtr.h"
michael@0 11
michael@0 12 #include "ClippedImage.h"
michael@0 13 #include "Orientation.h"
michael@0 14 #include "SVGImageContext.h"
michael@0 15
michael@0 16 using namespace mozilla;
michael@0 17 using namespace mozilla::gfx;
michael@0 18 using mozilla::layers::LayerManager;
michael@0 19 using mozilla::layers::ImageContainer;
michael@0 20
michael@0 21 namespace mozilla {
michael@0 22 namespace image {
michael@0 23
michael@0 24 class ClippedImageCachedSurface
michael@0 25 {
michael@0 26 public:
michael@0 27 ClippedImageCachedSurface(TemporaryRef<SourceSurface> aSurface,
michael@0 28 const nsIntSize& aViewportSize,
michael@0 29 const SVGImageContext* aSVGContext,
michael@0 30 float aFrame,
michael@0 31 uint32_t aFlags)
michael@0 32 : mSurface(aSurface)
michael@0 33 , mViewportSize(aViewportSize)
michael@0 34 , mFrame(aFrame)
michael@0 35 , mFlags(aFlags)
michael@0 36 {
michael@0 37 MOZ_ASSERT(mSurface, "Must have a valid surface");
michael@0 38 if (aSVGContext) {
michael@0 39 mSVGContext.construct(*aSVGContext);
michael@0 40 }
michael@0 41 }
michael@0 42
michael@0 43 bool Matches(const nsIntSize& aViewportSize,
michael@0 44 const SVGImageContext* aSVGContext,
michael@0 45 float aFrame,
michael@0 46 uint32_t aFlags)
michael@0 47 {
michael@0 48 bool matchesSVGContext = (!aSVGContext && mSVGContext.empty()) ||
michael@0 49 *aSVGContext == mSVGContext.ref();
michael@0 50 return mViewportSize == aViewportSize &&
michael@0 51 matchesSVGContext &&
michael@0 52 mFrame == aFrame &&
michael@0 53 mFlags == aFlags;
michael@0 54 }
michael@0 55
michael@0 56 TemporaryRef<SourceSurface> Surface() {
michael@0 57 return mSurface;
michael@0 58 }
michael@0 59
michael@0 60 private:
michael@0 61 RefPtr<SourceSurface> mSurface;
michael@0 62 const nsIntSize mViewportSize;
michael@0 63 Maybe<SVGImageContext> mSVGContext;
michael@0 64 const float mFrame;
michael@0 65 const uint32_t mFlags;
michael@0 66 };
michael@0 67
michael@0 68 class DrawSingleTileCallback : public gfxDrawingCallback
michael@0 69 {
michael@0 70 public:
michael@0 71 DrawSingleTileCallback(ClippedImage* aImage,
michael@0 72 const nsIntRect& aClip,
michael@0 73 const nsIntSize& aViewportSize,
michael@0 74 const SVGImageContext* aSVGContext,
michael@0 75 uint32_t aWhichFrame,
michael@0 76 uint32_t aFlags)
michael@0 77 : mImage(aImage)
michael@0 78 , mClip(aClip)
michael@0 79 , mViewportSize(aViewportSize)
michael@0 80 , mSVGContext(aSVGContext)
michael@0 81 , mWhichFrame(aWhichFrame)
michael@0 82 , mFlags(aFlags)
michael@0 83 {
michael@0 84 MOZ_ASSERT(mImage, "Must have an image to clip");
michael@0 85 }
michael@0 86
michael@0 87 virtual bool operator()(gfxContext* aContext,
michael@0 88 const gfxRect& aFillRect,
michael@0 89 const GraphicsFilter& aFilter,
michael@0 90 const gfxMatrix& aTransform)
michael@0 91 {
michael@0 92 // Draw the image. |gfxCallbackDrawable| always calls this function with
michael@0 93 // arguments that guarantee we never tile.
michael@0 94 mImage->DrawSingleTile(aContext, aFilter, aTransform, aFillRect, mClip,
michael@0 95 mViewportSize, mSVGContext, mWhichFrame, mFlags);
michael@0 96
michael@0 97 return true;
michael@0 98 }
michael@0 99
michael@0 100 private:
michael@0 101 nsRefPtr<ClippedImage> mImage;
michael@0 102 const nsIntRect mClip;
michael@0 103 const nsIntSize mViewportSize;
michael@0 104 const SVGImageContext* mSVGContext;
michael@0 105 const uint32_t mWhichFrame;
michael@0 106 const uint32_t mFlags;
michael@0 107 };
michael@0 108
michael@0 109 ClippedImage::ClippedImage(Image* aImage,
michael@0 110 nsIntRect aClip)
michael@0 111 : ImageWrapper(aImage)
michael@0 112 , mClip(aClip)
michael@0 113 {
michael@0 114 MOZ_ASSERT(aImage != nullptr, "ClippedImage requires an existing Image");
michael@0 115 }
michael@0 116
michael@0 117 ClippedImage::~ClippedImage()
michael@0 118 { }
michael@0 119
michael@0 120 bool
michael@0 121 ClippedImage::ShouldClip()
michael@0 122 {
michael@0 123 // We need to evaluate the clipping region against the image's width and height
michael@0 124 // once they're available to determine if it's valid and whether we actually
michael@0 125 // need to do any work. We may fail if the image's width and height aren't
michael@0 126 // available yet, in which case we'll try again later.
michael@0 127 if (mShouldClip.empty()) {
michael@0 128 int32_t width, height;
michael@0 129 nsRefPtr<imgStatusTracker> innerImageStatusTracker =
michael@0 130 InnerImage()->GetStatusTracker();
michael@0 131 if (InnerImage()->HasError()) {
michael@0 132 // If there's a problem with the inner image we'll let it handle everything.
michael@0 133 mShouldClip.construct(false);
michael@0 134 } else if (NS_SUCCEEDED(InnerImage()->GetWidth(&width)) && width > 0 &&
michael@0 135 NS_SUCCEEDED(InnerImage()->GetHeight(&height)) && height > 0) {
michael@0 136 // Clamp the clipping region to the size of the underlying image.
michael@0 137 mClip = mClip.Intersect(nsIntRect(0, 0, width, height));
michael@0 138
michael@0 139 // If the clipping region is the same size as the underlying image we
michael@0 140 // don't have to do anything.
michael@0 141 mShouldClip.construct(!mClip.IsEqualInterior(nsIntRect(0, 0, width, height)));
michael@0 142 } else if (innerImageStatusTracker &&
michael@0 143 innerImageStatusTracker->IsLoading()) {
michael@0 144 // The image just hasn't finished loading yet. We don't yet know whether
michael@0 145 // clipping with be needed or not for now. Just return without memoizing
michael@0 146 // anything.
michael@0 147 return false;
michael@0 148 } else {
michael@0 149 // We have a fully loaded image without a clearly defined width and
michael@0 150 // height. This can happen with SVG images.
michael@0 151 mShouldClip.construct(false);
michael@0 152 }
michael@0 153 }
michael@0 154
michael@0 155 MOZ_ASSERT(!mShouldClip.empty(), "Should have computed a result");
michael@0 156 return mShouldClip.ref();
michael@0 157 }
michael@0 158
michael@0 159 NS_IMPL_ISUPPORTS(ClippedImage, imgIContainer)
michael@0 160
michael@0 161 nsIntRect
michael@0 162 ClippedImage::FrameRect(uint32_t aWhichFrame)
michael@0 163 {
michael@0 164 if (!ShouldClip()) {
michael@0 165 return InnerImage()->FrameRect(aWhichFrame);
michael@0 166 }
michael@0 167
michael@0 168 return nsIntRect(0, 0, mClip.width, mClip.height);
michael@0 169 }
michael@0 170
michael@0 171 NS_IMETHODIMP
michael@0 172 ClippedImage::GetWidth(int32_t* aWidth)
michael@0 173 {
michael@0 174 if (!ShouldClip()) {
michael@0 175 return InnerImage()->GetWidth(aWidth);
michael@0 176 }
michael@0 177
michael@0 178 *aWidth = mClip.width;
michael@0 179 return NS_OK;
michael@0 180 }
michael@0 181
michael@0 182 NS_IMETHODIMP
michael@0 183 ClippedImage::GetHeight(int32_t* aHeight)
michael@0 184 {
michael@0 185 if (!ShouldClip()) {
michael@0 186 return InnerImage()->GetHeight(aHeight);
michael@0 187 }
michael@0 188
michael@0 189 *aHeight = mClip.height;
michael@0 190 return NS_OK;
michael@0 191 }
michael@0 192
michael@0 193 NS_IMETHODIMP
michael@0 194 ClippedImage::GetIntrinsicSize(nsSize* aSize)
michael@0 195 {
michael@0 196 if (!ShouldClip()) {
michael@0 197 return InnerImage()->GetIntrinsicSize(aSize);
michael@0 198 }
michael@0 199
michael@0 200 *aSize = nsSize(mClip.width, mClip.height);
michael@0 201 return NS_OK;
michael@0 202 }
michael@0 203
michael@0 204 NS_IMETHODIMP
michael@0 205 ClippedImage::GetIntrinsicRatio(nsSize* aRatio)
michael@0 206 {
michael@0 207 if (!ShouldClip()) {
michael@0 208 return InnerImage()->GetIntrinsicRatio(aRatio);
michael@0 209 }
michael@0 210
michael@0 211 *aRatio = nsSize(mClip.width, mClip.height);
michael@0 212 return NS_OK;
michael@0 213 }
michael@0 214
michael@0 215 NS_IMETHODIMP_(TemporaryRef<SourceSurface>)
michael@0 216 ClippedImage::GetFrame(uint32_t aWhichFrame,
michael@0 217 uint32_t aFlags)
michael@0 218 {
michael@0 219 return GetFrameInternal(mClip.Size(), nullptr, aWhichFrame, aFlags);
michael@0 220 }
michael@0 221
michael@0 222 TemporaryRef<SourceSurface>
michael@0 223 ClippedImage::GetFrameInternal(const nsIntSize& aViewportSize,
michael@0 224 const SVGImageContext* aSVGContext,
michael@0 225 uint32_t aWhichFrame,
michael@0 226 uint32_t aFlags)
michael@0 227 {
michael@0 228 if (!ShouldClip()) {
michael@0 229 return InnerImage()->GetFrame(aWhichFrame, aFlags);
michael@0 230 }
michael@0 231
michael@0 232 float frameToDraw = InnerImage()->GetFrameIndex(aWhichFrame);
michael@0 233 if (!mCachedSurface || !mCachedSurface->Matches(aViewportSize,
michael@0 234 aSVGContext,
michael@0 235 frameToDraw,
michael@0 236 aFlags)) {
michael@0 237 // Create a surface to draw into.
michael@0 238 RefPtr<DrawTarget> target = gfxPlatform::GetPlatform()->
michael@0 239 CreateOffscreenContentDrawTarget(IntSize(mClip.width, mClip.height),
michael@0 240 SurfaceFormat::B8G8R8A8);
michael@0 241
michael@0 242 nsRefPtr<gfxContext> ctx = new gfxContext(target);
michael@0 243
michael@0 244 // Create our callback.
michael@0 245 nsRefPtr<gfxDrawingCallback> drawTileCallback =
michael@0 246 new DrawSingleTileCallback(this, mClip, aViewportSize, aSVGContext, aWhichFrame, aFlags);
michael@0 247 nsRefPtr<gfxDrawable> drawable =
michael@0 248 new gfxCallbackDrawable(drawTileCallback, mClip.Size());
michael@0 249
michael@0 250 // Actually draw. The callback will end up invoking DrawSingleTile.
michael@0 251 gfxRect imageRect(0, 0, mClip.width, mClip.height);
michael@0 252 gfxUtils::DrawPixelSnapped(ctx, drawable, gfxMatrix(),
michael@0 253 imageRect, imageRect, imageRect, imageRect,
michael@0 254 gfxImageFormat::ARGB32,
michael@0 255 GraphicsFilter::FILTER_FAST);
michael@0 256
michael@0 257 // Cache the resulting surface.
michael@0 258 mCachedSurface = new ClippedImageCachedSurface(target->Snapshot(),
michael@0 259 aViewportSize,
michael@0 260 aSVGContext,
michael@0 261 frameToDraw,
michael@0 262 aFlags);
michael@0 263 }
michael@0 264
michael@0 265 MOZ_ASSERT(mCachedSurface, "Should have a cached surface now");
michael@0 266 return mCachedSurface->Surface();
michael@0 267 }
michael@0 268
michael@0 269 NS_IMETHODIMP
michael@0 270 ClippedImage::GetImageContainer(LayerManager* aManager, ImageContainer** _retval)
michael@0 271 {
michael@0 272 // XXX(seth): We currently don't have a way of clipping the result of
michael@0 273 // GetImageContainer. We work around this by always returning null, but if it
michael@0 274 // ever turns out that ClippedImage is widely used on codepaths that can
michael@0 275 // actually benefit from GetImageContainer, it would be a good idea to fix
michael@0 276 // that method for performance reasons.
michael@0 277
michael@0 278 if (!ShouldClip()) {
michael@0 279 return InnerImage()->GetImageContainer(aManager, _retval);
michael@0 280 }
michael@0 281
michael@0 282 *_retval = nullptr;
michael@0 283 return NS_OK;
michael@0 284 }
michael@0 285
michael@0 286 bool
michael@0 287 ClippedImage::MustCreateSurface(gfxContext* aContext,
michael@0 288 const gfxMatrix& aTransform,
michael@0 289 const gfxRect& aSourceRect,
michael@0 290 const nsIntRect& aSubimage,
michael@0 291 const uint32_t aFlags) const
michael@0 292 {
michael@0 293 gfxRect gfxImageRect(0, 0, mClip.width, mClip.height);
michael@0 294 nsIntRect intImageRect(0, 0, mClip.width, mClip.height);
michael@0 295 bool willTile = !gfxImageRect.Contains(aSourceRect) &&
michael@0 296 !(aFlags & imgIContainer::FLAG_CLAMP);
michael@0 297 bool willResample = (aContext->CurrentMatrix().HasNonIntegerTranslation() ||
michael@0 298 aTransform.HasNonIntegerTranslation()) &&
michael@0 299 (willTile || !aSubimage.Contains(intImageRect));
michael@0 300 return willTile || willResample;
michael@0 301 }
michael@0 302
michael@0 303 NS_IMETHODIMP
michael@0 304 ClippedImage::Draw(gfxContext* aContext,
michael@0 305 GraphicsFilter aFilter,
michael@0 306 const gfxMatrix& aUserSpaceToImageSpace,
michael@0 307 const gfxRect& aFill,
michael@0 308 const nsIntRect& aSubimage,
michael@0 309 const nsIntSize& aViewportSize,
michael@0 310 const SVGImageContext* aSVGContext,
michael@0 311 uint32_t aWhichFrame,
michael@0 312 uint32_t aFlags)
michael@0 313 {
michael@0 314 if (!ShouldClip()) {
michael@0 315 return InnerImage()->Draw(aContext, aFilter, aUserSpaceToImageSpace,
michael@0 316 aFill, aSubimage, aViewportSize, aSVGContext,
michael@0 317 aWhichFrame, aFlags);
michael@0 318 }
michael@0 319
michael@0 320 // Check for tiling. If we need to tile then we need to create a
michael@0 321 // gfxCallbackDrawable to handle drawing for us.
michael@0 322 gfxRect sourceRect = aUserSpaceToImageSpace.Transform(aFill);
michael@0 323 if (MustCreateSurface(aContext, aUserSpaceToImageSpace, sourceRect, aSubimage, aFlags)) {
michael@0 324 // Create a temporary surface containing a single tile of this image.
michael@0 325 // GetFrame will call DrawSingleTile internally.
michael@0 326 RefPtr<SourceSurface> surface =
michael@0 327 GetFrameInternal(aViewportSize, aSVGContext, aWhichFrame, aFlags);
michael@0 328 NS_ENSURE_TRUE(surface, NS_ERROR_FAILURE);
michael@0 329
michael@0 330 // Create a drawable from that surface.
michael@0 331 nsRefPtr<gfxSurfaceDrawable> drawable =
michael@0 332 new gfxSurfaceDrawable(surface, gfxIntSize(mClip.width, mClip.height));
michael@0 333
michael@0 334 // Draw.
michael@0 335 gfxRect imageRect(0, 0, mClip.width, mClip.height);
michael@0 336 gfxRect subimage(aSubimage.x, aSubimage.y, aSubimage.width, aSubimage.height);
michael@0 337 gfxUtils::DrawPixelSnapped(aContext, drawable, aUserSpaceToImageSpace,
michael@0 338 subimage, sourceRect, imageRect, aFill,
michael@0 339 gfxImageFormat::ARGB32, aFilter);
michael@0 340
michael@0 341 return NS_OK;
michael@0 342 }
michael@0 343
michael@0 344 // Determine the appropriate subimage for the inner image.
michael@0 345 nsIntRect innerSubimage(aSubimage);
michael@0 346 innerSubimage.MoveBy(mClip.x, mClip.y);
michael@0 347 innerSubimage.Intersect(mClip);
michael@0 348
michael@0 349 return DrawSingleTile(aContext, aFilter, aUserSpaceToImageSpace, aFill, innerSubimage,
michael@0 350 aViewportSize, aSVGContext, aWhichFrame, aFlags);
michael@0 351 }
michael@0 352
michael@0 353 gfxFloat
michael@0 354 ClippedImage::ClampFactor(const gfxFloat aToClamp, const int aReference) const
michael@0 355 {
michael@0 356 return aToClamp > aReference ? aReference / aToClamp
michael@0 357 : 1.0;
michael@0 358 }
michael@0 359
michael@0 360 nsresult
michael@0 361 ClippedImage::DrawSingleTile(gfxContext* aContext,
michael@0 362 GraphicsFilter aFilter,
michael@0 363 const gfxMatrix& aUserSpaceToImageSpace,
michael@0 364 const gfxRect& aFill,
michael@0 365 const nsIntRect& aSubimage,
michael@0 366 const nsIntSize& aViewportSize,
michael@0 367 const SVGImageContext* aSVGContext,
michael@0 368 uint32_t aWhichFrame,
michael@0 369 uint32_t aFlags)
michael@0 370 {
michael@0 371 MOZ_ASSERT(!MustCreateSurface(aContext, aUserSpaceToImageSpace,
michael@0 372 aUserSpaceToImageSpace.Transform(aFill),
michael@0 373 aSubimage - nsIntPoint(mClip.x, mClip.y), aFlags),
michael@0 374 "DrawSingleTile shouldn't need to create a surface");
michael@0 375
michael@0 376 // Make the viewport reflect the original image's size.
michael@0 377 nsIntSize viewportSize(aViewportSize);
michael@0 378 int32_t imgWidth, imgHeight;
michael@0 379 if (NS_SUCCEEDED(InnerImage()->GetWidth(&imgWidth)) &&
michael@0 380 NS_SUCCEEDED(InnerImage()->GetHeight(&imgHeight))) {
michael@0 381 viewportSize = nsIntSize(imgWidth, imgHeight);
michael@0 382 } else {
michael@0 383 MOZ_ASSERT(false, "If ShouldClip() led us to draw then we should never get here");
michael@0 384 }
michael@0 385
michael@0 386 // Add a translation to the transform to reflect the clipping region.
michael@0 387 gfxMatrix transform(aUserSpaceToImageSpace);
michael@0 388 transform.Multiply(gfxMatrix().Translate(gfxPoint(mClip.x, mClip.y)));
michael@0 389
michael@0 390 // "Clamp the source rectangle" to the clipping region's width and height.
michael@0 391 // Really, this means modifying the transform to get the results we want.
michael@0 392 gfxRect sourceRect = transform.Transform(aFill);
michael@0 393 if (sourceRect.width > mClip.width || sourceRect.height > mClip.height) {
michael@0 394 gfxMatrix clampSource;
michael@0 395 clampSource.Translate(gfxPoint(sourceRect.x, sourceRect.y));
michael@0 396 clampSource.Scale(ClampFactor(sourceRect.width, mClip.width),
michael@0 397 ClampFactor(sourceRect.height, mClip.height));
michael@0 398 clampSource.Translate(gfxPoint(-sourceRect.x, -sourceRect.y));
michael@0 399 transform.Multiply(clampSource);
michael@0 400 }
michael@0 401
michael@0 402 return InnerImage()->Draw(aContext, aFilter, transform, aFill, aSubimage,
michael@0 403 viewportSize, aSVGContext, aWhichFrame, aFlags);
michael@0 404 }
michael@0 405
michael@0 406 NS_IMETHODIMP
michael@0 407 ClippedImage::RequestDiscard()
michael@0 408 {
michael@0 409 // We're very aggressive about discarding.
michael@0 410 mCachedSurface = nullptr;
michael@0 411
michael@0 412 return InnerImage()->RequestDiscard();
michael@0 413 }
michael@0 414
michael@0 415 NS_IMETHODIMP_(Orientation)
michael@0 416 ClippedImage::GetOrientation()
michael@0 417 {
michael@0 418 // XXX(seth): This should not actually be here; this is just to work around a
michael@0 419 // what appears to be a bug in MSVC's linker.
michael@0 420 return InnerImage()->GetOrientation();
michael@0 421 }
michael@0 422
michael@0 423 } // namespace image
michael@0 424 } // namespace mozilla

mercurial