epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #include "GrRectanizer.h" |
| 12 | #include "GrTBSearch.h" |
| 13 | |
| 14 | #define MIN_HEIGHT_POW2 2 |
| 15 | |
| 16 | class GrRectanizerPow2 : public GrRectanizer { |
| 17 | public: |
| 18 | GrRectanizerPow2(int w, int h) : GrRectanizer(w, h) { |
| 19 | fNextStripY = 0; |
| 20 | fAreaSoFar = 0; |
reed@google.com | 939ca7c | 2013-09-26 19:56:51 +0000 | [diff] [blame] | 21 | sk_bzero(fRows, sizeof(fRows)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | virtual ~GrRectanizerPow2() { |
| 25 | } |
| 26 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 27 | virtual void reset() { |
| 28 | fNextStripY = 0; |
| 29 | fAreaSoFar = 0; |
skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 30 | sk_bzero(fRows, sizeof(fRows)); |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 31 | } |
| 32 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 33 | virtual bool addRect(int w, int h, GrIPoint16* loc); |
| 34 | |
| 35 | virtual float percentFull() const { |
| 36 | return fAreaSoFar / ((float)this->width() * this->height()); |
| 37 | } |
| 38 | |
| 39 | virtual int stripToPurge(int height) const { return -1; } |
| 40 | virtual void purgeStripAtY(int yCoord) { } |
| 41 | |
| 42 | /////////////////////////////////////////////////////////////////////////// |
| 43 | |
| 44 | struct Row { |
| 45 | GrIPoint16 fLoc; |
| 46 | int fRowHeight; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 47 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 48 | bool canAddWidth(int width, int containerWidth) const { |
| 49 | return fLoc.fX + width <= containerWidth; |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | Row fRows[16]; |
| 54 | |
| 55 | static int HeightToRowIndex(int height) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 56 | SkASSERT(height >= MIN_HEIGHT_POW2); |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 57 | return 32 - SkCLZ(height - 1); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | int fNextStripY; |
| 61 | int32_t fAreaSoFar; |
| 62 | |
| 63 | bool canAddStrip(int height) const { |
| 64 | return fNextStripY + height <= this->height(); |
| 65 | } |
| 66 | |
| 67 | void initRow(Row* row, int rowHeight) { |
| 68 | row->fLoc.set(0, fNextStripY); |
| 69 | row->fRowHeight = rowHeight; |
| 70 | fNextStripY += rowHeight; |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | bool GrRectanizerPow2::addRect(int width, int height, GrIPoint16* loc) { |
| 75 | if ((unsigned)width > (unsigned)this->width() || |
| 76 | (unsigned)height > (unsigned)this->height()) { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | int32_t area = width * height; |
| 81 | |
| 82 | /* |
| 83 | We use bsearch, but there may be more than one row with the same height, |
| 84 | so we actually search for height-1, which can only be a pow2 itself if |
| 85 | height == 2. Thus we set a minimum height. |
| 86 | */ |
| 87 | height = GrNextPow2(height); |
| 88 | if (height < MIN_HEIGHT_POW2) { |
| 89 | height = MIN_HEIGHT_POW2; |
| 90 | } |
| 91 | |
| 92 | Row* row = &fRows[HeightToRowIndex(height)]; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 93 | SkASSERT(row->fRowHeight == 0 || row->fRowHeight == height); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 94 | |
| 95 | if (0 == row->fRowHeight) { |
| 96 | if (!this->canAddStrip(height)) { |
| 97 | return false; |
| 98 | } |
| 99 | this->initRow(row, height); |
| 100 | } else { |
| 101 | if (!row->canAddWidth(width, this->width())) { |
| 102 | if (!this->canAddStrip(height)) { |
| 103 | return false; |
| 104 | } |
| 105 | // that row is now "full", so retarget our Row record for |
| 106 | // another one |
| 107 | this->initRow(row, height); |
| 108 | } |
| 109 | } |
| 110 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 111 | SkASSERT(row->fRowHeight == height); |
| 112 | SkASSERT(row->canAddWidth(width, this->width())); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 113 | *loc = row->fLoc; |
| 114 | row->fLoc.fX += width; |
| 115 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 116 | SkASSERT(row->fLoc.fX <= this->width()); |
| 117 | SkASSERT(row->fLoc.fY <= this->height()); |
| 118 | SkASSERT(fNextStripY <= this->height()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 119 | fAreaSoFar += area; |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | /////////////////////////////////////////////////////////////////////////////// |
| 124 | |
commit-bot@chromium.org | 09846a0 | 2013-10-02 17:37:59 +0000 | [diff] [blame] | 125 | // factory is now in GrRectanizer_skyline.cpp |
| 126 | //GrRectanizer* GrRectanizer::Factory(int width, int height) { |
| 127 | // return SkNEW_ARGS(GrRectanizerPow2, (width, height)); |
| 128 | //} |