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: getOffsetX()34 int getOffsetX() const { return fOffset.fX; } getOffsetY()35 int getOffsetY() const { return fOffset.fY; } 36 texture()37 GrTexture* texture() const { return fTexture; } 38 39 bool addSubImage(int width, int height, const void*, GrIPoint16*); 40 drawToken()41 GrDrawTarget::DrawToken drawToken() const { return fDrawToken; } setDrawToken(GrDrawTarget::DrawToken draw)42 void setDrawToken(GrDrawTarget::DrawToken draw) { fDrawToken = draw; } 43 44 private: 45 GrPlot(); 46 ~GrPlot(); // does not try to delete the fNext field 47 48 // for recycling 49 GrDrawTarget::DrawToken fDrawToken; 50 51 GrPlot* fNext; 52 53 GrTexture* fTexture; 54 GrRectanizer* fRects; 55 GrAtlasMgr* fAtlasMgr; 56 GrIPoint16 fOffset; 57 size_t fBytesPerPixel; 58 59 friend class GrAtlasMgr; 60 }; 61 62 class GrAtlasMgr { 63 public: 64 GrAtlasMgr(GrGpu*, GrPixelConfig); 65 ~GrAtlasMgr(); 66 67 // add subimage of width, height dimensions to atlas 68 // returns the containing GrPlot and location relative to the backing texture 69 GrPlot* addToAtlas(GrAtlas*, int width, int height, const void*, GrIPoint16*); 70 71 // free up any plots that are not waiting on a draw call 72 bool removeUnusedPlots(GrAtlas* atlas); 73 74 // to be called by ~GrAtlas() 75 void deletePlotList(GrPlot* plot); 76 getTexture()77 GrTexture* getTexture() const { 78 return fTexture; 79 } 80 81 private: 82 GrPlot* allocPlot(); 83 void freePlot(GrPlot* plot); 84 85 GrGpu* fGpu; 86 GrPixelConfig fPixelConfig; 87 GrTexture* fTexture; 88 89 // allocated array of GrPlots 90 GrPlot* fPlots; 91 // linked list of free GrPlots 92 GrPlot* fFreePlots; 93 }; 94 95 class GrAtlas { 96 public: GrAtlas(GrAtlasMgr * mgr)97 GrAtlas(GrAtlasMgr* mgr) : fPlots(NULL), fAtlasMgr(mgr) { } ~GrAtlas()98 ~GrAtlas() { fAtlasMgr->deletePlotList(fPlots); } 99 isEmpty()100 bool isEmpty() { return NULL == fPlots; } 101 102 private: 103 GrPlot* fPlots; 104 GrAtlasMgr* fAtlasMgr; 105 106 friend class GrAtlasMgr; 107 }; 108 109 #endif 110