reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 8 | #ifndef GrPlotMgr_DEFINED |
| 9 | #define GrPlotMgr_DEFINED |
| 10 | |
| 11 | #include "GrTypes.h" |
| 12 | #include "GrPoint.h" |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 13 | #include "SkTypes.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 14 | |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame^] | 15 | class GrPlotMgr : SkNoncopyable { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 16 | public: |
| 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.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 23 | fBusy = SkNEW_ARRAY(char, needed); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 24 | } |
| 25 | this->reset(); |
| 26 | } |
| 27 | |
| 28 | ~GrPlotMgr() { |
| 29 | if (fBusy != fStorage) { |
| 30 | delete[] fBusy; |
| 31 | } |
| 32 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 33 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 34 | void reset() { |
reed@google.com | 939ca7c | 2013-09-26 19:56:51 +0000 | [diff] [blame] | 35 | sk_bzero(fBusy, fDim.fX * fDim.fY); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 36 | } |
| 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.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 54 | SkASSERT((unsigned)x < (unsigned)fDim.fX); |
| 55 | SkASSERT((unsigned)y < (unsigned)fDim.fY); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 56 | return fBusy[y * fDim.fX + x] != 0; |
| 57 | } |
| 58 | |
| 59 | void freePlot(int x, int y) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 60 | SkASSERT((unsigned)x < (unsigned)fDim.fX); |
| 61 | SkASSERT((unsigned)y < (unsigned)fDim.fY); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 62 | fBusy[y * fDim.fX + x] = false; |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | enum { |
| 67 | STORAGE = 64 |
| 68 | }; |
| 69 | char fStorage[STORAGE]; |
| 70 | char* fBusy; |
| 71 | GrIPoint16 fDim; |
| 72 | }; |
| 73 | |
| 74 | #endif |