2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
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"
|
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
|
2015-04-15 22:04:38 +03:00
|
|
|
#include "mozilla/gfx/Logging.h"
|
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,
|
2015-06-01 11:26:19 +03:00
|
|
|
const IntSize aSize,
|
2013-11-27 05:05:03 +04:00
|
|
|
const gfxMatrix aTransform)
|
|
|
|
: gfxDrawable(aSize), mSourceSurface(aSurface), mTransform(aTransform) {
|
2015-04-15 22:04:38 +03:00
|
|
|
if (!mSourceSurface) {
|
|
|
|
gfxWarning() << "Creating gfxSurfaceDrawable with null SourceSurface";
|
|
|
|
}
|
2013-11-27 05:05:03 +04:00
|
|
|
}
|
|
|
|
|
2016-01-07 05:26:33 +03:00
|
|
|
bool gfxSurfaceDrawable::DrawWithSamplingRect(
|
|
|
|
DrawTarget* aDrawTarget, CompositionOp aOp, AntialiasMode aAntialiasMode,
|
2014-09-12 09:18:21 +04:00
|
|
|
const gfxRect& aFillRect, const gfxRect& aSamplingRect,
|
2016-05-25 19:01:18 +03:00
|
|
|
ExtendMode aExtendMode, const SamplingFilter aSamplingFilter,
|
2014-09-12 09:18:21 +04:00
|
|
|
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.
|
2017-12-19 23:48:39 +03:00
|
|
|
IntRect intRect =
|
|
|
|
IntRect::RoundOut(aSamplingRect.X(), aSamplingRect.Y(),
|
|
|
|
aSamplingRect.Width(), aSamplingRect.Height());
|
2014-09-12 09:18:21 +04:00
|
|
|
|
|
|
|
IntSize size = mSourceSurface->GetSize();
|
2016-09-16 18:49:39 +03:00
|
|
|
if (!IntRect(IntPoint(), size).Contains(intRect)) {
|
2014-09-12 09:18:21 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-07 05:26:33 +03:00
|
|
|
DrawInternal(aDrawTarget, aOp, aAntialiasMode, aFillRect, intRect,
|
2016-05-25 19:01:18 +03:00
|
|
|
ExtendMode::CLAMP, aSamplingFilter, aOpacity, gfxMatrix());
|
2014-09-12 09:18:21 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-13 17:30:14 +04:00
|
|
|
bool gfxSurfaceDrawable::Draw(gfxContext* aContext, const gfxRect& aFillRect,
|
2015-11-23 19:17:35 +03:00
|
|
|
ExtendMode aExtendMode,
|
2016-05-25 19:01:18 +03:00
|
|
|
const SamplingFilter aSamplingFilter,
|
2014-08-07 02:43:25 +04:00
|
|
|
gfxFloat aOpacity, const gfxMatrix& aTransform)
|
2015-11-23 19:17:35 +03:00
|
|
|
|
2014-09-12 09:18:21 +04:00
|
|
|
{
|
2015-04-15 22:04:38 +03:00
|
|
|
if (!mSourceSurface) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-07 05:26:33 +03:00
|
|
|
DrawInternal(aContext->GetDrawTarget(), aContext->CurrentOp(),
|
|
|
|
aContext->CurrentAntialiasMode(), aFillRect, IntRect(),
|
2016-05-25 19:01:18 +03:00
|
|
|
aExtendMode, aSamplingFilter, aOpacity, aTransform);
|
2014-09-12 09:18:21 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-07 05:26:33 +03:00
|
|
|
void gfxSurfaceDrawable::DrawInternal(
|
|
|
|
DrawTarget* aDrawTarget, CompositionOp aOp, AntialiasMode aAntialiasMode,
|
2014-09-12 09:18:21 +04:00
|
|
|
const gfxRect& aFillRect, const IntRect& aSamplingRect,
|
2016-05-25 19:01:18 +03:00
|
|
|
ExtendMode aExtendMode, const SamplingFilter aSamplingFilter,
|
2014-09-12 09:18:21 +04:00
|
|
|
gfxFloat aOpacity, const gfxMatrix& aTransform) {
|
2014-07-29 04:07:32 +04:00
|
|
|
Matrix patternTransform = ToMatrix(aTransform * mTransform);
|
|
|
|
patternTransform.Invert();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-23 19:17:35 +03:00
|
|
|
SurfacePattern pattern(mSourceSurface, aExtendMode, patternTransform,
|
2016-05-25 19:01:18 +03:00
|
|
|
aSamplingFilter, aSamplingRect);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-07-29 04:07:32 +04:00
|
|
|
Rect fillRect = ToRect(aFillRect);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-01-07 05:26:33 +03:00
|
|
|
if (aOp == CompositionOp::OP_SOURCE && aOpacity == 1.0) {
|
2014-07-29 04:07:32 +04:00
|
|
|
// Emulate cairo operator source which is bound by mask!
|
2016-01-07 05:26:33 +03:00
|
|
|
aDrawTarget->ClearRect(fillRect);
|
|
|
|
aDrawTarget->FillRect(fillRect, pattern);
|
2010-12-12 08:18:27 +03:00
|
|
|
} else {
|
2016-01-07 05:26:33 +03:00
|
|
|
aDrawTarget->FillRect(fillRect, pattern,
|
|
|
|
DrawOptions(aOpacity, aOp, aAntialiasMode));
|
2010-08-13 17:30:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxCallbackDrawable::gfxCallbackDrawable(gfxDrawingCallback* aCallback,
|
2015-06-01 11:26:19 +03:00
|
|
|
const IntSize aSize)
|
2010-08-13 17:30:14 +04:00
|
|
|
: gfxDrawable(aSize), mCallback(aCallback) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-06-10 01:12:40 +03:00
|
|
|
already_AddRefed<gfxSurfaceDrawable> gfxCallbackDrawable::MakeSurfaceDrawable(
|
|
|
|
gfxContext* aContext, const SamplingFilter aSamplingFilter) {
|
2014-04-16 06:23:05 +04:00
|
|
|
SurfaceFormat format = gfxPlatform::GetPlatform()->Optimal2DFormatForContent(
|
|
|
|
gfxContentType::COLOR_ALPHA);
|
2019-02-27 20:34:23 +03:00
|
|
|
if (!aContext->GetDrawTarget()->CanCreateSimilarDrawTarget(mSize, format)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DrawTarget> dt =
|
2017-06-10 01:12:40 +03:00
|
|
|
aContext->GetDrawTarget()->CreateSimilarDrawTarget(mSize, format);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-04-12 22:18:11 +03:00
|
|
|
if (!dt || !dt->IsValid()) return nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-06-07 02:39:56 +03:00
|
|
|
RefPtr<gfxContext> ctx = gfxContext::CreateOrNull(dt);
|
2016-04-12 22:18:11 +03:00
|
|
|
MOZ_ASSERT(ctx); // already checked for target above
|
2016-05-25 19:01:18 +03:00
|
|
|
Draw(ctx, gfxRect(0, 0, mSize.width, mSize.height), ExtendMode::CLAMP,
|
|
|
|
aSamplingFilter);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SourceSurface> surface = dt->Snapshot();
|
2015-04-15 22:04:38 +03:00
|
|
|
if (surface) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfxSurfaceDrawable> drawable =
|
|
|
|
new gfxSurfaceDrawable(surface, mSize);
|
2015-04-15 22:04:38 +03:00
|
|
|
return drawable.forget();
|
|
|
|
}
|
|
|
|
return nullptr;
|
2010-08-13 17:30:14 +04:00
|
|
|
}
|
|
|
|
|
2015-11-23 19:17:35 +03:00
|
|
|
static bool IsRepeatingExtendMode(ExtendMode aExtendMode) {
|
|
|
|
switch (aExtendMode) {
|
|
|
|
case ExtendMode::REPEAT:
|
|
|
|
case ExtendMode::REPEAT_X:
|
|
|
|
case ExtendMode::REPEAT_Y:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-13 17:30:14 +04:00
|
|
|
bool gfxCallbackDrawable::Draw(gfxContext* aContext, const gfxRect& aFillRect,
|
2015-11-23 19:17:35 +03:00
|
|
|
ExtendMode aExtendMode,
|
2016-05-25 19:01:18 +03:00
|
|
|
const SamplingFilter aSamplingFilter,
|
2014-08-07 02:43:25 +04:00
|
|
|
gfxFloat aOpacity, const gfxMatrix& aTransform) {
|
2016-09-13 18:05:58 +03:00
|
|
|
if ((IsRepeatingExtendMode(aExtendMode) || aOpacity != 1.0 ||
|
|
|
|
aContext->CurrentOp() != CompositionOp::OP_OVER) &&
|
|
|
|
!mSurfaceDrawable) {
|
2017-06-10 01:12:40 +03:00
|
|
|
mSurfaceDrawable = MakeSurfaceDrawable(aContext, aSamplingFilter);
|
2010-08-13 17:30:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mSurfaceDrawable)
|
2016-05-25 19:01:18 +03:00
|
|
|
return mSurfaceDrawable->Draw(aContext, aFillRect, aExtendMode,
|
2014-08-29 18:04:34 +04:00
|
|
|
aSamplingFilter, aOpacity, aTransform);
|
2010-08-13 17:30:14 +04:00
|
|
|
|
|
|
|
if (mCallback)
|
2016-05-25 19:01:18 +03:00
|
|
|
return (*mCallback)(aContext, aFillRect, aSamplingFilter, aTransform);
|
2010-08-13 17:30:14 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-08-13 17:30:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxPatternDrawable::gfxPatternDrawable(gfxPattern* aPattern,
|
2015-06-01 11:26:19 +03:00
|
|
|
const IntSize aSize)
|
2010-08-13 17:30:14 +04:00
|
|
|
: gfxDrawable(aSize), mPattern(aPattern) {}
|
|
|
|
|
2016-11-15 12:04:18 +03:00
|
|
|
gfxPatternDrawable::~gfxPatternDrawable() = default;
|
2013-10-16 02:00:28 +04:00
|
|
|
|
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!");
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-04-11 15:36:51 +03:00
|
|
|
virtual ~DrawingCallbackFromDrawable() = default;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-11-15 12:16:29 +03:00
|
|
|
bool operator()(gfxContext* aContext, const gfxRect& aFillRect,
|
|
|
|
const SamplingFilter aSamplingFilter,
|
|
|
|
const gfxMatrix& aTransform = gfxMatrix()) override {
|
2016-05-25 19:01:18 +03:00
|
|
|
return mDrawable->Draw(aContext, aFillRect, ExtendMode::CLAMP,
|
|
|
|
aSamplingFilter, 1.0, aTransform);
|
2010-08-13 17:30:14 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-08-13 17:30:14 +04:00
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfxDrawable> mDrawable;
|
2010-08-13 17:30:14 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
already_AddRefed<gfxCallbackDrawable>
|
|
|
|
gfxPatternDrawable::MakeCallbackDrawable() {
|
|
|
|
RefPtr<gfxDrawingCallback> callback = new DrawingCallbackFromDrawable(this);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfxCallbackDrawable> callbackDrawable =
|
2010-08-13 17:30:14 +04:00
|
|
|
new gfxCallbackDrawable(callback, mSize);
|
|
|
|
return callbackDrawable.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool gfxPatternDrawable::Draw(gfxContext* aContext, const gfxRect& aFillRect,
|
2015-11-23 19:17:35 +03:00
|
|
|
ExtendMode aExtendMode,
|
2016-05-25 19:01:18 +03:00
|
|
|
const SamplingFilter aSamplingFilter,
|
2014-08-07 02:43:25 +04:00
|
|
|
gfxFloat aOpacity, const gfxMatrix& aTransform) {
|
2014-12-03 01:29:44 +03:00
|
|
|
DrawTarget& aDrawTarget = *aContext->GetDrawTarget();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-08-13 17:30:14 +04:00
|
|
|
if (!mPattern) return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-23 19:17:35 +03:00
|
|
|
if (IsRepeatingExtendMode(aExtendMode)) {
|
2010-08-13 17:30:14 +04:00
|
|
|
// 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.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfxCallbackDrawable> callbackDrawable = MakeCallbackDrawable();
|
2016-05-25 19:01:18 +03:00
|
|
|
return callbackDrawable->Draw(aContext, aFillRect, aExtendMode,
|
2014-08-29 18:04:34 +04:00
|
|
|
aSamplingFilter, aOpacity, aTransform);
|
2010-08-13 17:30:14 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
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
|
|
|
}
|