Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 | |
| 2 /* | |
| 3 * Copyright 2013 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. | |
| 7 */ | |
| 8 | |
| 9 #include "GrRectanizer.h" | |
| 10 #include "SkTDArray.h" | |
| 11 | |
| 12 // Pack rectangles and track the current silhouette | |
| 13 // Based in part on Jukka Jylänki's work at http://clb.demon.fi | |
| 14 | |
| 15 class GrRectanizerSkyline : public GrRectanizer { | |
| 16 public: | |
| 17 GrRectanizerSkyline(int w, int h) : GrRectanizer(w, h) { | |
| 18 reset(); | |
| 19 } | |
| 20 | |
| 21 virtual ~GrRectanizerSkyline() { | |
| 22 } | |
| 23 | |
| 24 virtual void reset() { | |
| 25 fAreaSoFar = 0; | |
| 26 fSkyline.reset(); | |
| 27 SkylineSegment* seg = fSkyline.append(1); | |
| 28 seg->fX = 0; | |
| 29 seg->fY = 0; | |
| 30 seg->fWidth = width(); | |
| 31 } | |
| 32 | |
| 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 SkylineSegment { | |
| 45 int fX; | |
| 46 int fY; | |
| 47 int fWidth; | |
| 48 }; | |
| 49 | |
| 50 SkTDArray<SkylineSegment> fSkyline; | |
| 51 | |
| 52 int32_t fAreaSoFar; | |
| 53 | |
| 54 bool rectangleFits(int skylineIndex, int width, int height, int* y) const; | |
| 55 void addSkylineLevel(int skylineIndex, int x, int y, int width, int height); | |
| 56 }; | |
| 57 | |
| 58 bool GrRectanizerSkyline::addRect(int width, int height, GrIPoint16* loc) { | |
| 59 if ((unsigned)width > (unsigned)this->width() || | |
| 60 (unsigned)height > (unsigned)this->height()) { | |
| 61 return false; | |
| 62 } | |
| 63 | |
| 64 // find position for new rectangle | |
| 65 int bestWidth = this->width() + 1; | |
| 66 int bestX; | |
| 67 int bestY = this->height() + 1; | |
| 68 int bestIndex = -1; | |
| 69 for (int i = 0; i < fSkyline.count(); ++i) { | |
| 70 int y; | |
| 71 if (rectangleFits(i, width, height, &y)) { | |
|
bsalomon
2013/10/02 16:40:06
this->
tfarina
2013/10/02 16:49:59
there may be a better place to ask, but what is th
| |
| 72 // minimize y position first, then width of skyline | |
| 73 if (y < bestY || (y == bestY && fSkyline[i].fWidth < bestWidth)) { | |
| 74 bestIndex = i; | |
| 75 bestWidth = fSkyline[i].fWidth; | |
| 76 bestX = fSkyline[i].fX; | |
| 77 bestY = y; | |
| 78 } | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 // add rectangle to skyline | |
| 83 if (-1 != bestIndex) { | |
| 84 addSkylineLevel(bestIndex, bestX, bestY, width, height); | |
|
bsalomon
2013/10/02 16:40:06
this->
| |
| 85 loc->fX = bestX; | |
| 86 loc->fY = bestY; | |
| 87 | |
| 88 fAreaSoFar += width*height; | |
| 89 return true; | |
| 90 } | |
| 91 | |
| 92 loc->fX = 0; | |
| 93 loc->fY = 0; | |
| 94 return false; | |
| 95 } | |
| 96 | |
| 97 bool GrRectanizerSkyline::rectangleFits(int skylineIndex, int width, int height, int* ypos) const { | |
| 98 int x = fSkyline[skylineIndex].fX; | |
| 99 if (x + width > this->width()) | |
|
bsalomon
2013/10/02 16:40:06
{}
| |
| 100 return false; | |
| 101 | |
| 102 int widthLeft = width; | |
| 103 int i = skylineIndex; | |
| 104 int y = fSkyline[skylineIndex].fY; | |
| 105 while (widthLeft > 0) | |
| 106 { | |
|
bsalomon
2013/10/02 16:40:06
brace goes on prev line (a bunch in the next funct
| |
| 107 y = SkMax32(y, fSkyline[i].fY); | |
| 108 if (y + height > this->height()) | |
| 109 return false; | |
| 110 widthLeft -= fSkyline[i].fWidth; | |
| 111 ++i; | |
| 112 SkASSERT(i < fSkyline.count() || widthLeft <= 0); | |
| 113 } | |
| 114 | |
| 115 *ypos = y; | |
| 116 return true; | |
| 117 } | |
| 118 | |
| 119 void GrRectanizerSkyline::addSkylineLevel(int skylineIndex, int x, int y, int wi dth, int height) { | |
| 120 SkylineSegment newSegment; | |
| 121 newSegment.fX = x; | |
| 122 newSegment.fY = y + height; | |
| 123 newSegment.fWidth = width; | |
| 124 fSkyline.insert(skylineIndex, 1, &newSegment); | |
| 125 | |
| 126 SkASSERT(newSegment.fX + newSegment.fWidth <= this->width()); | |
| 127 SkASSERT(newSegment.fY <= this->height()); | |
| 128 | |
| 129 // delete width of this skyline segment from following ones | |
| 130 for (int i = skylineIndex+1; i < fSkyline.count(); ++i) | |
| 131 { | |
| 132 SkASSERT(fSkyline[i-1].fX <= fSkyline[i].fX); | |
| 133 | |
| 134 if (fSkyline[i].fX < fSkyline[i-1].fX + fSkyline[i-1].fWidth) | |
| 135 { | |
| 136 int shrink = fSkyline[i-1].fX + fSkyline[i-1].fWidth - fSkyline[i].f X; | |
| 137 | |
| 138 fSkyline[i].fX += shrink; | |
| 139 fSkyline[i].fWidth -= shrink; | |
| 140 | |
| 141 if (fSkyline[i].fWidth <= 0) | |
| 142 { | |
| 143 fSkyline.remove(i); | |
| 144 --i; | |
| 145 } | |
| 146 else | |
| 147 break; | |
| 148 } | |
| 149 else | |
| 150 break; | |
| 151 } | |
| 152 | |
| 153 // merge fSkylines | |
| 154 for (int i = 0; i < fSkyline.count()-1; ++i) { | |
| 155 if (fSkyline[i].fY == fSkyline[i+1].fY) | |
| 156 { | |
| 157 fSkyline[i].fWidth += fSkyline[i+1].fWidth; | |
| 158 fSkyline.remove(i+1); | |
| 159 --i; | |
| 160 } | |
| 161 } | |
| 162 } | |
| 163 | |
| 164 /////////////////////////////////////////////////////////////////////////////// | |
| 165 | |
| 166 GrRectanizer* GrRectanizer::Factory(int width, int height) { | |
| 167 return SkNEW_ARGS(GrRectanizerSkyline, (width, height)); | |
| 168 } | |
| 169 | |
| OLD | NEW |