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