blob: 9f0d846e6c986d73fae36b765d51c7a4f8803ad6 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#include "GrRectanizer.h"
12#include "GrTBSearch.h"
13
14#define MIN_HEIGHT_POW2 2
15
16class GrRectanizerPow2 : public GrRectanizer {
17public:
18 GrRectanizerPow2(int w, int h) : GrRectanizer(w, h) {
19 fNextStripY = 0;
20 fAreaSoFar = 0;
reed@google.com939ca7c2013-09-26 19:56:51 +000021 sk_bzero(fRows, sizeof(fRows));
reed@google.comac10a2d2010-12-22 21:39:39 +000022 }
23
24 virtual ~GrRectanizerPow2() {
25 }
26
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000027 virtual void reset() {
28 fNextStripY = 0;
29 fAreaSoFar = 0;
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +000030 sk_bzero(fRows, sizeof(fRows));
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000031 }
32
reed@google.comac10a2d2010-12-22 21:39:39 +000033 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.comd6176b02012-08-23 18:14:13 +000047
reed@google.comac10a2d2010-12-22 21:39:39 +000048 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.orgf6de4752013-08-17 00:02:59 +000056 SkASSERT(height >= MIN_HEIGHT_POW2);
bsalomon@google.comb9086a02012-11-01 18:02:54 +000057 return 32 - SkCLZ(height - 1);
reed@google.comac10a2d2010-12-22 21:39:39 +000058 }
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
74bool 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.orgf6de4752013-08-17 00:02:59 +000093 SkASSERT(row->fRowHeight == 0 || row->fRowHeight == height);
reed@google.comac10a2d2010-12-22 21:39:39 +000094
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.orgf6de4752013-08-17 00:02:59 +0000111 SkASSERT(row->fRowHeight == height);
112 SkASSERT(row->canAddWidth(width, this->width()));
reed@google.comac10a2d2010-12-22 21:39:39 +0000113 *loc = row->fLoc;
114 row->fLoc.fX += width;
115
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000116 SkASSERT(row->fLoc.fX <= this->width());
117 SkASSERT(row->fLoc.fY <= this->height());
118 SkASSERT(fNextStripY <= this->height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000119 fAreaSoFar += area;
120 return true;
121}
122
123///////////////////////////////////////////////////////////////////////////////
124
commit-bot@chromium.org09846a02013-10-02 17:37:59 +0000125// factory is now in GrRectanizer_skyline.cpp
126//GrRectanizer* GrRectanizer::Factory(int width, int height) {
127// return SkNEW_ARGS(GrRectanizerPow2, (width, height));
128//}