1
2/*
3 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10
11#ifndef GrPlotMgr_DEFINED
12#define GrPlotMgr_DEFINED
13
14#include "GrTypes.h"
15#include "GrPoint.h"
16
17class GrPlotMgr : GrNoncopyable {
18public:
19    GrPlotMgr(int width, int height) {
20        fDim.set(width, height);
21        size_t needed = width * height;
22        if (needed <= sizeof(fStorage)) {
23            fBusy = fStorage;
24        } else {
25            fBusy = SkNEW_ARRAY(char, needed);
26        }
27        this->reset();
28    }
29
30    ~GrPlotMgr() {
31        if (fBusy != fStorage) {
32            delete[] fBusy;
33        }
34    }
35
36    void reset() {
37        Gr_bzero(fBusy, fDim.fX * fDim.fY);
38    }
39
40    bool newPlot(GrIPoint16* loc) {
41        char* busy = fBusy;
42        for (int y = 0; y < fDim.fY; y++) {
43            for (int x = 0; x < fDim.fX; x++) {
44                if (!*busy) {
45                    *busy = true;
46                    loc->set(x, y);
47                    return true;
48                }
49                busy++;
50            }
51        }
52        return false;
53    }
54
55    bool isBusy(int x, int y) const {
56        GrAssert((unsigned)x < (unsigned)fDim.fX);
57        GrAssert((unsigned)y < (unsigned)fDim.fY);
58        return fBusy[y * fDim.fX + x] != 0;
59    }
60
61    void freePlot(int x, int y) {
62        GrAssert((unsigned)x < (unsigned)fDim.fX);
63        GrAssert((unsigned)y < (unsigned)fDim.fY);
64        fBusy[y * fDim.fX + x] = false;
65    }
66
67private:
68    enum {
69        STORAGE = 64
70    };
71    char fStorage[STORAGE];
72    char* fBusy;
73    GrIPoint16  fDim;
74};
75
76#endif
77