Bug 1540028 [wpt PR 16099] - Catch more exceptions in Document-createElement-namespac...
[gecko.git] / image / imgFrame.cpp
bloba487024a5df108c6ac50aa0bce7394cb50623008
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/. */
7 #include "imgFrame.h"
8 #include "ImageRegion.h"
9 #include "ShutdownTracker.h"
10 #include "SurfaceCache.h"
12 #include "prenv.h"
14 #include "gfx2DGlue.h"
15 #include "gfxPlatform.h"
16 #include "gfxPrefs.h"
17 #include "gfxUtils.h"
19 #include "GeckoProfiler.h"
20 #include "MainThreadUtils.h"
21 #include "mozilla/CheckedInt.h"
22 #include "mozilla/gfx/gfxVars.h"
23 #include "mozilla/gfx/Tools.h"
24 #include "mozilla/gfx/SourceSurfaceRawData.h"
25 #include "mozilla/image/RecyclingSourceSurface.h"
26 #include "mozilla/layers/SourceSurfaceSharedData.h"
27 #include "mozilla/layers/SourceSurfaceVolatileData.h"
28 #include "mozilla/Likely.h"
29 #include "mozilla/MemoryReporting.h"
30 #include "nsMargin.h"
31 #include "nsRefreshDriver.h"
32 #include "nsThreadUtils.h"
34 namespace mozilla {
36 using namespace gfx;
38 namespace image {
40 static void ScopedMapRelease(void* aMap) {
41 delete static_cast<DataSourceSurface::ScopedMap*>(aMap);
44 static int32_t VolatileSurfaceStride(const IntSize& size,
45 SurfaceFormat format) {
46 // Stride must be a multiple of four or cairo will complain.
47 return (size.width * BytesPerPixel(format) + 0x3) & ~0x3;
50 static already_AddRefed<DataSourceSurface> CreateLockedSurface(
51 DataSourceSurface* aSurface, const IntSize& size, SurfaceFormat format) {
52 // Shared memory is never released until the surface itself is released
53 if (aSurface->GetType() == SurfaceType::DATA_SHARED) {
54 RefPtr<DataSourceSurface> surf(aSurface);
55 return surf.forget();
58 DataSourceSurface::ScopedMap* smap =
59 new DataSourceSurface::ScopedMap(aSurface, DataSourceSurface::READ_WRITE);
60 if (smap->IsMapped()) {
61 // The ScopedMap is held by this DataSourceSurface.
62 RefPtr<DataSourceSurface> surf = Factory::CreateWrappingDataSourceSurface(
63 smap->GetData(), aSurface->Stride(), size, format, &ScopedMapRelease,
64 static_cast<void*>(smap));
65 if (surf) {
66 return surf.forget();
70 delete smap;
71 return nullptr;
74 static bool ShouldUseHeap(const IntSize& aSize, int32_t aStride,
75 bool aIsAnimated) {
76 // On some platforms (i.e. Android), a volatile buffer actually keeps a file
77 // handle active. We would like to avoid too many since we could easily
78 // exhaust the pool. However, other platforms we do not have the file handle
79 // problem, and additionally we may avoid a superfluous memset since the
80 // volatile memory starts out as zero-filled. Hence the knobs below.
82 // For as long as an animated image is retained, its frames will never be
83 // released to let the OS purge volatile buffers.
84 if (aIsAnimated && gfxPrefs::ImageMemAnimatedUseHeap()) {
85 return true;
88 // Lets us avoid too many small images consuming all of the handles. The
89 // actual allocation checks for overflow.
90 int32_t bufferSize = (aStride * aSize.width) / 1024;
91 if (bufferSize < gfxPrefs::ImageMemVolatileMinThresholdKB()) {
92 return true;
95 return false;
98 static already_AddRefed<DataSourceSurface> AllocateBufferForImage(
99 const IntSize& size, SurfaceFormat format, bool aIsAnimated = false) {
100 int32_t stride = VolatileSurfaceStride(size, format);
102 if (gfxVars::GetUseWebRenderOrDefault() && gfxPrefs::ImageMemShared()) {
103 RefPtr<SourceSurfaceSharedData> newSurf = new SourceSurfaceSharedData();
104 if (newSurf->Init(size, stride, format)) {
105 return newSurf.forget();
107 } else if (ShouldUseHeap(size, stride, aIsAnimated)) {
108 RefPtr<SourceSurfaceAlignedRawData> newSurf =
109 new SourceSurfaceAlignedRawData();
110 if (newSurf->Init(size, format, false, 0, stride)) {
111 return newSurf.forget();
113 } else {
114 RefPtr<SourceSurfaceVolatileData> newSurf = new SourceSurfaceVolatileData();
115 if (newSurf->Init(size, stride, format)) {
116 return newSurf.forget();
119 return nullptr;
122 static bool GreenSurface(DataSourceSurface* aSurface, const IntSize& aSize,
123 SurfaceFormat aFormat) {
124 int32_t stride = aSurface->Stride();
125 uint32_t* surfaceData = reinterpret_cast<uint32_t*>(aSurface->GetData());
126 uint32_t surfaceDataLength = (stride * aSize.height) / sizeof(uint32_t);
128 // Start by assuming that GG is in the second byte and
129 // AA is in the final byte -- the most common case.
130 uint32_t color = mozilla::NativeEndian::swapFromBigEndian(0x00FF00FF);
132 // We are only going to handle this type of test under
133 // certain circumstances.
134 MOZ_ASSERT(surfaceData);
135 MOZ_ASSERT(aFormat == SurfaceFormat::B8G8R8A8 ||
136 aFormat == SurfaceFormat::B8G8R8X8 ||
137 aFormat == SurfaceFormat::R8G8B8A8 ||
138 aFormat == SurfaceFormat::R8G8B8X8 ||
139 aFormat == SurfaceFormat::A8R8G8B8 ||
140 aFormat == SurfaceFormat::X8R8G8B8);
141 MOZ_ASSERT((stride * aSize.height) % sizeof(uint32_t));
143 if (aFormat == SurfaceFormat::A8R8G8B8 ||
144 aFormat == SurfaceFormat::X8R8G8B8) {
145 color = mozilla::NativeEndian::swapFromBigEndian(0xFF00FF00);
148 for (uint32_t i = 0; i < surfaceDataLength; i++) {
149 surfaceData[i] = color;
152 return true;
155 static bool ClearSurface(DataSourceSurface* aSurface, const IntSize& aSize,
156 SurfaceFormat aFormat) {
157 int32_t stride = aSurface->Stride();
158 uint8_t* data = aSurface->GetData();
159 MOZ_ASSERT(data);
161 if (aFormat == SurfaceFormat::B8G8R8X8) {
162 // Skia doesn't support RGBX surfaces, so ensure the alpha value is set
163 // to opaque white. While it would be nice to only do this for Skia,
164 // imgFrame can run off main thread and past shutdown where
165 // we might not have gfxPlatform, so just memset everytime instead.
166 memset(data, 0xFF, stride * aSize.height);
167 } else if (aSurface->OnHeap()) {
168 // We only need to memset it if the buffer was allocated on the heap.
169 // Otherwise, it's allocated via mmap and refers to a zeroed page and will
170 // be COW once it's written to.
171 memset(data, 0, stride * aSize.height);
174 return true;
177 imgFrame::imgFrame()
178 : mMonitor("imgFrame"),
179 mDecoded(0, 0, 0, 0),
180 mLockCount(0),
181 mRecycleLockCount(0),
182 mAborted(false),
183 mFinished(false),
184 mOptimizable(false),
185 mShouldRecycle(false),
186 mTimeout(FrameTimeout::FromRawMilliseconds(100)),
187 mDisposalMethod(DisposalMethod::NOT_SPECIFIED),
188 mBlendMethod(BlendMethod::OVER),
189 mFormat(SurfaceFormat::UNKNOWN),
190 mNonPremult(false) {}
192 imgFrame::~imgFrame() {
193 #ifdef DEBUG
194 MonitorAutoLock lock(mMonitor);
195 MOZ_ASSERT(mAborted || AreAllPixelsWritten());
196 MOZ_ASSERT(mAborted || mFinished);
197 #endif
200 nsresult imgFrame::InitForDecoder(const nsIntSize& aImageSize,
201 SurfaceFormat aFormat, bool aNonPremult,
202 const Maybe<AnimationParams>& aAnimParams,
203 bool aShouldRecycle) {
204 // Assert for properties that should be verified by decoders,
205 // warn for properties related to bad content.
206 if (!SurfaceCache::IsLegalSize(aImageSize)) {
207 NS_WARNING("Should have legal image size");
208 mAborted = true;
209 return NS_ERROR_FAILURE;
212 mImageSize = aImageSize;
214 // May be updated shortly after InitForDecoder by BlendAnimationFilter
215 // because it needs to take into consideration the previous frames to
216 // properly calculate. We start with the whole frame as dirty.
217 mDirtyRect = GetRect();
219 if (aAnimParams) {
220 mBlendRect = aAnimParams->mBlendRect;
221 mTimeout = aAnimParams->mTimeout;
222 mBlendMethod = aAnimParams->mBlendMethod;
223 mDisposalMethod = aAnimParams->mDisposalMethod;
224 } else {
225 mBlendRect = GetRect();
228 if (aShouldRecycle) {
229 // If we are recycling then we should always use BGRA for the underlying
230 // surface because if we use BGRX, the next frame composited into the
231 // surface could be BGRA and cause rendering problems.
232 MOZ_ASSERT(aAnimParams);
233 mFormat = SurfaceFormat::B8G8R8A8;
234 } else {
235 mFormat = aFormat;
238 mNonPremult = aNonPremult;
239 mShouldRecycle = aShouldRecycle;
241 MOZ_ASSERT(!mLockedSurface, "Called imgFrame::InitForDecoder() twice?");
243 bool postFirstFrame = aAnimParams && aAnimParams->mFrameNum > 0;
244 mRawSurface = AllocateBufferForImage(mImageSize, mFormat, postFirstFrame);
245 if (!mRawSurface) {
246 mAborted = true;
247 return NS_ERROR_OUT_OF_MEMORY;
250 if (StaticPrefs::browser_measurement_render_anims_and_video_solid() &&
251 aAnimParams) {
252 mBlankRawSurface = AllocateBufferForImage(mImageSize, mFormat);
253 if (!mBlankRawSurface) {
254 mAborted = true;
255 return NS_ERROR_OUT_OF_MEMORY;
259 mLockedSurface = CreateLockedSurface(mRawSurface, mImageSize, mFormat);
260 if (!mLockedSurface) {
261 NS_WARNING("Failed to create LockedSurface");
262 mAborted = true;
263 return NS_ERROR_OUT_OF_MEMORY;
266 if (mBlankRawSurface) {
267 mBlankLockedSurface =
268 CreateLockedSurface(mBlankRawSurface, mImageSize, mFormat);
269 if (!mBlankLockedSurface) {
270 NS_WARNING("Failed to create BlankLockedSurface");
271 mAborted = true;
272 return NS_ERROR_OUT_OF_MEMORY;
276 if (!ClearSurface(mRawSurface, mImageSize, mFormat)) {
277 NS_WARNING("Could not clear allocated buffer");
278 mAborted = true;
279 return NS_ERROR_OUT_OF_MEMORY;
282 if (mBlankRawSurface) {
283 if (!GreenSurface(mBlankRawSurface, mImageSize, mFormat)) {
284 NS_WARNING("Could not clear allocated blank buffer");
285 mAborted = true;
286 return NS_ERROR_OUT_OF_MEMORY;
290 return NS_OK;
293 nsresult imgFrame::InitForDecoderRecycle(const AnimationParams& aAnimParams) {
294 // We want to recycle this frame, but there is no guarantee that consumers are
295 // done with it in a timely manner. Let's ensure they are done with it first.
296 MonitorAutoLock lock(mMonitor);
298 MOZ_ASSERT(mLockCount > 0);
299 MOZ_ASSERT(mLockedSurface);
301 if (!mShouldRecycle) {
302 // This frame either was never marked as recyclable, or the flag was cleared
303 // for a caller which does not support recycling.
304 return NS_ERROR_NOT_AVAILABLE;
307 if (mRecycleLockCount > 0) {
308 if (NS_IsMainThread()) {
309 // We should never be both decoding and recycling on the main thread. Sync
310 // decoding can only be used to produce the first set of frames. Those
311 // either never use recycling because advancing was blocked (main thread
312 // is busy) or we were auto-advancing (to seek to a frame) and the frames
313 // were never accessed (and thus cannot have recycle locks).
314 MOZ_ASSERT_UNREACHABLE("Recycling/decoding on the main thread?");
315 return NS_ERROR_NOT_AVAILABLE;
318 // We don't want to wait forever to reclaim the frame because we have no
319 // idea why it is still held. It is possibly due to OMTP. Since we are off
320 // the main thread, and we generally have frames already buffered for the
321 // animation, we can afford to wait a short period of time to hopefully
322 // complete the transaction and reclaim the buffer.
324 // We choose to wait for, at most, the refresh driver interval, so that we
325 // won't skip more than one frame. If the frame is still in use due to
326 // outstanding transactions, we are already skipping frames. If the frame
327 // is still in use for some other purpose, it won't be returned to the pool
328 // and its owner can hold onto it forever without additional impact here.
329 TimeDuration timeout =
330 TimeDuration::FromMilliseconds(nsRefreshDriver::DefaultInterval());
331 while (true) {
332 TimeStamp start = TimeStamp::Now();
333 mMonitor.Wait(timeout);
334 if (mRecycleLockCount == 0) {
335 break;
338 TimeDuration delta = TimeStamp::Now() - start;
339 if (delta >= timeout) {
340 // We couldn't secure the frame for recycling. It will allocate a new
341 // frame instead.
342 return NS_ERROR_NOT_AVAILABLE;
345 timeout -= delta;
349 mBlendRect = aAnimParams.mBlendRect;
350 mTimeout = aAnimParams.mTimeout;
351 mBlendMethod = aAnimParams.mBlendMethod;
352 mDisposalMethod = aAnimParams.mDisposalMethod;
353 mDirtyRect = GetRect();
355 return NS_OK;
358 nsresult imgFrame::InitWithDrawable(
359 gfxDrawable* aDrawable, const nsIntSize& aSize, const SurfaceFormat aFormat,
360 SamplingFilter aSamplingFilter, uint32_t aImageFlags,
361 gfx::BackendType aBackend, DrawTarget* aTargetDT) {
362 // Assert for properties that should be verified by decoders,
363 // warn for properties related to bad content.
364 if (!SurfaceCache::IsLegalSize(aSize)) {
365 NS_WARNING("Should have legal image size");
366 mAborted = true;
367 return NS_ERROR_FAILURE;
370 mImageSize = aSize;
371 mFormat = aFormat;
373 RefPtr<DrawTarget> target;
375 bool canUseDataSurface = Factory::DoesBackendSupportDataDrawtarget(aBackend);
376 if (canUseDataSurface) {
377 // It's safe to use data surfaces for content on this platform, so we can
378 // get away with using volatile buffers.
379 MOZ_ASSERT(!mLockedSurface, "Called imgFrame::InitWithDrawable() twice?");
381 mRawSurface = AllocateBufferForImage(mImageSize, mFormat);
382 if (!mRawSurface) {
383 mAborted = true;
384 return NS_ERROR_OUT_OF_MEMORY;
387 mLockedSurface = CreateLockedSurface(mRawSurface, mImageSize, mFormat);
388 if (!mLockedSurface) {
389 NS_WARNING("Failed to create LockedSurface");
390 mAborted = true;
391 return NS_ERROR_OUT_OF_MEMORY;
394 if (!ClearSurface(mRawSurface, mImageSize, mFormat)) {
395 NS_WARNING("Could not clear allocated buffer");
396 mAborted = true;
397 return NS_ERROR_OUT_OF_MEMORY;
400 target = gfxPlatform::CreateDrawTargetForData(
401 mLockedSurface->GetData(), mImageSize, mLockedSurface->Stride(),
402 mFormat);
403 } else {
404 // We can't use data surfaces for content, so we'll create an offscreen
405 // surface instead. This means if someone later calls RawAccessRef(), we
406 // may have to do an expensive readback, but we warned callers about that in
407 // the documentation for this method.
408 MOZ_ASSERT(!mOptSurface, "Called imgFrame::InitWithDrawable() twice?");
410 if (aTargetDT && !gfxVars::UseWebRender()) {
411 target = aTargetDT->CreateSimilarDrawTarget(mImageSize, mFormat);
412 } else {
413 if (gfxPlatform::GetPlatform()->SupportsAzureContentForType(aBackend)) {
414 target = gfxPlatform::GetPlatform()->CreateDrawTargetForBackend(
415 aBackend, mImageSize, mFormat);
416 } else {
417 target = gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
418 mImageSize, mFormat);
423 if (!target || !target->IsValid()) {
424 mAborted = true;
425 return NS_ERROR_OUT_OF_MEMORY;
428 // Draw using the drawable the caller provided.
429 RefPtr<gfxContext> ctx = gfxContext::CreateOrNull(target);
430 MOZ_ASSERT(ctx); // Already checked the draw target above.
431 gfxUtils::DrawPixelSnapped(ctx, aDrawable, SizeDouble(mImageSize),
432 ImageRegion::Create(ThebesRect(GetRect())),
433 mFormat, aSamplingFilter, aImageFlags);
435 if (canUseDataSurface && !mLockedSurface) {
436 NS_WARNING("Failed to create VolatileDataSourceSurface");
437 mAborted = true;
438 return NS_ERROR_OUT_OF_MEMORY;
441 if (!canUseDataSurface) {
442 // We used an offscreen surface, which is an "optimized" surface from
443 // imgFrame's perspective.
444 mOptSurface = target->Snapshot();
445 } else {
446 FinalizeSurface();
449 // If we reach this point, we should regard ourselves as complete.
450 mDecoded = GetRect();
451 mFinished = true;
453 #ifdef DEBUG
454 MonitorAutoLock lock(mMonitor);
455 MOZ_ASSERT(AreAllPixelsWritten());
456 #endif
458 return NS_OK;
461 nsresult imgFrame::Optimize(DrawTarget* aTarget) {
462 MOZ_ASSERT(NS_IsMainThread());
463 mMonitor.AssertCurrentThreadOwns();
465 if (mLockCount > 0 || !mOptimizable) {
466 // Don't optimize right now.
467 return NS_OK;
470 // Check whether image optimization is disabled -- not thread safe!
471 static bool gDisableOptimize = false;
472 static bool hasCheckedOptimize = false;
473 if (!hasCheckedOptimize) {
474 if (PR_GetEnv("MOZ_DISABLE_IMAGE_OPTIMIZE")) {
475 gDisableOptimize = true;
477 hasCheckedOptimize = true;
480 // Don't optimize during shutdown because gfxPlatform may not be available.
481 if (ShutdownTracker::ShutdownHasStarted()) {
482 return NS_OK;
485 if (gDisableOptimize) {
486 return NS_OK;
489 if (mOptSurface) {
490 return NS_OK;
493 // XXX(seth): It's currently unclear if there's any reason why we can't
494 // optimize non-premult surfaces. We should look into removing this.
495 if (mNonPremult) {
496 return NS_OK;
498 if (!gfxVars::UseWebRender()) {
499 mOptSurface = aTarget->OptimizeSourceSurface(mLockedSurface);
500 } else {
501 mOptSurface = gfxPlatform::GetPlatform()
502 ->ScreenReferenceDrawTarget()
503 ->OptimizeSourceSurface(mLockedSurface);
505 if (mOptSurface == mLockedSurface) {
506 mOptSurface = nullptr;
509 if (mOptSurface) {
510 // There's no reason to keep our original surface around if we have an
511 // optimized surface. Release our reference to it. This will leave
512 // |mLockedSurface| as the only thing keeping it alive, so it'll get freed
513 // below.
514 mRawSurface = nullptr;
517 // Release all strong references to the surface's memory. If the underlying
518 // surface is volatile, this will allow the operating system to free the
519 // memory if it needs to.
520 mLockedSurface = nullptr;
521 mOptimizable = false;
523 return NS_OK;
526 DrawableFrameRef imgFrame::DrawableRef() { return DrawableFrameRef(this); }
528 RawAccessFrameRef imgFrame::RawAccessRef(bool aOnlyFinished /*= false*/) {
529 return RawAccessFrameRef(this, aOnlyFinished);
532 void imgFrame::SetRawAccessOnly() {
533 AssertImageDataLocked();
535 // Lock our data and throw away the key.
536 LockImageData(false);
539 imgFrame::SurfaceWithFormat imgFrame::SurfaceForDrawing(
540 bool aDoPartialDecode, bool aDoTile, ImageRegion& aRegion,
541 SourceSurface* aSurface) {
542 MOZ_ASSERT(NS_IsMainThread());
543 mMonitor.AssertCurrentThreadOwns();
545 if (!aDoPartialDecode) {
546 return SurfaceWithFormat(new gfxSurfaceDrawable(aSurface, mImageSize),
547 mFormat);
550 gfxRect available =
551 gfxRect(mDecoded.X(), mDecoded.Y(), mDecoded.Width(), mDecoded.Height());
553 if (aDoTile) {
554 // Create a temporary surface.
555 // Give this surface an alpha channel because there are
556 // transparent pixels in the padding or undecoded area
557 RefPtr<DrawTarget> target =
558 gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
559 mImageSize, SurfaceFormat::B8G8R8A8);
560 if (!target) {
561 return SurfaceWithFormat();
564 SurfacePattern pattern(aSurface, aRegion.GetExtendMode(),
565 Matrix::Translation(mDecoded.X(), mDecoded.Y()));
566 target->FillRect(ToRect(aRegion.Intersect(available).Rect()), pattern);
568 RefPtr<SourceSurface> newsurf = target->Snapshot();
569 return SurfaceWithFormat(new gfxSurfaceDrawable(newsurf, mImageSize),
570 target->GetFormat());
573 // Not tiling, and we have a surface, so we can account for
574 // a partial decode just by twiddling parameters.
575 aRegion = aRegion.Intersect(available);
576 IntSize availableSize(mDecoded.Width(), mDecoded.Height());
578 return SurfaceWithFormat(new gfxSurfaceDrawable(aSurface, availableSize),
579 mFormat);
582 bool imgFrame::Draw(gfxContext* aContext, const ImageRegion& aRegion,
583 SamplingFilter aSamplingFilter, uint32_t aImageFlags,
584 float aOpacity) {
585 AUTO_PROFILER_LABEL("imgFrame::Draw", GRAPHICS);
587 MOZ_ASSERT(NS_IsMainThread());
588 NS_ASSERTION(!aRegion.Rect().IsEmpty(), "Drawing empty region!");
589 NS_ASSERTION(!aRegion.IsRestricted() ||
590 !aRegion.Rect().Intersect(aRegion.Restriction()).IsEmpty(),
591 "We must be allowed to sample *some* source pixels!");
593 // Perform the draw and freeing of the surface outside the lock. We want to
594 // avoid contention with the decoder if we can. The surface may also attempt
595 // to relock the monitor if it is freed (e.g. RecyclingSourceSurface).
596 RefPtr<SourceSurface> surf;
597 SurfaceWithFormat surfaceResult;
598 ImageRegion region(aRegion);
599 gfxRect imageRect(0, 0, mImageSize.width, mImageSize.height);
602 MonitorAutoLock lock(mMonitor);
604 // Possibly convert this image into a GPU texture, this may also cause our
605 // mLockedSurface to be released and the OS to release the underlying
606 // memory.
607 Optimize(aContext->GetDrawTarget());
609 bool doPartialDecode = !AreAllPixelsWritten();
611 // Most draw targets will just use the surface only during DrawPixelSnapped
612 // but captures/recordings will retain a reference outside this stack
613 // context. While in theory a decoder thread could be trying to recycle this
614 // frame at this very moment, in practice the only way we can get here is if
615 // this frame is the current frame of the animation. Since we can only
616 // advance on the main thread, we know nothing else will try to use it.
617 DrawTarget* drawTarget = aContext->GetDrawTarget();
618 bool recording = drawTarget->GetBackendType() == BackendType::RECORDING;
619 bool temporary = !drawTarget->IsCaptureDT() && !recording;
620 RefPtr<SourceSurface> surf = GetSourceSurfaceInternal(temporary);
621 if (!surf) {
622 return false;
625 bool doTile = !imageRect.Contains(aRegion.Rect()) &&
626 !(aImageFlags & imgIContainer::FLAG_CLAMP);
628 surfaceResult = SurfaceForDrawing(doPartialDecode, doTile, region, surf);
630 // If we are recording, then we cannot recycle the surface. The blob
631 // rasterizer is not properly synchronized for recycling in the compositor
632 // process. The easiest thing to do is just mark the frames it consumes as
633 // non-recyclable.
634 if (recording && surfaceResult.IsValid()) {
635 mShouldRecycle = false;
639 if (surfaceResult.IsValid()) {
640 gfxUtils::DrawPixelSnapped(aContext, surfaceResult.mDrawable,
641 imageRect.Size(), region, surfaceResult.mFormat,
642 aSamplingFilter, aImageFlags, aOpacity);
645 return true;
648 nsresult imgFrame::ImageUpdated(const nsIntRect& aUpdateRect) {
649 MonitorAutoLock lock(mMonitor);
650 return ImageUpdatedInternal(aUpdateRect);
653 nsresult imgFrame::ImageUpdatedInternal(const nsIntRect& aUpdateRect) {
654 mMonitor.AssertCurrentThreadOwns();
656 // Clamp to the frame rect to ensure that decoder bugs don't result in a
657 // decoded rect that extends outside the bounds of the frame rect.
658 IntRect updateRect = aUpdateRect.Intersect(GetRect());
659 if (updateRect.IsEmpty()) {
660 return NS_OK;
663 mDecoded.UnionRect(mDecoded, updateRect);
665 // Update our invalidation counters for any consumers watching for changes
666 // in the surface.
667 if (mRawSurface) {
668 mRawSurface->Invalidate(updateRect);
670 if (mLockedSurface && mRawSurface != mLockedSurface) {
671 mLockedSurface->Invalidate(updateRect);
673 return NS_OK;
676 void imgFrame::Finish(Opacity aFrameOpacity /* = Opacity::SOME_TRANSPARENCY */,
677 bool aFinalize /* = true */) {
678 MonitorAutoLock lock(mMonitor);
679 MOZ_ASSERT(mLockCount > 0, "Image data should be locked");
681 IntRect frameRect(GetRect());
682 if (!mDecoded.IsEqualEdges(frameRect)) {
683 // The decoder should have produced rows starting from either the bottom or
684 // the top of the image. We need to calculate the region for which we have
685 // not yet invalidated.
686 IntRect delta(0, 0, frameRect.width, 0);
687 if (mDecoded.y == 0) {
688 delta.y = mDecoded.height;
689 delta.height = frameRect.height - mDecoded.height;
690 } else if (mDecoded.y + mDecoded.height == frameRect.height) {
691 delta.height = frameRect.height - mDecoded.y;
692 } else {
693 MOZ_ASSERT_UNREACHABLE("Decoder only updated middle of image!");
694 delta = frameRect;
697 ImageUpdatedInternal(delta);
700 MOZ_ASSERT(mDecoded.IsEqualEdges(frameRect));
702 if (aFinalize) {
703 FinalizeSurfaceInternal();
706 mFinished = true;
708 // The image is now complete, wake up anyone who's waiting.
709 mMonitor.NotifyAll();
712 uint32_t imgFrame::GetImageBytesPerRow() const {
713 mMonitor.AssertCurrentThreadOwns();
715 if (mRawSurface) {
716 return mImageSize.width * BytesPerPixel(mFormat);
719 return 0;
722 uint32_t imgFrame::GetImageDataLength() const {
723 return GetImageBytesPerRow() * mImageSize.height;
726 void imgFrame::GetImageData(uint8_t** aData, uint32_t* aLength) const {
727 MonitorAutoLock lock(mMonitor);
728 GetImageDataInternal(aData, aLength);
731 void imgFrame::GetImageDataInternal(uint8_t** aData, uint32_t* aLength) const {
732 mMonitor.AssertCurrentThreadOwns();
733 MOZ_ASSERT(mLockCount > 0, "Image data should be locked");
734 MOZ_ASSERT(mLockedSurface);
736 if (mLockedSurface) {
737 // TODO: This is okay for now because we only realloc shared surfaces on
738 // the main thread after decoding has finished, but if animations want to
739 // read frame data off the main thread, we will need to reconsider this.
740 *aData = mLockedSurface->GetData();
741 MOZ_ASSERT(
742 *aData,
743 "mLockedSurface is non-null, but GetData is null in GetImageData");
744 } else {
745 *aData = nullptr;
748 *aLength = GetImageDataLength();
751 uint8_t* imgFrame::GetImageData() const {
752 uint8_t* data;
753 uint32_t length;
754 GetImageData(&data, &length);
755 return data;
758 uint8_t* imgFrame::LockImageData(bool aOnlyFinished) {
759 MonitorAutoLock lock(mMonitor);
761 MOZ_ASSERT(mLockCount >= 0, "Unbalanced locks and unlocks");
762 if (mLockCount < 0 || (aOnlyFinished && !mFinished)) {
763 return nullptr;
766 uint8_t* data;
767 if (mLockedSurface) {
768 data = mLockedSurface->GetData();
769 } else {
770 data = nullptr;
773 // If the raw data is still available, we should get a valid pointer for it.
774 if (!data) {
775 MOZ_ASSERT_UNREACHABLE("It's illegal to re-lock an optimized imgFrame");
776 return nullptr;
779 ++mLockCount;
780 return data;
783 void imgFrame::AssertImageDataLocked() const {
784 #ifdef DEBUG
785 MonitorAutoLock lock(mMonitor);
786 MOZ_ASSERT(mLockCount > 0, "Image data should be locked");
787 #endif
790 nsresult imgFrame::UnlockImageData() {
791 MonitorAutoLock lock(mMonitor);
793 MOZ_ASSERT(mLockCount > 0, "Unlocking an unlocked image!");
794 if (mLockCount <= 0) {
795 return NS_ERROR_FAILURE;
798 MOZ_ASSERT(mLockCount > 1 || mFinished || mAborted,
799 "Should have Finish()'d or aborted before unlocking");
801 mLockCount--;
803 return NS_OK;
806 void imgFrame::SetOptimizable() {
807 AssertImageDataLocked();
808 MonitorAutoLock lock(mMonitor);
809 mOptimizable = true;
812 void imgFrame::FinalizeSurface() {
813 MonitorAutoLock lock(mMonitor);
814 FinalizeSurfaceInternal();
817 void imgFrame::FinalizeSurfaceInternal() {
818 mMonitor.AssertCurrentThreadOwns();
820 // Not all images will have mRawSurface to finalize (i.e. paletted images).
821 if (mShouldRecycle || !mRawSurface ||
822 mRawSurface->GetType() != SurfaceType::DATA_SHARED) {
823 return;
826 auto sharedSurf = static_cast<SourceSurfaceSharedData*>(mRawSurface.get());
827 sharedSurf->Finalize();
830 already_AddRefed<SourceSurface> imgFrame::GetSourceSurface() {
831 MonitorAutoLock lock(mMonitor);
832 return GetSourceSurfaceInternal(/* aTemporary */ false);
835 already_AddRefed<SourceSurface> imgFrame::GetSourceSurfaceInternal(
836 bool aTemporary) {
837 mMonitor.AssertCurrentThreadOwns();
839 if (mOptSurface) {
840 if (mOptSurface->IsValid()) {
841 RefPtr<SourceSurface> surf(mOptSurface);
842 return surf.forget();
843 } else {
844 mOptSurface = nullptr;
848 if (mBlankLockedSurface) {
849 // We are going to return the blank surface because of the flags.
850 // We are including comments here that are copied from below
851 // just so that we are on the same page!
853 // We don't need to create recycling wrapper for some callers because they
854 // promise to release the surface immediately after.
855 if (!aTemporary && mShouldRecycle) {
856 RefPtr<SourceSurface> surf =
857 new RecyclingSourceSurface(this, mBlankLockedSurface);
858 return surf.forget();
861 RefPtr<SourceSurface> surf(mBlankLockedSurface);
862 return surf.forget();
865 if (mLockedSurface) {
866 // We don't need to create recycling wrapper for some callers because they
867 // promise to release the surface immediately after.
868 if (!aTemporary && mShouldRecycle) {
869 RefPtr<SourceSurface> surf =
870 new RecyclingSourceSurface(this, mLockedSurface);
871 return surf.forget();
874 RefPtr<SourceSurface> surf(mLockedSurface);
875 return surf.forget();
878 MOZ_ASSERT(!mShouldRecycle, "Should recycle but no locked surface!");
880 if (!mRawSurface) {
881 return nullptr;
884 return CreateLockedSurface(mRawSurface, mImageSize, mFormat);
887 void imgFrame::Abort() {
888 MonitorAutoLock lock(mMonitor);
890 mAborted = true;
892 // Wake up anyone who's waiting.
893 mMonitor.NotifyAll();
896 bool imgFrame::IsAborted() const {
897 MonitorAutoLock lock(mMonitor);
898 return mAborted;
901 bool imgFrame::IsFinished() const {
902 MonitorAutoLock lock(mMonitor);
903 return mFinished;
906 void imgFrame::WaitUntilFinished() const {
907 MonitorAutoLock lock(mMonitor);
909 while (true) {
910 // Return if we're aborted or complete.
911 if (mAborted || mFinished) {
912 return;
915 // Not complete yet, so we'll have to wait.
916 mMonitor.Wait();
920 bool imgFrame::AreAllPixelsWritten() const {
921 mMonitor.AssertCurrentThreadOwns();
922 return mDecoded.IsEqualInterior(GetRect());
925 void imgFrame::AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
926 const AddSizeOfCb& aCallback) const {
927 MonitorAutoLock lock(mMonitor);
929 AddSizeOfCbData metadata;
930 if (mLockedSurface) {
931 metadata.heap += aMallocSizeOf(mLockedSurface);
933 if (mOptSurface) {
934 metadata.heap += aMallocSizeOf(mOptSurface);
936 if (mRawSurface) {
937 metadata.heap += aMallocSizeOf(mRawSurface);
938 mRawSurface->AddSizeOfExcludingThis(aMallocSizeOf, metadata.heap,
939 metadata.nonHeap, metadata.handles,
940 metadata.externalId);
943 aCallback(metadata);
946 RecyclingSourceSurface::RecyclingSourceSurface(imgFrame* aParent,
947 DataSourceSurface* aSurface)
948 : mParent(aParent), mSurface(aSurface), mType(SurfaceType::DATA) {
949 mParent->mMonitor.AssertCurrentThreadOwns();
950 ++mParent->mRecycleLockCount;
952 if (aSurface->GetType() == SurfaceType::DATA_SHARED) {
953 mType = SurfaceType::DATA_RECYCLING_SHARED;
957 RecyclingSourceSurface::~RecyclingSourceSurface() {
958 MonitorAutoLock lock(mParent->mMonitor);
959 MOZ_ASSERT(mParent->mRecycleLockCount > 0);
960 if (--mParent->mRecycleLockCount == 0) {
961 mParent->mMonitor.NotifyAll();
965 } // namespace image
966 } // namespace mozilla