SkDrawLooper.cpp revision 8b79028d27653fafcde6901affba48b987b52e43
1/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkDrawLooper.h"
9#include "SkCanvas.h"
10#include "SkMatrix.h"
11#include "SkPaint.h"
12#include "SkRect.h"
13
14SK_DEFINE_INST_COUNT(SkDrawLooper)
15
16bool SkDrawLooper::canComputeFastBounds(const SkPaint& paint) {
17    SkCanvas canvas;
18
19    this->init(&canvas);
20    for (;;) {
21        SkPaint p(paint);
22        if (this->next(&canvas, &p)) {
23            p.setLooper(NULL);
24            if (!p.canComputeFastBounds()) {
25                return false;
26            }
27        } else {
28            break;
29        }
30    }
31    return true;
32}
33
34void SkDrawLooper::computeFastBounds(const SkPaint& paint, const SkRect& src,
35                                     SkRect* dst) {
36    SkCanvas canvas;
37
38    this->init(&canvas);
39    for (bool firstTime = true;; firstTime = false) {
40        SkPaint p(paint);
41        if (this->next(&canvas, &p)) {
42            SkRect r(src);
43
44            p.setLooper(NULL);
45            p.computeFastBounds(r, &r);
46            canvas.getTotalMatrix().mapRect(&r);
47
48            if (firstTime) {
49                *dst = r;
50            } else {
51                dst->join(r);
52            }
53        } else {
54            break;
55        }
56    }
57}
58