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