blob: 1441611327403524ae1546333efc9726e4430114 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
reed@google.comac10a2d2010-12-22 21:39:39 +00008#ifndef GrPlotMgr_DEFINED
9#define GrPlotMgr_DEFINED
10
11#include "GrTypes.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000012#include "SkTypes.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000013
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000014class GrPlotMgr : SkNoncopyable {
reed@google.comac10a2d2010-12-22 21:39:39 +000015public:
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 {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000022 fBusy = SkNEW_ARRAY(char, needed);
reed@google.comac10a2d2010-12-22 21:39:39 +000023 }
24 this->reset();
25 }
26
27 ~GrPlotMgr() {
28 if (fBusy != fStorage) {
29 delete[] fBusy;
30 }
31 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000032
reed@google.comac10a2d2010-12-22 21:39:39 +000033 void reset() {
reed@google.com939ca7c2013-09-26 19:56:51 +000034 sk_bzero(fBusy, fDim.fX * fDim.fY);
reed@google.comac10a2d2010-12-22 21:39:39 +000035 }
36
robertphillipsd5373412014-06-02 10:20:14 -070037 bool newPlot(SkIPoint16* loc) {
reed@google.comac10a2d2010-12-22 21:39:39 +000038 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 {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000053 SkASSERT((unsigned)x < (unsigned)fDim.fX);
54 SkASSERT((unsigned)y < (unsigned)fDim.fY);
reed@google.comac10a2d2010-12-22 21:39:39 +000055 return fBusy[y * fDim.fX + x] != 0;
56 }
57
58 void freePlot(int x, int y) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000059 SkASSERT((unsigned)x < (unsigned)fDim.fX);
60 SkASSERT((unsigned)y < (unsigned)fDim.fY);
reed@google.comac10a2d2010-12-22 21:39:39 +000061 fBusy[y * fDim.fX + x] = false;
62 }
63
64private:
65 enum {
66 STORAGE = 64
67 };
68 char fStorage[STORAGE];
69 char* fBusy;
robertphillipsd5373412014-06-02 10:20:14 -070070 SkIPoint16 fDim;
reed@google.comac10a2d2010-12-22 21:39:39 +000071};
72
73#endif