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 | |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 13 | #include "SkTDArray.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 14 | #include "GrTexture.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 | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 44 | void resetRects(); |
| 45 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 46 | private: |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 47 | GrPlot(); |
| 48 | ~GrPlot(); // does not try to delete the fNext field |
robertphillips | 17dabfc | 2014-07-16 13:26:24 -0700 | [diff] [blame] | 49 | 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] | 50 | bool batchUploads); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 51 | |
robertphillips | 17dabfc | 2014-07-16 13:26:24 -0700 | [diff] [blame] | 52 | int fID; |
commit-bot@chromium.org | 7801faa | 2014-05-14 15:14:51 +0000 | [diff] [blame] | 53 | unsigned char* fPlotData; |
commit-bot@chromium.org | a8916ff | 2013-08-16 15:53:46 +0000 | [diff] [blame] | 54 | GrTexture* fTexture; |
| 55 | GrRectanizer* fRects; |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 56 | GrAtlas* fAtlas; |
robertphillips | d537341 | 2014-06-02 10:20:14 -0700 | [diff] [blame] | 57 | SkIPoint16 fOffset; // the offset of the plot in the backing texture |
robertphillips@google.com | 8b16931 | 2013-10-15 17:47:36 +0000 | [diff] [blame] | 58 | size_t fBytesPerPixel; |
commit-bot@chromium.org | 7801faa | 2014-05-14 15:14:51 +0000 | [diff] [blame] | 59 | SkIRect fDirtyRect; |
| 60 | bool fDirty; |
| 61 | bool fBatchUploads; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 62 | |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 63 | friend class GrAtlas; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 66 | typedef SkTInternalLList<GrPlot> GrPlotList; |
| 67 | |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 68 | class GrAtlas { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 69 | public: |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 70 | // This class allows each client to independently track the GrPlots in |
| 71 | // which its data is stored. |
| 72 | class ClientPlotUsage { |
| 73 | public: |
| 74 | bool isEmpty() const { return 0 == fPlots.count(); } |
| 75 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 76 | #ifdef SK_DEBUG |
| 77 | bool contains(const GrPlot* plot) const { |
| 78 | return fPlots.contains(const_cast<GrPlot*>(plot)); |
| 79 | } |
| 80 | #endif |
| 81 | |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 82 | private: |
| 83 | SkTDArray<GrPlot*> fPlots; |
| 84 | |
| 85 | friend class GrAtlas; |
| 86 | }; |
| 87 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 88 | GrAtlas(GrGpu*, GrPixelConfig, GrSurfaceFlags flags, |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 89 | const SkISize& backingTextureSize, |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 90 | int numPlotsX, int numPlotsY, bool batchUploads); |
| 91 | ~GrAtlas(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 92 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 93 | // Adds a width x height subimage to the atlas. Upon success it returns |
| 94 | // the containing GrPlot and absolute location in the backing texture. |
| 95 | // NULL is returned if the subimage cannot fit in the atlas. |
| 96 | // If provided, the image data will either be immediately uploaded or |
| 97 | // written to the CPU-side backing bitmap. |
| 98 | 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] | 99 | |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 100 | // remove reference to this plot |
robertphillips | c4f30b1 | 2014-07-13 10:09:42 -0700 | [diff] [blame] | 101 | static void RemovePlot(ClientPlotUsage* usage, const GrPlot* plot); |
skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 102 | |
commit-bot@chromium.org | 3fddf0e | 2013-09-26 12:57:19 +0000 | [diff] [blame] | 103 | GrTexture* getTexture() const { |
| 104 | return fTexture; |
reed@google.com | 759c16e | 2011-03-15 19:15:15 +0000 | [diff] [blame] | 105 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 106 | |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 107 | enum IterOrder { |
| 108 | kLRUFirst_IterOrder, |
| 109 | kMRUFirst_IterOrder |
| 110 | }; |
| 111 | |
| 112 | typedef GrPlotList::Iter PlotIter; |
| 113 | GrPlot* iterInit(PlotIter* iter, IterOrder order) { |
joshualitt | 8debd89 | 2015-05-19 15:05:24 -0700 | [diff] [blame] | 114 | return iter->init(fPlotList, kLRUFirst_IterOrder == order |
robertphillips | 320c923 | 2014-07-29 06:07:19 -0700 | [diff] [blame] | 115 | ? GrPlotList::Iter::kTail_IterStart |
| 116 | : GrPlotList::Iter::kHead_IterStart); |
| 117 | } |
| 118 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 119 | private: |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 120 | void makeMRU(GrPlot* plot); |
skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 121 | |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 122 | GrGpu* fGpu; |
| 123 | GrPixelConfig fPixelConfig; |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 124 | GrSurfaceFlags fFlags; |
robertphillips | 952841b | 2014-06-30 08:26:50 -0700 | [diff] [blame] | 125 | GrTexture* fTexture; |
| 126 | SkISize fBackingTextureSize; |
| 127 | int fNumPlotsX; |
| 128 | int fNumPlotsY; |
| 129 | bool fBatchUploads; |
skia.committer@gmail.com | 50df4d0 | 2013-09-28 07:01:33 +0000 | [diff] [blame] | 130 | |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 131 | // allocated array of GrPlots |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 132 | GrPlot* fPlotArray; |
robertphillips | 1d86ee8 | 2014-06-24 15:08:49 -0700 | [diff] [blame] | 133 | // LRU list of GrPlots (MRU at head - LRU at tail) |
commit-bot@chromium.org | c9b2c88 | 2014-03-03 14:30:25 +0000 | [diff] [blame] | 134 | GrPlotList fPlotList; |
commit-bot@chromium.org | 7d330eb | 2013-09-27 19:39:38 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 137 | #endif |