michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "BasicLayersImpl.h" // for FillRectWithMask, etc michael@0: #include "ImageContainer.h" // for AutoLockImage, etc michael@0: #include "ImageLayers.h" // for ImageLayer michael@0: #include "Layers.h" // for Layer (ptr only), etc michael@0: #include "basic/BasicImplData.h" // for BasicImplData michael@0: #include "basic/BasicLayers.h" // for BasicLayerManager michael@0: #include "mozilla/mozalloc.h" // for operator new michael@0: #include "nsAutoPtr.h" // for nsRefPtr, getter_AddRefs, etc michael@0: #include "nsCOMPtr.h" // for already_AddRefed michael@0: #include "nsDebug.h" // for NS_ASSERTION michael@0: #include "nsISupportsImpl.h" // for gfxPattern::Release, etc michael@0: #include "nsRect.h" // for nsIntRect michael@0: #include "nsRegion.h" // for nsIntRegion michael@0: #include "mozilla/gfx/Point.h" // for IntSize michael@0: michael@0: using namespace mozilla::gfx; michael@0: michael@0: namespace mozilla { michael@0: namespace layers { michael@0: michael@0: class BasicImageLayer : public ImageLayer, public BasicImplData { michael@0: public: michael@0: BasicImageLayer(BasicLayerManager* aLayerManager) : michael@0: ImageLayer(aLayerManager, michael@0: static_cast(MOZ_THIS_IN_INITIALIZER_LIST())), michael@0: mSize(-1, -1) michael@0: { michael@0: MOZ_COUNT_CTOR(BasicImageLayer); michael@0: } michael@0: virtual ~BasicImageLayer() michael@0: { michael@0: MOZ_COUNT_DTOR(BasicImageLayer); michael@0: } michael@0: michael@0: virtual void SetVisibleRegion(const nsIntRegion& aRegion) michael@0: { michael@0: NS_ASSERTION(BasicManager()->InConstruction(), michael@0: "Can only set properties in construction phase"); michael@0: ImageLayer::SetVisibleRegion(aRegion); michael@0: } michael@0: michael@0: virtual void Paint(DrawTarget* aDT, michael@0: const gfx::Point& aDeviceOffset, michael@0: Layer* aMaskLayer) MOZ_OVERRIDE; michael@0: michael@0: virtual TemporaryRef GetAsSourceSurface() MOZ_OVERRIDE; michael@0: michael@0: protected: michael@0: BasicLayerManager* BasicManager() michael@0: { michael@0: return static_cast(mManager); michael@0: } michael@0: michael@0: // only paints the image if aContext is non-null michael@0: void michael@0: GetAndPaintCurrentImage(DrawTarget* aTarget, michael@0: float aOpacity, michael@0: SourceSurface* aMaskSurface); michael@0: michael@0: gfx::IntSize mSize; michael@0: }; michael@0: michael@0: void michael@0: BasicImageLayer::Paint(DrawTarget* aDT, michael@0: const gfx::Point& aDeviceOffset, michael@0: Layer* aMaskLayer) michael@0: { michael@0: if (IsHidden() || !mContainer) { michael@0: return; michael@0: } michael@0: michael@0: mContainer->SetImageFactory(mManager->IsCompositingCheap() ? nullptr : BasicManager()->GetImageFactory()); michael@0: michael@0: RefPtr surface; michael@0: AutoLockImage autoLock(mContainer, &surface); michael@0: Image *image = autoLock.GetImage(); michael@0: gfx::IntSize size = mSize = autoLock.GetSize(); michael@0: michael@0: if (!surface || !surface->IsValid()) { michael@0: return; michael@0: } michael@0: michael@0: FillRectWithMask(aDT, aDeviceOffset, Rect(0, 0, size.width, size.height), michael@0: surface, ToFilter(mFilter), michael@0: DrawOptions(GetEffectiveOpacity(), GetEffectiveOperator(this)), michael@0: aMaskLayer); michael@0: michael@0: GetContainer()->NotifyPaintedImage(image); michael@0: } michael@0: michael@0: void michael@0: BasicImageLayer::GetAndPaintCurrentImage(DrawTarget* aTarget, michael@0: float aOpacity, michael@0: SourceSurface* aMaskSurface) michael@0: { michael@0: if (!mContainer) { michael@0: return; michael@0: } michael@0: michael@0: mContainer->SetImageFactory(mManager->IsCompositingCheap() ? michael@0: nullptr : michael@0: BasicManager()->GetImageFactory()); michael@0: IntSize size; michael@0: Image* image = nullptr; michael@0: RefPtr surf = michael@0: mContainer->LockCurrentAsSourceSurface(&size, &image); michael@0: michael@0: if (!surf) { michael@0: return; michael@0: } michael@0: michael@0: if (aTarget) { michael@0: // The visible region can extend outside the image, so just draw michael@0: // within the image bounds. michael@0: SurfacePattern pat(surf, ExtendMode::CLAMP, Matrix(), ToFilter(mFilter)); michael@0: CompositionOp op = GetEffectiveOperator(this); michael@0: DrawOptions opts(aOpacity, op); michael@0: michael@0: aTarget->MaskSurface(pat, aMaskSurface, Point(0, 0), opts); michael@0: michael@0: GetContainer()->NotifyPaintedImage(image); michael@0: } michael@0: michael@0: mContainer->UnlockCurrentImage(); michael@0: } michael@0: michael@0: TemporaryRef michael@0: BasicImageLayer::GetAsSourceSurface() michael@0: { michael@0: if (!mContainer) { michael@0: return nullptr; michael@0: } michael@0: michael@0: gfx::IntSize dontCare; michael@0: return mContainer->GetCurrentAsSourceSurface(&dontCare); michael@0: } michael@0: michael@0: already_AddRefed michael@0: BasicLayerManager::CreateImageLayer() michael@0: { michael@0: NS_ASSERTION(InConstruction(), "Only allowed in construction phase"); michael@0: nsRefPtr layer = new BasicImageLayer(this); michael@0: return layer.forget(); michael@0: } michael@0: michael@0: } michael@0: }