blob: 50538c2d7edb66b34cae7ac1cc62536cc8307108 [file] [log] [blame]
Chris Daltona550cf22020-02-07 13:35:31 -07001/*
2 * Copyright 2020 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 GrDynamicAtlas_DEFINED
9#define GrDynamicAtlas_DEFINED
10
Chris Daltond2dc8dd2020-05-19 16:32:02 -060011#include "src/core/SkArenaAlloc.h"
Chris Daltona550cf22020-02-07 13:35:31 -070012#include "src/gpu/GrTextureProxy.h"
13
14class GrOnFlushResourceProvider;
Chris Daltona550cf22020-02-07 13:35:31 -070015class GrResourceProvider;
16struct SkIPoint16;
17struct SkIRect;
18
19/**
20 * This class implements a dynamic size GrRectanizer that grows until it reaches the implementation-
21 * dependent max texture size. When finalized, it also creates and stores a GrTextureProxy for the
22 * underlying atlas.
23 */
24class GrDynamicAtlas {
25public:
26 // As long as GrSurfaceOrigin exists, we just have to decide on one for the atlas texture.
27 static constexpr GrSurfaceOrigin kTextureOrigin = kTopLeft_GrSurfaceOrigin;
28 static constexpr int kPadding = 1; // Amount of padding below and to the right of each path.
29
Brian Salomon63410e92020-03-23 18:32:50 -040030 using LazyAtlasDesc = GrSurfaceProxy::LazySurfaceDesc;
31 using LazyInstantiateAtlasCallback = GrSurfaceProxy::LazyInstantiateCallback;
Chris Daltona550cf22020-02-07 13:35:31 -070032
33 enum class InternalMultisample : bool {
34 kNo = false,
35 kYes = true
36 };
37
Brian Salomon63410e92020-03-23 18:32:50 -040038 static sk_sp<GrTextureProxy> MakeLazyAtlasProxy(LazyInstantiateAtlasCallback&&,
39 GrColorType colorType,
40 InternalMultisample,
41 const GrCaps&,
42 GrSurfaceProxy::UseAllocator);
Chris Daltona550cf22020-02-07 13:35:31 -070043
Chris Daltond2dc8dd2020-05-19 16:32:02 -060044 enum class RectanizerAlgorithm {
45 kSkyline,
46 kPow2
47 };
48
Chris Daltona550cf22020-02-07 13:35:31 -070049 GrDynamicAtlas(GrColorType colorType, InternalMultisample, SkISize initialSize,
Chris Daltond2dc8dd2020-05-19 16:32:02 -060050 int maxAtlasSize, const GrCaps&,
51 RectanizerAlgorithm = RectanizerAlgorithm::kSkyline);
Chris Daltona550cf22020-02-07 13:35:31 -070052 virtual ~GrDynamicAtlas();
53
Brian Salomon63410e92020-03-23 18:32:50 -040054 void reset(SkISize initialSize, const GrCaps& desc);
Chris Daltona550cf22020-02-07 13:35:31 -070055
Chris Dalton83420eb2021-06-23 18:47:09 -060056 GrColorType colorType() const { return fColorType; }
Chris Daltonb96995d2020-06-04 16:44:29 -060057 int maxAtlasSize() const { return fMaxAtlasSize; }
Chris Daltona550cf22020-02-07 13:35:31 -070058 GrTextureProxy* textureProxy() const { return fTextureProxy.get(); }
Chris Dalton83420eb2021-06-23 18:47:09 -060059 GrSurfaceProxyView readView(const GrCaps&) const;
60 GrSurfaceProxyView writeView(const GrCaps&) const;
Chris Daltona550cf22020-02-07 13:35:31 -070061 bool isInstantiated() const { return fTextureProxy->isInstantiated(); }
Chris Daltona550cf22020-02-07 13:35:31 -070062
Chris Daltond2dc8dd2020-05-19 16:32:02 -060063 // Attempts to add a rect to the atlas. Returns true if successful, along with the rect's
64 // top-left location in the atlas.
65 bool addRect(int width, int height, SkIPoint16* location);
Chris Daltona550cf22020-02-07 13:35:31 -070066 const SkISize& drawBounds() { return fDrawBounds; }
67
Chris Dalton83420eb2021-06-23 18:47:09 -060068 // Instantiates our texture proxy for the atlas. After this call, it is no longer valid to call
Chris Daltona550cf22020-02-07 13:35:31 -070069 // addRect(), setUserBatchID(), or this method again.
70 //
71 // 'backingTexture', if provided, is a renderable texture with which to instantiate our proxy.
72 // If null then we will create a texture using the resource provider. The purpose of this param
Chris Dalton83420eb2021-06-23 18:47:09 -060073 // is to provide a guaranteed way to recycle textures from previous atlases.
74 void instantiate(GrOnFlushResourceProvider*, sk_sp<GrTexture> backingTexture = nullptr);
Chris Daltona550cf22020-02-07 13:35:31 -070075
76private:
77 class Node;
78
Chris Daltond2dc8dd2020-05-19 16:32:02 -060079 Node* makeNode(Node* previous, int l, int t, int r, int b);
Chris Daltona550cf22020-02-07 13:35:31 -070080 bool internalPlaceRect(int w, int h, SkIPoint16* loc);
81
82 const GrColorType fColorType;
83 const InternalMultisample fInternalMultisample;
84 const int fMaxAtlasSize;
Chris Daltond2dc8dd2020-05-19 16:32:02 -060085 const RectanizerAlgorithm fRectanizerAlgorithm;
Chris Daltona550cf22020-02-07 13:35:31 -070086 int fWidth;
87 int fHeight;
Chris Daltona550cf22020-02-07 13:35:31 -070088 SkISize fDrawBounds;
89
Herb Derby6e2c56f2020-08-01 16:26:04 -040090 SkSTArenaAllocWithReset<512> fNodeAllocator;
Chris Daltond2dc8dd2020-05-19 16:32:02 -060091 Node* fTopNode = nullptr;
92
Chris Daltona550cf22020-02-07 13:35:31 -070093 sk_sp<GrTextureProxy> fTextureProxy;
94 sk_sp<GrTexture> fBackingTexture;
95};
96
97#endif