3 * Copyright 2010 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 #ifndef GrAtlas_DEFINED
10 #define GrAtlas_DEFINED
13 #include "GrTexture.h"
14 #include "GrDrawTarget.h"
16 #include "SkTInternalLList.h"
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 GrAtlas::addToAtlas().
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 // GrAtlas::removeUnusedPlots() will free up any finished plots for a given GrAtlas.
34 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrPlot);
36 // This returns a plot ID unique to each plot in a given GrAtlas. They are
37 // consecutive and start at 0.
38 int id() const { return fID; }
40 GrTexture* texture() const { return fTexture; }
42 bool addSubImage(int width, int height, const void*, SkIPoint16*);
44 GrDrawTarget::DrawToken drawToken() const { return fDrawToken; }
45 void setDrawToken(GrDrawTarget::DrawToken draw) { fDrawToken = draw; }
47 void uploadToTexture();
53 ~GrPlot(); // does not try to delete the fNext field
54 void init(GrAtlas* atlas, int id, int offX, int offY, int width, int height, size_t bpp,
58 GrDrawTarget::DrawToken fDrawToken;
61 unsigned char* fPlotData;
65 SkIPoint16 fOffset; // the offset of the plot in the backing texture
66 size_t fBytesPerPixel;
74 typedef SkTInternalLList<GrPlot> GrPlotList;
78 // This class allows each client to independently track the GrPlots in
79 // which its data is stored.
80 class ClientPlotUsage {
82 bool isEmpty() const { return 0 == fPlots.count(); }
85 bool contains(const GrPlot* plot) const {
86 return fPlots.contains(const_cast<GrPlot*>(plot));
91 SkTDArray<GrPlot*> fPlots;
96 GrAtlas(GrGpu*, GrPixelConfig, GrTextureFlags flags,
97 const SkISize& backingTextureSize,
98 int numPlotsX, int numPlotsY, bool batchUploads);
101 // Adds a width x height subimage to the atlas. Upon success it returns
102 // the containing GrPlot and absolute location in the backing texture.
103 // NULL is returned if the subimage cannot fit in the atlas.
104 // If provided, the image data will either be immediately uploaded or
105 // written to the CPU-side backing bitmap.
106 GrPlot* addToAtlas(ClientPlotUsage*, int width, int height, const void* image, SkIPoint16* loc);
108 // remove reference to this plot
109 static void RemovePlot(ClientPlotUsage* usage, const GrPlot* plot);
111 // get a plot that's not being used by the current draw
112 // this allows us to overwrite this plot without flushing
113 GrPlot* getUnusedPlot();
115 GrTexture* getTexture() const {
119 void uploadPlotsToTexture();
126 typedef GrPlotList::Iter PlotIter;
127 GrPlot* iterInit(PlotIter* iter, IterOrder order) {
128 return iter->init(fPlotList, kLRUFirst_IterOrder == order
129 ? GrPlotList::Iter::kTail_IterStart
130 : GrPlotList::Iter::kHead_IterStart);
134 void makeMRU(GrPlot* plot);
137 GrPixelConfig fPixelConfig;
138 GrTextureFlags fFlags;
140 SkISize fBackingTextureSize;
145 // allocated array of GrPlots
147 // LRU list of GrPlots (MRU at head - LRU at tail)
148 GrPlotList fPlotList;