blob: b30bdbec6f15ab3e0290130515b9f2cb6b1f9ca1 [file] [log] [blame]
Chris Dalton383a2ef2018-01-08 17:21:41 -05001/*
2 * Copyright 2017 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 GrCCAtlas_DEFINED
9#define GrCCAtlas_DEFINED
10
11#include "SkRefCnt.h"
12#include "SkSize.h"
13
14class GrCaps;
Chris Dalton9ca27842018-01-18 12:24:50 -070015class GrCCPathParser;
Chris Dalton383a2ef2018-01-08 17:21:41 -050016class GrDrawOp;
17class GrOnFlushResourceProvider;
18class GrRenderTargetContext;
19class GrTextureProxy;
20struct SkIPoint16;
21
22/**
23 * This class implements a dynamic size GrRectanizer that grows until it reaches the implementation-
24 * dependent max texture size. When finalized, it also creates and stores a GrTextureProxy for the
25 * underlying atlas.
26 */
27class GrCCAtlas {
28public:
Chris Dalton9ca27842018-01-18 12:24:50 -070029 using CoverageCountBatchID = int;
Chris Dalton42c21152018-06-13 15:28:19 -060030 static constexpr int kPadding = 1; // Amount of padding below and to the right of each path.
Chris Dalton9ca27842018-01-18 12:24:50 -070031
Chris Dalton42c21152018-06-13 15:28:19 -060032 // This struct encapsulates the minimum and desired requirements for an atlas, as well as an
33 // approximate number of pixels to help select a good initial size.
34 struct Specs {
35 int fMaxPreferredTextureSize = 0;
36 int fMinTextureSize = 0;
37 int fMinWidth = 0; // If there are 100 20x10 paths, this should be 20.
38 int fMinHeight = 0; // If there are 100 20x10 paths, this should be 10.
39 int fApproxNumPixels = 0;
40
41 // Add space for a rect in the desired atlas specs.
42 void accountForSpace(int width, int height);
43 };
44
45 GrCCAtlas(const Specs&);
Chris Dalton383a2ef2018-01-08 17:21:41 -050046 ~GrCCAtlas();
47
48 bool addRect(int devWidth, int devHeight, SkIPoint16* loc);
49 const SkISize& drawBounds() { return fDrawBounds; }
50
Chris Dalton9ca27842018-01-18 12:24:50 -070051 void setCoverageCountBatchID(CoverageCountBatchID batchID) {
52 SkASSERT(!fCoverageCountBatchID);
53 SkASSERT(!fTextureProxy);
54 fCoverageCountBatchID = batchID;
55 }
56
57 sk_sp<GrRenderTargetContext> SK_WARN_UNUSED_RESULT finalize(GrOnFlushResourceProvider*,
58 sk_sp<const GrCCPathParser>);
Chris Dalton383a2ef2018-01-08 17:21:41 -050059
60 GrTextureProxy* textureProxy() const { return fTextureProxy.get(); }
61
62private:
63 class Node;
Chris Dalton9ca27842018-01-18 12:24:50 -070064 class DrawCoverageCountOp;
Chris Dalton383a2ef2018-01-08 17:21:41 -050065
66 bool internalPlaceRect(int w, int h, SkIPoint16* loc);
67
Chris Dalton42c21152018-06-13 15:28:19 -060068 const int fMaxTextureSize;
Chris Dalton2612bae2018-02-22 13:41:37 -070069 int fWidth, fHeight;
Chris Dalton383a2ef2018-01-08 17:21:41 -050070 std::unique_ptr<Node> fTopNode;
Chris Dalton2612bae2018-02-22 13:41:37 -070071 SkISize fDrawBounds = {0, 0};
Chris Dalton383a2ef2018-01-08 17:21:41 -050072
Chris Dalton9ca27842018-01-18 12:24:50 -070073 CoverageCountBatchID fCoverageCountBatchID SkDEBUGCODE(= 0);
Chris Dalton383a2ef2018-01-08 17:21:41 -050074 sk_sp<GrTextureProxy> fTextureProxy;
75};
76
Chris Dalton42c21152018-06-13 15:28:19 -060077inline void GrCCAtlas::Specs::accountForSpace(int width, int height) {
78 fMinWidth = SkTMax(width, fMinWidth);
79 fMinHeight = SkTMax(height, fMinHeight);
80 fApproxNumPixels += (width + kPadding) * (height + kPadding);
81}
82
Chris Dalton383a2ef2018-01-08 17:21:41 -050083#endif