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