1/*
2 * Copyright 2010 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#ifndef GrPlotMgr_DEFINED
9#define GrPlotMgr_DEFINED
10
11#include "GrTypes.h"
12#include "SkTypes.h"
13
14class GrPlotMgr : SkNoncopyable {
15public:
16    GrPlotMgr(int width, int height) {
17        fDim.set(width, height);
18        size_t needed = width * height;
19        if (needed <= sizeof(fStorage)) {
20            fBusy = fStorage;
21        } else {
22            fBusy = SkNEW_ARRAY(char, needed);
23        }
24        this->reset();
25    }
26
27    ~GrPlotMgr() {
28        if (fBusy != fStorage) {
29            delete[] fBusy;
30        }
31    }
32
33    void reset() {
34        sk_bzero(fBusy, fDim.fX * fDim.fY);
35    }
36
37    bool newPlot(SkIPoint16* loc) {
38        char* busy = fBusy;
39        for (int y = 0; y < fDim.fY; y++) {
40            for (int x = 0; x < fDim.fX; x++) {
41                if (!*busy) {
42                    *busy = true;
43                    loc->set(x, y);
44                    return true;
45                }
46                busy++;
47            }
48        }
49        return false;
50    }
51
52    bool isBusy(int x, int y) const {
53        SkASSERT((unsigned)x < (unsigned)fDim.fX);
54        SkASSERT((unsigned)y < (unsigned)fDim.fY);
55        return fBusy[y * fDim.fX + x] != 0;
56    }
57
58    void freePlot(int x, int y) {
59        SkASSERT((unsigned)x < (unsigned)fDim.fX);
60        SkASSERT((unsigned)y < (unsigned)fDim.fY);
61        fBusy[y * fDim.fX + x] = false;
62    }
63
64private:
65    enum {
66        STORAGE = 64
67    };
68    char fStorage[STORAGE];
69    char* fBusy;
70    SkIPoint16  fDim;
71};
72
73#endif
74