summaryrefslogtreecommitdiffstats
path: root/gfx/layers/basic/BasicLayersImpl.cpp
blob: c2262c512f553351d790140fe66edf35e4f7cf67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* -*- 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"
#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"
#include "AutoMaskData.h"

namespace mozilla {
namespace layers {

using namespace mozilla::gfx;

bool
GetMaskData(Layer* aMaskLayer,
            const Point& aDeviceOffset,
            AutoMoz2DMaskData* aMaskData)
{
  if (aMaskLayer) {
    RefPtr<SourceSurface> surface =
      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?!");
      transform.PostTranslate(-aDeviceOffset.x, -aDeviceOffset.y);
      aMaskData->Construct(transform, surface);
      return true;
    }
  }
  return false;
}

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;
}

void
PaintWithMask(gfxContext* aContext, float aOpacity, Layer* aMaskLayer)
{
  AutoMoz2DMaskData mask;
  if (GetMaskData(aMaskLayer, Point(), &mask)) {
    aContext->SetMatrix(ThebesMatrix(mask.GetTransform()));
    aContext->Mask(mask.GetSurface(), aOpacity);
    return;
  }

  // if there is no mask, just paint normally
  aContext->Paint(aOpacity);
}

void
FillRectWithMask(DrawTarget* aDT,
                 const Rect& aRect,
                 const Color& aColor,
                 const DrawOptions& aOptions,
                 SourceSurface* aMaskSource,
                 const Matrix* aMaskTransform)
{
  if (aMaskSource && aMaskTransform) {
    aDT->PushClipRect(aRect);
    Matrix oldTransform = aDT->GetTransform();

    aDT->SetTransform(*aMaskTransform);
    aDT->MaskSurface(ColorPattern(aColor), aMaskSource, Point(), aOptions);
    aDT->SetTransform(oldTransform);
    aDT->PopClip();
    return;
  }

  aDT->FillRect(aRect, ColorPattern(aColor), aOptions);
}
void
FillRectWithMask(DrawTarget* aDT,
                 const gfx::Point& aDeviceOffset,
                 const Rect& aRect,
                 const Color& aColor,
                 const DrawOptions& aOptions,
                 Layer* aMaskLayer)
{
  AutoMoz2DMaskData mask;
  if (GetMaskData(aMaskLayer, aDeviceOffset, &mask)) {
    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,
                 SamplingFilter aSamplingFilter,
                 const DrawOptions& aOptions,
                 ExtendMode aExtendMode,
                 SourceSurface* aMaskSource,
                 const Matrix* aMaskTransform,
                 const Matrix* aSurfaceTransform)
{
  if (aMaskSource && aMaskTransform) {
    aDT->PushClipRect(aRect);
    Matrix oldTransform = aDT->GetTransform();

    Matrix inverseMask = *aMaskTransform;
    inverseMask.Invert();

    Matrix transform = oldTransform * inverseMask;
    if (aSurfaceTransform) {
      transform = (*aSurfaceTransform) * transform;
    }

    SurfacePattern source(aSurface, aExtendMode, transform, aSamplingFilter);

    aDT->SetTransform(*aMaskTransform);
    aDT->MaskSurface(source, aMaskSource, Point(0, 0), aOptions);
    aDT->SetTransform(oldTransform);
    aDT->PopClip();
    return;
  }

  aDT->FillRect(aRect,
                SurfacePattern(aSurface, aExtendMode,
                               aSurfaceTransform ? (*aSurfaceTransform) : Matrix(),
                               aSamplingFilter), aOptions);
}

void
FillRectWithMask(DrawTarget* aDT,
                 const gfx::Point& aDeviceOffset,
                 const Rect& aRect,
                 SourceSurface* aSurface,
                 SamplingFilter aSamplingFilter,
                 const DrawOptions& aOptions,
                 Layer* aMaskLayer)
{
  AutoMoz2DMaskData mask;
  if (GetMaskData(aMaskLayer, aDeviceOffset, &mask)) {
    const Matrix& maskTransform = mask.GetTransform();
    FillRectWithMask(aDT, aRect, aSurface, aSamplingFilter, aOptions,
                     ExtendMode::CLAMP,
                     mask.GetSurface(), &maskTransform);
    return;
  }

  FillRectWithMask(aDT, aRect, aSurface, aSamplingFilter, aOptions,
                   ExtendMode::CLAMP);
}

BasicImplData*
ToData(Layer* aLayer)
{
  return static_cast<BasicImplData*>(aLayer->ImplData());
}

gfx::CompositionOp
GetEffectiveOperator(Layer* aLayer)
{
  CompositionOp op = aLayer->GetEffectiveMixBlendMode();

  if (op != CompositionOp::OP_OVER) {
    return op;
  }

  return ToData(aLayer)->GetOperator();
}

ShadowableLayer*
ToShadowable(Layer* aLayer)
{
  return aLayer->AsShadowableLayer();
}

bool
ShouldShadow(Layer* aLayer)
{
  if (!ToShadowable(aLayer)) {
    MOZ_ASSERT(aLayer->GetType() == Layer::TYPE_READBACK,
               "Only expect not to shadow ReadbackLayers");
    return false;
  }
  return true;
}


} // namespace layers
} // namespace mozilla