1/*
2 * 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 */
7
8#ifndef GrRectanizerSkyline_DEFINED
9#define GrRectanizerSkyline_DEFINED
10
11#include "include/private/SkTDArray.h"
12#include "src/gpu/GrRectanizer.h"
13
14// Pack rectangles and track the current silhouette
15// Based, in part, on Jukka Jylanki's work at http://clb.demon.fi
16//
17// Mark this class final in an effort to avoid the vtable when this subclass is used explicitly.
18class GrRectanizerSkyline final : public GrRectanizer {
19public:
20 GrRectanizerSkyline(int w, int h) : INHERITED(w, h) {
21 this->reset();
22 }
23
24 ~GrRectanizerSkyline() final { }
25
26 void reset() final {
27 fAreaSoFar = 0;
28 fSkyline.reset();
29 SkylineSegment* seg = fSkyline.append(1);
30 seg->fX = 0;
31 seg->fY = 0;
32 seg->fWidth = this->width();
33 }
34
35 bool addRect(int w, int h, SkIPoint16* loc) final;
36
37 float percentFull() const final {
38 return fAreaSoFar / ((float)this->width() * this->height());
39 }
40
41private:
42 struct SkylineSegment {
43 int fX;
44 int fY;
45 int fWidth;
46 };
47
48 SkTDArray<SkylineSegment> fSkyline;
49
50 int32_t fAreaSoFar;
51
52 // Can a width x height rectangle fit in the free space represented by
53 // the skyline segments >= 'skylineIndex'? If so, return true and fill in
54 // 'y' with the y-location at which it fits (the x location is pulled from
55 // 'skylineIndex's segment.
56 bool rectangleFits(int skylineIndex, int width, int height, int* y) const;
57 // Update the skyline structure to include a width x height rect located
58 // at x,y.
59 void addSkylineLevel(int skylineIndex, int x, int y, int width, int height);
60
61 typedef GrRectanizer INHERITED;
62};
63
64#endif
65