Files
palemoon27/image/imgFrame.cpp
T
roytam1 b6f6aa65fb import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1165518 - Part 2: Replace prlog.h with Logging.h. rs=froydnj (342dfd711)
- Bug 1085567 - Fixup synth events within pointer lock implementation for e10s. r=billm (45c50a0e6)
- Bug 1128238 - Ajust NPP_SetWindow and synth native event coords to account for the chrome offset of remote tabs. Prior to this the content process always assumed the tab was the window origin. r=aklotz (202ffc006)
- Bug 1128934 - Avoid calling GetClientBounds from BasicComposior. r=roc, karlt (5e98232bd)
- Bug 1149041 - Make nsIWidget::SizeConstraints use LayoutDeviceIntSize instead of unit-less nsIntSize. r=roc (d0b345926)
- shuffle around (fcdc51510)
- Bug 1142852 P1 Fix NS_NewNonOwningRunnableMethodWithArgs() so that it compiles. r=froydnj (394d2b200)
- Bug 1142852 P2 Make Cache Action use non-threadsafe ref-counting. r=ehsan (0b2b91d86)
- Bug 1146349 - Allow NS_NewRunnableMethodWithArgs to take up to 8 arguments. r=froydnj (7207e74d0)
- Bug 1146349 - Make the native event synthesization functions in DOMWindowUtils async. r=smaug (e43579a1d)
- Bug 949614 - Use === for SimpleTest.is; r=Waldo (9f54faca4)
- Bug 1146349 - Update some widget tests to deal with async native key event synthesization. r=smaug,masayuki (f1e82df79)
- Bug 920013 - Rewrite test_cocoa_focus.html to use SpecialPowers. r=smichaud (8708d6a71)
- Bug 1146349 - Update DOM mochitests to deal with async native event synthesization. r=smaug (a4eceb9f6)
- Bug 1146349 - Update browser-chrome mochitests to deal with async native event synthesization. r=Gijs (c864ad939)
- Bug 1146349 - Wire up native event synthesization in child processes.  r=smaug (e8574457e)
- Bug 1146349 - Mark SynthesizedEventObserver::Observe() as override (9063f04b5)
- Bug 1149613 - Remove unneded field. r=botond (9aa7155e2)
- Bug 1153613 - [e10s], ensure the buffer for the drag image is large enough for SourceSurfaceRawData::GuaranteePersistance() to succeed, r=mattwoodrow (aa885dae7)
- remove spurious file (ff199d3ae)
- Bug 1043013 - Generalize nsIntRegion into a BaseIntRegion template. r=jrmuizel (9d3e02820)
- Bug 1043013 - Introduce IntRegionTyped. r=jrmuizel (f73902921)
- Bug 1043013 - Use strongly-typed regions in HitTestingTreeNode. r=kats (7b8c8ae44)
- Bug 913443 - Remove mention of the word ThebesLayer in a comment. r=roc (e9706e025)
- Bug 913443 - Remove some #ifdefs. r=roc (da76f831b)
- Bug 913443 - Break up CreateOrRecyclePaintedLayer into more parts. r=roc (dd94560fe)
- Bug 913443 - Extract layer hint calculation. r=roc (5c7806a95)
- Bug 913443 - Break CreateOrRecyclePaintedLayer up even more. r=roc (9e5edab17)
- Bug 913443 - Remove unused aItemVisibleRect argument. r=roc (f3d1a5195)
- Bug 913443 - Move IsWidgetLayerManager() check out of UpdateCommonClipCount. r=roc (3be7b19a6)
- Bug 913443 - Change the order of these calls. r=roc (c7bc59fee)
- Bug 913443 - Add a display item buffer for PaintedLayerData so that we can assign items without needing to know the actual Layer. r=roc (674df6aa8)
- Bug 913443 - Delay PaintedLayer recycling until PopPaintedLayerData() r=roc (9d6de961a)
- Bug 913443 - When determining the layer to recycle, only consider layers that have display items in common with the layer we need. r=roc (3327c31c5)
- Bug 913443 - Recycle PaintedLayers as soon as possible. r=roc (37ef6d530)
- Bug 913443 - Remove duplicated argument variables. r=roc (19c5af270)
- Bug 1141408 - Don't include the display item's memory address in the profile label. r=BenWa (5bb1d73c1)
- Bug 1141281 - Don't put empty Info: lines into the layers dump. r=BenWa (85f4b28be)
- Bug 1035611 - Part 1: Updated Matrix4x4::ProjectRectBounds to properly handle infinite values when untransformed rects cross the w=0 plane. r=mattwoodrow (f47ed4383)
- Bug 1035611 - Part 2: Test to ensure that transformed rects crossing the w=0 plane are clipped correctly. r=mattwoodrow (5701b435f)
- Bug 1141089 - Add some release assertions to gather more data. r=roc (805c2994d)
- Bug 1141595 - Make scroll info display item hoisting work after bug 913443. r=tn (1efcaf164)
- Bug 1144307 - Destroy scroll info layer items when we're done with them. r=tn (3ffd3b1f2)
- Bug 1148855 - Mark some ContainerState methods as const. r=roc (791585baf)
- Bug 1144820 - Use rvalue reference in AddProfileTimelineMarker. r=smaug (12316b65f)
- Bug 1148855 - Set overflow:hidden on scrollbar tracks so that layerization knows that the scrollbar thumb won't leave the scrollbar. r=roc (082ec87bf)
- Bug 1148855 - Intermediate state that unifies APZ and non-APZ layerization behavior somewhat. (3da45ab96)
- Bug 1148855 - Rework how display items are assigned to PaintedLayers and make it work the same way with and without APZ. r=roc (7258f9c90)
- Bug 913443 - Add some tests. r=roc (9977682da)
- Bug 1039926 - Add a test for not invalidating stuff inside mask layers. (1f8aef65e)
- Bug 1148855 - Add some tests. r=roc (2ba1f7f18)
- Bug 1148855 - Tweak reftest fuzziness. (a600be7d5)
- Bug 1151306 - Add a missing check during background color pulling. r=roc (b3a6c886b)
- Bug 1150774 (Part 1) - Add helpers to simplify using typed units in ConfigureLayer. r=botond (baecb2c2e)
- Bug 1150774 (Part 2) - Use the correct units in nsDisplayImageContainer::ConfigureLayer and related code. r=botond (a79beb1fb)
- Bug 1141089 - Initialize DisplayItemData with a non-null mLayer, so that mLayer is non-null throughout the object's whole lifetime. r=roc (7c2f9eb09)
- Bug 1141089 - Remove unused DisplayItemData copy constructor. r=roc (ba5c46c45)
- Bug 1141089 - Add a few more MOZ_RELEASE_ASSERT(mLayer) checks, just for fun. r=roc (50a8f2db7)
- Bug 1141089 - Use const nsTArray* in all the places where we don't change the array's contents. r=roc (cd07a5bbd)
- Bug 1141089 - Turn these two assertions into release assertions. r=roc (71daf5ef3)
- Bug 1141089 - Check that we're unsetting sDestroyedFrame correctly. r=roc (6c7da67e7)
- Bug 1143522 - Convert Layer::mClipRect to ParentLayerIntRect. r=botond (ddab1c0ff)
- Bug 1144951. Warn if the result of ConvertAppUnits is unused. r=mats (e1d79c6ca)
- Bug 1144951. Rename ConvertAppUnits to ScaleToOtherAppUnits. r=mats (675c918c3)
- Bug 1149035 - Make nsXULPopupManager::PopupResized take a LayoutDeviceIntSize param instead of a unit-less nsIntSize. r=roc (eb9529af7)
- missing bit of Bug 1156110 - Make nsView::mWindow an nsCOMPtr; r=roc (972255b4f)
- Bug 1147706 - Warn if we don't use the result of const methods on BaseRect and its subclasses. r=tn (9e91deada)
- Bug 1152838 - Fix BaseRect::InflateToMultiple and move it to nsIntRect. r=mattwoodrow, r=jrmuizel (5a942df29)
- Bug 1125055 - Sync decode layerized images if necessary. r=roc,tn (1379db5c7)
- Bug 1145167 - make imgIContainer::type getter infallible; r=seth (7b7091b89)
- Bug 1148213 - Implement image locking for VectorImage. r=dholbert (240e14cb3)
- Bug 1143653 - Crash in CompositorD3D9 rather than TextureD3D9 if device reset fails, after a few attemps. r=jrmuizel (76607ddc3)
- Bug 1147894 - Only use non-null compositors with TextureHost::SetCompositor. r=sotaro (605bf34f6)
- Bug 1146912 - Finish replacing TextureHost::GetTextureSource by BindTextureSource. r=sotaro (cf3763879)
- Bug 1132854 - Remove useless ThebesIntSize conversion helper. r=Bas (78cdfee14)
- Bug 1068881 - Inflate the dest rect to at least one device pixel when computing the optimal image size for drawing. r=roc (3d5644913)
2020-05-30 12:49:08 +08:00

1198 lines
33 KiB
C++

/* -*- 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 "ShutdownTracker.h"
#include "prenv.h"
#include "gfx2DGlue.h"
#include "gfxPlatform.h"
#include "gfxUtils.h"
#include "gfxAlphaRecovery.h"
static bool gDisableOptimize = false;
#include "GeckoProfiler.h"
#include "mozilla/Likely.h"
#include "MainThreadUtils.h"
#include "mozilla/MemoryReporting.h"
#include "nsMargin.h"
#include "nsThreadUtils.h"
#include "mozilla/CheckedInt.h"
#include "mozilla/gfx/Tools.h"
namespace mozilla {
using namespace gfx;
namespace image {
static UserDataKey kVolatileBuffer;
static void
VolatileBufferRelease(void *vbuf)
{
delete static_cast<VolatileBufferPtr<unsigned char>*>(vbuf);
}
static int32_t
VolatileSurfaceStride(const IntSize& size, SurfaceFormat format)
{
// Stride must be a multiple of four or cairo will complain.
return (size.width * BytesPerPixel(format) + 0x3) & ~0x3;
}
static already_AddRefed<DataSourceSurface>
CreateLockedSurface(VolatileBuffer *vbuf,
const IntSize& size,
SurfaceFormat format)
{
VolatileBufferPtr<unsigned char> *vbufptr =
new VolatileBufferPtr<unsigned char>(vbuf);
MOZ_ASSERT(!vbufptr->WasBufferPurged(), "Expected image data!");
int32_t stride = VolatileSurfaceStride(size, format);
RefPtr<DataSourceSurface> surf =
Factory::CreateWrappingDataSourceSurface(*vbufptr, stride, size, format);
if (!surf) {
delete vbufptr;
return nullptr;
}
surf->AddUserData(&kVolatileBuffer, vbufptr, VolatileBufferRelease);
return surf.forget();
}
static already_AddRefed<VolatileBuffer>
AllocateBufferForImage(const IntSize& size, SurfaceFormat format)
{
int32_t stride = VolatileSurfaceStride(size, format);
RefPtr<VolatileBuffer> buf = new VolatileBuffer();
if (buf->Init(stride * size.height,
1 << gfxAlphaRecovery::GoodAlignmentLog2()))
return buf.forget();
return nullptr;
}
// Returns true if an image of aWidth x aHeight is allowed and legal.
static bool AllowedImageSize(int32_t aWidth, int32_t aHeight)
{
// reject over-wide or over-tall images
const int32_t k64KLimit = 0x0000FFFF;
if (MOZ_UNLIKELY(aWidth > k64KLimit || aHeight > k64KLimit )) {
NS_WARNING("image too big");
return false;
}
// protect against invalid sizes
if (MOZ_UNLIKELY(aHeight <= 0 || aWidth <= 0)) {
return false;
}
// check to make sure we don't overflow a 32-bit
CheckedInt32 requiredBytes = CheckedInt32(aWidth) * CheckedInt32(aHeight) * 4;
if (MOZ_UNLIKELY(!requiredBytes.isValid())) {
NS_WARNING("width or height too large");
return false;
}
#if defined(XP_MACOSX)
// CoreGraphics is limited to images < 32K in *height*, so clamp all surfaces on the Mac to that height
if (MOZ_UNLIKELY(aHeight > SHRT_MAX)) {
NS_WARNING("image too big");
return false;
}
#endif
return true;
}
static bool AllowedImageAndFrameDimensions(const nsIntSize& aImageSize,
const nsIntRect& aFrameRect)
{
if (!AllowedImageSize(aImageSize.width, aImageSize.height)) {
return false;
}
if (!AllowedImageSize(aFrameRect.width, aFrameRect.height)) {
return false;
}
nsIntRect imageRect(0, 0, aImageSize.width, aImageSize.height);
if (!imageRect.Contains(aFrameRect)) {
NS_WARNING("Animated image frame does not fit inside bounds of image");
}
return true;
}
imgFrame::imgFrame()
: mMonitor("imgFrame")
, mDecoded(0, 0, 0, 0)
, mLockCount(0)
, mTimeout(100)
, mDisposalMethod(DisposalMethod::NOT_SPECIFIED)
, mBlendMethod(BlendMethod::OVER)
, mHasNoAlpha(false)
, mAborted(false)
, mPalettedImageData(nullptr)
, mPaletteDepth(0)
, mNonPremult(false)
, mSinglePixel(false)
, mCompositingFailed(false)
, mOptimizable(false)
{
static bool hasCheckedOptimize = false;
if (!hasCheckedOptimize) {
if (PR_GetEnv("MOZ_DISABLE_IMAGE_OPTIMIZE")) {
gDisableOptimize = true;
}
hasCheckedOptimize = true;
}
}
imgFrame::~imgFrame()
{
#ifdef DEBUG
MonitorAutoLock lock(mMonitor);
MOZ_ASSERT(mAborted || IsImageCompleteInternal());
#endif
moz_free(mPalettedImageData);
mPalettedImageData = nullptr;
}
nsresult
imgFrame::ReinitForDecoder(const nsIntSize& aImageSize,
const nsIntRect& aRect,
SurfaceFormat aFormat,
uint8_t aPaletteDepth /* = 0 */,
bool aNonPremult /* = false */)
{
MonitorAutoLock lock(mMonitor);
if (mDecoded.x != 0 || mDecoded.y != 0 ||
mDecoded.width != 0 || mDecoded.height != 0) {
MOZ_ASSERT_UNREACHABLE("Shouldn't reinit after write");
return NS_ERROR_FAILURE;
}
if (mAborted) {
MOZ_ASSERT_UNREACHABLE("Shouldn't reinit if aborted");
return NS_ERROR_FAILURE;
}
if (mLockCount < 1) {
MOZ_ASSERT_UNREACHABLE("Shouldn't reinit unless locked");
return NS_ERROR_FAILURE;
}
// Restore everything (except mLockCount, which we need to keep) to how it was
// when we were first created.
// XXX(seth): This is probably a little excessive, but I want to be *really*
// sure that nothing got missed.
mDecoded = nsIntRect(0, 0, 0, 0);
mTimeout = 100;
mDisposalMethod = DisposalMethod::NOT_SPECIFIED;
mBlendMethod = BlendMethod::OVER;
mHasNoAlpha = false;
mAborted = false;
mPaletteDepth = 0;
mNonPremult = false;
mSinglePixel = false;
mCompositingFailed = false;
mOptimizable = false;
mImageSize = IntSize();
mSize = IntSize();
mOffset = nsIntPoint();
mSinglePixelColor = Color();
// Release all surfaces.
mImageSurface = nullptr;
mOptSurface = nullptr;
mVBuf = nullptr;
mVBufPtr = nullptr;
moz_free(mPalettedImageData);
mPalettedImageData = nullptr;
// Reinitialize.
nsresult rv = InitForDecoder(aImageSize, aRect, aFormat,
aPaletteDepth, aNonPremult);
if (NS_FAILED(rv)) {
return rv;
}
// We were locked before; perform the same actions we would've performed when
// we originally got locked.
if (mImageSurface) {
mVBufPtr = mVBuf;
return NS_OK;
}
if (!mPalettedImageData) {
MOZ_ASSERT_UNREACHABLE("We got optimized somehow during reinit");
return NS_ERROR_FAILURE;
}
// Paletted images don't have surfaces, so there's nothing to do.
return NS_OK;
}
nsresult
imgFrame::InitForDecoder(const nsIntSize& aImageSize,
const nsIntRect& aRect,
SurfaceFormat aFormat,
uint8_t aPaletteDepth /* = 0 */,
bool aNonPremult /* = false */)
{
// Assert for properties that should be verified by decoders,
// warn for properties related to bad content.
if (!AllowedImageAndFrameDimensions(aImageSize, aRect)) {
NS_WARNING("Should have legal image size");
mAborted = true;
return NS_ERROR_FAILURE;
}
mImageSize = aImageSize;
mOffset.MoveTo(aRect.x, aRect.y);
mSize.SizeTo(aRect.width, aRect.height);
mFormat = aFormat;
mPaletteDepth = aPaletteDepth;
mNonPremult = aNonPremult;
if (aPaletteDepth != 0) {
// We're creating for a paletted image.
if (aPaletteDepth > 8) {
NS_WARNING("Should have legal palette depth");
NS_ERROR("This Depth is not supported");
mAborted = true;
return NS_ERROR_FAILURE;
}
// Use the fallible allocator here. Paletted images always use 1 byte per
// pixel, so calculating the amount of memory we need is straightforward.
size_t dataSize = PaletteDataLength() + (mSize.width * mSize.height);
mPalettedImageData = static_cast<uint8_t*>(moz_calloc(dataSize, sizeof(uint8_t)));
if (!mPalettedImageData)
NS_WARNING("Call to moz_calloc for paletted image data should succeed");
NS_ENSURE_TRUE(mPalettedImageData, NS_ERROR_OUT_OF_MEMORY);
} else {
MOZ_ASSERT(!mImageSurface, "Called imgFrame::InitForDecoder() twice?");
mVBuf = AllocateBufferForImage(mSize, mFormat);
if (!mVBuf) {
mAborted = true;
return NS_ERROR_OUT_OF_MEMORY;
}
if (mVBuf->OnHeap()) {
int32_t stride = VolatileSurfaceStride(mSize, mFormat);
VolatileBufferPtr<uint8_t> ptr(mVBuf);
memset(ptr, 0, stride * mSize.height);
}
mImageSurface = CreateLockedSurface(mVBuf, mSize, mFormat);
if (!mImageSurface) {
NS_WARNING("Failed to create VolatileDataSourceSurface");
mAborted = true;
return NS_ERROR_OUT_OF_MEMORY;
}
}
return NS_OK;
}
nsresult
imgFrame::InitWithDrawable(gfxDrawable* aDrawable,
const nsIntSize& aSize,
const SurfaceFormat aFormat,
GraphicsFilter aFilter,
uint32_t aImageFlags)
{
// Assert for properties that should be verified by decoders,
// warn for properties related to bad content.
if (!AllowedImageSize(aSize.width, aSize.height)) {
NS_WARNING("Should have legal image size");
mAborted = true;
return NS_ERROR_FAILURE;
}
mImageSize = aSize;
mOffset.MoveTo(0, 0);
mSize.SizeTo(aSize.width, aSize.height);
mFormat = aFormat;
mPaletteDepth = 0;
RefPtr<DrawTarget> target;
bool canUseDataSurface =
gfxPlatform::GetPlatform()->CanRenderContentToDataSurface();
if (canUseDataSurface) {
// It's safe to use data surfaces for content on this platform, so we can
// get away with using volatile buffers.
MOZ_ASSERT(!mImageSurface, "Called imgFrame::InitWithDrawable() twice?");
mVBuf = AllocateBufferForImage(mSize, mFormat);
if (!mVBuf) {
mAborted = true;
return NS_ERROR_OUT_OF_MEMORY;
}
int32_t stride = VolatileSurfaceStride(mSize, mFormat);
VolatileBufferPtr<uint8_t> ptr(mVBuf);
if (!ptr) {
mAborted = true;
return NS_ERROR_OUT_OF_MEMORY;
}
if (mVBuf->OnHeap()) {
memset(ptr, 0, stride * mSize.height);
}
mImageSurface = CreateLockedSurface(mVBuf, mSize, mFormat);
target = gfxPlatform::GetPlatform()->
CreateDrawTargetForData(ptr, mSize, 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.
MOZ_ASSERT(!mOptSurface, "Called imgFrame::InitWithDrawable() twice?");
target = gfxPlatform::GetPlatform()->
CreateOffscreenContentDrawTarget(mSize, mFormat);
}
if (!target) {
mAborted = true;
return NS_ERROR_OUT_OF_MEMORY;
}
// Draw using the drawable the caller provided.
nsIntRect imageRect(0, 0, mSize.width, mSize.height);
nsRefPtr<gfxContext> ctx = new gfxContext(target);
gfxUtils::DrawPixelSnapped(ctx, aDrawable, mSize,
ImageRegion::Create(imageRect),
mFormat, aFilter, aImageFlags);
if (canUseDataSurface && !mImageSurface) {
NS_WARNING("Failed to create VolatileDataSourceSurface");
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();
}
// If we reach this point, we should regard ourselves as complete.
mDecoded = GetRect();
MOZ_ASSERT(IsImageComplete());
return NS_OK;
}
nsresult imgFrame::Optimize()
{
MOZ_ASSERT(NS_IsMainThread());
mMonitor.AssertCurrentThreadOwns();
MOZ_ASSERT(mLockCount == 1,
"Should only optimize when holding the lock exclusively");
// Don't optimize during shutdown because gfxPlatform may not be available.
if (ShutdownTracker::ShutdownHasStarted())
return NS_OK;
if (!mOptimizable || gDisableOptimize)
return NS_OK;
if (mPalettedImageData || mOptSurface || mSinglePixel)
return NS_OK;
// Don't do single-color opts on non-premult data.
// Cairo doesn't support non-premult single-colors.
if (mNonPremult)
return NS_OK;
/* Figure out if the entire image is a constant color */
if (gfxPrefs::ImageSingleColorOptimizationEnabled() &&
mImageSurface->Stride() == mSize.width * 4) {
uint32_t *imgData = (uint32_t*) ((uint8_t *)mVBufPtr);
uint32_t firstPixel = * (uint32_t*) imgData;
uint32_t pixelCount = mSize.width * mSize.height + 1;
while (--pixelCount && *imgData++ == firstPixel)
;
if (pixelCount == 0) {
// all pixels were the same
if (mFormat == SurfaceFormat::B8G8R8A8 ||
mFormat == SurfaceFormat::B8G8R8X8) {
mSinglePixel = true;
mSinglePixelColor.a = ((firstPixel >> 24) & 0xFF) * (1.0f / 255.0f);
mSinglePixelColor.r = ((firstPixel >> 16) & 0xFF) * (1.0f / 255.0f);
mSinglePixelColor.g = ((firstPixel >> 8) & 0xFF) * (1.0f / 255.0f);
mSinglePixelColor.b = ((firstPixel >> 0) & 0xFF) * (1.0f / 255.0f);
mSinglePixelColor.r /= mSinglePixelColor.a;
mSinglePixelColor.g /= mSinglePixelColor.a;
mSinglePixelColor.b /= mSinglePixelColor.a;
// blow away the older surfaces (if they exist), to release their memory
mVBuf = nullptr;
mVBufPtr = nullptr;
mImageSurface = nullptr;
mOptSurface = nullptr;
return NS_OK;
}
}
// if it's not RGB24/ARGB32, don't optimize, but we never hit this at the moment
}
#ifdef ANDROID
SurfaceFormat optFormat =
gfxPlatform::GetPlatform()->Optimal2DFormatForContent(gfxContentType::COLOR);
if (mFormat != SurfaceFormat::B8G8R8A8 &&
optFormat == SurfaceFormat::R5G6B5) {
RefPtr<VolatileBuffer> buf =
AllocateBufferForImage(mSize, optFormat);
if (!buf)
return NS_OK;
RefPtr<DataSourceSurface> surf =
CreateLockedSurface(buf, mSize, optFormat);
if (!surf)
return NS_ERROR_OUT_OF_MEMORY;
DataSourceSurface::MappedSurface mapping;
if (!surf->Map(DataSourceSurface::MapType::WRITE, &mapping)) {
gfxCriticalError() << "imgFrame::Optimize failed to map surface";
return NS_ERROR_FAILURE;
}
RefPtr<DrawTarget> target =
Factory::CreateDrawTargetForData(BackendType::CAIRO,
mapping.mData,
mSize,
mapping.mStride,
optFormat);
if (!target) {
gfxWarning() << "imgFrame::Optimize failed in CreateDrawTargetForData";
return NS_ERROR_OUT_OF_MEMORY;
}
Rect rect(0, 0, mSize.width, mSize.height);
target->DrawSurface(mImageSurface, rect, rect);
target->Flush();
surf->Unmap();
mImageSurface = surf;
mVBuf = buf;
mFormat = optFormat;
}
#else
mOptSurface = gfxPlatform::GetPlatform()->ScreenReferenceDrawTarget()->OptimizeSourceSurface(mImageSurface);
if (mOptSurface == mImageSurface)
mOptSurface = nullptr;
#endif
if (mOptSurface) {
mVBuf = nullptr;
mVBufPtr = nullptr;
mImageSurface = nullptr;
}
#ifdef MOZ_WIDGET_ANDROID
// On Android, free mImageSurface unconditionally if we're discardable. This
// allows the operating system to free our volatile buffer.
// XXX(seth): We'd eventually like to do this on all platforms, but right now
// converting raw memory to a SourceSurface is expensive on some backends.
mImageSurface = nullptr;
#endif
return NS_OK;
}
DrawableFrameRef
imgFrame::DrawableRef()
{
return DrawableFrameRef(this);
}
RawAccessFrameRef
imgFrame::RawAccessRef()
{
return RawAccessFrameRef(this);
}
void
imgFrame::SetRawAccessOnly()
{
AssertImageDataLocked();
// Lock our data and throw away the key.
LockImageData();
}
imgFrame::SurfaceWithFormat
imgFrame::SurfaceForDrawing(bool aDoPadding,
bool aDoPartialDecode,
bool aDoTile,
gfxContext* aContext,
const nsIntMargin& aPadding,
gfxRect& aImageRect,
ImageRegion& aRegion,
SourceSurface* aSurface)
{
MOZ_ASSERT(NS_IsMainThread());
mMonitor.AssertCurrentThreadOwns();
IntSize size(int32_t(aImageRect.Width()), int32_t(aImageRect.Height()));
if (!aDoPadding && !aDoPartialDecode) {
NS_ASSERTION(!mSinglePixel, "This should already have been handled");
return SurfaceWithFormat(new gfxSurfaceDrawable(aSurface, size), mFormat);
}
gfxRect available = gfxRect(mDecoded.x, mDecoded.y, mDecoded.width, mDecoded.height);
if (aDoTile || mSinglePixel) {
// 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(size, SurfaceFormat::B8G8R8A8);
if (!target)
return SurfaceWithFormat();
// Fill 'available' with whatever we've got
if (mSinglePixel) {
target->FillRect(ToRect(aRegion.Intersect(available).Rect()),
ColorPattern(mSinglePixelColor),
DrawOptions(1.0f, CompositionOp::OP_SOURCE));
} else {
SurfacePattern pattern(aSurface,
ExtendMode::REPEAT,
Matrix::Translation(mDecoded.x, mDecoded.y));
target->FillRect(ToRect(aRegion.Intersect(available).Rect()), pattern);
}
RefPtr<SourceSurface> newsurf = target->Snapshot();
return SurfaceWithFormat(new gfxSurfaceDrawable(newsurf, size), target->GetFormat());
}
// Not tiling, and we have a surface, so we can account for
// padding and/or a partial decode just by twiddling parameters.
gfxPoint paddingTopLeft(aPadding.left, aPadding.top);
aRegion = aRegion.Intersect(available) - paddingTopLeft;
aContext->Multiply(gfxMatrix::Translation(paddingTopLeft));
aImageRect = gfxRect(0, 0, mSize.width, mSize.height);
gfxIntSize availableSize(mDecoded.width, mDecoded.height);
return SurfaceWithFormat(new gfxSurfaceDrawable(aSurface, availableSize),
mFormat);
}
bool imgFrame::Draw(gfxContext* aContext, const ImageRegion& aRegion,
GraphicsFilter aFilter, uint32_t aImageFlags)
{
PROFILER_LABEL("imgFrame", "Draw",
js::ProfileEntry::Category::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!");
NS_ASSERTION(!mPalettedImageData, "Directly drawing a paletted image!");
MonitorAutoLock lock(mMonitor);
nsIntMargin padding(mOffset.y,
mImageSize.width - (mOffset.x + mSize.width),
mImageSize.height - (mOffset.y + mSize.height),
mOffset.x);
bool doPadding = padding != nsIntMargin(0,0,0,0);
bool doPartialDecode = !IsImageCompleteInternal();
if (mSinglePixel && !doPadding && !doPartialDecode) {
if (mSinglePixelColor.a == 0.0) {
return true;
}
RefPtr<DrawTarget> dt = aContext->GetDrawTarget();
dt->FillRect(ToRect(aRegion.Rect()),
ColorPattern(mSinglePixelColor),
DrawOptions(1.0f,
CompositionOpForOp(aContext->CurrentOperator())));
return true;
}
RefPtr<SourceSurface> surf = GetSurfaceInternal();
if (!surf && !mSinglePixel) {
return false;
}
gfxRect imageRect(0, 0, mImageSize.width, mImageSize.height);
bool doTile = !imageRect.Contains(aRegion.Rect()) &&
!(aImageFlags & imgIContainer::FLAG_CLAMP);
ImageRegion region(aRegion);
// SurfaceForDrawing changes the current transform, and we need it to still
// be changed when we call gfxUtils::DrawPixelSnapped. We still need to
// restore it before returning though.
// XXXjwatt In general having functions require someone further up the stack
// to undo transform changes that they make is bad practice. We should
// change how this code works.
gfxContextMatrixAutoSaveRestore autoSR(aContext);
SurfaceWithFormat surfaceResult =
SurfaceForDrawing(doPadding, doPartialDecode, doTile, aContext,
padding, imageRect, region, surf);
if (surfaceResult.IsValid()) {
gfxUtils::DrawPixelSnapped(aContext, surfaceResult.mDrawable,
imageRect.Size(), region, surfaceResult.mFormat,
aFilter, aImageFlags);
}
return true;
}
nsresult
imgFrame::ImageUpdated(const nsIntRect& aUpdateRect)
{
MonitorAutoLock lock(mMonitor);
return ImageUpdatedInternal(aUpdateRect);
}
nsresult
imgFrame::ImageUpdatedInternal(const nsIntRect& aUpdateRect)
{
mMonitor.AssertCurrentThreadOwns();
mDecoded.UnionRect(mDecoded, aUpdateRect);
// clamp to bounds, in case someone sends a bogus updateRect (I'm looking at
// you, gif decoder)
nsIntRect boundsRect(mOffset, nsIntSize(mSize.width, mSize.height));
mDecoded.IntersectRect(mDecoded, boundsRect);
// If the image is now complete, wake up anyone who's waiting.
if (IsImageCompleteInternal()) {
mMonitor.NotifyAll();
}
return NS_OK;
}
void
imgFrame::Finish(Opacity aFrameOpacity /* = Opacity::SOME_TRANSPARENCY */,
DisposalMethod aDisposalMethod /* = DisposalMethod::KEEP */,
int32_t aRawTimeout /* = 0 */,
BlendMethod aBlendMethod /* = BlendMethod::OVER */)
{
MonitorAutoLock lock(mMonitor);
MOZ_ASSERT(mLockCount > 0, "Image data should be locked");
if (aFrameOpacity == Opacity::OPAQUE) {
mHasNoAlpha = true;
}
mDisposalMethod = aDisposalMethod;
mTimeout = aRawTimeout;
mBlendMethod = aBlendMethod;
ImageUpdatedInternal(GetRect());
}
nsIntRect imgFrame::GetRect() const
{
return nsIntRect(mOffset, nsIntSize(mSize.width, mSize.height));
}
int32_t
imgFrame::GetStride() const
{
mMonitor.AssertCurrentThreadOwns();
if (mImageSurface) {
return mImageSurface->Stride();
}
return VolatileSurfaceStride(mSize, mFormat);
}
SurfaceFormat imgFrame::GetFormat() const
{
MonitorAutoLock lock(mMonitor);
return mFormat;
}
uint32_t imgFrame::GetImageBytesPerRow() const
{
mMonitor.AssertCurrentThreadOwns();
if (mVBuf)
return mSize.width * BytesPerPixel(mFormat);
if (mPaletteDepth)
return mSize.width;
return 0;
}
uint32_t imgFrame::GetImageDataLength() const
{
return GetImageBytesPerRow() * mSize.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(mLockCount > 0, "Image data should be locked");
if (mImageSurface) {
*aData = mVBufPtr;
MOZ_ASSERT(*aData, "mImageSurface is non-null, but mVBufPtr is null in GetImageData");
} else if (mPalettedImageData) {
*aData = mPalettedImageData + PaletteDataLength();
MOZ_ASSERT(*aData, "mPalettedImageData is non-null, but result is null in GetImageData");
} else {
MOZ_ASSERT(false, "Have neither mImageSurface nor mPalettedImageData in GetImageData");
*aData = nullptr;
}
*aLength = GetImageDataLength();
}
uint8_t* imgFrame::GetImageData() const
{
uint8_t *data;
uint32_t length;
GetImageData(&data, &length);
return data;
}
bool imgFrame::GetIsPaletted() const
{
return mPalettedImageData != nullptr;
}
void imgFrame::GetPaletteData(uint32_t **aPalette, uint32_t *length) const
{
AssertImageDataLocked();
if (!mPalettedImageData) {
*aPalette = nullptr;
*length = 0;
} else {
*aPalette = (uint32_t *) mPalettedImageData;
*length = PaletteDataLength();
}
}
uint32_t* imgFrame::GetPaletteData() const
{
uint32_t* data;
uint32_t length;
GetPaletteData(&data, &length);
return data;
}
nsresult
imgFrame::LockImageData()
{
MonitorAutoLock lock(mMonitor);
MOZ_ASSERT(mLockCount >= 0, "Unbalanced locks and unlocks");
if (mLockCount < 0) {
return NS_ERROR_FAILURE;
}
mLockCount++;
// If we are not the first lock, there's nothing to do.
if (mLockCount != 1) {
return NS_OK;
}
// If we're the first lock, but have an image surface, we're OK.
if (mImageSurface) {
mVBufPtr = mVBuf;
return NS_OK;
}
// Paletted images don't have surfaces, so there's nothing to do.
if (mPalettedImageData) {
return NS_OK;
}
double imgPixelSize = mSize.width * mSize.height;
if (imgPixelSize < (8092 * 8092)) {
// We should be safe to Deoptimize at this size (<64Mpix)
return Deoptimize();
}
return NS_ERROR_FAILURE;
}
nsresult
imgFrame::Deoptimize()
{
MOZ_ASSERT(NS_IsMainThread());
mMonitor.AssertCurrentThreadOwns();
MOZ_ASSERT(!mImageSurface);
if (!mImageSurface) {
if (mVBuf) {
VolatileBufferPtr<uint8_t> ref(mVBuf);
if (ref.WasBufferPurged())
return NS_ERROR_FAILURE;
mImageSurface = CreateLockedSurface(mVBuf, mSize, mFormat);
if (!mImageSurface)
return NS_ERROR_OUT_OF_MEMORY;
}
if (mOptSurface || mSinglePixel || mFormat == SurfaceFormat::R5G6B5) {
SurfaceFormat format = mFormat;
if (mFormat == SurfaceFormat::R5G6B5)
format = SurfaceFormat::B8G8R8A8;
// Recover the pixels
RefPtr<VolatileBuffer> buf =
AllocateBufferForImage(mSize, format);
if (!buf) {
return NS_ERROR_OUT_OF_MEMORY;
}
RefPtr<DataSourceSurface> surf =
CreateLockedSurface(buf, mSize, format);
if (!surf)
return NS_ERROR_OUT_OF_MEMORY;
DataSourceSurface::MappedSurface mapping;
if (!surf->Map(DataSourceSurface::MapType::WRITE, &mapping)) {
gfxCriticalError() << "imgFrame::Deoptimize failed to map surface";
return NS_ERROR_FAILURE;
}
RefPtr<DrawTarget> target =
Factory::CreateDrawTargetForData(BackendType::CAIRO,
mapping.mData,
mSize,
mapping.mStride,
format);
if (!target) {
gfxWarning() << "imgFrame::Deoptimize failed in CreateDrawTargetForData";
return NS_ERROR_OUT_OF_MEMORY;
}
Rect rect(0, 0, mSize.width, mSize.height);
if (mSinglePixel)
target->FillRect(rect, ColorPattern(mSinglePixelColor),
DrawOptions(1.0f, CompositionOp::OP_SOURCE));
else if (mFormat == SurfaceFormat::R5G6B5)
target->DrawSurface(mImageSurface, rect, rect);
else
target->DrawSurface(mOptSurface, rect, rect,
DrawSurfaceOptions(),
DrawOptions(1.0f, CompositionOp::OP_SOURCE));
target->Flush();
surf->Unmap();
mFormat = format;
mVBuf = buf;
mImageSurface = surf;
mOptSurface = nullptr;
}
}
mVBufPtr = mVBuf;
return NS_OK;
}
void
imgFrame::AssertImageDataLocked() const
{
#ifdef DEBUG
MonitorAutoLock lock(mMonitor);
MOZ_ASSERT(mLockCount > 0, "Image data should be locked");
#endif
}
class UnlockImageDataRunnable : public nsRunnable
{
public:
explicit UnlockImageDataRunnable(imgFrame* aTarget)
: mTarget(aTarget)
{
MOZ_ASSERT(mTarget);
}
NS_IMETHOD Run() { return mTarget->UnlockImageData(); }
private:
nsRefPtr<imgFrame> mTarget;
};
nsresult
imgFrame::UnlockImageData()
{
MonitorAutoLock lock(mMonitor);
MOZ_ASSERT(mLockCount > 0, "Unlocking an unlocked image!");
if (mLockCount <= 0) {
return NS_ERROR_FAILURE;
}
MOZ_ASSERT(mLockCount > 1 || IsImageCompleteInternal() || mAborted,
"Should have marked complete or aborted before unlocking");
// If we're about to become unlocked, we don't need to hold on to our data
// surface anymore. (But we don't need to do anything for paletted images,
// which don't have surfaces.)
if (mLockCount == 1 && !mPalettedImageData) {
// We can't safely optimize off-main-thread, so create a runnable to do it.
if (!NS_IsMainThread()) {
nsCOMPtr<nsIRunnable> runnable = new UnlockImageDataRunnable(this);
NS_DispatchToMainThread(runnable);
return NS_OK;
}
// If we're using a surface format with alpha but the image has no alpha,
// change the format. This doesn't change the underlying data at all, but
// allows DrawTargets to avoid blending when drawing known opaque images.
if (mHasNoAlpha && mFormat == SurfaceFormat::B8G8R8A8 && mImageSurface) {
mFormat = SurfaceFormat::B8G8R8X8;
mImageSurface = CreateLockedSurface(mVBuf, mSize, mFormat);
}
// Convert the data surface to a GPU surface or a single color if possible.
// This will also release mImageSurface if possible.
Optimize();
// Allow the OS to release our data surface.
mVBufPtr = nullptr;
}
mLockCount--;
return NS_OK;
}
void
imgFrame::SetOptimizable()
{
MOZ_ASSERT(NS_IsMainThread());
AssertImageDataLocked();
mOptimizable = true;
}
Color
imgFrame::SinglePixelColor() const
{
MOZ_ASSERT(NS_IsMainThread());
return mSinglePixelColor;
}
bool
imgFrame::IsSinglePixel() const
{
MOZ_ASSERT(NS_IsMainThread());
return mSinglePixel;
}
already_AddRefed<SourceSurface>
imgFrame::GetSurface()
{
MonitorAutoLock lock(mMonitor);
return GetSurfaceInternal();
}
already_AddRefed<SourceSurface>
imgFrame::GetSurfaceInternal()
{
mMonitor.AssertCurrentThreadOwns();
if (mOptSurface) {
if (mOptSurface->IsValid()) {
RefPtr<SourceSurface> surf(mOptSurface);
return surf.forget();
} else {
mOptSurface = nullptr;
}
}
if (mImageSurface) {
RefPtr<SourceSurface> surf(mImageSurface);
return surf.forget();
}
if (!mVBuf)
return nullptr;
VolatileBufferPtr<char> buf(mVBuf);
if (buf.WasBufferPurged())
return nullptr;
return CreateLockedSurface(mVBuf, mSize, mFormat);
}
already_AddRefed<DrawTarget>
imgFrame::GetDrawTarget()
{
MonitorAutoLock lock(mMonitor);
uint8_t* data;
uint32_t length;
GetImageDataInternal(&data, &length);
if (!data) {
return nullptr;
}
int32_t stride = GetStride();
return gfxPlatform::GetPlatform()->
CreateDrawTargetForData(data, mSize, stride, mFormat);
}
AnimationData
imgFrame::GetAnimationData() const
{
MonitorAutoLock lock(mMonitor);
MOZ_ASSERT(mLockCount > 0, "Image data should be locked");
uint8_t* data;
if (mPalettedImageData) {
data = mPalettedImageData;
} else {
uint32_t length;
GetImageDataInternal(&data, &length);
}
bool hasAlpha = mFormat == SurfaceFormat::B8G8R8A8;
return AnimationData(data, PaletteDataLength(), mTimeout, GetRect(),
mBlendMethod, mDisposalMethod, hasAlpha);
}
ScalingData
imgFrame::GetScalingData() const
{
MonitorAutoLock lock(mMonitor);
MOZ_ASSERT(mLockCount > 0, "Image data should be locked");
MOZ_ASSERT(!GetIsPaletted(), "GetScalingData can't handle paletted images");
uint8_t* data;
uint32_t length;
GetImageDataInternal(&data, &length);
return ScalingData(data, mSize, GetImageBytesPerRow(), mFormat);
}
void
imgFrame::Abort()
{
MonitorAutoLock lock(mMonitor);
mAborted = true;
// Wake up anyone who's waiting.
mMonitor.NotifyAll();
}
bool
imgFrame::IsImageComplete() const
{
MonitorAutoLock lock(mMonitor);
return IsImageCompleteInternal();
}
void
imgFrame::WaitUntilComplete() const
{
MonitorAutoLock lock(mMonitor);
while (true) {
// Return if we're aborted or complete.
if (mAborted || IsImageCompleteInternal()) {
return;
}
// Not complete yet, so we'll have to wait.
mMonitor.Wait();
}
}
bool
imgFrame::IsImageCompleteInternal() const
{
mMonitor.AssertCurrentThreadOwns();
return mDecoded.IsEqualInterior(nsIntRect(mOffset.x, mOffset.y,
mSize.width, mSize.height));
}
bool imgFrame::GetCompositingFailed() const
{
MOZ_ASSERT(NS_IsMainThread());
return mCompositingFailed;
}
void imgFrame::SetCompositingFailed(bool val)
{
MOZ_ASSERT(NS_IsMainThread());
mCompositingFailed = val;
}
size_t
imgFrame::SizeOfExcludingThis(gfxMemoryLocation aLocation,
MallocSizeOf aMallocSizeOf) const
{
MonitorAutoLock lock(mMonitor);
// aMallocSizeOf is only used if aLocation==gfxMemoryLocation::IN_PROCESS_HEAP. It
// should be nullptr otherwise.
MOZ_ASSERT(
(aLocation == gfxMemoryLocation::IN_PROCESS_HEAP && aMallocSizeOf) ||
(aLocation != gfxMemoryLocation::IN_PROCESS_HEAP && !aMallocSizeOf),
"mismatch between aLocation and aMallocSizeOf");
size_t n = 0;
if (mPalettedImageData && aLocation == gfxMemoryLocation::IN_PROCESS_HEAP) {
n += aMallocSizeOf(mPalettedImageData);
}
if (mImageSurface && aLocation == gfxMemoryLocation::IN_PROCESS_HEAP) {
n += aMallocSizeOf(mImageSurface);
}
if (mOptSurface && aLocation == gfxMemoryLocation::IN_PROCESS_HEAP) {
n += aMallocSizeOf(mOptSurface);
}
if (mVBuf && aLocation == gfxMemoryLocation::IN_PROCESS_HEAP) {
n += aMallocSizeOf(mVBuf);
n += mVBuf->HeapSizeOfExcludingThis(aMallocSizeOf);
}
if (mVBuf && aLocation == gfxMemoryLocation::IN_PROCESS_NONHEAP) {
n += mVBuf->NonHeapSizeOfExcludingThis();
}
return n;
}
} // namespace image
} // namespace mozilla