blob: 36af34db61871fca35ad6feee3e8cd1588c4848c [file] [log] [blame]
commit-bot@chromium.orgad854bf2014-05-29 18:46:38 +00001/*
robertphillips901e96d2014-06-02 07:15:18 -07002 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
commit-bot@chromium.orgad854bf2014-05-29 18:46:38 +00007
Herb Derby73c75872020-01-22 18:09:16 -05008#ifndef GrRectanizerSkyline_DEFINED
9#define GrRectanizerSkyline_DEFINED
commit-bot@chromium.orgad854bf2014-05-29 18:46:38 +000010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/SkTDArray.h"
Herb Derby73c75872020-01-22 18:09:16 -050012#include "src/core/SkIPoint16.h"
commit-bot@chromium.orgad854bf2014-05-29 18:46:38 +000013
14// Pack rectangles and track the current silhouette
robertphillips901e96d2014-06-02 07:15:18 -070015// Based, in part, on Jukka Jylanki's work at http://clb.demon.fi
Herb Derby73c75872020-01-22 18:09:16 -050016class GrRectanizerSkyline {
commit-bot@chromium.orgad854bf2014-05-29 18:46:38 +000017public:
Herb Derby73c75872020-01-22 18:09:16 -050018 GrRectanizerSkyline(int w, int h) : fWidth{w}, fHeight{h} {
commit-bot@chromium.orgad854bf2014-05-29 18:46:38 +000019 this->reset();
20 }
21
Herb Derby73c75872020-01-22 18:09:16 -050022 void reset() {
commit-bot@chromium.orgad854bf2014-05-29 18:46:38 +000023 fAreaSoFar = 0;
24 fSkyline.reset();
25 SkylineSegment* seg = fSkyline.append(1);
26 seg->fX = 0;
27 seg->fY = 0;
28 seg->fWidth = this->width();
29 }
30
Herb Derby73c75872020-01-22 18:09:16 -050031 bool addRect(int w, int h, SkIPoint16* loc);
commit-bot@chromium.orgad854bf2014-05-29 18:46:38 +000032
Herb Derby73c75872020-01-22 18:09:16 -050033 int width() const { return fWidth; }
34 int height() const { return fHeight; }
commit-bot@chromium.orgad854bf2014-05-29 18:46:38 +000035
36private:
37 struct SkylineSegment {
38 int fX;
39 int fY;
40 int fWidth;
41 };
42
robertphillips4cbf8e32014-06-09 07:59:25 -070043 // Can a width x height rectangle fit in the free space represented by
44 // the skyline segments >= 'skylineIndex'? If so, return true and fill in
halcanary9d524f22016-03-29 09:03:52 -070045 // 'y' with the y-location at which it fits (the x location is pulled from
robertphillips4cbf8e32014-06-09 07:59:25 -070046 // 'skylineIndex's segment.
commit-bot@chromium.orgad854bf2014-05-29 18:46:38 +000047 bool rectangleFits(int skylineIndex, int width, int height, int* y) const;
robertphillips4cbf8e32014-06-09 07:59:25 -070048 // Update the skyline structure to include a width x height rect located
49 // at x,y.
commit-bot@chromium.orgad854bf2014-05-29 18:46:38 +000050 void addSkylineLevel(int skylineIndex, int x, int y, int width, int height);
51
Chris Daltona550cf22020-02-07 13:35:31 -070052 const int fWidth;
53 const int fHeight;
Herb Derby73c75872020-01-22 18:09:16 -050054 SkTDArray<SkylineSegment> fSkyline;
55 int32_t fAreaSoFar;
commit-bot@chromium.orgad854bf2014-05-29 18:46:38 +000056};
57
Herb Derby73c75872020-01-22 18:09:16 -050058#endif // GrRectanizerSkyline_DEFINED