|
1 |
|
2 /* |
|
3 * 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. |
|
7 */ |
|
8 |
|
9 #ifndef GrAtlas_DEFINED |
|
10 #define GrAtlas_DEFINED |
|
11 |
|
12 |
|
13 #include "GrPoint.h" |
|
14 #include "GrTexture.h" |
|
15 #include "GrDrawTarget.h" |
|
16 |
|
17 class GrGpu; |
|
18 class GrRectanizer; |
|
19 class GrAtlasMgr; |
|
20 class GrAtlas; |
|
21 |
|
22 // The backing GrTexture for a set of GrAtlases is broken into a spatial grid of GrPlots. When |
|
23 // a GrAtlas needs space on the texture, it requests a GrPlot. Each GrAtlas can claim one |
|
24 // or more GrPlots. The GrPlots keep track of subimage placement via their GrRectanizer. Once a |
|
25 // GrPlot is "full" (i.e. there is no room for the new subimage according to the GrRectanizer), the |
|
26 // GrAtlas can request a new GrPlot via GrAtlasMgr::addToAtlas(). |
|
27 // |
|
28 // If all GrPlots are allocated, the replacement strategy is up to the client. The drawToken is |
|
29 // available to ensure that all draw calls are finished for that particular GrPlot. |
|
30 // GrAtlasMgr::removeUnusedPlots() will free up any finished plots for a given GrAtlas. |
|
31 |
|
32 class GrPlot { |
|
33 public: |
|
34 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrPlot); |
|
35 |
|
36 int getOffsetX() const { return fOffset.fX; } |
|
37 int getOffsetY() const { return fOffset.fY; } |
|
38 |
|
39 GrTexture* texture() const { return fTexture; } |
|
40 |
|
41 bool addSubImage(int width, int height, const void*, GrIPoint16*); |
|
42 |
|
43 GrDrawTarget::DrawToken drawToken() const { return fDrawToken; } |
|
44 void setDrawToken(GrDrawTarget::DrawToken draw) { fDrawToken = draw; } |
|
45 |
|
46 void resetRects(); |
|
47 |
|
48 private: |
|
49 GrPlot(); |
|
50 ~GrPlot(); // does not try to delete the fNext field |
|
51 |
|
52 // for recycling |
|
53 GrDrawTarget::DrawToken fDrawToken; |
|
54 |
|
55 GrTexture* fTexture; |
|
56 GrRectanizer* fRects; |
|
57 GrAtlasMgr* fAtlasMgr; |
|
58 GrIPoint16 fOffset; |
|
59 size_t fBytesPerPixel; |
|
60 |
|
61 friend class GrAtlasMgr; |
|
62 }; |
|
63 |
|
64 typedef SkTInternalLList<GrPlot> GrPlotList; |
|
65 |
|
66 class GrAtlasMgr { |
|
67 public: |
|
68 GrAtlasMgr(GrGpu*, GrPixelConfig); |
|
69 ~GrAtlasMgr(); |
|
70 |
|
71 // add subimage of width, height dimensions to atlas |
|
72 // returns the containing GrPlot and location relative to the backing texture |
|
73 GrPlot* addToAtlas(GrAtlas*, int width, int height, const void*, GrIPoint16*); |
|
74 |
|
75 // remove reference to this plot |
|
76 bool removePlot(GrAtlas* atlas, const GrPlot* plot); |
|
77 |
|
78 // get a plot that's not being used by the current draw |
|
79 // this allows us to overwrite this plot without flushing |
|
80 GrPlot* getUnusedPlot(); |
|
81 |
|
82 GrTexture* getTexture() const { |
|
83 return fTexture; |
|
84 } |
|
85 |
|
86 private: |
|
87 void moveToHead(GrPlot* plot); |
|
88 |
|
89 GrGpu* fGpu; |
|
90 GrPixelConfig fPixelConfig; |
|
91 GrTexture* fTexture; |
|
92 |
|
93 // allocated array of GrPlots |
|
94 GrPlot* fPlotArray; |
|
95 // LRU list of GrPlots |
|
96 GrPlotList fPlotList; |
|
97 }; |
|
98 |
|
99 class GrAtlas { |
|
100 public: |
|
101 GrAtlas() { } |
|
102 ~GrAtlas() { } |
|
103 |
|
104 bool isEmpty() { return 0 == fPlots.count(); } |
|
105 |
|
106 SkISize getSize() const; |
|
107 |
|
108 private: |
|
109 SkTDArray<GrPlot*> fPlots; |
|
110 |
|
111 friend class GrAtlasMgr; |
|
112 }; |
|
113 |
|
114 #endif |