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