2012-06-29 10:01:34 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* 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/. */
|
|
|
|
|
2013-08-12 03:17:23 +04:00
|
|
|
#include "BasicLayersImpl.h" // for FillWithMask, etc
|
|
|
|
#include "ImageContainer.h" // for AutoLockImage, etc
|
|
|
|
#include "ImageLayers.h" // for ImageLayer
|
|
|
|
#include "Layers.h" // for Layer (ptr only), etc
|
|
|
|
#include "basic/BasicImplData.h" // for BasicImplData
|
|
|
|
#include "basic/BasicLayers.h" // for BasicLayerManager
|
|
|
|
#include "gfxASurface.h" // for gfxASurface, etc
|
|
|
|
#include "gfxContext.h" // for gfxContext
|
|
|
|
#include "gfxPattern.h" // for gfxPattern, etc
|
|
|
|
#include "gfxUtils.h" // for gfxUtils
|
2012-08-14 15:47:49 +04:00
|
|
|
#ifdef MOZ_X11
|
2013-08-12 03:17:23 +04:00
|
|
|
#include "gfxXlibSurface.h" // for gfxXlibSurface
|
2012-08-14 15:47:49 +04:00
|
|
|
#endif
|
2013-08-12 03:17:23 +04:00
|
|
|
#include "mozilla/mozalloc.h" // for operator new
|
|
|
|
#include "nsAutoPtr.h" // for nsRefPtr, getter_AddRefs, etc
|
|
|
|
#include "nsCOMPtr.h" // for already_AddRefed
|
|
|
|
#include "nsDebug.h" // for NS_ASSERTION
|
|
|
|
#include "nsISupportsImpl.h" // for gfxPattern::Release, etc
|
|
|
|
#include "nsRect.h" // for nsIntRect
|
|
|
|
#include "nsRegion.h" // for nsIntRegion
|
2013-12-13 21:32:02 +04:00
|
|
|
#include "mozilla/gfx/Point.h" // for IntSize
|
2012-06-29 10:01:34 +04:00
|
|
|
|
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
|
|
|
namespace mozilla {
|
2012-07-12 16:51:57 +04:00
|
|
|
namespace layers {
|
2012-06-29 10:01:34 +04:00
|
|
|
|
|
|
|
class BasicImageLayer : public ImageLayer, public BasicImplData {
|
|
|
|
public:
|
|
|
|
BasicImageLayer(BasicLayerManager* aLayerManager) :
|
2013-08-16 17:18:36 +04:00
|
|
|
ImageLayer(aLayerManager,
|
|
|
|
static_cast<BasicImplData*>(MOZ_THIS_IN_INITIALIZER_LIST())),
|
2012-06-29 10:01:34 +04:00
|
|
|
mSize(-1, -1)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(BasicImageLayer);
|
|
|
|
}
|
|
|
|
virtual ~BasicImageLayer()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(BasicImageLayer);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void SetVisibleRegion(const nsIntRegion& aRegion)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(BasicManager()->InConstruction(),
|
|
|
|
"Can only set properties in construction phase");
|
|
|
|
ImageLayer::SetVisibleRegion(aRegion);
|
|
|
|
}
|
|
|
|
|
2014-02-12 19:07:46 +04:00
|
|
|
virtual void Paint(DrawTarget* aTarget, SourceSurface* aMaskSurface);
|
|
|
|
virtual void DeprecatedPaint(gfxContext* aContext, Layer* aMaskLayer);
|
2012-06-29 10:01:34 +04:00
|
|
|
|
2012-07-12 16:51:58 +04:00
|
|
|
virtual bool GetAsSurface(gfxASurface** aSurface,
|
|
|
|
SurfaceDescriptor* aDescriptor);
|
2012-06-29 10:01:34 +04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
BasicLayerManager* BasicManager()
|
|
|
|
{
|
|
|
|
return static_cast<BasicLayerManager*>(mManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
// only paints the image if aContext is non-null
|
2014-02-12 19:07:47 +04:00
|
|
|
void
|
|
|
|
GetAndPaintCurrentImage(DrawTarget* aTarget,
|
2012-06-29 10:01:34 +04:00
|
|
|
float aOpacity,
|
2014-02-12 19:07:47 +04:00
|
|
|
SourceSurface* aMaskSurface);
|
|
|
|
already_AddRefed<gfxPattern>
|
|
|
|
DeprecatedGetAndPaintCurrentImage(gfxContext* aContext,
|
|
|
|
float aOpacity,
|
|
|
|
Layer* aMaskLayer);
|
2012-06-29 10:01:34 +04:00
|
|
|
|
2013-12-13 21:32:02 +04:00
|
|
|
gfx::IntSize mSize;
|
2012-06-29 10:01:34 +04:00
|
|
|
};
|
|
|
|
|
2014-02-12 19:07:47 +04:00
|
|
|
static void
|
|
|
|
DeprecatedPaintContext(gfxPattern* aPattern,
|
|
|
|
const nsIntRegion& aVisible,
|
|
|
|
float aOpacity,
|
|
|
|
gfxContext* aContext,
|
|
|
|
Layer* aMaskLayer);
|
|
|
|
|
2012-06-29 10:01:34 +04:00
|
|
|
void
|
2014-02-12 19:07:46 +04:00
|
|
|
BasicImageLayer::Paint(DrawTarget* aTarget, SourceSurface* aMaskSurface)
|
|
|
|
{
|
2014-02-12 19:07:47 +04:00
|
|
|
if (IsHidden()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GetAndPaintCurrentImage(aTarget, GetEffectiveOpacity(), aMaskSurface);
|
2014-02-12 19:07:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BasicImageLayer::DeprecatedPaint(gfxContext* aContext, Layer* aMaskLayer)
|
2012-06-29 10:01:34 +04:00
|
|
|
{
|
2014-02-12 19:07:47 +04:00
|
|
|
if (IsHidden()) {
|
2012-06-29 10:01:34 +04:00
|
|
|
return;
|
2014-02-12 19:07:47 +04:00
|
|
|
}
|
2012-06-29 10:01:34 +04:00
|
|
|
nsRefPtr<gfxPattern> dontcare =
|
2014-02-12 19:07:47 +04:00
|
|
|
DeprecatedGetAndPaintCurrentImage(aContext,
|
|
|
|
GetEffectiveOpacity(),
|
|
|
|
aMaskLayer);
|
2012-06-29 10:01:34 +04:00
|
|
|
}
|
|
|
|
|
2014-02-12 19:07:47 +04:00
|
|
|
void
|
|
|
|
BasicImageLayer::GetAndPaintCurrentImage(DrawTarget* aTarget,
|
2012-06-29 10:01:34 +04:00
|
|
|
float aOpacity,
|
2014-02-12 19:07:47 +04:00
|
|
|
SourceSurface* aMaskSurface)
|
|
|
|
{
|
|
|
|
if (!mContainer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mContainer->SetImageFactory(mManager->IsCompositingCheap() ?
|
|
|
|
nullptr :
|
|
|
|
BasicManager()->GetImageFactory());
|
|
|
|
IntSize size;
|
|
|
|
Image* image = nullptr;
|
|
|
|
RefPtr<SourceSurface> surf =
|
|
|
|
mContainer->LockCurrentAsSourceSurface(&size, &image);
|
|
|
|
|
|
|
|
if (!surf) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aTarget) {
|
|
|
|
// The visible region can extend outside the image, so just draw
|
|
|
|
// within the image bounds.
|
|
|
|
SurfacePattern pat(surf, ExtendMode::CLAMP, Matrix(), ToFilter(mFilter));
|
2014-02-27 20:56:48 +04:00
|
|
|
CompositionOp op = GetEffectiveOperator(this);
|
2014-02-12 19:07:47 +04:00
|
|
|
DrawOptions opts(aOpacity, op);
|
|
|
|
|
|
|
|
aTarget->MaskSurface(pat, aMaskSurface, Point(0, 0), opts);
|
|
|
|
|
|
|
|
GetContainer()->NotifyPaintedImage(image);
|
|
|
|
}
|
|
|
|
|
|
|
|
mContainer->UnlockCurrentImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxPattern>
|
|
|
|
BasicImageLayer::DeprecatedGetAndPaintCurrentImage(gfxContext* aContext,
|
|
|
|
float aOpacity,
|
|
|
|
Layer* aMaskLayer)
|
2012-06-29 10:01:34 +04:00
|
|
|
{
|
|
|
|
if (!mContainer)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-06-29 10:01:34 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mContainer->SetImageFactory(mManager->IsCompositingCheap() ? nullptr : BasicManager()->GetImageFactory());
|
2012-06-29 10:01:34 +04:00
|
|
|
|
2014-02-12 19:24:21 +04:00
|
|
|
RefPtr<gfx::SourceSurface> surface;
|
|
|
|
AutoLockImage autoLock(mContainer, &surface);
|
2012-06-29 10:01:34 +04:00
|
|
|
Image *image = autoLock.GetImage();
|
2013-12-13 21:32:02 +04:00
|
|
|
gfx::IntSize size = mSize = autoLock.GetSize();
|
2012-06-29 10:01:34 +04:00
|
|
|
|
2014-02-12 19:24:21 +04:00
|
|
|
if (!surface || !surface->IsValid()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-06-29 10:01:34 +04:00
|
|
|
}
|
|
|
|
|
2014-02-12 19:24:21 +04:00
|
|
|
nsRefPtr<gfxPattern> pat = new gfxPattern(surface, gfx::Matrix());
|
2012-06-29 10:01:34 +04:00
|
|
|
if (!pat) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-06-29 10:01:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pat->SetFilter(mFilter);
|
|
|
|
|
|
|
|
// The visible region can extend outside the image, so just draw
|
|
|
|
// within the image bounds.
|
|
|
|
if (aContext) {
|
2014-02-27 20:56:48 +04:00
|
|
|
CompositionOp op = GetEffectiveOperator(this);
|
2014-02-12 19:07:46 +04:00
|
|
|
AutoSetOperator setOptimizedOperator(aContext, ThebesOp(op));
|
2013-09-16 03:43:10 +04:00
|
|
|
|
2014-02-12 19:07:47 +04:00
|
|
|
DeprecatedPaintContext(pat,
|
2012-06-29 10:01:34 +04:00
|
|
|
nsIntRegion(nsIntRect(0, 0, size.width, size.height)),
|
|
|
|
aOpacity, aContext, aMaskLayer);
|
|
|
|
|
|
|
|
GetContainer()->NotifyPaintedImage(image);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pat.forget();
|
|
|
|
}
|
|
|
|
|
2014-02-12 19:07:47 +04:00
|
|
|
static void
|
|
|
|
DeprecatedPaintContext(gfxPattern* aPattern,
|
|
|
|
const nsIntRegion& aVisible,
|
|
|
|
float aOpacity,
|
|
|
|
gfxContext* aContext,
|
|
|
|
Layer* aMaskLayer)
|
2012-06-29 10:01:34 +04:00
|
|
|
{
|
|
|
|
// Set PAD mode so that when the video is being scaled, we do not sample
|
|
|
|
// outside the bounds of the video image.
|
|
|
|
gfxPattern::GraphicsExtend extend = gfxPattern::EXTEND_PAD;
|
|
|
|
|
2012-08-14 15:47:49 +04:00
|
|
|
#ifdef MOZ_X11
|
|
|
|
// PAD is slow with cairo and old X11 servers, so prefer speed over
|
|
|
|
// correctness and use NONE.
|
2012-06-29 10:01:34 +04:00
|
|
|
if (aContext->IsCairo()) {
|
|
|
|
nsRefPtr<gfxASurface> target = aContext->CurrentSurface();
|
2014-01-23 22:26:40 +04:00
|
|
|
if (target->GetType() == gfxSurfaceType::Xlib &&
|
2012-08-14 15:47:49 +04:00
|
|
|
static_cast<gfxXlibSurface*>(target.get())->IsPadSlow()) {
|
2012-06-29 10:01:34 +04:00
|
|
|
extend = gfxPattern::EXTEND_NONE;
|
|
|
|
}
|
|
|
|
}
|
2012-08-14 15:47:49 +04:00
|
|
|
#endif
|
2012-06-29 10:01:34 +04:00
|
|
|
|
|
|
|
aContext->NewPath();
|
|
|
|
// No need to snap here; our transform has already taken care of it.
|
|
|
|
// XXX true for arbitrary regions? Don't care yet though
|
|
|
|
gfxUtils::PathFromRegion(aContext, aVisible);
|
|
|
|
aPattern->SetExtend(extend);
|
|
|
|
aContext->SetPattern(aPattern);
|
|
|
|
FillWithMask(aContext, aOpacity, aMaskLayer);
|
|
|
|
|
|
|
|
// Reset extend mode for callers that need to reuse the pattern
|
|
|
|
aPattern->SetExtend(extend);
|
|
|
|
}
|
|
|
|
|
2012-07-12 16:51:58 +04:00
|
|
|
bool
|
|
|
|
BasicImageLayer::GetAsSurface(gfxASurface** aSurface,
|
|
|
|
SurfaceDescriptor* aDescriptor)
|
2012-06-29 10:01:34 +04:00
|
|
|
{
|
|
|
|
if (!mContainer) {
|
2012-07-12 16:51:58 +04:00
|
|
|
return false;
|
2012-06-29 10:01:34 +04:00
|
|
|
}
|
|
|
|
|
2013-12-13 21:32:02 +04:00
|
|
|
gfx::IntSize dontCare;
|
2014-01-15 19:06:43 +04:00
|
|
|
nsRefPtr<gfxASurface> surface = mContainer->DeprecatedGetCurrentAsSurface(&dontCare);
|
2014-03-15 23:00:15 +04:00
|
|
|
surface.forget(aSurface);
|
2012-07-12 16:51:58 +04:00
|
|
|
return true;
|
2012-06-29 10:01:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<ImageLayer>
|
|
|
|
BasicLayerManager::CreateImageLayer()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(InConstruction(), "Only allowed in construction phase");
|
|
|
|
nsRefPtr<ImageLayer> layer = new BasicImageLayer(this);
|
|
|
|
return layer.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|