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 | |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame^] | 13 | // This Rectanizer quantizes the incoming rects to powers of 2. Each power |
| 14 | // of two can have, at most, one active row/shelf. Once a row/shelf for |
| 15 | // a particular power of two gets full its fRows entry is recycled to point |
| 16 | // to a new row. |
| 17 | // The skyline algorithm almost always provides a better packing. |
commit-bot@chromium.org | ad854bf | 2014-05-29 18:46:38 +0000 | [diff] [blame] | 18 | class GrRectanizerPow2 : public GrRectanizer { |
| 19 | public: |
| 20 | GrRectanizerPow2(int w, int h) : INHERITED(w, h) { |
| 21 | this->reset(); |
| 22 | } |
| 23 | |
| 24 | virtual ~GrRectanizerPow2() { } |
| 25 | |
| 26 | virtual void reset() SK_OVERRIDE { |
| 27 | fNextStripY = 0; |
| 28 | fAreaSoFar = 0; |
| 29 | sk_bzero(fRows, sizeof(fRows)); |
| 30 | } |
| 31 | |
| 32 | virtual bool addRect(int w, int h, GrIPoint16* loc) SK_OVERRIDE; |
| 33 | |
| 34 | virtual float percentFull() const SK_OVERRIDE { |
| 35 | return fAreaSoFar / ((float)this->width() * this->height()); |
| 36 | } |
| 37 | |
| 38 | private: |
| 39 | static const int kMIN_HEIGHT_POW2 = 2; |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame^] | 40 | static const int kMaxExponent = 16; |
commit-bot@chromium.org | ad854bf | 2014-05-29 18:46:38 +0000 | [diff] [blame] | 41 | |
| 42 | struct Row { |
| 43 | GrIPoint16 fLoc; |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame^] | 44 | // fRowHeight is actually known by this struct's position in fRows |
| 45 | // but it is used to signal if there exists an open row of this height |
commit-bot@chromium.org | ad854bf | 2014-05-29 18:46:38 +0000 | [diff] [blame] | 46 | int fRowHeight; |
| 47 | |
| 48 | bool canAddWidth(int width, int containerWidth) const { |
| 49 | return fLoc.fX + width <= containerWidth; |
| 50 | } |
| 51 | }; |
| 52 | |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame^] | 53 | Row fRows[kMaxExponent]; // 0-th entry will be unused |
commit-bot@chromium.org | ad854bf | 2014-05-29 18:46:38 +0000 | [diff] [blame] | 54 | |
| 55 | int fNextStripY; |
| 56 | int32_t fAreaSoFar; |
| 57 | |
| 58 | static int HeightToRowIndex(int height) { |
| 59 | SkASSERT(height >= kMIN_HEIGHT_POW2); |
robertphillips | 901e96d | 2014-06-02 07:15:18 -0700 | [diff] [blame^] | 60 | int index = 32 - SkCLZ(height - 1); |
| 61 | SkASSERT(index < kMaxExponent); |
| 62 | return index; |
commit-bot@chromium.org | ad854bf | 2014-05-29 18:46:38 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | bool canAddStrip(int height) const { |
| 66 | return fNextStripY + height <= this->height(); |
| 67 | } |
| 68 | |
| 69 | void initRow(Row* row, int rowHeight) { |
| 70 | row->fLoc.set(0, fNextStripY); |
| 71 | row->fRowHeight = rowHeight; |
| 72 | fNextStripY += rowHeight; |
| 73 | } |
| 74 | |
| 75 | typedef GrRectanizer INHERITED; |
| 76 | }; |
| 77 | |
| 78 | #endif |