blob: 200fa8327912f229e3956cfe1648761fd07f4b87 [file] [log] [blame]
commit-bot@chromium.org09846a02013-10-02 17:37:59 +00001
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
commit-bot@chromium.orgad854bf2014-05-29 18:46:38 +00009#include "GrRectanizer_skyline.h"
commit-bot@chromium.org09846a02013-10-02 17:37:59 +000010
11bool GrRectanizerSkyline::addRect(int width, int height, GrIPoint16* loc) {
12 if ((unsigned)width > (unsigned)this->width() ||
13 (unsigned)height > (unsigned)this->height()) {
14 return false;
15 }
16
17 // find position for new rectangle
18 int bestWidth = this->width() + 1;
19 int bestX;
20 int bestY = this->height() + 1;
21 int bestIndex = -1;
22 for (int i = 0; i < fSkyline.count(); ++i) {
23 int y;
24 if (this->rectangleFits(i, width, height, &y)) {
25 // minimize y position first, then width of skyline
26 if (y < bestY || (y == bestY && fSkyline[i].fWidth < bestWidth)) {
27 bestIndex = i;
28 bestWidth = fSkyline[i].fWidth;
29 bestX = fSkyline[i].fX;
30 bestY = y;
31 }
32 }
33 }
34
35 // add rectangle to skyline
36 if (-1 != bestIndex) {
37 this->addSkylineLevel(bestIndex, bestX, bestY, width, height);
38 loc->fX = bestX;
39 loc->fY = bestY;
40
41 fAreaSoFar += width*height;
42 return true;
43 }
44
45 loc->fX = 0;
46 loc->fY = 0;
47 return false;
48}
49
50bool GrRectanizerSkyline::rectangleFits(int skylineIndex, int width, int height, int* ypos) const {
51 int x = fSkyline[skylineIndex].fX;
52 if (x + width > this->width()) {
53 return false;
54 }
55
56 int widthLeft = width;
57 int i = skylineIndex;
58 int y = fSkyline[skylineIndex].fY;
59 while (widthLeft > 0) {
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +000060 y = SkMax32(y, fSkyline[i].fY);
commit-bot@chromium.org09846a02013-10-02 17:37:59 +000061 if (y + height > this->height()) {
62 return false;
63 }
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +000064 widthLeft -= fSkyline[i].fWidth;
65 ++i;
66 SkASSERT(i < fSkyline.count() || widthLeft <= 0);
commit-bot@chromium.org09846a02013-10-02 17:37:59 +000067 }
68
69 *ypos = y;
70 return true;
71}
72
73void GrRectanizerSkyline::addSkylineLevel(int skylineIndex, int x, int y, int width, int height) {
74 SkylineSegment newSegment;
75 newSegment.fX = x;
76 newSegment.fY = y + height;
77 newSegment.fWidth = width;
78 fSkyline.insert(skylineIndex, 1, &newSegment);
79
80 SkASSERT(newSegment.fX + newSegment.fWidth <= this->width());
81 SkASSERT(newSegment.fY <= this->height());
82
83 // delete width of this skyline segment from following ones
84 for (int i = skylineIndex+1; i < fSkyline.count(); ++i) {
85 SkASSERT(fSkyline[i-1].fX <= fSkyline[i].fX);
86
87 if (fSkyline[i].fX < fSkyline[i-1].fX + fSkyline[i-1].fWidth) {
88 int shrink = fSkyline[i-1].fX + fSkyline[i-1].fWidth - fSkyline[i].fX;
89
90 fSkyline[i].fX += shrink;
91 fSkyline[i].fWidth -= shrink;
92
93 if (fSkyline[i].fWidth <= 0) {
94 fSkyline.remove(i);
95 --i;
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +000096 } else {
commit-bot@chromium.org09846a02013-10-02 17:37:59 +000097 break;
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +000098 }
99 } else {
commit-bot@chromium.org09846a02013-10-02 17:37:59 +0000100 break;
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +0000101 }
commit-bot@chromium.org09846a02013-10-02 17:37:59 +0000102 }
103
104 // merge fSkylines
105 for (int i = 0; i < fSkyline.count()-1; ++i) {
106 if (fSkyline[i].fY == fSkyline[i+1].fY) {
107 fSkyline[i].fWidth += fSkyline[i+1].fWidth;
108 fSkyline.remove(i+1);
109 --i;
110 }
111 }
112}
113
114///////////////////////////////////////////////////////////////////////////////
115
116GrRectanizer* GrRectanizer::Factory(int width, int height) {
117 return SkNEW_ARGS(GrRectanizerSkyline, (width, height));
118}