epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 9 | #ifndef GrAtlas_DEFINED |
| 10 | #define GrAtlas_DEFINED |
| 11 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 12 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 13 | #include "GrTexture.h" |
commit-bot@chromium.org | a8916ff | 2013-08-16 15:53:46 +0000 | [diff] [blame] | 14 | #include "GrDrawTarget.h" |
bsalomon | c44be0e | 2014-07-25 07:32:33 -0700 | [diff] [blame] | 15 | #include "SkPoint.h" |
| 16 | #include "SkTInternalLList.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 17 | |
| 18 | class GrGpu; |
| 19 | class GrRectanizer; |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 20 | class GrAtlas; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 21 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 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 |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 26 | // GrAtlas can request a new GrPlot via GrAtlas::addToAtlas(). |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 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. |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 30 | // GrAtlas::removeUnusedPlots() will free up any finished plots for a given GrAtlas. |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 31 | |
| 32 | class GrPlot { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 33 | public: |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 34 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrPlot); |
skia.committer@gmail.com | ade9a34 | 2014-03-04 03:02:32 +0000 | [diff] [blame] | 35 | |
robertphillips | 17dabfc | 2014-07-16 13:26:24 -0700 | [diff] [blame] | 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; } |
| 39 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 40 | GrTexture* texture() const { return fTexture; } |
| 41 | |
robertphillips | d537341 | 2014-06-02 10:20:14 -0700 | [diff] [blame] | 42 | bool addSubImage(int width, int height, const void*, SkIPoint16*); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 43 | |
commit-bot@chromium.org | a8916ff | 2013-08-16 15:53:46 +0000 | [diff] [blame] | 44 | GrDrawTarget::DrawToken drawToken() const { return fDrawToken; } |
| 45 | void setDrawToken(GrDrawTarget::DrawToken draw) { fDrawToken = draw; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 46 | |
commit-bot@chromium.org | 7801faa | 2014-05-14 15:14:51 +0000 | [diff] [blame] | 47 | void uploadToTexture(); |
| 48 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 49 | void resetRects(); |
| 50 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 51 | private: |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 52 | GrPlot(); |
| 53 | ~GrPlot(); // does not try to delete the fNext field |
robertphillips | 17dabfc | 2014-07-16 13:26:24 -0700 | [diff] [blame] | 54 | void init(GrAtlas* atlas, int id, int offX, int offY, int width, int height, size_t bpp, |
commit-bot@chromium.org | 7801faa | 2014-05-14 15:14:51 +0000 | [diff] [blame] | 55 | bool batchUploads); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 56 | |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 57 | // for recycling |
commit-bot@chromium.org | a8916ff | 2013-08-16 15:53:46 +0000 | [diff] [blame] | 58 | GrDrawTarget::DrawToken fDrawToken; |
commit-bot@chromium.org | 67ed64e | 2013-08-05 19:42:56 +0000 | [diff] [blame] | 59 | |
robertphillips | 17dabfc | 2014-07-16 13:26:24 -0700 | [diff] [blame] | 60 | int fID; |
commit-bot@chromium.org | 7801faa | 2014-05-14 15:14:51 +0000 | [diff] [blame] | 61 | unsigned char* fPlotData; |
commit-bot@chromium.org | a8916ff | 2013-08-16 15:53:46 +0000 | [diff] [blame] | 62 | GrTexture* fTexture; |
| 63 | GrRectanizer* fRects; |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 64 | GrAtlas* fAtlas; |
robertphillips | d537341 | 2014-06-02 10:20:14 -0700 | [diff] [blame] | 65 | SkIPoint16 fOffset; // the offset of the plot in the backing texture |
robertphillips@google.com | 8b16931 | 2013-10-15 17:47:36 +0000 | [diff] [blame] | 66 | size_t fBytesPerPixel; |
commit-bot@chromium.org | 7801faa | 2014-05-14 15:14:51 +0000 | [diff] [blame] | 67 | SkIRect fDirtyRect; |
| 68 | bool fDirty; |
| 69 | bool fBatchUploads; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 70 | |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 71 | friend class GrAtlas; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 74 | typedef SkTInternalLList<GrPlot> GrPlotList; |
| 75 | |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 76 | class GrAtlas { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 77 | public: |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 78 | // This class allows each client to independently track the GrPlots in |
| 79 | // which its data is stored. |
| 80 | class ClientPlotUsage { |
| 81 | public: |
| 82 | bool isEmpty() const { return 0 == fPlots.count(); } |
| 83 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 84 | #ifdef SK_DEBUG |
| 85 | bool contains(const GrPlot* plot) const { |
| 86 | return fPlots.contains(const_cast<GrPlot*>(plot)); |
| 87 | } |
| 88 | #endif |
| 89 | |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 90 | private: |
| 91 | SkTDArray<GrPlot*> fPlots; |
| 92 | |
| 93 | friend class GrAtlas; |
| 94 | }; |
| 95 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 96 | GrAtlas(GrGpu*, GrPixelConfig, GrTextureFlags flags, |
| 97 | const SkISize& backingTextureSize, |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 98 | int numPlotsX, int numPlotsY, bool batchUploads); |
| 99 | ~GrAtlas(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 100 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 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); |
skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 107 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 108 | // remove reference to this plot |
robertphillips | c4f30b1 | 2014-07-13 10:09:42 -0700 | [diff] [blame] | 109 | static void RemovePlot(ClientPlotUsage* usage, const GrPlot* plot); |
skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 110 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 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(); |
skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 114 | |
commit-bot@chromium.org | 3fddf0e | 2013-09-26 12:57:19 +0000 | [diff] [blame] | 115 | GrTexture* getTexture() const { |
| 116 | return fTexture; |
reed@google.com | 759c16e | 2011-03-15 19:15:15 +0000 | [diff] [blame] | 117 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 118 | |
commit-bot@chromium.org | 7801faa | 2014-05-14 15:14:51 +0000 | [diff] [blame] | 119 | void uploadPlotsToTexture(); |
| 120 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 121 | enum IterOrder { |
| 122 | kLRUFirst_IterOrder, |
| 123 | kMRUFirst_IterOrder |
| 124 | }; |
| 125 | |
| 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); |
| 131 | } |
| 132 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 133 | private: |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 134 | void makeMRU(GrPlot* plot); |
skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 135 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 136 | GrGpu* fGpu; |
| 137 | GrPixelConfig fPixelConfig; |
| 138 | GrTextureFlags fFlags; |
| 139 | GrTexture* fTexture; |
| 140 | SkISize fBackingTextureSize; |
| 141 | int fNumPlotsX; |
| 142 | int fNumPlotsY; |
| 143 | bool fBatchUploads; |
skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 144 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 145 | // allocated array of GrPlots |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 146 | GrPlot* fPlotArray; |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 147 | // LRU list of GrPlots (MRU at head - LRU at tail) |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 148 | GrPlotList fPlotList; |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 151 | #endif |