blob: 47048a81c7c32030c181720fd4c4f7d95c94d544 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.comac10a2d2010-12-22 21:39:39 +00007 */
8
reed@google.comac10a2d2010-12-22 21:39:39 +00009#ifndef GrAtlas_DEFINED
10#define GrAtlas_DEFINED
11
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000012
robertphillipsd5373412014-06-02 10:20:14 -070013#include "SkPoint.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014#include "GrTexture.h"
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000015#include "GrDrawTarget.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016
17class GrGpu;
18class GrRectanizer;
19class GrAtlasMgr;
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000020class GrAtlas;
reed@google.comac10a2d2010-12-22 21:39:39 +000021
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000022// 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
32class GrPlot {
reed@google.comac10a2d2010-12-22 21:39:39 +000033public:
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000034 SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrPlot);
skia.committer@gmail.comade9a342014-03-04 03:02:32 +000035
reed@google.comac10a2d2010-12-22 21:39:39 +000036 GrTexture* texture() const { return fTexture; }
37
robertphillipsd5373412014-06-02 10:20:14 -070038 bool addSubImage(int width, int height, const void*, SkIPoint16*);
reed@google.comac10a2d2010-12-22 21:39:39 +000039
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000040 GrDrawTarget::DrawToken drawToken() const { return fDrawToken; }
41 void setDrawToken(GrDrawTarget::DrawToken draw) { fDrawToken = draw; }
reed@google.comac10a2d2010-12-22 21:39:39 +000042
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000043 void uploadToTexture();
44
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000045 void resetRects();
46
reed@google.comac10a2d2010-12-22 21:39:39 +000047private:
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000048 GrPlot();
49 ~GrPlot(); // does not try to delete the fNext field
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000050 void init(GrAtlasMgr* mgr, int offX, int offY, int width, int height, size_t bpp,
51 bool batchUploads);
reed@google.comac10a2d2010-12-22 21:39:39 +000052
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000053 // for recycling
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000054 GrDrawTarget::DrawToken fDrawToken;
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000055
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000056 unsigned char* fPlotData;
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000057 GrTexture* fTexture;
58 GrRectanizer* fRects;
59 GrAtlasMgr* fAtlasMgr;
robertphillipsd5373412014-06-02 10:20:14 -070060 SkIPoint16 fOffset; // the offset of the plot in the backing texture
robertphillips@google.com8b169312013-10-15 17:47:36 +000061 size_t fBytesPerPixel;
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000062 SkIRect fDirtyRect;
63 bool fDirty;
64 bool fBatchUploads;
reed@google.comac10a2d2010-12-22 21:39:39 +000065
66 friend class GrAtlasMgr;
67};
68
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000069typedef SkTInternalLList<GrPlot> GrPlotList;
70
reed@google.comac10a2d2010-12-22 21:39:39 +000071class GrAtlasMgr {
72public:
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +000073 GrAtlasMgr(GrGpu*, GrPixelConfig, const SkISize& backingTextureSize,
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000074 int numPlotsX, int numPlotsY, bool batchUploads);
reed@google.comac10a2d2010-12-22 21:39:39 +000075 ~GrAtlasMgr();
76
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000077 // add subimage of width, height dimensions to atlas
78 // returns the containing GrPlot and location relative to the backing texture
robertphillipsd5373412014-06-02 10:20:14 -070079 GrPlot* addToAtlas(GrAtlas*, int width, int height, const void*, SkIPoint16*);
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +000080
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000081 // remove reference to this plot
82 bool removePlot(GrAtlas* atlas, const GrPlot* plot);
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +000083
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000084 // get a plot that's not being used by the current draw
85 // this allows us to overwrite this plot without flushing
86 GrPlot* getUnusedPlot();
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +000087
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +000088 GrTexture* getTexture() const {
89 return fTexture;
reed@google.com759c16e2011-03-15 19:15:15 +000090 }
reed@google.comac10a2d2010-12-22 21:39:39 +000091
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000092 void uploadPlotsToTexture();
93
reed@google.comac10a2d2010-12-22 21:39:39 +000094private:
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000095 void moveToHead(GrPlot* plot);
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +000096
commit-bot@chromium.org95294412013-09-26 15:28:40 +000097 GrGpu* fGpu;
98 GrPixelConfig fPixelConfig;
99 GrTexture* fTexture;
commit-bot@chromium.org53e1e4d2014-04-01 16:25:11 +0000100 SkISize fBackingTextureSize;
101 int fNumPlotsX;
102 int fNumPlotsY;
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +0000103 bool fBatchUploads;
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000104
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000105 // allocated array of GrPlots
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000106 GrPlot* fPlotArray;
107 // LRU list of GrPlots
108 GrPlotList fPlotList;
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000109};
110
111class GrAtlas {
112public:
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000113 GrAtlas() { }
114 ~GrAtlas() { }
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000115
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000116 bool isEmpty() { return 0 == fPlots.count(); }
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000117
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000118private:
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000119 SkTDArray<GrPlot*> fPlots;
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000120
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000121 friend class GrAtlasMgr;
reed@google.comac10a2d2010-12-22 21:39:39 +0000122};
123
124#endif