2010-08-13 17:30:14 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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/. */
|
2010-08-13 17:30:14 +04:00
|
|
|
|
|
|
|
#include "gfxDrawable.h"
|
|
|
|
#include "gfxASurface.h"
|
|
|
|
#include "gfxContext.h"
|
|
|
|
#include "gfxPlatform.h"
|
2013-12-17 22:33:34 +04:00
|
|
|
#include "gfxColor.h"
|
2014-07-29 04:07:32 +04:00
|
|
|
#include "gfx2DGlue.h"
|
2010-09-10 00:27:10 +04:00
|
|
|
#ifdef MOZ_X11
|
|
|
|
#include "cairo.h"
|
|
|
|
#include "gfxXlibSurface.h"
|
|
|
|
#endif
|
2010-08-13 17:30:14 +04:00
|
|
|
|
2013-11-13 08:31:12 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2013-11-27 05:05:03 +04:00
|
|
|
gfxSurfaceDrawable::gfxSurfaceDrawable(SourceSurface* aSurface,
|
|
|
|
const gfxIntSize aSize,
|
|
|
|
const gfxMatrix aTransform)
|
|
|
|
: gfxDrawable(aSize)
|
|
|
|
, mSourceSurface(aSurface)
|
|
|
|
, mTransform(aTransform)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-09-12 09:18:21 +04:00
|
|
|
bool
|
|
|
|
gfxSurfaceDrawable::DrawWithSamplingRect(gfxContext* aContext,
|
|
|
|
const gfxRect& aFillRect,
|
|
|
|
const gfxRect& aSamplingRect,
|
|
|
|
bool aRepeat,
|
|
|
|
const GraphicsFilter& aFilter,
|
|
|
|
gfxFloat aOpacity)
|
|
|
|
{
|
2014-09-23 00:49:40 +04:00
|
|
|
if (!mSourceSurface) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-12 09:18:21 +04:00
|
|
|
// When drawing with CLAMP we can expand the sampling rect to the nearest pixel
|
|
|
|
// without changing the result.
|
|
|
|
gfxRect samplingRect = aSamplingRect;
|
|
|
|
samplingRect.RoundOut();
|
|
|
|
IntRect intRect(samplingRect.x, samplingRect.y, samplingRect.width, samplingRect.height);
|
|
|
|
|
|
|
|
IntSize size = mSourceSurface->GetSize();
|
|
|
|
if (!IntRect(0, 0, size.width, size.height).Contains(intRect)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawInternal(aContext, aFillRect, intRect, false, aFilter, aOpacity, gfxMatrix());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2010-08-13 17:30:14 +04:00
|
|
|
gfxSurfaceDrawable::Draw(gfxContext* aContext,
|
|
|
|
const gfxRect& aFillRect,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aRepeat,
|
2013-10-02 01:01:19 +04:00
|
|
|
const GraphicsFilter& aFilter,
|
2014-08-29 18:04:34 +04:00
|
|
|
gfxFloat aOpacity,
|
2014-08-07 02:43:25 +04:00
|
|
|
const gfxMatrix& aTransform)
|
2014-09-12 09:18:21 +04:00
|
|
|
{
|
|
|
|
DrawInternal(aContext, aFillRect, IntRect(), aRepeat, aFilter, aOpacity, aTransform);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxSurfaceDrawable::DrawInternal(gfxContext* aContext,
|
|
|
|
const gfxRect& aFillRect,
|
|
|
|
const IntRect& aSamplingRect,
|
|
|
|
bool aRepeat,
|
|
|
|
const GraphicsFilter& aFilter,
|
|
|
|
gfxFloat aOpacity,
|
|
|
|
const gfxMatrix& aTransform)
|
2010-08-13 17:30:14 +04:00
|
|
|
{
|
2014-07-29 04:07:32 +04:00
|
|
|
ExtendMode extend = ExtendMode::CLAMP;
|
2014-07-29 04:05:40 +04:00
|
|
|
|
2010-08-13 17:30:14 +04:00
|
|
|
if (aRepeat) {
|
2014-07-29 04:07:32 +04:00
|
|
|
extend = ExtendMode::REPEAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix patternTransform = ToMatrix(aTransform * mTransform);
|
|
|
|
patternTransform.Invert();
|
|
|
|
|
|
|
|
SurfacePattern pattern(mSourceSurface, extend,
|
2014-09-12 09:18:21 +04:00
|
|
|
patternTransform, ToFilter(aFilter), aSamplingRect);
|
2014-07-29 04:07:32 +04:00
|
|
|
|
|
|
|
Rect fillRect = ToRect(aFillRect);
|
|
|
|
DrawTarget* dt = aContext->GetDrawTarget();
|
|
|
|
|
2014-11-20 13:40:04 +03:00
|
|
|
if (aContext->CurrentOperator() == gfxContext::OPERATOR_SOURCE &&
|
|
|
|
aOpacity == 1.0) {
|
2014-07-29 04:07:32 +04:00
|
|
|
// Emulate cairo operator source which is bound by mask!
|
|
|
|
dt->ClearRect(fillRect);
|
|
|
|
dt->FillRect(fillRect, pattern);
|
2010-12-12 08:18:27 +03:00
|
|
|
} else {
|
2014-09-15 15:10:30 +04:00
|
|
|
dt->FillRect(fillRect, pattern,
|
|
|
|
DrawOptions(aOpacity,
|
|
|
|
CompositionOpForOp(aContext->CurrentOperator()),
|
|
|
|
aContext->CurrentAntialiasMode()));
|
2010-08-13 17:30:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxCallbackDrawable::gfxCallbackDrawable(gfxDrawingCallback* aCallback,
|
|
|
|
const gfxIntSize aSize)
|
|
|
|
: gfxDrawable(aSize)
|
|
|
|
, mCallback(aCallback)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxSurfaceDrawable>
|
2013-10-02 01:01:19 +04:00
|
|
|
gfxCallbackDrawable::MakeSurfaceDrawable(const GraphicsFilter aFilter)
|
2010-08-13 17:30:14 +04:00
|
|
|
{
|
2014-04-16 06:23:05 +04:00
|
|
|
SurfaceFormat format =
|
|
|
|
gfxPlatform::GetPlatform()->Optimal2DFormatForContent(gfxContentType::COLOR_ALPHA);
|
|
|
|
RefPtr<DrawTarget> dt =
|
|
|
|
gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(mSize.ToIntSize(),
|
|
|
|
format);
|
|
|
|
if (!dt)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-08-13 17:30:14 +04:00
|
|
|
|
2014-04-16 06:23:05 +04:00
|
|
|
nsRefPtr<gfxContext> ctx = new gfxContext(dt);
|
2011-10-17 18:59:28 +04:00
|
|
|
Draw(ctx, gfxRect(0, 0, mSize.width, mSize.height), false, aFilter);
|
2014-04-16 06:23:05 +04:00
|
|
|
|
|
|
|
RefPtr<SourceSurface> surface = dt->Snapshot();
|
2010-08-13 17:30:14 +04:00
|
|
|
nsRefPtr<gfxSurfaceDrawable> drawable = new gfxSurfaceDrawable(surface, mSize);
|
|
|
|
return drawable.forget();
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2010-08-13 17:30:14 +04:00
|
|
|
gfxCallbackDrawable::Draw(gfxContext* aContext,
|
|
|
|
const gfxRect& aFillRect,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aRepeat,
|
2013-10-02 01:01:19 +04:00
|
|
|
const GraphicsFilter& aFilter,
|
2014-08-29 18:04:34 +04:00
|
|
|
gfxFloat aOpacity,
|
2014-08-07 02:43:25 +04:00
|
|
|
const gfxMatrix& aTransform)
|
2010-08-13 17:30:14 +04:00
|
|
|
{
|
2014-08-29 18:04:34 +04:00
|
|
|
if ((aRepeat || aOpacity != 1.0) && !mSurfaceDrawable) {
|
2010-08-13 17:30:14 +04:00
|
|
|
mSurfaceDrawable = MakeSurfaceDrawable(aFilter);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mSurfaceDrawable)
|
|
|
|
return mSurfaceDrawable->Draw(aContext, aFillRect, aRepeat, aFilter,
|
2014-08-29 18:04:34 +04:00
|
|
|
aOpacity, aTransform);
|
2010-08-13 17:30:14 +04:00
|
|
|
|
|
|
|
if (mCallback)
|
|
|
|
return (*mCallback)(aContext, aFillRect, aFilter, aTransform);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-08-13 17:30:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxPatternDrawable::gfxPatternDrawable(gfxPattern* aPattern,
|
|
|
|
const gfxIntSize aSize)
|
|
|
|
: gfxDrawable(aSize)
|
|
|
|
, mPattern(aPattern)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-10-16 02:00:28 +04:00
|
|
|
gfxPatternDrawable::~gfxPatternDrawable()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-08-13 17:30:14 +04:00
|
|
|
class DrawingCallbackFromDrawable : public gfxDrawingCallback {
|
|
|
|
public:
|
2014-08-08 05:17:30 +04:00
|
|
|
explicit DrawingCallbackFromDrawable(gfxDrawable* aDrawable)
|
2010-08-13 17:30:14 +04:00
|
|
|
: mDrawable(aDrawable) {
|
|
|
|
NS_ASSERTION(aDrawable, "aDrawable is null!");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~DrawingCallbackFromDrawable() {}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
virtual bool operator()(gfxContext* aContext,
|
2010-08-13 17:30:14 +04:00
|
|
|
const gfxRect& aFillRect,
|
2013-10-02 01:01:19 +04:00
|
|
|
const GraphicsFilter& aFilter,
|
2010-08-13 17:30:14 +04:00
|
|
|
const gfxMatrix& aTransform = gfxMatrix())
|
|
|
|
{
|
2014-08-29 18:04:34 +04:00
|
|
|
return mDrawable->Draw(aContext, aFillRect, false, aFilter, 1.0,
|
2010-08-13 17:30:14 +04:00
|
|
|
aTransform);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsRefPtr<gfxDrawable> mDrawable;
|
|
|
|
};
|
|
|
|
|
|
|
|
already_AddRefed<gfxCallbackDrawable>
|
|
|
|
gfxPatternDrawable::MakeCallbackDrawable()
|
|
|
|
{
|
|
|
|
nsRefPtr<gfxDrawingCallback> callback =
|
|
|
|
new DrawingCallbackFromDrawable(this);
|
|
|
|
nsRefPtr<gfxCallbackDrawable> callbackDrawable =
|
|
|
|
new gfxCallbackDrawable(callback, mSize);
|
|
|
|
return callbackDrawable.forget();
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2010-08-13 17:30:14 +04:00
|
|
|
gfxPatternDrawable::Draw(gfxContext* aContext,
|
|
|
|
const gfxRect& aFillRect,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aRepeat,
|
2013-10-02 01:01:19 +04:00
|
|
|
const GraphicsFilter& aFilter,
|
2014-08-29 18:04:34 +04:00
|
|
|
gfxFloat aOpacity,
|
2014-08-07 02:43:25 +04:00
|
|
|
const gfxMatrix& aTransform)
|
2010-08-13 17:30:14 +04:00
|
|
|
{
|
2014-12-03 01:29:44 +03:00
|
|
|
DrawTarget& aDrawTarget = *aContext->GetDrawTarget();
|
|
|
|
|
2010-08-13 17:30:14 +04:00
|
|
|
if (!mPattern)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-08-13 17:30:14 +04:00
|
|
|
|
|
|
|
if (aRepeat) {
|
|
|
|
// We can't use mPattern directly: We want our repeated tiles to have
|
|
|
|
// the size mSize, which might not be the case in mPattern.
|
|
|
|
// So we need to draw mPattern into a surface of size mSize, create
|
|
|
|
// a pattern from the surface and draw that pattern.
|
|
|
|
// gfxCallbackDrawable and gfxSurfaceDrawable already know how to do
|
|
|
|
// those things, so we use them here. Drawing mPattern into the surface
|
2011-10-17 18:59:28 +04:00
|
|
|
// will happen through this Draw() method with aRepeat = false.
|
2010-08-13 17:30:14 +04:00
|
|
|
nsRefPtr<gfxCallbackDrawable> callbackDrawable = MakeCallbackDrawable();
|
2011-10-17 18:59:28 +04:00
|
|
|
return callbackDrawable->Draw(aContext, aFillRect, true, aFilter,
|
2014-08-29 18:04:34 +04:00
|
|
|
aOpacity, aTransform);
|
2010-08-13 17:30:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxMatrix oldMatrix = mPattern->GetMatrix();
|
2014-07-11 11:07:07 +04:00
|
|
|
mPattern->SetMatrix(aTransform * oldMatrix);
|
2014-12-03 01:29:44 +03:00
|
|
|
DrawOptions drawOptions(aOpacity);
|
|
|
|
aDrawTarget.FillRect(ToRect(aFillRect),
|
|
|
|
*mPattern->GetPattern(&aDrawTarget), drawOptions);
|
2010-08-13 17:30:14 +04:00
|
|
|
mPattern->SetMatrix(oldMatrix);
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-08-13 17:30:14 +04:00
|
|
|
}
|