imgFrame.cpp (23332B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 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/. */ 6 7 #include "imgFrame.h" 8 #include "ImageRegion.h" 9 #include "SurfaceCache.h" 10 11 #include "prenv.h" 12 13 #include "gfx2DGlue.h" 14 #include "gfxContext.h" 15 #include "gfxPlatform.h" 16 17 #include "gfxUtils.h" 18 19 #include "MainThreadUtils.h" 20 #include "mozilla/gfx/Tools.h" 21 #include "mozilla/MemoryReporting.h" 22 #include "mozilla/ProfilerLabels.h" 23 #include "mozilla/StaticPrefs_browser.h" 24 #include "nsMargin.h" 25 #include "nsRefreshDriver.h" 26 #include "nsThreadUtils.h" 27 28 #include <algorithm> // for min, max 29 30 namespace mozilla { 31 32 using namespace gfx; 33 34 namespace image { 35 36 /** 37 * This class is identical to SourceSurfaceSharedData but returns a different 38 * type so that SharedSurfacesChild is aware imagelib wants to recycle this 39 * surface for future animation frames. 40 */ 41 class RecyclingSourceSurfaceSharedData final : public SourceSurfaceSharedData { 42 public: 43 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(RecyclingSourceSurfaceSharedData, 44 override) 45 46 SurfaceType GetType() const override { 47 return SurfaceType::DATA_RECYCLING_SHARED; 48 } 49 }; 50 51 static already_AddRefed<SourceSurfaceSharedData> AllocateBufferForImage( 52 const IntSize& size, SurfaceFormat format, bool aShouldRecycle = false) { 53 // Stride must be a multiple of four or cairo will complain. 54 int32_t stride = (size.width * BytesPerPixel(format) + 0x3) & ~0x3; 55 56 RefPtr<SourceSurfaceSharedData> newSurf; 57 if (aShouldRecycle) { 58 newSurf = new RecyclingSourceSurfaceSharedData(); 59 } else { 60 newSurf = new SourceSurfaceSharedData(); 61 } 62 if (!newSurf->Init(size, stride, format)) { 63 return nullptr; 64 } 65 return newSurf.forget(); 66 } 67 68 static bool GreenSurface(SourceSurfaceSharedData* aSurface, 69 const IntSize& aSize, SurfaceFormat aFormat) { 70 int32_t stride = aSurface->Stride(); 71 uint32_t* surfaceData = reinterpret_cast<uint32_t*>(aSurface->GetData()); 72 uint32_t surfaceDataLength = (stride * aSize.height) / sizeof(uint32_t); 73 74 // Start by assuming that GG is in the second byte and 75 // AA is in the final byte -- the most common case. 76 uint32_t color = mozilla::NativeEndian::swapFromBigEndian(0x00FF00FF); 77 78 // We are only going to handle this type of test under 79 // certain circumstances. 80 MOZ_ASSERT(surfaceData); 81 MOZ_ASSERT(aFormat == SurfaceFormat::B8G8R8A8 || 82 aFormat == SurfaceFormat::B8G8R8X8 || 83 aFormat == SurfaceFormat::R8G8B8A8 || 84 aFormat == SurfaceFormat::R8G8B8X8 || 85 aFormat == SurfaceFormat::A8R8G8B8 || 86 aFormat == SurfaceFormat::X8R8G8B8); 87 MOZ_ASSERT((stride * aSize.height) % sizeof(uint32_t)); 88 89 if (aFormat == SurfaceFormat::A8R8G8B8 || 90 aFormat == SurfaceFormat::X8R8G8B8) { 91 color = mozilla::NativeEndian::swapFromBigEndian(0xFF00FF00); 92 } 93 94 for (uint32_t i = 0; i < surfaceDataLength; i++) { 95 surfaceData[i] = color; 96 } 97 98 return true; 99 } 100 101 static bool ClearSurface(SourceSurfaceSharedData* aSurface, 102 const IntSize& aSize, SurfaceFormat aFormat) { 103 int32_t stride = aSurface->Stride(); 104 uint8_t* data = aSurface->GetData(); 105 MOZ_ASSERT(data); 106 107 if (aFormat == SurfaceFormat::OS_RGBX) { 108 // Skia doesn't support RGBX surfaces, so ensure the alpha value is set 109 // to opaque white. While it would be nice to only do this for Skia, 110 // imgFrame can run off main thread and past shutdown where 111 // we might not have gfxPlatform, so just memset every time instead. 112 memset(data, 0xFF, stride * aSize.height); 113 } else if (aSurface->OnHeap()) { 114 // We only need to memset it if the buffer was allocated on the heap. 115 // Otherwise, it's allocated via mmap and refers to a zeroed page and will 116 // be COW once it's written to. 117 memset(data, 0, stride * aSize.height); 118 } 119 120 return true; 121 } 122 123 imgFrame::imgFrame() 124 : mMonitor("imgFrame"), 125 mDecoded(0, 0, 0, 0), 126 mAborted(false), 127 mFinished(false), 128 mShouldRecycle(false), 129 mTimeout(FrameTimeout::FromRawMilliseconds(100)), 130 mDisposalMethod(DisposalMethod::NOT_SPECIFIED), 131 mBlendMethod(BlendMethod::OVER), 132 mFormat(SurfaceFormat::UNKNOWN), 133 mNonPremult(false) {} 134 135 imgFrame::~imgFrame() { 136 #ifdef DEBUG 137 MonitorAutoLock lock(mMonitor); 138 MOZ_ASSERT(mAborted || AreAllPixelsWritten()); 139 MOZ_ASSERT(mAborted || mFinished); 140 #endif 141 } 142 143 nsresult imgFrame::InitForDecoder(const nsIntSize& aImageSize, 144 SurfaceFormat aFormat, bool aNonPremult, 145 const Maybe<AnimationParams>& aAnimParams, 146 bool aShouldRecycle, 147 uint32_t* aImageDataLength) { 148 // Assert for properties that should be verified by decoders, 149 // warn for properties related to bad content. 150 if (!SurfaceCache::IsLegalSize(aImageSize)) { 151 NS_WARNING("Should have legal image size"); 152 MonitorAutoLock lock(mMonitor); 153 mAborted = true; 154 return NS_ERROR_FAILURE; 155 } 156 157 mImageSize = aImageSize; 158 159 // May be updated shortly after InitForDecoder by BlendAnimationFilter 160 // because it needs to take into consideration the previous frames to 161 // properly calculate. We start with the whole frame as dirty. 162 mDirtyRect = GetRect(); 163 164 if (aAnimParams) { 165 mBlendRect = aAnimParams->mBlendRect; 166 mTimeout = aAnimParams->mTimeout; 167 mBlendMethod = aAnimParams->mBlendMethod; 168 mDisposalMethod = aAnimParams->mDisposalMethod; 169 } else { 170 mBlendRect = GetRect(); 171 } 172 173 if (aShouldRecycle) { 174 // If we are recycling then we should always use BGRA for the underlying 175 // surface because if we use BGRX, the next frame composited into the 176 // surface could be BGRA and cause rendering problems. 177 MOZ_ASSERT(aAnimParams); 178 mFormat = SurfaceFormat::OS_RGBA; 179 } else { 180 mFormat = aFormat; 181 } 182 183 mNonPremult = aNonPremult; 184 185 MonitorAutoLock lock(mMonitor); 186 mShouldRecycle = aShouldRecycle; 187 188 MOZ_ASSERT(!mRawSurface, "Called imgFrame::InitForDecoder() twice?"); 189 190 mRawSurface = AllocateBufferForImage(mImageSize, mFormat, mShouldRecycle); 191 if (!mRawSurface) { 192 mAborted = true; 193 return NS_ERROR_OUT_OF_MEMORY; 194 } 195 196 if (StaticPrefs::browser_measurement_render_anims_and_video_solid() && 197 aAnimParams) { 198 mBlankRawSurface = AllocateBufferForImage(mImageSize, mFormat); 199 if (!mBlankRawSurface) { 200 mAborted = true; 201 return NS_ERROR_OUT_OF_MEMORY; 202 } 203 } 204 205 if (!ClearSurface(mRawSurface, mImageSize, mFormat)) { 206 NS_WARNING("Could not clear allocated buffer"); 207 mAborted = true; 208 return NS_ERROR_OUT_OF_MEMORY; 209 } 210 211 if (mBlankRawSurface) { 212 if (!GreenSurface(mBlankRawSurface, mImageSize, mFormat)) { 213 NS_WARNING("Could not clear allocated blank buffer"); 214 mAborted = true; 215 return NS_ERROR_OUT_OF_MEMORY; 216 } 217 } 218 219 if (aImageDataLength) { 220 *aImageDataLength = GetImageDataLength(); 221 } 222 223 return NS_OK; 224 } 225 226 nsresult imgFrame::InitForDecoderRecycle(const AnimationParams& aAnimParams, 227 uint32_t* aImageDataLength) { 228 // We want to recycle this frame, but there is no guarantee that consumers are 229 // done with it in a timely manner. Let's ensure they are done with it first. 230 MonitorAutoLock lock(mMonitor); 231 232 MOZ_ASSERT(mRawSurface); 233 234 if (!mShouldRecycle) { 235 // This frame either was never marked as recyclable, or the flag was cleared 236 // for a caller which does not support recycling. 237 return NS_ERROR_NOT_AVAILABLE; 238 } 239 240 // Ensure we account for all internal references to the surface. 241 MozRefCountType internalRefs = 1; 242 if (mOptSurface == mRawSurface) { 243 ++internalRefs; 244 } 245 246 if (mRawSurface->refCount() > internalRefs) { 247 if (NS_IsMainThread()) { 248 // We should never be both decoding and recycling on the main thread. Sync 249 // decoding can only be used to produce the first set of frames. Those 250 // either never use recycling because advancing was blocked (main thread 251 // is busy) or we were auto-advancing (to seek to a frame) and the frames 252 // were never accessed (and thus cannot have recycle locks). 253 MOZ_ASSERT_UNREACHABLE("Recycling/decoding on the main thread?"); 254 return NS_ERROR_NOT_AVAILABLE; 255 } 256 257 // We don't want to wait forever to reclaim the frame because we have no 258 // idea why it is still held. It is possibly due to OMTP. Since we are off 259 // the main thread, and we generally have frames already buffered for the 260 // animation, we can afford to wait a short period of time to hopefully 261 // complete the transaction and reclaim the buffer. 262 // 263 // We choose to wait for, at most, the refresh driver interval, so that we 264 // won't skip more than one frame. If the frame is still in use due to 265 // outstanding transactions, we are already skipping frames. If the frame 266 // is still in use for some other purpose, it won't be returned to the pool 267 // and its owner can hold onto it forever without additional impact here. 268 int32_t refreshInterval = 269 std::clamp(nsRefreshDriver::DefaultInterval(), 4, 20); 270 TimeDuration waitInterval = 271 TimeDuration::FromMilliseconds(refreshInterval >> 2); 272 TimeStamp timeout = 273 TimeStamp::Now() + TimeDuration::FromMilliseconds(refreshInterval); 274 while (true) { 275 mMonitor.Wait(waitInterval); 276 if (mRawSurface->refCount() <= internalRefs) { 277 break; 278 } 279 280 if (timeout <= TimeStamp::Now()) { 281 // We couldn't secure the frame for recycling. It will allocate a new 282 // frame instead. 283 return NS_ERROR_NOT_AVAILABLE; 284 } 285 } 286 } 287 288 mBlendRect = aAnimParams.mBlendRect; 289 mTimeout = aAnimParams.mTimeout; 290 mBlendMethod = aAnimParams.mBlendMethod; 291 mDisposalMethod = aAnimParams.mDisposalMethod; 292 mDirtyRect = GetRect(); 293 294 if (aImageDataLength) { 295 *aImageDataLength = GetImageDataLength(); 296 } 297 298 return NS_OK; 299 } 300 301 nsresult imgFrame::InitWithDrawable(gfxDrawable* aDrawable, 302 const nsIntSize& aSize, 303 const SurfaceFormat aFormat, 304 SamplingFilter aSamplingFilter, 305 uint32_t aImageFlags, 306 gfx::BackendType aBackend) { 307 // Assert for properties that should be verified by decoders, 308 // warn for properties related to bad content. 309 if (!SurfaceCache::IsLegalSize(aSize)) { 310 NS_WARNING("Should have legal image size"); 311 MonitorAutoLock lock(mMonitor); 312 mAborted = true; 313 return NS_ERROR_FAILURE; 314 } 315 316 mImageSize = aSize; 317 mFormat = aFormat; 318 319 RefPtr<DrawTarget> target; 320 321 bool canUseDataSurface = Factory::DoesBackendSupportDataDrawtarget(aBackend); 322 if (canUseDataSurface) { 323 MonitorAutoLock lock(mMonitor); 324 // It's safe to use data surfaces for content on this platform, so we can 325 // get away with using volatile buffers. 326 MOZ_ASSERT(!mRawSurface, "Called imgFrame::InitWithDrawable() twice?"); 327 328 mRawSurface = AllocateBufferForImage(mImageSize, mFormat); 329 if (!mRawSurface) { 330 mAborted = true; 331 return NS_ERROR_OUT_OF_MEMORY; 332 } 333 334 if (!ClearSurface(mRawSurface, mImageSize, mFormat)) { 335 NS_WARNING("Could not clear allocated buffer"); 336 mAborted = true; 337 return NS_ERROR_OUT_OF_MEMORY; 338 } 339 340 target = gfxPlatform::CreateDrawTargetForData( 341 mRawSurface->GetData(), mImageSize, mRawSurface->Stride(), mFormat); 342 } else { 343 // We can't use data surfaces for content, so we'll create an offscreen 344 // surface instead. This means if someone later calls RawAccessRef(), we 345 // may have to do an expensive readback, but we warned callers about that in 346 // the documentation for this method. 347 #ifdef DEBUG 348 { 349 MonitorAutoLock lock(mMonitor); 350 MOZ_ASSERT(!mOptSurface, "Called imgFrame::InitWithDrawable() twice?"); 351 } 352 #endif 353 354 if (gfxPlatform::GetPlatform()->SupportsAzureContentForType(aBackend)) { 355 target = gfxPlatform::GetPlatform()->CreateDrawTargetForBackend( 356 aBackend, mImageSize, mFormat); 357 } else { 358 target = gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget( 359 mImageSize, mFormat); 360 } 361 } 362 363 if (!target || !target->IsValid()) { 364 MonitorAutoLock lock(mMonitor); 365 mAborted = true; 366 return NS_ERROR_OUT_OF_MEMORY; 367 } 368 369 // Draw using the drawable the caller provided. 370 gfxContext ctx(target); 371 372 gfxUtils::DrawPixelSnapped(&ctx, aDrawable, SizeDouble(mImageSize), 373 ImageRegion::Create(ThebesRect(GetRect())), 374 mFormat, aSamplingFilter, aImageFlags); 375 376 MonitorAutoLock lock(mMonitor); 377 if (canUseDataSurface && !mRawSurface) { 378 NS_WARNING("Failed to create SourceSurfaceSharedData"); 379 mAborted = true; 380 return NS_ERROR_OUT_OF_MEMORY; 381 } 382 383 if (!canUseDataSurface) { 384 // We used an offscreen surface, which is an "optimized" surface from 385 // imgFrame's perspective. 386 mOptSurface = target->Snapshot(); 387 } else { 388 FinalizeSurfaceInternal(); 389 } 390 391 // If we reach this point, we should regard ourselves as complete. 392 mDecoded = GetRect(); 393 mFinished = true; 394 395 MOZ_ASSERT(AreAllPixelsWritten()); 396 397 return NS_OK; 398 } 399 400 DrawableFrameRef imgFrame::DrawableRef() { return DrawableFrameRef(this); } 401 402 RawAccessFrameRef imgFrame::RawAccessRef( 403 gfx::DataSourceSurface::MapType aMapType) { 404 return RawAccessFrameRef(this, aMapType); 405 } 406 407 imgFrame::SurfaceWithFormat imgFrame::SurfaceForDrawing( 408 bool aDoPartialDecode, bool aDoTile, ImageRegion& aRegion, 409 SourceSurface* aSurface) { 410 MOZ_ASSERT(NS_IsMainThread()); 411 mMonitor.AssertCurrentThreadOwns(); 412 413 if (!aDoPartialDecode) { 414 return SurfaceWithFormat(new gfxSurfaceDrawable(aSurface, mImageSize), 415 mFormat); 416 } 417 418 gfxRect available = 419 gfxRect(mDecoded.X(), mDecoded.Y(), mDecoded.Width(), mDecoded.Height()); 420 421 if (aDoTile) { 422 // Create a temporary surface. 423 // Give this surface an alpha channel because there are 424 // transparent pixels in the padding or undecoded area 425 RefPtr<DrawTarget> target = 426 gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget( 427 mImageSize, SurfaceFormat::OS_RGBA); 428 if (!target) { 429 return SurfaceWithFormat(); 430 } 431 432 SurfacePattern pattern(aSurface, aRegion.GetExtendMode(), 433 Matrix::Translation(mDecoded.X(), mDecoded.Y())); 434 target->FillRect(ToRect(aRegion.Intersect(available).Rect()), pattern); 435 436 RefPtr<SourceSurface> newsurf = target->Snapshot(); 437 return SurfaceWithFormat(new gfxSurfaceDrawable(newsurf, mImageSize), 438 target->GetFormat()); 439 } 440 441 // Not tiling, and we have a surface, so we can account for 442 // a partial decode just by twiddling parameters. 443 aRegion = aRegion.Intersect(available); 444 IntSize availableSize(mDecoded.Width(), mDecoded.Height()); 445 446 return SurfaceWithFormat(new gfxSurfaceDrawable(aSurface, availableSize), 447 mFormat); 448 } 449 450 bool imgFrame::Draw(gfxContext* aContext, const ImageRegion& aRegion, 451 SamplingFilter aSamplingFilter, uint32_t aImageFlags, 452 float aOpacity) { 453 AUTO_PROFILER_LABEL("imgFrame::Draw", GRAPHICS); 454 455 MOZ_ASSERT(NS_IsMainThread()); 456 NS_ASSERTION(!aRegion.Rect().IsEmpty(), "Drawing empty region!"); 457 NS_ASSERTION(!aRegion.IsRestricted() || 458 !aRegion.Rect().Intersect(aRegion.Restriction()).IsEmpty(), 459 "We must be allowed to sample *some* source pixels!"); 460 461 // Perform the draw and freeing of the surface outside the lock. We want to 462 // avoid contention with the decoder if we can. The surface may also attempt 463 // to relock the monitor if it is freed (e.g. RecyclingSourceSurface). 464 RefPtr<SourceSurface> surf; 465 SurfaceWithFormat surfaceResult; 466 ImageRegion region(aRegion); 467 gfxRect imageRect(0, 0, mImageSize.width, mImageSize.height); 468 469 { 470 MonitorAutoLock lock(mMonitor); 471 472 bool doPartialDecode = !AreAllPixelsWritten(); 473 474 // Most draw targets will just use the surface only during DrawPixelSnapped 475 // but captures/recordings will retain a reference outside this stack 476 // context. While in theory a decoder thread could be trying to recycle this 477 // frame at this very moment, in practice the only way we can get here is if 478 // this frame is the current frame of the animation. Since we can only 479 // advance on the main thread, we know nothing else will try to use it. 480 DrawTarget* drawTarget = aContext->GetDrawTarget(); 481 bool recording = drawTarget->GetBackendType() == BackendType::RECORDING; 482 RefPtr<SourceSurface> surf = GetSourceSurfaceInternal(); 483 if (!surf) { 484 return false; 485 } 486 487 bool doTile = !imageRect.Contains(aRegion.Rect()) && 488 !(aImageFlags & imgIContainer::FLAG_CLAMP); 489 490 surfaceResult = SurfaceForDrawing(doPartialDecode, doTile, region, surf); 491 492 // If we are recording, then we cannot recycle the surface. The blob 493 // rasterizer is not properly synchronized for recycling in the compositor 494 // process. The easiest thing to do is just mark the frames it consumes as 495 // non-recyclable. 496 if (recording && surfaceResult.IsValid()) { 497 mShouldRecycle = false; 498 } 499 } 500 501 if (surfaceResult.IsValid()) { 502 gfxUtils::DrawPixelSnapped(aContext, surfaceResult.mDrawable, 503 imageRect.Size(), region, surfaceResult.mFormat, 504 aSamplingFilter, aImageFlags, aOpacity); 505 } 506 507 return true; 508 } 509 510 nsresult imgFrame::ImageUpdated(const nsIntRect& aUpdateRect) { 511 MonitorAutoLock lock(mMonitor); 512 return ImageUpdatedInternal(aUpdateRect); 513 } 514 515 nsresult imgFrame::ImageUpdatedInternal(const nsIntRect& aUpdateRect) { 516 mMonitor.AssertCurrentThreadOwns(); 517 518 // Clamp to the frame rect to ensure that decoder bugs don't result in a 519 // decoded rect that extends outside the bounds of the frame rect. 520 IntRect updateRect = aUpdateRect.Intersect(GetRect()); 521 if (updateRect.IsEmpty()) { 522 return NS_OK; 523 } 524 525 mDecoded.UnionRect(mDecoded, updateRect); 526 527 // Update our invalidation counters for any consumers watching for changes 528 // in the surface. 529 if (mRawSurface) { 530 mRawSurface->Invalidate(updateRect); 531 } 532 return NS_OK; 533 } 534 535 void imgFrame::Finish(Opacity aFrameOpacity /* = Opacity::SOME_TRANSPARENCY */, 536 bool aFinalize /* = true */, 537 bool aOrientationSwapsWidthAndHeight /* = false */) { 538 MonitorAutoLock lock(mMonitor); 539 540 IntRect frameRect(GetRect()); 541 if (!mDecoded.IsEqualEdges(frameRect)) { 542 // The decoder should have produced rows starting from either the bottom or 543 // the top of the image. We need to calculate the region for which we have 544 // not yet invalidated. And if the orientation swaps width and height then 545 // its from the left or right. 546 IntRect delta(0, 0, frameRect.width, 0); 547 if (!aOrientationSwapsWidthAndHeight) { 548 delta.width = frameRect.width; 549 if (mDecoded.y == 0) { 550 delta.y = mDecoded.height; 551 delta.height = frameRect.height - mDecoded.height; 552 } else if (mDecoded.y + mDecoded.height == frameRect.height) { 553 delta.height = frameRect.height - mDecoded.y; 554 } else { 555 MOZ_ASSERT_UNREACHABLE("Decoder only updated middle of image!"); 556 delta = frameRect; 557 } 558 } else { 559 delta.height = frameRect.height; 560 if (mDecoded.x == 0) { 561 delta.x = mDecoded.width; 562 delta.width = frameRect.width - mDecoded.width; 563 } else if (mDecoded.x + mDecoded.width == frameRect.width) { 564 delta.width = frameRect.width - mDecoded.x; 565 } else { 566 MOZ_ASSERT_UNREACHABLE("Decoder only updated middle of image!"); 567 delta = frameRect; 568 } 569 } 570 571 ImageUpdatedInternal(delta); 572 } 573 574 MOZ_ASSERT(mDecoded.IsEqualEdges(frameRect)); 575 576 if (aFinalize) { 577 FinalizeSurfaceInternal(); 578 } 579 580 mFinished = true; 581 582 // The image is now complete, wake up anyone who's waiting. 583 mMonitor.NotifyAll(); 584 } 585 586 uint32_t imgFrame::GetImageBytesPerRow() const { 587 mMonitor.AssertCurrentThreadOwns(); 588 589 if (mRawSurface) { 590 return mImageSize.width * BytesPerPixel(mFormat); 591 } 592 593 return 0; 594 } 595 596 uint32_t imgFrame::GetImageDataLength() const { 597 return GetImageBytesPerRow() * mImageSize.height; 598 } 599 600 void imgFrame::FinalizeSurface() { 601 MonitorAutoLock lock(mMonitor); 602 FinalizeSurfaceInternal(); 603 } 604 605 void imgFrame::FinalizeSurfaceInternal() { 606 mMonitor.AssertCurrentThreadOwns(); 607 608 // Not all images will have mRawSurface to finalize (i.e. paletted images). 609 if (mShouldRecycle || !mRawSurface || 610 mRawSurface->GetType() != SurfaceType::DATA_SHARED) { 611 return; 612 } 613 614 auto* sharedSurf = static_cast<SourceSurfaceSharedData*>(mRawSurface.get()); 615 sharedSurf->Finalize(); 616 } 617 618 already_AddRefed<SourceSurface> imgFrame::GetSourceSurface() { 619 MonitorAutoLock lock(mMonitor); 620 return GetSourceSurfaceInternal(); 621 } 622 623 already_AddRefed<SourceSurface> imgFrame::GetSourceSurfaceInternal() { 624 mMonitor.AssertCurrentThreadOwns(); 625 626 if (mOptSurface) { 627 if (mOptSurface->IsValid()) { 628 RefPtr<SourceSurface> surf(mOptSurface); 629 return surf.forget(); 630 } 631 mOptSurface = nullptr; 632 } 633 634 if (mBlankRawSurface) { 635 // We are going to return the blank surface because of the flags. 636 // We are including comments here that are copied from below 637 // just so that we are on the same page! 638 RefPtr<SourceSurface> surf(mBlankRawSurface); 639 return surf.forget(); 640 } 641 642 RefPtr<SourceSurface> surf(mRawSurface); 643 return surf.forget(); 644 } 645 646 void imgFrame::Abort() { 647 MonitorAutoLock lock(mMonitor); 648 649 mAborted = true; 650 651 // Wake up anyone who's waiting. 652 mMonitor.NotifyAll(); 653 } 654 655 bool imgFrame::IsAborted() const { 656 MonitorAutoLock lock(mMonitor); 657 return mAborted; 658 } 659 660 bool imgFrame::IsFinished() const { 661 MonitorAutoLock lock(mMonitor); 662 return mFinished; 663 } 664 665 void imgFrame::WaitUntilFinished() const { 666 MonitorAutoLock lock(mMonitor); 667 668 while (true) { 669 // Return if we're aborted or complete. 670 if (mAborted || mFinished) { 671 return; 672 } 673 674 // Not complete yet, so we'll have to wait. 675 mMonitor.Wait(); 676 } 677 } 678 679 bool imgFrame::AreAllPixelsWritten() const { 680 mMonitor.AssertCurrentThreadOwns(); 681 return mDecoded.IsEqualInterior(GetRect()); 682 } 683 684 void imgFrame::AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf, 685 const AddSizeOfCb& aCallback) const { 686 MonitorAutoLock lock(mMonitor); 687 688 AddSizeOfCbData metadata; 689 metadata.mFinished = mFinished; 690 691 if (mOptSurface) { 692 metadata.mHeapBytes += aMallocSizeOf(mOptSurface); 693 694 SourceSurface::SizeOfInfo info; 695 mOptSurface->SizeOfExcludingThis(aMallocSizeOf, info); 696 metadata.Accumulate(info); 697 } 698 if (mRawSurface) { 699 metadata.mHeapBytes += aMallocSizeOf(mRawSurface); 700 701 SourceSurface::SizeOfInfo info; 702 mRawSurface->SizeOfExcludingThis(aMallocSizeOf, info); 703 metadata.Accumulate(info); 704 } 705 706 aCallback(metadata); 707 } 708 709 } // namespace image 710 } // namespace mozilla