blob: 4f79a2135ef19548aa34e75c068f3623bf036a85 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#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 {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000025 fBusy = SkNEW_ARRAY(char, needed);
reed@google.comac10a2d2010-12-22 21:39:39 +000026 }
27 this->reset();
28 }
29
30 ~GrPlotMgr() {
31 if (fBusy != fStorage) {
32 delete[] fBusy;
33 }
34 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000035
reed@google.comac10a2d2010-12-22 21:39:39 +000036 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