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