2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkDeviceLooper.h"
10 SkDeviceLooper::SkDeviceLooper(const SkBitmap& base,
11 const SkRasterClip& rc,
12 const SkIRect& bounds, bool aa)
15 , fSubsetRC(rc.isForceConservativeRects())
16 , fDelta(aa ? kAA_Delta : kBW_Delta)
18 // sentinels that next() has not yet been called, and so our mapper functions
19 // should not be called either.
24 // clip must be contained by the bitmap
25 SkASSERT(SkIRect::MakeWH(base.width(), base.height()).contains(rc.getBounds()));
28 if (rc.isEmpty() || !fClippedBounds.intersect(bounds, rc.getBounds())) {
30 } else if (this->fitsInDelta(fClippedBounds)) {
31 fState = kSimple_State;
33 // back up by 1 DX, so that next() will put us in a correct starting
35 fCurrOffset.set(fClippedBounds.left() - fDelta,
36 fClippedBounds.top());
37 fState = kComplex_State;
41 SkDeviceLooper::~SkDeviceLooper() {
44 void SkDeviceLooper::mapRect(SkRect* dst, const SkRect& src) const {
45 SkASSERT(kDone_State != fState);
46 SkASSERT(fCurrBitmap);
50 dst->offset(SkIntToScalar(-fCurrOffset.fX),
51 SkIntToScalar(-fCurrOffset.fY));
54 void SkDeviceLooper::mapMatrix(SkMatrix* dst, const SkMatrix& src) const {
55 SkASSERT(kDone_State != fState);
56 SkASSERT(fCurrBitmap);
60 dst->postTranslate(SkIntToScalar(-fCurrOffset.fX),
61 SkIntToScalar(-fCurrOffset.fY));
64 bool SkDeviceLooper::computeCurrBitmapAndClip() {
65 SkASSERT(kComplex_State == fState);
67 SkIRect r = SkIRect::MakeXYWH(fCurrOffset.x(), fCurrOffset.y(),
69 if (!fBaseBitmap.extractSubset(&fSubsetBitmap, r)) {
72 fSubsetBitmap.lockPixels();
73 fBaseRC.translate(-r.left(), -r.top(), &fSubsetRC);
74 (void)fSubsetRC.op(SkIRect::MakeWH(fDelta, fDelta),
75 SkRegion::kIntersect_Op);
78 fCurrBitmap = &fSubsetBitmap;
80 return !fCurrRC->isEmpty();
83 static bool next_tile(const SkIRect& boundary, int delta, SkIPoint* offset) {
84 // can we move to the right?
85 if (offset->x() + delta < boundary.right()) {
90 // reset to the left, but move down a row
91 offset->fX = boundary.left();
92 if (offset->y() + delta < boundary.bottom()) {
97 // offset is now outside of boundary, so we're done
101 bool SkDeviceLooper::next() {
104 // in theory, we should not get called here, since we must have
105 // previously returned false, but we check anyway.
109 // first time for simple
110 if (NULL == fCurrBitmap) {
111 fCurrBitmap = &fBaseBitmap;
113 fCurrOffset.set(0, 0);
116 // 2nd time for simple, we are done
120 // need to propogate fCurrOffset through clippedbounds
121 // left to right, until we wrap around and move down
123 while (next_tile(fClippedBounds, fDelta, &fCurrOffset)) {
124 if (this->computeCurrBitmapAndClip()) {
130 fState = kDone_State;