reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2010 Google Inc. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #ifndef GrPlotMgr_DEFINED |
| 19 | #define GrPlotMgr_DEFINED |
| 20 | |
| 21 | #include "GrTypes.h" |
| 22 | #include "GrPoint.h" |
| 23 | |
| 24 | class GrPlotMgr : GrNoncopyable { |
| 25 | public: |
| 26 | GrPlotMgr(int width, int height) { |
| 27 | fDim.set(width, height); |
| 28 | size_t needed = width * height; |
| 29 | if (needed <= sizeof(fStorage)) { |
| 30 | fBusy = fStorage; |
| 31 | } else { |
| 32 | fBusy = new char[needed]; |
| 33 | } |
| 34 | this->reset(); |
| 35 | } |
| 36 | |
| 37 | ~GrPlotMgr() { |
| 38 | if (fBusy != fStorage) { |
| 39 | delete[] fBusy; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | void reset() { |
| 44 | Gr_bzero(fBusy, fDim.fX * fDim.fY); |
| 45 | } |
| 46 | |
| 47 | bool newPlot(GrIPoint16* loc) { |
| 48 | char* busy = fBusy; |
| 49 | for (int y = 0; y < fDim.fY; y++) { |
| 50 | for (int x = 0; x < fDim.fX; x++) { |
| 51 | if (!*busy) { |
| 52 | *busy = true; |
| 53 | loc->set(x, y); |
| 54 | return true; |
| 55 | } |
| 56 | busy++; |
| 57 | } |
| 58 | } |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | bool isBusy(int x, int y) const { |
| 63 | GrAssert((unsigned)x < (unsigned)fDim.fX); |
| 64 | GrAssert((unsigned)y < (unsigned)fDim.fY); |
| 65 | return fBusy[y * fDim.fX + x] != 0; |
| 66 | } |
| 67 | |
| 68 | void freePlot(int x, int y) { |
| 69 | GrAssert((unsigned)x < (unsigned)fDim.fX); |
| 70 | GrAssert((unsigned)y < (unsigned)fDim.fY); |
| 71 | fBusy[y * fDim.fX + x] = false; |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | enum { |
| 76 | STORAGE = 64 |
| 77 | }; |
| 78 | char fStorage[STORAGE]; |
| 79 | char* fBusy; |
| 80 | GrIPoint16 fDim; |
| 81 | }; |
| 82 | |
| 83 | #endif |
| 84 | |