epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #ifndef GrPlotMgr_DEFINED |
| 12 | #define GrPlotMgr_DEFINED |
| 13 | |
| 14 | #include "GrTypes.h" |
| 15 | #include "GrPoint.h" |
| 16 | |
| 17 | class GrPlotMgr : GrNoncopyable { |
| 18 | public: |
| 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.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 25 | fBusy = SkNEW_ARRAY(char, needed); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 26 | } |
| 27 | this->reset(); |
| 28 | } |
| 29 | |
| 30 | ~GrPlotMgr() { |
| 31 | if (fBusy != fStorage) { |
| 32 | delete[] fBusy; |
| 33 | } |
| 34 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 35 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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 | |
| 67 | private: |
| 68 | enum { |
| 69 | STORAGE = 64 |
| 70 | }; |
| 71 | char fStorage[STORAGE]; |
| 72 | char* fBusy; |
| 73 | GrIPoint16 fDim; |
| 74 | }; |
| 75 | |
| 76 | #endif |