blob: 8d55ed0b9c7d398d490cad7e8b3d0818fc77764d [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"
12#include "GrPoint.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000013#include "SkTypes.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000015class GrPlotMgr : SkNoncopyable {
reed@google.comac10a2d2010-12-22 21:39:39 +000016public:
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 {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000023 fBusy = SkNEW_ARRAY(char, needed);
reed@google.comac10a2d2010-12-22 21:39:39 +000024 }
25 this->reset();
26 }
27
28 ~GrPlotMgr() {
29 if (fBusy != fStorage) {
30 delete[] fBusy;
31 }
32 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000033
reed@google.comac10a2d2010-12-22 21:39:39 +000034 void reset() {
reed@google.com939ca7c2013-09-26 19:56:51 +000035 sk_bzero(fBusy, fDim.fX * fDim.fY);
reed@google.comac10a2d2010-12-22 21:39:39 +000036 }
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 {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000054 SkASSERT((unsigned)x < (unsigned)fDim.fX);
55 SkASSERT((unsigned)y < (unsigned)fDim.fY);
reed@google.comac10a2d2010-12-22 21:39:39 +000056 return fBusy[y * fDim.fX + x] != 0;
57 }
58
59 void freePlot(int x, int y) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000060 SkASSERT((unsigned)x < (unsigned)fDim.fX);
61 SkASSERT((unsigned)y < (unsigned)fDim.fY);
reed@google.comac10a2d2010-12-22 21:39:39 +000062 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