summaryrefslogtreecommitdiffstats
path: root/image/imgFrame.cpp
blob: 4ff6dcec197733d03e50eade0471834473bece7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "imgFrame.h"
#include "ImageRegion.h"
#include "SurfaceCache.h"

#include "prenv.h"

#include "gfx2DGlue.h"
#include "gfxContext.h"
#include "gfxPlatform.h"

#include "gfxUtils.h"

#include "MainThreadUtils.h"
#include "mozilla/CheckedInt.h"
#include "mozilla/gfx/Tools.h"
#include "mozilla/Likely.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/ProfilerLabels.h"
#include "mozilla/StaticPrefs_browser.h"
#include "nsMargin.h"
#include "nsRefreshDriver.h"
#include "nsThreadUtils.h"

#include <algorithm>  // for min, max

namespace mozilla {

using namespace gfx;

namespace image {

/**
 * This class is identical to SourceSurfaceSharedData but returns a different
 * type so that SharedSurfacesChild is aware imagelib wants to recycle this
 * surface for future animation frames.
 */
class RecyclingSourceSurfaceSharedData final : public SourceSurfaceSharedData {
 public:
  MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(RecyclingSourceSurfaceSharedData,
                                          override)

  SurfaceType GetType() const override {
    return SurfaceType::DATA_RECYCLING_SHARED;
  }
};

static already_AddRefed<SourceSurfaceSharedData> AllocateBufferForImage(
    const IntSize& size, SurfaceFormat format, bool aShouldRecycle = false) {
  // Stride must be a multiple of four or cairo will complain.
  int32_t stride = (size.width * BytesPerPixel(format) + 0x3) & ~0x3;

  RefPtr<SourceSurfaceSharedData> newSurf;
  if (aShouldRecycle) {
    newSurf = new RecyclingSourceSurfaceSharedData();
  } else {
    newSurf = new SourceSurfaceSharedData();
  }
  if (!newSurf->Init(size, stride, format)) {
    return nullptr;
  }
  return newSurf.forget();
}

static bool GreenSurface(SourceSurfaceSharedData* aSurface,
                         const IntSize& aSize, SurfaceFormat aFormat) {
  int32_t stride = aSurface->Stride();
  uint32_t* surfaceData = reinterpret_cast<uint32_t*>(aSurface->GetData());
  uint32_t surfaceDataLength = (stride * aSize.height) / sizeof(uint32_t);

  // Start by assuming that GG is in the second byte and
  // AA is in the final byte -- the most common case.
  uint32_t color = mozilla::NativeEndian::swapFromBigEndian(0x00FF00FF);

  // We are only going to handle this type of test under
  // certain circumstances.
  MOZ_ASSERT(surfaceData);
  MOZ_ASSERT(aFormat == SurfaceFormat::B8G8R8A8 ||
             aFormat == SurfaceFormat::B8G8R8X8 ||
             aFormat == SurfaceFormat::R8G8B8A8 ||
             aFormat == SurfaceFormat::R8G8B8X8 ||
             aFormat == SurfaceFormat::A8R8G8B8 ||
             aFormat == SurfaceFormat::X8R8G8B8);
  MOZ_ASSERT((stride * aSize.height) % sizeof(uint32_t));

  if (aFormat == SurfaceFormat::A8R8G8B8 ||
      aFormat == SurfaceFormat::X8R8G8B8) {
    color = mozilla::NativeEndian::swapFromBigEndian(0xFF00FF00);
  }

  for (uint32_t i = 0; i < surfaceDataLength; i++) {
    surfaceData[i] = color;
  }

  return true;
}

static bool ClearSurface(SourceSurfaceSharedData* aSurface,
                         const IntSize& aSize, SurfaceFormat aFormat) {
  int32_t stride = aSurface->Stride();
  uint8_t* data = aSurface->GetData();
  MOZ_ASSERT(data);

  if (aFormat == SurfaceFormat::OS_RGBX) {
    // Skia doesn't support RGBX surfaces, so ensure the alpha value is set
    // to opaque white. While it would be nice to only do this for Skia,
    // imgFrame can run off main thread and past shutdown where
    // we might not have gfxPlatform, so just memset every time instead.
    memset(data, 0xFF, stride * aSize.height);
  } else if (aSurface->OnHeap()) {
    // We only need to memset it if the buffer was allocated on the heap.
    // Otherwise, it's allocated via mmap and refers to a zeroed page and will
    // be COW once it's written to.
    memset(data, 0, stride * aSize.height);
  }

  return true;
}

imgFrame::imgFrame()
    : mMonitor("imgFrame"),
      mDecoded(0, 0, 0, 0),
      mAborted(false),
      mFinished(false),
      mShouldRecycle(false),
      mTimeout(FrameTimeout::FromRawMilliseconds(100)),
      mDisposalMethod(DisposalMethod::NOT_SPECIFIED),
      mBlendMethod(BlendMethod::OVER),
      mFormat(SurfaceFormat::UNKNOWN),
      mNonPremult(false) {}

imgFrame::~imgFrame() {
#ifdef DEBUG
  MonitorAutoLock lock(mMonitor);
  MOZ_ASSERT(mAborted || AreAllPixelsWritten());
  MOZ_ASSERT(mAborted || mFinished);
#endif
}

nsresult imgFrame::InitForDecoder(const nsIntSize& aImageSize,
                                  SurfaceFormat aFormat, bool aNonPremult,
                                  const Maybe<AnimationParams>& aAnimParams,
                                  bool aShouldRecycle) {
  // Assert for properties that should be verified by decoders,
  // warn for properties related to bad content.
  if (!SurfaceCache::IsLegalSize(aImageSize)) {
    NS_WARNING("Should have legal image size");
    MonitorAutoLock lock(mMonitor);
    mAborted = true;
    return NS_ERROR_FAILURE;
  }

  mImageSize = aImageSize;

  // May be updated shortly after InitForDecoder by BlendAnimationFilter
  // because it needs to take into consideration the previous frames to
  // properly calculate. We start with the whole frame as dirty.
  mDirtyRect = GetRect();

  if (aAnimParams) {
    mBlendRect = aAnimParams->mBlendRect;
    mTimeout = aAnimParams->mTimeout;
    mBlendMethod = aAnimParams->mBlendMethod;
    mDisposalMethod = aAnimParams->mDisposalMethod;
  } else {
    mBlendRect = GetRect();
  }

  if (aShouldRecycle) {
    // If we are recycling then we should always use BGRA for the underlying
    // surface because if we use BGRX, the next frame composited into the
    // surface could be BGRA and cause rendering problems.
    MOZ_ASSERT(aAnimParams);
    mFormat = SurfaceFormat::OS_RGBA;
  } else {
    mFormat = aFormat;
  }

  mNonPremult = aNonPremult;

  MonitorAutoLock lock(mMonitor);
  mShouldRecycle = aShouldRecycle;

  MOZ_ASSERT(!mRawSurface, "Called imgFrame::InitForDecoder() twice?");

  mRawSurface = AllocateBufferForImage(mImageSize, mFormat, mShouldRecycle);
  if (!mRawSurface) {
    mAborted = true;
    return NS_ERROR_OUT_OF_MEMORY;
  }

  if (StaticPrefs::browser_measurement_render_anims_and_video_solid() &&
      aAnimParams) {
    mBlankRawSurface = AllocateBufferForImage(mImageSize, mFormat);
    if (!mBlankRawSurface) {
      mAborted = true;
      return NS_ERROR_OUT_OF_MEMORY;
    }
  }

  if (!ClearSurface(mRawSurface, mImageSize, mFormat)) {
    NS_WARNING("Could not clear allocated buffer");
    mAborted = true;
    return NS_ERROR_OUT_OF_MEMORY;
  }

  if (mBlankRawSurface) {
    if (!GreenSurface(mBlankRawSurface, mImageSize, mFormat)) {
      NS_WARNING("Could not clear allocated blank buffer");
      mAborted = true;
      return NS_ERROR_OUT_OF_MEMORY;
    }
  }

  return NS_OK;
}

nsresult imgFrame::InitForDecoderRecycle(const AnimationParams& aAnimParams) {
  // We want to recycle this frame, but there is no guarantee that consumers are
  // done with it in a timely manner. Let's ensure they are done with it first.
  MonitorAutoLock lock(mMonitor);

  MOZ_ASSERT(mRawSurface);

  if (!mShouldRecycle) {
    // This frame either was never marked as recyclable, or the flag was cleared
    // for a caller which does not support recycling.
    return NS_ERROR_NOT_AVAILABLE;
  }

  // Ensure we account for all internal references to the surface.
  MozRefCountType internalRefs = 1;
  if (mOptSurface == mRawSurface) {
    ++internalRefs;
  }

  if (mRawSurface->refCount() > internalRefs) {
    if (NS_IsMainThread()) {
      // We should never be both decoding and recycling on the main thread. Sync
      // decoding can only be used to produce the first set of frames. Those
      // either never use recycling because advancing was blocked (main thread
      // is busy) or we were auto-advancing (to seek to a frame) and the frames
      // were never accessed (and thus cannot have recycle locks).
      MOZ_ASSERT_UNREACHABLE("Recycling/decoding on the main thread?");
      return NS_ERROR_NOT_AVAILABLE;
    }

    // We don't want to wait forever to reclaim the frame because we have no
    // idea why it is still held. It is possibly due to OMTP. Since we are off
    // the main thread, and we generally have frames already buffered for the
    // animation, we can afford to wait a short period of time to hopefully
    // complete the transaction and reclaim the buffer.
    //
    // We choose to wait for, at most, the refresh driver interval, so that we
    // won't skip more than one frame. If the frame is still in use due to
    // outstanding transactions, we are already skipping frames. If the frame
    // is still in use for some other purpose, it won't be returned to the pool
    // and its owner can hold onto it forever without additional impact here.
    int32_t refreshInterval =
        std::max(std::min(nsRefreshDriver::DefaultInterval(), 20), 4);
    TimeDuration waitInterval =
        TimeDuration::FromMilliseconds(refreshInterval >> 2);
    TimeStamp timeout =
        TimeStamp::Now() + TimeDuration::FromMilliseconds(refreshInterval);
    while (true) {
      mMonitor.Wait(waitInterval);
      if (mRawSurface->refCount() <= internalRefs) {
        break;
      }

      if (timeout <= TimeStamp::Now()) {
        // We couldn't secure the frame for recycling. It will allocate a new
        // frame instead.
        return NS_ERROR_NOT_AVAILABLE;
      }
    }
  }

  mBlendRect = aAnimParams.mBlendRect;
  mTimeout = aAnimParams.mTimeout;
  mBlendMethod = aAnimParams.mBlendMethod;
  mDisposalMethod = aAnimParams.mDisposalMethod;
  mDirtyRect = GetRect();

  return NS_OK;
}

nsresult imgFrame::InitWithDrawable(gfxDrawable* aDrawable,
                                    const nsIntSize& aSize,
                                    const SurfaceFormat aFormat,
                                    SamplingFilter aSamplingFilter,
                                    uint32_t aImageFlags,
                                    gfx::BackendType aBackend) {
  // Assert for properties that should be verified by decoders,
  // warn for properties related to bad content.
  if (!SurfaceCache::IsLegalSize(aSize)) {
    NS_WARNING("Should have legal image size");
    MonitorAutoLock lock(mMonitor);
    mAborted = true;
    return NS_ERROR_FAILURE;
  }

  mImageSize = aSize;
  mFormat = aFormat;

  RefPtr<DrawTarget> target;

  bool canUseDataSurface = Factory::DoesBackendSupportDataDrawtarget(aBackend);
  if (canUseDataSurface) {
    MonitorAutoLock lock(mMonitor);
    // It's safe to use data surfaces for content on this platform, so we can
    // get away with using volatile buffers.
    MOZ_ASSERT(!mRawSurface, "Called imgFrame::InitWithDrawable() twice?");

    mRawSurface = AllocateBufferForImage(mImageSize, mFormat);
    if (!mRawSurface) {
      mAborted = true;
      return NS_ERROR_OUT_OF_MEMORY;
    }

    if (!ClearSurface(mRawSurface, mImageSize, mFormat)) {
      NS_WARNING("Could not clear allocated buffer");
      mAborted = true;
      return NS_ERROR_OUT_OF_MEMORY;
    }

    target = gfxPlatform::CreateDrawTargetForData(
        mRawSurface->GetData(), mImageSize, mRawSurface->Stride(), mFormat);
  } else {
    // We can't use data surfaces for content, so we'll create an offscreen
    // surface instead.  This means if someone later calls RawAccessRef(), we
    // may have to do an expensive readback, but we warned callers about that in
    // the documentation for this method.
#ifdef DEBUG
    {
      MonitorAutoLock lock(mMonitor);
      MOZ_ASSERT(!mOptSurface, "Called imgFrame::InitWithDrawable() twice?");
    }
#endif

    if (gfxPlatform::GetPlatform()->SupportsAzureContentForType(aBackend)) {
      target = gfxPlatform::GetPlatform()->CreateDrawTargetForBackend(
          aBackend, mImageSize, mFormat);
    } else {
      target = gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
          mImageSize, mFormat);
    }
  }

  if (!target || !target->IsValid()) {
    MonitorAutoLock lock(mMonitor);
    mAborted = true;
    return NS_ERROR_OUT_OF_MEMORY;
  }

  // Draw using the drawable the caller provided.
  RefPtr<gfxContext> ctx = gfxContext::CreateOrNull(target);
  MOZ_ASSERT(ctx);  // Already checked the draw target above.
  gfxUtils::DrawPixelSnapped(ctx, aDrawable, SizeDouble(mImageSize),
                             ImageRegion::Create(ThebesRect(GetRect())),
                             mFormat, aSamplingFilter, aImageFlags);

  MonitorAutoLock lock(mMonitor);
  if (canUseDataSurface && !mRawSurface) {
    NS_WARNING("Failed to create SourceSurfaceSharedData");
    mAborted = true;
    return NS_ERROR_OUT_OF_MEMORY;
  }

  if (!canUseDataSurface) {
    // We used an offscreen surface, which is an "optimized" surface from
    // imgFrame's perspective.
    mOptSurface = target->Snapshot();
  } else {
    FinalizeSurfaceInternal();
  }

  // If we reach this point, we should regard ourselves as complete.
  mDecoded = GetRect();
  mFinished = true;

  MOZ_ASSERT(AreAllPixelsWritten());

  return NS_OK;
}

DrawableFrameRef imgFrame::DrawableRef() { return DrawableFrameRef(this); }

RawAccessFrameRef imgFrame::RawAccessRef() { return RawAccessFrameRef(this); }

imgFrame::SurfaceWithFormat imgFrame::SurfaceForDrawing(
    bool aDoPartialDecode, bool aDoTile, ImageRegion& aRegion,
    SourceSurface* aSurface) {
  MOZ_ASSERT(NS_IsMainThread());
  mMonitor.AssertCurrentThreadOwns();

  if (!aDoPartialDecode) {
    return SurfaceWithFormat(new gfxSurfaceDrawable(aSurface, mImageSize),
                             mFormat);
  }

  gfxRect available =
      gfxRect(mDecoded.X(), mDecoded.Y(), mDecoded.Width(), mDecoded.Height());

  if (aDoTile) {
    // Create a temporary surface.
    // Give this surface an alpha channel because there are
    // transparent pixels in the padding or undecoded area
    RefPtr<DrawTarget> target =
        gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
            mImageSize, SurfaceFormat::OS_RGBA);
    if (!target) {
      return SurfaceWithFormat();
    }

    SurfacePattern pattern(aSurface, aRegion.GetExtendMode(),
                           Matrix::Translation(mDecoded.X(), mDecoded.Y()));
    target->FillRect(ToRect(aRegion.Intersect(available).Rect()), pattern);

    RefPtr<SourceSurface> newsurf = target->Snapshot();
    return SurfaceWithFormat(new gfxSurfaceDrawable(newsurf, mImageSize),
                             target->GetFormat());
  }

  // Not tiling, and we have a surface, so we can account for
  // a partial decode just by twiddling parameters.
  aRegion = aRegion.Intersect(available);
  IntSize availableSize(mDecoded.Width(), mDecoded.Height());

  return SurfaceWithFormat(new gfxSurfaceDrawable(aSurface, availableSize),
                           mFormat);
}

bool imgFrame::Draw(gfxContext* aContext, const ImageRegion& aRegion,
                    SamplingFilter aSamplingFilter, uint32_t aImageFlags,
                    float aOpacity) {
  AUTO_PROFILER_LABEL("imgFrame::Draw", GRAPHICS);

  MOZ_ASSERT(NS_IsMainThread());
  NS_ASSERTION(!aRegion.Rect().IsEmpty(), "Drawing empty region!");
  NS_ASSERTION(!aRegion.IsRestricted() ||
                   !aRegion.Rect().Intersect(aRegion.Restriction()).IsEmpty(),
               "We must be allowed to sample *some* source pixels!");

  // Perform the draw and freeing of the surface outside the lock. We want to
  // avoid contention with the decoder if we can. The surface may also attempt
  // to relock the monitor if it is freed (e.g. RecyclingSourceSurface).
  RefPtr<SourceSurface> surf;
  SurfaceWithFormat surfaceResult;
  ImageRegion region(aRegion);
  gfxRect imageRect(0, 0, mImageSize.width, mImageSize.height);

  {
    MonitorAutoLock lock(mMonitor);

    bool doPartialDecode = !AreAllPixelsWritten();

    // Most draw targets will just use the surface only during DrawPixelSnapped
    // but captures/recordings will retain a reference outside this stack
    // context. While in theory a decoder thread could be trying to recycle this
    // frame at this very moment, in practice the only way we can get here is if
    // this frame is the current frame of the animation. Since we can only
    // advance on the main thread, we know nothing else will try to use it.
    DrawTarget* drawTarget = aContext->GetDrawTarget();
    bool recording = drawTarget->GetBackendType() == BackendType::RECORDING;
    RefPtr<SourceSurface> surf = GetSourceSurfaceInternal();
    if (!surf) {
      return false;
    }

    bool doTile = !imageRect.Contains(aRegion.Rect()) &&
                  !(aImageFlags & imgIContainer::FLAG_CLAMP);

    surfaceResult = SurfaceForDrawing(doPartialDecode, doTile, region, surf);

    // If we are recording, then we cannot recycle the surface. The blob
    // rasterizer is not properly synchronized for recycling in the compositor
    // process. The easiest thing to do is just mark the frames it consumes as
    // non-recyclable.
    if (recording && surfaceResult.IsValid()) {
      mShouldRecycle = false;
    }
  }

  if (surfaceResult.IsValid()) {
    gfxUtils::DrawPixelSnapped(aContext, surfaceResult.mDrawable,
                               imageRect.Size(), region, surfaceResult.mFormat,
                               aSamplingFilter, aImageFlags, aOpacity);
  }

  return true;
}

nsresult imgFrame::ImageUpdated(const nsIntRect& aUpdateRect) {
  MonitorAutoLock lock(mMonitor);
  return ImageUpdatedInternal(aUpdateRect);
}

nsresult imgFrame::ImageUpdatedInternal(const nsIntRect& aUpdateRect) {
  mMonitor.AssertCurrentThreadOwns();

  // Clamp to the frame rect to ensure that decoder bugs don't result in a
  // decoded rect that extends outside the bounds of the frame rect.
  IntRect updateRect = aUpdateRect.Intersect(GetRect());
  if (updateRect.IsEmpty()) {
    return NS_OK;
  }

  mDecoded.UnionRect(mDecoded, updateRect);

  // Update our invalidation counters for any consumers watching for changes
  // in the surface.
  if (mRawSurface) {
    mRawSurface->Invalidate(updateRect);
  }
  return NS_OK;
}

void imgFrame::Finish(Opacity aFrameOpacity /* = Opacity::SOME_TRANSPARENCY */,
                      bool aFinalize /* = true */,
                      bool aOrientationSwapsWidthAndHeight /* = false */) {
  MonitorAutoLock lock(mMonitor);

  IntRect frameRect(GetRect());
  if (!mDecoded.IsEqualEdges(frameRect)) {
    // The decoder should have produced rows starting from either the bottom or
    // the top of the image. We need to calculate the region for which we have
    // not yet invalidated. And if the orientation swaps width and height then
    // its from the left or right.
    IntRect delta(0, 0, frameRect.width, 0);
    if (!aOrientationSwapsWidthAndHeight) {
      delta.width = frameRect.width;
      if (mDecoded.y == 0) {
        delta.y = mDecoded.height;
        delta.height = frameRect.height - mDecoded.height;
      } else if (mDecoded.y + mDecoded.height == frameRect.height) {
        delta.height = frameRect.height - mDecoded.y;
      } else {
        MOZ_ASSERT_UNREACHABLE("Decoder only updated middle of image!");
        delta = frameRect;
      }
    } else {
      delta.height = frameRect.height;
      if (mDecoded.x == 0) {
        delta.x = mDecoded.width;
        delta.width = frameRect.width - mDecoded.width;
      } else if (mDecoded.x + mDecoded.width == frameRect.width) {
        delta.width = frameRect.width - mDecoded.x;
      } else {
        MOZ_ASSERT_UNREACHABLE("Decoder only updated middle of image!");
        delta = frameRect;
      }
    }

    ImageUpdatedInternal(delta);
  }

  MOZ_ASSERT(mDecoded.IsEqualEdges(frameRect));

  if (aFinalize) {
    FinalizeSurfaceInternal();
  }

  mFinished = true;

  // The image is now complete, wake up anyone who's waiting.
  mMonitor.NotifyAll();
}

uint32_t imgFrame::GetImageBytesPerRow() const {
  mMonitor.AssertCurrentThreadOwns();

  if (mRawSurface) {
    return mImageSize.width * BytesPerPixel(mFormat);
  }

  return 0;
}

uint32_t imgFrame::GetImageDataLength() const {
  return GetImageBytesPerRow() * mImageSize.height;
}

void imgFrame::GetImageData(uint8_t** aData, uint32_t* aLength) const {
  MonitorAutoLock lock(mMonitor);
  GetImageDataInternal(aData, aLength);
}

void imgFrame::GetImageDataInternal(uint8_t** aData, uint32_t* aLength) const {
  mMonitor.AssertCurrentThreadOwns();
  MOZ_ASSERT(mRawSurface);

  if (mRawSurface) {
    // TODO: This is okay for now because we only realloc shared surfaces on
    // the main thread after decoding has finished, but if animations want to
    // read frame data off the main thread, we will need to reconsider this.
    *aData = mRawSurface->GetData();
    MOZ_ASSERT(*aData,
               "mRawSurface is non-null, but GetData is null in GetImageData");
  } else {
    *aData = nullptr;
  }

  *aLength = GetImageDataLength();
}

uint8_t* imgFrame::GetImageData() const {
  uint8_t* data;
  uint32_t length;
  GetImageData(&data, &length);
  return data;
}

void imgFrame::FinalizeSurface() {
  MonitorAutoLock lock(mMonitor);
  FinalizeSurfaceInternal();
}

void imgFrame::FinalizeSurfaceInternal() {
  mMonitor.AssertCurrentThreadOwns();

  // Not all images will have mRawSurface to finalize (i.e. paletted images).
  if (mShouldRecycle || !mRawSurface ||
      mRawSurface->GetType() != SurfaceType::DATA_SHARED) {
    return;
  }

  auto* sharedSurf = static_cast<SourceSurfaceSharedData*>(mRawSurface.get());
  sharedSurf->Finalize();
}

already_AddRefed<SourceSurface> imgFrame::GetSourceSurface() {
  MonitorAutoLock lock(mMonitor);
  return GetSourceSurfaceInternal();
}

already_AddRefed<SourceSurface> imgFrame::GetSourceSurfaceInternal() {
  mMonitor.AssertCurrentThreadOwns();

  if (mOptSurface) {
    if (mOptSurface->IsValid()) {
      RefPtr<SourceSurface> surf(mOptSurface);
      return surf.forget();
    }
    mOptSurface = nullptr;
  }

  if (mBlankRawSurface) {
    // We are going to return the blank surface because of the flags.
    // We are including comments here that are copied from below
    // just so that we are on the same page!
    RefPtr<SourceSurface> surf(mBlankRawSurface);
    return surf.forget();
  }

  RefPtr<SourceSurface> surf(mRawSurface);
  return surf.forget();
}

void imgFrame::Abort() {
  MonitorAutoLock lock(mMonitor);

  mAborted = true;

  // Wake up anyone who's waiting.
  mMonitor.NotifyAll();
}

bool imgFrame::IsAborted() const {
  MonitorAutoLock lock(mMonitor);
  return mAborted;
}

bool imgFrame::IsFinished() const {
  MonitorAutoLock lock(mMonitor);
  return mFinished;
}

void imgFrame::WaitUntilFinished() const {
  MonitorAutoLock lock(mMonitor);

  while (true) {
    // Return if we're aborted or complete.
    if (mAborted || mFinished) {
      return;
    }

    // Not complete yet, so we'll have to wait.
    mMonitor.Wait();
  }
}

bool imgFrame::AreAllPixelsWritten() const {
  mMonitor.AssertCurrentThreadOwns();
  return mDecoded.IsEqualInterior(GetRect());
}

void imgFrame::AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
                                      const AddSizeOfCb& aCallback) const {
  MonitorAutoLock lock(mMonitor);

  AddSizeOfCbData metadata;
  metadata.mFinished = mFinished;

  if (mOptSurface) {
    metadata.mHeapBytes += aMallocSizeOf(mOptSurface);

    SourceSurface::SizeOfInfo info;
    mOptSurface->SizeOfExcludingThis(aMallocSizeOf, info);
    metadata.Accumulate(info);
  }
  if (mRawSurface) {
    metadata.mHeapBytes += aMallocSizeOf(mRawSurface);

    SourceSurface::SizeOfInfo info;
    mRawSurface->SizeOfExcludingThis(aMallocSizeOf, info);
    metadata.Accumulate(info);
  }

  aCallback(metadata);
}

}  // namespace image
}  // namespace mozilla