commit-bot@chromium.org | ad854bf | 2014-05-29 18:46:38 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 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. |
| 6 | */ |
| 7 | |
| 8 | #ifndef GrRectanizer_pow2_DEFINED |
| 9 | #define GrRectanizer_pow2_DEFINED |
| 10 | |
| 11 | #include "GrRectanizer.h" |
| 12 | |
| 13 | class GrRectanizerPow2 : public GrRectanizer { |
| 14 | public: |
| 15 | GrRectanizerPow2(int w, int h) : INHERITED(w, h) { |
| 16 | this->reset(); |
| 17 | } |
| 18 | |
| 19 | virtual ~GrRectanizerPow2() { } |
| 20 | |
| 21 | virtual void reset() SK_OVERRIDE { |
| 22 | fNextStripY = 0; |
| 23 | fAreaSoFar = 0; |
| 24 | sk_bzero(fRows, sizeof(fRows)); |
| 25 | } |
| 26 | |
| 27 | virtual bool addRect(int w, int h, GrIPoint16* loc) SK_OVERRIDE; |
| 28 | |
| 29 | virtual float percentFull() const SK_OVERRIDE { |
| 30 | return fAreaSoFar / ((float)this->width() * this->height()); |
| 31 | } |
| 32 | |
| 33 | private: |
| 34 | static const int kMIN_HEIGHT_POW2 = 2; |
| 35 | |
| 36 | struct Row { |
| 37 | GrIPoint16 fLoc; |
| 38 | int fRowHeight; |
| 39 | |
| 40 | bool canAddWidth(int width, int containerWidth) const { |
| 41 | return fLoc.fX + width <= containerWidth; |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | Row fRows[16]; |
| 46 | |
| 47 | int fNextStripY; |
| 48 | int32_t fAreaSoFar; |
| 49 | |
| 50 | static int HeightToRowIndex(int height) { |
| 51 | SkASSERT(height >= kMIN_HEIGHT_POW2); |
| 52 | return 32 - SkCLZ(height - 1); |
| 53 | } |
| 54 | |
| 55 | bool canAddStrip(int height) const { |
| 56 | return fNextStripY + height <= this->height(); |
| 57 | } |
| 58 | |
| 59 | void initRow(Row* row, int rowHeight) { |
| 60 | row->fLoc.set(0, fNextStripY); |
| 61 | row->fRowHeight = rowHeight; |
| 62 | fNextStripY += rowHeight; |
| 63 | } |
| 64 | |
| 65 | typedef GrRectanizer INHERITED; |
| 66 | }; |
| 67 | |
| 68 | #endif |