2010-08-13 13:30:14 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 11:12:37 +00: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 13:30:14 +00:00
|
|
|
|
|
|
|
#include "gfxDrawable.h"
|
|
|
|
#include "gfxASurface.h"
|
|
|
|
#include "gfxContext.h"
|
|
|
|
#include "gfxPlatform.h"
|
2013-12-17 18:33:34 +00:00
|
|
|
#include "gfxColor.h"
|
2014-07-29 00:07:32 +00:00
|
|
|
#include "gfx2DGlue.h"
|
2010-09-09 20:27:10 +00:00
|
|
|
#ifdef MOZ_X11
|
|
|
|
#include "cairo.h"
|
|
|
|
#include "gfxXlibSurface.h"
|
|
|
|
#endif
|
2010-08-13 13:30:14 +00:00
|
|
|
|
2013-11-13 04:31:12 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2013-11-27 01:05:03 +00:00
|
|
|
gfxSurfaceDrawable::gfxSurfaceDrawable(SourceSurface* aSurface,
|
|
|
|
const gfxIntSize aSize,
|
|
|
|
const gfxMatrix aTransform)
|
|
|
|
: gfxDrawable(aSize)
|
|
|
|
, mSourceSurface(aSurface)
|
|
|
|
, mTransform(aTransform)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-09-12 05:18:21 +00:00
|
|
|
bool
|
|
|
|
gfxSurfaceDrawable::DrawWithSamplingRect(gfxContext* aContext,
|
|
|
|
const gfxRect& aFillRect,
|
|
|
|
const gfxRect& aSamplingRect,
|
|
|
|
bool aRepeat,
|
|
|
|
const GraphicsFilter& aFilter,
|
|
|
|
gfxFloat aOpacity)
|
|
|
|
{
|
2014-09-22 20:49:40 +00:00
|
|
|
if (!mSourceSurface) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-12 05:18:21 +00: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 06:19:26 +00:00
|
|
|
bool
|
2010-08-13 13:30:14 +00:00
|
|
|
gfxSurfaceDrawable::Draw(gfxContext* aContext,
|
|
|
|
const gfxRect& aFillRect,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aRepeat,
|
2013-10-01 21:01:19 +00:00
|
|
|
const GraphicsFilter& aFilter,
|
2014-08-29 14:04:34 +00:00
|
|
|
gfxFloat aOpacity,
|
2014-08-06 22:43:25 +00:00
|
|
|
const gfxMatrix& aTransform)
|
2014-09-12 05:18:21 +00: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 13:30:14 +00:00
|
|
|
{
|
2014-07-29 00:07:32 +00:00
|
|
|
ExtendMode extend = ExtendMode::CLAMP;
|
2014-07-29 00:05:40 +00:00
|
|
|
|
2010-08-13 13:30:14 +00:00
|
|
|
if (aRepeat) {
|
2014-07-29 00:07:32 +00:00
|
|
|
extend = ExtendMode::REPEAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix patternTransform = ToMatrix(aTransform * mTransform);
|
|
|
|
patternTransform.Invert();
|
|
|
|
|
|
|
|
SurfacePattern pattern(mSourceSurface, extend,
|
2014-09-12 05:18:21 +00:00
|
|
|
patternTransform, ToFilter(aFilter), aSamplingRect);
|
2014-07-29 00:07:32 +00:00
|
|
|
|
|
|
|
Rect fillRect = ToRect(aFillRect);
|
|
|
|
DrawTarget* dt = aContext->GetDrawTarget();
|
|
|
|
|
|
|
|
if (aContext->CurrentOperator() == gfxContext::OPERATOR_CLEAR) {
|
|
|
|
dt->ClearRect(fillRect);
|
2014-08-29 14:04:34 +00:00
|
|
|
} else if (aContext->CurrentOperator() == gfxContext::OPERATOR_SOURCE &&
|
|
|
|
aOpacity == 1.0) {
|
2014-07-29 00:07:32 +00:00
|
|
|
// Emulate cairo operator source which is bound by mask!
|
|
|
|
dt->ClearRect(fillRect);
|
|
|
|
dt->FillRect(fillRect, pattern);
|
2010-12-12 05:18:27 +00:00
|
|
|
} else {
|
2014-09-15 11:10:30 +00:00
|
|
|
dt->FillRect(fillRect, pattern,
|
|
|
|
DrawOptions(aOpacity,
|
|
|
|
CompositionOpForOp(aContext->CurrentOperator()),
|
|
|
|
aContext->CurrentAntialiasMode()));
|
2010-08-13 13:30:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxCallbackDrawable::gfxCallbackDrawable(gfxDrawingCallback* aCallback,
|
|
|
|
const gfxIntSize aSize)
|
|
|
|
: gfxDrawable(aSize)
|
|
|
|
, mCallback(aCallback)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<gfxSurfaceDrawable>
|
2013-10-01 21:01:19 +00:00
|
|
|
gfxCallbackDrawable::MakeSurfaceDrawable(const GraphicsFilter aFilter)
|
2010-08-13 13:30:14 +00:00
|
|
|
{
|
2014-04-16 02:23:05 +00:00
|
|
|
SurfaceFormat format =
|
|
|
|
gfxPlatform::GetPlatform()->Optimal2DFormatForContent(gfxContentType::COLOR_ALPHA);
|
|
|
|
RefPtr<DrawTarget> dt =
|
|
|
|
gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(mSize.ToIntSize(),
|
|
|
|
format);
|
|
|
|
if (!dt)
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2010-08-13 13:30:14 +00:00
|
|
|
|
2014-04-16 02:23:05 +00:00
|
|
|
nsRefPtr<gfxContext> ctx = new gfxContext(dt);
|
2011-10-17 14:59:28 +00:00
|
|
|
Draw(ctx, gfxRect(0, 0, mSize.width, mSize.height), false, aFilter);
|
2014-04-16 02:23:05 +00:00
|
|
|
|
|
|
|
RefPtr<SourceSurface> surface = dt->Snapshot();
|
2010-08-13 13:30:14 +00:00
|
|
|
nsRefPtr<gfxSurfaceDrawable> drawable = new gfxSurfaceDrawable(surface, mSize);
|
|
|
|
return drawable.forget();
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2010-08-13 13:30:14 +00:00
|
|
|
gfxCallbackDrawable::Draw(gfxContext* aContext,
|
|
|
|
const gfxRect& aFillRect,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aRepeat,
|
2013-10-01 21:01:19 +00:00
|
|
|
const GraphicsFilter& aFilter,
|
2014-08-29 14:04:34 +00:00
|
|
|
gfxFloat aOpacity,
|
2014-08-06 22:43:25 +00:00
|
|
|
const gfxMatrix& aTransform)
|
2010-08-13 13:30:14 +00:00
|
|
|
{
|
2014-08-29 14:04:34 +00:00
|
|
|
if ((aRepeat || aOpacity != 1.0) && !mSurfaceDrawable) {
|
2010-08-13 13:30:14 +00:00
|
|
|
mSurfaceDrawable = MakeSurfaceDrawable(aFilter);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mSurfaceDrawable)
|
|
|
|
return mSurfaceDrawable->Draw(aContext, aFillRect, aRepeat, aFilter,
|
2014-08-29 14:04:34 +00:00
|
|
|
aOpacity, aTransform);
|
2010-08-13 13:30:14 +00:00
|
|
|
|
|
|
|
if (mCallback)
|
|
|
|
return (*mCallback)(aContext, aFillRect, aFilter, aTransform);
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2010-08-13 13:30:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxPatternDrawable::gfxPatternDrawable(gfxPattern* aPattern,
|
|
|
|
const gfxIntSize aSize)
|
|
|
|
: gfxDrawable(aSize)
|
|
|
|
, mPattern(aPattern)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-10-15 22:00:28 +00:00
|
|
|
gfxPatternDrawable::~gfxPatternDrawable()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-08-13 13:30:14 +00:00
|
|
|
class DrawingCallbackFromDrawable : public gfxDrawingCallback {
|
|
|
|
public:
|
2014-08-08 01:17:30 +00:00
|
|
|
explicit DrawingCallbackFromDrawable(gfxDrawable* aDrawable)
|
2010-08-13 13:30:14 +00:00
|
|
|
: mDrawable(aDrawable) {
|
|
|
|
NS_ASSERTION(aDrawable, "aDrawable is null!");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~DrawingCallbackFromDrawable() {}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
virtual bool operator()(gfxContext* aContext,
|
2010-08-13 13:30:14 +00:00
|
|
|
const gfxRect& aFillRect,
|
2013-10-01 21:01:19 +00:00
|
|
|
const GraphicsFilter& aFilter,
|
2010-08-13 13:30:14 +00:00
|
|
|
const gfxMatrix& aTransform = gfxMatrix())
|
|
|
|
{
|
2014-08-29 14:04:34 +00:00
|
|
|
return mDrawable->Draw(aContext, aFillRect, false, aFilter, 1.0,
|
2010-08-13 13:30:14 +00: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 06:19:26 +00:00
|
|
|
bool
|
2010-08-13 13:30:14 +00:00
|
|
|
gfxPatternDrawable::Draw(gfxContext* aContext,
|
|
|
|
const gfxRect& aFillRect,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aRepeat,
|
2013-10-01 21:01:19 +00:00
|
|
|
const GraphicsFilter& aFilter,
|
2014-08-29 14:04:34 +00:00
|
|
|
gfxFloat aOpacity,
|
2014-08-06 22:43:25 +00:00
|
|
|
const gfxMatrix& aTransform)
|
2010-08-13 13:30:14 +00:00
|
|
|
{
|
|
|
|
if (!mPattern)
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2010-08-13 13:30:14 +00: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 14:59:28 +00:00
|
|
|
// will happen through this Draw() method with aRepeat = false.
|
2010-08-13 13:30:14 +00:00
|
|
|
nsRefPtr<gfxCallbackDrawable> callbackDrawable = MakeCallbackDrawable();
|
2011-10-17 14:59:28 +00:00
|
|
|
return callbackDrawable->Draw(aContext, aFillRect, true, aFilter,
|
2014-08-29 14:04:34 +00:00
|
|
|
aOpacity, aTransform);
|
2010-08-13 13:30:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
aContext->NewPath();
|
|
|
|
gfxMatrix oldMatrix = mPattern->GetMatrix();
|
2014-07-11 07:07:07 +00:00
|
|
|
mPattern->SetMatrix(aTransform * oldMatrix);
|
2010-08-13 13:30:14 +00:00
|
|
|
aContext->SetPattern(mPattern);
|
|
|
|
aContext->Rectangle(aFillRect);
|
2014-08-29 14:04:34 +00:00
|
|
|
aContext->FillWithOpacity(aOpacity);
|
2010-08-13 13:30:14 +00:00
|
|
|
mPattern->SetMatrix(oldMatrix);
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2010-08-13 13:30:14 +00:00
|
|
|
}
|