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/. */
|
|
|
|
|
|
|
|
#include "BasicLayersImpl.h"
|
2013-08-12 03:17:23 +04:00
|
|
|
#include <new> // for operator new
|
|
|
|
#include "Layers.h" // for Layer, etc
|
|
|
|
#include "basic/BasicImplData.h" // for BasicImplData
|
|
|
|
#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc
|
|
|
|
#include "mozilla/DebugOnly.h" // for DebugOnly
|
|
|
|
#include "mozilla/layers/CompositorTypes.h"
|
|
|
|
#include "mozilla/layers/ISurfaceAllocator.h"
|
2013-09-25 00:45:14 +04:00
|
|
|
#include "AutoMaskData.h"
|
2016-12-22 22:11:15 +03:00
|
|
|
#include "mozilla/gfx/InlineTranslator.h"
|
|
|
|
#include "mozilla/gfx/DrawTargetRecording.h"
|
2012-06-29 10:01:34 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
2012-07-12 16:51:57 +04:00
|
|
|
|
2014-05-22 14:11:45 +04:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2014-04-01 08:02:09 +04:00
|
|
|
bool
|
2014-05-12 04:31:27 +04:00
|
|
|
GetMaskData(Layer* aMaskLayer,
|
|
|
|
const Point& aDeviceOffset,
|
|
|
|
AutoMoz2DMaskData* aMaskData)
|
2014-04-01 08:02:09 +04:00
|
|
|
{
|
|
|
|
if (aMaskLayer) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SourceSurface> surface =
|
2014-04-01 08:02:09 +04:00
|
|
|
static_cast<BasicImplData*>(aMaskLayer->ImplData())->GetAsSourceSurface();
|
|
|
|
if (surface) {
|
|
|
|
Matrix transform;
|
|
|
|
Matrix4x4 effectiveTransform = aMaskLayer->GetEffectiveTransform();
|
|
|
|
DebugOnly<bool> maskIs2D = effectiveTransform.CanDraw2D(&transform);
|
|
|
|
NS_ASSERTION(maskIs2D, "How did we end up with a 3D transform here?!");
|
2014-12-04 02:02:00 +03:00
|
|
|
transform.PostTranslate(-aDeviceOffset.x, -aDeviceOffset.y);
|
2014-04-01 08:02:09 +04:00
|
|
|
aMaskData->Construct(transform, surface);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-11 18:15:39 +03:00
|
|
|
already_AddRefed<SourceSurface>
|
|
|
|
GetMaskForLayer(Layer* aLayer, Matrix* aMaskTransform)
|
|
|
|
{
|
|
|
|
if (!aLayer->GetMaskLayer()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(aMaskTransform);
|
|
|
|
|
|
|
|
AutoMoz2DMaskData mask;
|
|
|
|
if (GetMaskData(aLayer->GetMaskLayer(), Point(), &mask)) {
|
|
|
|
*aMaskTransform = mask.GetTransform();
|
|
|
|
RefPtr<SourceSurface> surf = mask.GetSurface();
|
|
|
|
return surf.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-06-29 10:01:34 +04:00
|
|
|
void
|
|
|
|
PaintWithMask(gfxContext* aContext, float aOpacity, Layer* aMaskLayer)
|
|
|
|
{
|
2014-04-09 13:15:19 +04:00
|
|
|
AutoMoz2DMaskData mask;
|
2014-05-12 04:31:27 +04:00
|
|
|
if (GetMaskData(aMaskLayer, Point(), &mask)) {
|
2014-01-28 00:25:21 +04:00
|
|
|
aContext->SetMatrix(ThebesMatrix(mask.GetTransform()));
|
2015-04-09 17:52:11 +03:00
|
|
|
aContext->Mask(mask.GetSurface(), aOpacity);
|
2012-06-29 10:01:34 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there is no mask, just paint normally
|
|
|
|
aContext->Paint(aOpacity);
|
2012-07-12 16:51:57 +04:00
|
|
|
}
|
|
|
|
|
2014-04-01 08:02:09 +04:00
|
|
|
void
|
|
|
|
FillRectWithMask(DrawTarget* aDT,
|
|
|
|
const Rect& aRect,
|
|
|
|
const Color& aColor,
|
|
|
|
const DrawOptions& aOptions,
|
2014-04-11 16:23:09 +04:00
|
|
|
SourceSurface* aMaskSource,
|
|
|
|
const Matrix* aMaskTransform)
|
2014-04-01 08:02:09 +04:00
|
|
|
{
|
2014-04-11 16:23:09 +04:00
|
|
|
if (aMaskSource && aMaskTransform) {
|
2014-04-01 08:02:09 +04:00
|
|
|
aDT->PushClipRect(aRect);
|
|
|
|
Matrix oldTransform = aDT->GetTransform();
|
|
|
|
|
2014-04-11 16:23:09 +04:00
|
|
|
aDT->SetTransform(*aMaskTransform);
|
|
|
|
aDT->MaskSurface(ColorPattern(aColor), aMaskSource, Point(), aOptions);
|
2014-04-01 08:02:09 +04:00
|
|
|
aDT->SetTransform(oldTransform);
|
|
|
|
aDT->PopClip();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aDT->FillRect(aRect, ColorPattern(aColor), aOptions);
|
|
|
|
}
|
|
|
|
void
|
|
|
|
FillRectWithMask(DrawTarget* aDT,
|
2014-05-12 04:31:27 +04:00
|
|
|
const gfx::Point& aDeviceOffset,
|
2014-04-01 08:02:09 +04:00
|
|
|
const Rect& aRect,
|
2014-04-11 16:23:09 +04:00
|
|
|
const Color& aColor,
|
2014-04-01 08:02:09 +04:00
|
|
|
const DrawOptions& aOptions,
|
|
|
|
Layer* aMaskLayer)
|
|
|
|
{
|
|
|
|
AutoMoz2DMaskData mask;
|
2014-05-12 04:31:27 +04:00
|
|
|
if (GetMaskData(aMaskLayer, aDeviceOffset, &mask)) {
|
2014-04-11 16:23:09 +04:00
|
|
|
const Matrix& maskTransform = mask.GetTransform();
|
|
|
|
FillRectWithMask(aDT, aRect, aColor, aOptions, mask.GetSurface(), &maskTransform);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
FillRectWithMask(aDT, aRect, aColor, aOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FillRectWithMask(DrawTarget* aDT,
|
|
|
|
const Rect& aRect,
|
|
|
|
SourceSurface* aSurface,
|
2016-05-25 19:01:18 +03:00
|
|
|
SamplingFilter aSamplingFilter,
|
2014-04-11 16:23:09 +04:00
|
|
|
const DrawOptions& aOptions,
|
|
|
|
ExtendMode aExtendMode,
|
|
|
|
SourceSurface* aMaskSource,
|
|
|
|
const Matrix* aMaskTransform,
|
|
|
|
const Matrix* aSurfaceTransform)
|
|
|
|
{
|
2016-12-22 22:11:15 +03:00
|
|
|
MOZ_ASSERT(!aMaskSource || (aMaskSource && aMaskTransform),
|
|
|
|
"aMaskSource must not be passed without a transform");
|
|
|
|
|
2014-04-11 16:23:09 +04:00
|
|
|
if (aMaskSource && aMaskTransform) {
|
2014-04-01 08:02:09 +04:00
|
|
|
aDT->PushClipRect(aRect);
|
|
|
|
Matrix oldTransform = aDT->GetTransform();
|
|
|
|
|
2014-04-11 16:23:09 +04:00
|
|
|
Matrix inverseMask = *aMaskTransform;
|
2014-04-01 08:02:09 +04:00
|
|
|
inverseMask.Invert();
|
|
|
|
|
2014-04-24 11:38:35 +04:00
|
|
|
Matrix transform = oldTransform * inverseMask;
|
2014-04-11 16:23:09 +04:00
|
|
|
if (aSurfaceTransform) {
|
2014-05-28 05:21:32 +04:00
|
|
|
transform = (*aSurfaceTransform) * transform;
|
2014-04-11 16:23:09 +04:00
|
|
|
}
|
2014-04-01 08:02:09 +04:00
|
|
|
|
2016-05-25 19:01:18 +03:00
|
|
|
SurfacePattern source(aSurface, aExtendMode, transform, aSamplingFilter);
|
2014-04-01 08:02:09 +04:00
|
|
|
|
2014-04-11 16:23:09 +04:00
|
|
|
aDT->SetTransform(*aMaskTransform);
|
|
|
|
aDT->MaskSurface(source, aMaskSource, Point(0, 0), aOptions);
|
2016-12-22 22:11:15 +03:00
|
|
|
|
|
|
|
aDT->SetTransform(oldTransform);
|
|
|
|
aDT->PopClip();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aSurface->GetType() == SurfaceType::RECORDING) {
|
|
|
|
MOZ_ASSERT(aOptions.mAlpha == 1.0 &&
|
|
|
|
aOptions.mCompositionOp == CompositionOp::OP_OVER);
|
|
|
|
|
|
|
|
aDT->PushClipRect(aRect);
|
|
|
|
Matrix oldTransform = aDT->GetTransform();
|
|
|
|
|
|
|
|
Matrix transform = oldTransform;
|
|
|
|
if (aSurfaceTransform) {
|
|
|
|
transform = (*aSurfaceTransform) * transform;
|
|
|
|
}
|
|
|
|
|
|
|
|
InlineTranslator* translator = new InlineTranslator(aDT, transform);
|
|
|
|
SourceSurfaceRecording* ss = static_cast<SourceSurfaceRecording*>(aSurface);
|
|
|
|
DrawEventRecorderMemory* mr = static_cast<DrawEventRecorderMemory*>(ss->mRecorder.get());
|
|
|
|
|
|
|
|
size_t size = mr->RecordingSize();
|
|
|
|
char* buffer = new char[size];
|
|
|
|
mr->CopyRecording(buffer, size);
|
|
|
|
std::istringstream recording(std::string(buffer, size));
|
|
|
|
|
2017-01-12 15:54:01 +03:00
|
|
|
delete [] buffer;
|
2016-12-22 22:11:15 +03:00
|
|
|
translator->TranslateRecording(recording);
|
|
|
|
|
2014-04-01 08:02:09 +04:00
|
|
|
aDT->SetTransform(oldTransform);
|
|
|
|
aDT->PopClip();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-11 16:23:09 +04:00
|
|
|
aDT->FillRect(aRect,
|
|
|
|
SurfacePattern(aSurface, aExtendMode,
|
|
|
|
aSurfaceTransform ? (*aSurfaceTransform) : Matrix(),
|
2016-05-25 19:01:18 +03:00
|
|
|
aSamplingFilter), aOptions);
|
2014-04-11 16:23:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FillRectWithMask(DrawTarget* aDT,
|
2014-05-12 04:31:27 +04:00
|
|
|
const gfx::Point& aDeviceOffset,
|
2014-04-11 16:23:09 +04:00
|
|
|
const Rect& aRect,
|
|
|
|
SourceSurface* aSurface,
|
2016-05-25 19:01:18 +03:00
|
|
|
SamplingFilter aSamplingFilter,
|
2014-04-11 16:23:09 +04:00
|
|
|
const DrawOptions& aOptions,
|
|
|
|
Layer* aMaskLayer)
|
|
|
|
{
|
|
|
|
AutoMoz2DMaskData mask;
|
2014-05-12 04:31:27 +04:00
|
|
|
if (GetMaskData(aMaskLayer, aDeviceOffset, &mask)) {
|
2014-04-11 16:23:09 +04:00
|
|
|
const Matrix& maskTransform = mask.GetTransform();
|
2016-05-25 19:01:18 +03:00
|
|
|
FillRectWithMask(aDT, aRect, aSurface, aSamplingFilter, aOptions,
|
|
|
|
ExtendMode::CLAMP,
|
2014-04-11 16:23:09 +04:00
|
|
|
mask.GetSurface(), &maskTransform);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-25 19:01:18 +03:00
|
|
|
FillRectWithMask(aDT, aRect, aSurface, aSamplingFilter, aOptions,
|
|
|
|
ExtendMode::CLAMP);
|
2014-04-01 08:02:09 +04:00
|
|
|
}
|
|
|
|
|
2012-06-29 10:01:34 +04:00
|
|
|
BasicImplData*
|
|
|
|
ToData(Layer* aLayer)
|
|
|
|
{
|
|
|
|
return static_cast<BasicImplData*>(aLayer->ImplData());
|
|
|
|
}
|
|
|
|
|
2014-02-27 20:56:48 +04:00
|
|
|
gfx::CompositionOp
|
|
|
|
GetEffectiveOperator(Layer* aLayer)
|
|
|
|
{
|
|
|
|
CompositionOp op = aLayer->GetEffectiveMixBlendMode();
|
|
|
|
|
|
|
|
if (op != CompositionOp::OP_OVER) {
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ToData(aLayer)->GetOperator();
|
|
|
|
}
|
|
|
|
|
2012-06-29 10:01:34 +04:00
|
|
|
ShadowableLayer*
|
|
|
|
ToShadowable(Layer* aLayer)
|
|
|
|
{
|
|
|
|
return aLayer->AsShadowableLayer();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ShouldShadow(Layer* aLayer)
|
|
|
|
{
|
|
|
|
if (!ToShadowable(aLayer)) {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(aLayer->GetType() == Layer::TYPE_READBACK,
|
|
|
|
"Only expect not to shadow ReadbackLayers");
|
2012-06-29 10:01:34 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-12 16:51:57 +04:00
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace layers
|
|
|
|
} // namespace mozilla
|