OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrRectanizer_pow2_DEFINED | 8 #ifndef GrRectanizer_pow2_DEFINED |
9 #define GrRectanizer_pow2_DEFINED | 9 #define GrRectanizer_pow2_DEFINED |
10 | 10 |
11 #include "GrRectanizer.h" | 11 #include "GrRectanizer.h" |
12 | 12 |
| 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. |
13 class GrRectanizerPow2 : public GrRectanizer { | 18 class GrRectanizerPow2 : public GrRectanizer { |
14 public: | 19 public: |
15 GrRectanizerPow2(int w, int h) : INHERITED(w, h) { | 20 GrRectanizerPow2(int w, int h) : INHERITED(w, h) { |
16 this->reset(); | 21 this->reset(); |
17 } | 22 } |
18 | 23 |
19 virtual ~GrRectanizerPow2() { } | 24 virtual ~GrRectanizerPow2() { } |
20 | 25 |
21 virtual void reset() SK_OVERRIDE { | 26 virtual void reset() SK_OVERRIDE { |
22 fNextStripY = 0; | 27 fNextStripY = 0; |
23 fAreaSoFar = 0; | 28 fAreaSoFar = 0; |
24 sk_bzero(fRows, sizeof(fRows)); | 29 sk_bzero(fRows, sizeof(fRows)); |
25 } | 30 } |
26 | 31 |
27 virtual bool addRect(int w, int h, GrIPoint16* loc) SK_OVERRIDE; | 32 virtual bool addRect(int w, int h, GrIPoint16* loc) SK_OVERRIDE; |
28 | 33 |
29 virtual float percentFull() const SK_OVERRIDE { | 34 virtual float percentFull() const SK_OVERRIDE { |
30 return fAreaSoFar / ((float)this->width() * this->height()); | 35 return fAreaSoFar / ((float)this->width() * this->height()); |
31 } | 36 } |
32 | 37 |
33 private: | 38 private: |
34 static const int kMIN_HEIGHT_POW2 = 2; | 39 static const int kMIN_HEIGHT_POW2 = 2; |
| 40 static const int kMaxExponent = 16; |
35 | 41 |
36 struct Row { | 42 struct Row { |
37 GrIPoint16 fLoc; | 43 GrIPoint16 fLoc; |
| 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 |
38 int fRowHeight; | 46 int fRowHeight; |
39 | 47 |
40 bool canAddWidth(int width, int containerWidth) const { | 48 bool canAddWidth(int width, int containerWidth) const { |
41 return fLoc.fX + width <= containerWidth; | 49 return fLoc.fX + width <= containerWidth; |
42 } | 50 } |
43 }; | 51 }; |
44 | 52 |
45 Row fRows[16]; | 53 Row fRows[kMaxExponent]; // 0-th entry will be unused |
46 | 54 |
47 int fNextStripY; | 55 int fNextStripY; |
48 int32_t fAreaSoFar; | 56 int32_t fAreaSoFar; |
49 | 57 |
50 static int HeightToRowIndex(int height) { | 58 static int HeightToRowIndex(int height) { |
51 SkASSERT(height >= kMIN_HEIGHT_POW2); | 59 SkASSERT(height >= kMIN_HEIGHT_POW2); |
52 return 32 - SkCLZ(height - 1); | 60 int index = 32 - SkCLZ(height - 1); |
| 61 SkASSERT(index < kMaxExponent); |
| 62 return index; |
53 } | 63 } |
54 | 64 |
55 bool canAddStrip(int height) const { | 65 bool canAddStrip(int height) const { |
56 return fNextStripY + height <= this->height(); | 66 return fNextStripY + height <= this->height(); |
57 } | 67 } |
58 | 68 |
59 void initRow(Row* row, int rowHeight) { | 69 void initRow(Row* row, int rowHeight) { |
60 row->fLoc.set(0, fNextStripY); | 70 row->fLoc.set(0, fNextStripY); |
61 row->fRowHeight = rowHeight; | 71 row->fRowHeight = rowHeight; |
62 fNextStripY += rowHeight; | 72 fNextStripY += rowHeight; |
63 } | 73 } |
64 | 74 |
65 typedef GrRectanizer INHERITED; | 75 typedef GrRectanizer INHERITED; |
66 }; | 76 }; |
67 | 77 |
68 #endif | 78 #endif |
OLD | NEW |