summaryrefslogtreecommitdiffstats
path: root/gfx/2d/DrawTargetTiled.cpp
blob: 3e4f904bfe230092067bfe01f499ccd0a93304f0 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "DrawTargetTiled.h"
#include "Logging.h"
#include "nsRegion.h"
#include "PathHelpers.h"

namespace mozilla {
namespace gfx {

DrawTargetTiled::DrawTargetTiled() = default;

bool DrawTargetTiled::Init(const TileSet& aTiles) {
  if (!aTiles.mTileCount) {
    return false;
  }

  mTiles.reserve(aTiles.mTileCount);
  for (size_t i = 0; i < aTiles.mTileCount; ++i) {
    mTiles.push_back(TileInternal(aTiles.mTiles[i]));
    if (!aTiles.mTiles[i].mDrawTarget) {
      return false;
    }
    if (mTiles[0].mDrawTarget->GetFormat() !=
            mTiles.back().mDrawTarget->GetFormat() ||
        mTiles[0].mDrawTarget->GetBackendType() !=
            mTiles.back().mDrawTarget->GetBackendType()) {
      return false;
    }
    uint32_t newXMost =
        std::max(mRect.XMost(), mTiles[i].mTileOrigin.x +
                                    mTiles[i].mDrawTarget->GetSize().width);
    uint32_t newYMost =
        std::max(mRect.YMost(), mTiles[i].mTileOrigin.y +
                                    mTiles[i].mDrawTarget->GetSize().height);
    if (i == 0) {
      mRect.MoveTo(mTiles[0].mTileOrigin.x, mTiles[0].mTileOrigin.y);
    } else {
      mRect.MoveTo(std::min(mRect.X(), mTiles[i].mTileOrigin.x),
                   std::min(mRect.Y(), mTiles[i].mTileOrigin.y));
    }
    mRect.SetRightEdge(newXMost);
    mRect.SetBottomEdge(newYMost);
    mTiles[i].mDrawTarget->SetTransform(Matrix::Translation(
        -mTiles[i].mTileOrigin.x, -mTiles[i].mTileOrigin.y));
  }
  mFormat = mTiles[0].mDrawTarget->GetFormat();
  SetPermitSubpixelAA(IsOpaque(mFormat));
  return true;
}

already_AddRefed<SourceSurface> DrawTargetTiled::Snapshot() {
  return MakeAndAddRef<SnapshotTiled>(mTiles, mRect);
}

void DrawTargetTiled::DetachAllSnapshots() {}

// Skip the mClippedOut check since this is only used for Flush() which
// should happen even if we're clipped.
#define TILED_COMMAND(command)                   \
  void DrawTargetTiled::command() {              \
    for (size_t i = 0; i < mTiles.size(); i++) { \
      mTiles[i].mDrawTarget->command();          \
    }                                            \
  }
#define TILED_COMMAND1(command, type1)                                  \
  void DrawTargetTiled::command(type1 arg1) {                           \
    for (size_t i = 0; i < mTiles.size(); i++) {                        \
      if (!mTiles[i].mClippedOut) mTiles[i].mDrawTarget->command(arg1); \
    }                                                                   \
  }
#define TILED_COMMAND3(command, type1, type2, type3)                  \
  void DrawTargetTiled::command(type1 arg1, type2 arg2, type3 arg3) { \
    for (size_t i = 0; i < mTiles.size(); i++) {                      \
      if (!mTiles[i].mClippedOut)                                     \
        mTiles[i].mDrawTarget->command(arg1, arg2, arg3);             \
    }                                                                 \
  }
#define TILED_COMMAND4(command, type1, type2, type3, type4)         \
  void DrawTargetTiled::command(type1 arg1, type2 arg2, type3 arg3, \
                                type4 arg4) {                       \
    for (size_t i = 0; i < mTiles.size(); i++) {                    \
      if (!mTiles[i].mClippedOut)                                   \
        mTiles[i].mDrawTarget->command(arg1, arg2, arg3, arg4);     \
    }                                                               \
  }
#define TILED_COMMAND5(command, type1, type2, type3, type4, type5)    \
  void DrawTargetTiled::command(type1 arg1, type2 arg2, type3 arg3,   \
                                type4 arg4, type5 arg5) {             \
    for (size_t i = 0; i < mTiles.size(); i++) {                      \
      if (!mTiles[i].mClippedOut)                                     \
        mTiles[i].mDrawTarget->command(arg1, arg2, arg3, arg4, arg5); \
    }                                                                 \
  }

TILED_COMMAND(Flush)
TILED_COMMAND4(DrawFilter, FilterNode*, const Rect&, const Point&,
               const DrawOptions&)
TILED_COMMAND1(ClearRect, const Rect&)
TILED_COMMAND4(MaskSurface, const Pattern&, SourceSurface*, Point,
               const DrawOptions&)
TILED_COMMAND4(FillGlyphs, ScaledFont*, const GlyphBuffer&, const Pattern&,
               const DrawOptions&)
TILED_COMMAND3(Mask, const Pattern&, const Pattern&, const DrawOptions&)

void DrawTargetTiled::PushClip(const Path* aPath) {
  if (!mClippedOutTilesStack.append(std::vector<bool>(mTiles.size()))) {
    MOZ_CRASH("out of memory");
  }
  std::vector<bool>& clippedTiles = mClippedOutTilesStack.back();

  Rect deviceRect = aPath->GetBounds(mTransform);

  for (size_t i = 0; i < mTiles.size(); i++) {
    if (!mTiles[i].mClippedOut) {
      if (deviceRect.Intersects(
              Rect(mTiles[i].mTileOrigin.x, mTiles[i].mTileOrigin.y,
                   mTiles[i].mDrawTarget->GetSize().width,
                   mTiles[i].mDrawTarget->GetSize().height))) {
        mTiles[i].mDrawTarget->PushClip(aPath);
      } else {
        mTiles[i].mClippedOut = true;
        clippedTiles[i] = true;
      }
    }
  }
}

void DrawTargetTiled::PushClipRect(const Rect& aRect) {
  if (!mClippedOutTilesStack.append(std::vector<bool>(mTiles.size()))) {
    MOZ_CRASH("out of memory");
  }
  std::vector<bool>& clippedTiles = mClippedOutTilesStack.back();

  Rect deviceRect = mTransform.TransformBounds(aRect);

  for (size_t i = 0; i < mTiles.size(); i++) {
    if (!mTiles[i].mClippedOut) {
      if (deviceRect.Intersects(
              Rect(mTiles[i].mTileOrigin.x, mTiles[i].mTileOrigin.y,
                   mTiles[i].mDrawTarget->GetSize().width,
                   mTiles[i].mDrawTarget->GetSize().height))) {
        mTiles[i].mDrawTarget->PushClipRect(aRect);
      } else {
        mTiles[i].mClippedOut = true;
        clippedTiles[i] = true;
      }
    }
  }
}

void DrawTargetTiled::PopClip() {
  std::vector<bool>& clippedTiles = mClippedOutTilesStack.back();
  MOZ_ASSERT(clippedTiles.size() == mTiles.size());
  for (size_t i = 0; i < mTiles.size(); i++) {
    if (!mTiles[i].mClippedOut) {
      mTiles[i].mDrawTarget->PopClip();
    } else if (clippedTiles[i]) {
      mTiles[i].mClippedOut = false;
    }
  }

  mClippedOutTilesStack.popBack();
}

void DrawTargetTiled::CopySurface(SourceSurface* aSurface,
                                  const IntRect& aSourceRect,
                                  const IntPoint& aDestination) {
  for (size_t i = 0; i < mTiles.size(); i++) {
    IntPoint tileOrigin = mTiles[i].mTileOrigin;
    IntSize tileSize = mTiles[i].mDrawTarget->GetSize();
    if (!IntRect(aDestination, aSourceRect.Size())
             .Intersects(IntRect(tileOrigin, tileSize))) {
      continue;
    }
    // CopySurface ignores the transform, account for that here.
    mTiles[i].mDrawTarget->CopySurface(aSurface, aSourceRect,
                                       aDestination - tileOrigin);
  }
}

void DrawTargetTiled::SetTransform(const Matrix& aTransform) {
  for (size_t i = 0; i < mTiles.size(); i++) {
    Matrix mat = aTransform;
    mat.PostTranslate(Float(-mTiles[i].mTileOrigin.x),
                      Float(-mTiles[i].mTileOrigin.y));
    mTiles[i].mDrawTarget->SetTransform(mat);
  }
  DrawTarget::SetTransform(aTransform);
}

void DrawTargetTiled::SetPermitSubpixelAA(bool aPermitSubpixelAA) {
  DrawTarget::SetPermitSubpixelAA(aPermitSubpixelAA);
  for (size_t i = 0; i < mTiles.size(); i++) {
    mTiles[i].mDrawTarget->SetPermitSubpixelAA(aPermitSubpixelAA);
  }
}

void DrawTargetTiled::DrawSurface(SourceSurface* aSurface, const Rect& aDest,
                                  const Rect& aSource,
                                  const DrawSurfaceOptions& aSurfaceOptions,
                                  const DrawOptions& aDrawOptions) {
  Rect deviceRect = mTransform.TransformBounds(aDest);
  for (size_t i = 0; i < mTiles.size(); i++) {
    if (!mTiles[i].mClippedOut &&
        deviceRect.Intersects(Rect(mTiles[i].mTileOrigin.x,
                                   mTiles[i].mTileOrigin.y,
                                   mTiles[i].mDrawTarget->GetSize().width,
                                   mTiles[i].mDrawTarget->GetSize().height))) {
      mTiles[i].mDrawTarget->DrawSurface(aSurface, aDest, aSource,
                                         aSurfaceOptions, aDrawOptions);
    }
  }
}

void DrawTargetTiled::FillRect(const Rect& aRect, const Pattern& aPattern,
                               const DrawOptions& aDrawOptions) {
  Rect deviceRect = mTransform.TransformBounds(aRect);
  for (size_t i = 0; i < mTiles.size(); i++) {
    if (!mTiles[i].mClippedOut &&
        deviceRect.Intersects(Rect(mTiles[i].mTileOrigin.x,
                                   mTiles[i].mTileOrigin.y,
                                   mTiles[i].mDrawTarget->GetSize().width,
                                   mTiles[i].mDrawTarget->GetSize().height))) {
      mTiles[i].mDrawTarget->FillRect(aRect, aPattern, aDrawOptions);
    }
  }
}

void DrawTargetTiled::Stroke(const Path* aPath, const Pattern& aPattern,
                             const StrokeOptions& aStrokeOptions,
                             const DrawOptions& aDrawOptions) {
  // Approximate the stroke extents, since Path::GetStrokeExtents can be slow
  Rect deviceRect = aPath->GetBounds(mTransform);
  deviceRect.Inflate(MaxStrokeExtents(aStrokeOptions, mTransform));
  for (size_t i = 0; i < mTiles.size(); i++) {
    if (!mTiles[i].mClippedOut &&
        deviceRect.Intersects(Rect(mTiles[i].mTileOrigin.x,
                                   mTiles[i].mTileOrigin.y,
                                   mTiles[i].mDrawTarget->GetSize().width,
                                   mTiles[i].mDrawTarget->GetSize().height))) {
      mTiles[i].mDrawTarget->Stroke(aPath, aPattern, aStrokeOptions,
                                    aDrawOptions);
    }
  }
}

void DrawTargetTiled::StrokeRect(const Rect& aRect, const Pattern& aPattern,
                                 const StrokeOptions& aStrokeOptions,
                                 const DrawOptions& aDrawOptions) {
  Rect deviceRect = mTransform.TransformBounds(aRect);
  Margin strokeMargin = MaxStrokeExtents(aStrokeOptions, mTransform);
  Rect outerRect = deviceRect;
  outerRect.Inflate(strokeMargin);
  Rect innerRect;
  if (mTransform.IsRectilinear()) {
    // If rects are mapped to rects, we can compute the inner rect
    // of the stroked rect.
    innerRect = deviceRect;
    innerRect.Deflate(strokeMargin);
  }
  for (size_t i = 0; i < mTiles.size(); i++) {
    if (mTiles[i].mClippedOut) {
      continue;
    }
    Rect tileRect(mTiles[i].mTileOrigin.x, mTiles[i].mTileOrigin.y,
                  mTiles[i].mDrawTarget->GetSize().width,
                  mTiles[i].mDrawTarget->GetSize().height);
    if (outerRect.Intersects(tileRect) && !innerRect.Contains(tileRect)) {
      mTiles[i].mDrawTarget->StrokeRect(aRect, aPattern, aStrokeOptions,
                                        aDrawOptions);
    }
  }
}

void DrawTargetTiled::StrokeLine(const Point& aStart, const Point& aEnd,
                                 const Pattern& aPattern,
                                 const StrokeOptions& aStrokeOptions,
                                 const DrawOptions& aDrawOptions) {
  Rect lineBounds = Rect(aStart, Size()).UnionEdges(Rect(aEnd, Size()));
  Rect deviceRect = mTransform.TransformBounds(lineBounds);
  deviceRect.Inflate(MaxStrokeExtents(aStrokeOptions, mTransform));
  for (size_t i = 0; i < mTiles.size(); i++) {
    if (!mTiles[i].mClippedOut &&
        deviceRect.Intersects(Rect(mTiles[i].mTileOrigin.x,
                                   mTiles[i].mTileOrigin.y,
                                   mTiles[i].mDrawTarget->GetSize().width,
                                   mTiles[i].mDrawTarget->GetSize().height))) {
      mTiles[i].mDrawTarget->StrokeLine(aStart, aEnd, aPattern, aStrokeOptions,
                                        aDrawOptions);
    }
  }
}

void DrawTargetTiled::Fill(const Path* aPath, const Pattern& aPattern,
                           const DrawOptions& aDrawOptions) {
  Rect deviceRect = aPath->GetBounds(mTransform);
  for (size_t i = 0; i < mTiles.size(); i++) {
    if (!mTiles[i].mClippedOut &&
        deviceRect.Intersects(Rect(mTiles[i].mTileOrigin.x,
                                   mTiles[i].mTileOrigin.y,
                                   mTiles[i].mDrawTarget->GetSize().width,
                                   mTiles[i].mDrawTarget->GetSize().height))) {
      mTiles[i].mDrawTarget->Fill(aPath, aPattern, aDrawOptions);
    }
  }
}

void DrawTargetTiled::PushLayer(bool aOpaque, Float aOpacity,
                                SourceSurface* aMask,
                                const Matrix& aMaskTransform,
                                const IntRect& aBounds, bool aCopyBackground) {
  // XXX - not sure this is what we want or whether we want to continue drawing
  // to a larger intermediate surface, that would require tweaking the code in
  // here a little though.
  for (size_t i = 0; i < mTiles.size(); i++) {
    if (!mTiles[i].mClippedOut) {
      IntRect bounds = aBounds;
      bounds.MoveBy(-mTiles[i].mTileOrigin);
      mTiles[i].mDrawTarget->PushLayer(aOpaque, aOpacity, aMask, aMaskTransform,
                                       bounds, aCopyBackground);
    }
  }

  PushedLayer layer(GetPermitSubpixelAA());
  mPushedLayers.push_back(layer);
  SetPermitSubpixelAA(aOpaque);
}

void DrawTargetTiled::PushLayerWithBlend(bool aOpaque, Float aOpacity,
                                         SourceSurface* aMask,
                                         const Matrix& aMaskTransform,
                                         const IntRect& aBounds,
                                         bool aCopyBackground,
                                         CompositionOp aOp) {
  // XXX - not sure this is what we want or whether we want to continue drawing
  // to a larger intermediate surface, that would require tweaking the code in
  // here a little though.
  for (size_t i = 0; i < mTiles.size(); i++) {
    if (!mTiles[i].mClippedOut) {
      IntRect bounds = aBounds;
      bounds.MoveBy(-mTiles[i].mTileOrigin);
      mTiles[i].mDrawTarget->PushLayerWithBlend(aOpaque, aOpacity, aMask,
                                                aMaskTransform, bounds,
                                                aCopyBackground, aOp);
    }
  }

  PushedLayer layer(GetPermitSubpixelAA());
  mPushedLayers.push_back(layer);
  SetPermitSubpixelAA(aOpaque);
}

void DrawTargetTiled::PopLayer() {
  // XXX - not sure this is what we want or whether we want to continue drawing
  // to a larger intermediate surface, that would require tweaking the code in
  // here a little though.
  for (size_t i = 0; i < mTiles.size(); i++) {
    if (!mTiles[i].mClippedOut) {
      mTiles[i].mDrawTarget->PopLayer();
    }
  }

  MOZ_ASSERT(mPushedLayers.size());
  const PushedLayer& layer = mPushedLayers.back();
  SetPermitSubpixelAA(layer.mOldPermitSubpixelAA);
  mPushedLayers.pop_back();
}

RefPtr<DrawTarget> DrawTargetTiled::CreateClippedDrawTarget(
    const Rect& aBounds, SurfaceFormat aFormat) {
  Rect deviceRect = mTransform.TransformBounds(aBounds);

  // Build up an approximation of the current clip rect by unioning
  // the tiles that are not clipped
  Rect clipRectApproximation;
  for (size_t i = 0; i < mTiles.size(); i++) {
    if (!mTiles[i].mClippedOut) {
      clipRectApproximation = clipRectApproximation.Union(
          Rect(mTiles[i].mTileOrigin.x, mTiles[i].mTileOrigin.y,
               mTiles[i].mDrawTarget->GetSize().width,
               mTiles[i].mDrawTarget->GetSize().height));
    }
  }

  IntRect clipBounds;
  if (!aBounds.IsEmpty()) {
    clipBounds = IntRect::RoundOut(deviceRect.Intersect(clipRectApproximation));
  } else {
    clipBounds = IntRect::RoundOut(clipRectApproximation);
  }

  RefPtr<DrawTarget> result;
  if (!clipBounds.IsEmpty()) {
    RefPtr<DrawTarget> dt = CreateSimilarDrawTarget(
        IntSize(clipBounds.width, clipBounds.height), aFormat);
    result = gfx::Factory::CreateOffsetDrawTarget(
        dt, IntPoint(clipBounds.x, clipBounds.y));
    result->SetTransform(mTransform);
  } else {
    // Everything is clipped but we still want some kind of surface
    result = CreateSimilarDrawTarget(IntSize(1, 1), aFormat);
  }
  return result;
}

void DrawTargetTiled::PadEdges(const IntRegion& aRegion) {
  for (size_t i = 0; i < mTiles.size(); i++) {
    if (mTiles[i].mClippedOut) {
      continue;
    }

    auto tileRect =
        RoundedOut(Rect(mTiles[i].mTileOrigin.x, mTiles[i].mTileOrigin.y,
                        mTiles[i].mDrawTarget->GetSize().width,
                        mTiles[i].mDrawTarget->GetSize().height));

    // We only need to pad edges on tiles that intersect the edge of the region
    if (aRegion.Intersects(tileRect) && !aRegion.Contains(tileRect)) {
      IntRegion padRegion = aRegion;
      padRegion.MoveBy(-mTiles[i].mTileOrigin);
      padRegion.AndWith(IntRect(0, 0, mTiles[i].mDrawTarget->GetSize().width,
                                mTiles[i].mDrawTarget->GetSize().height));
      mTiles[i].mDrawTarget->PadEdges(padRegion);
    }
  }
}

}  // namespace gfx
}  // namespace mozilla