blob: 1e91d1e90ee111833cf0c3a83fe0c71e4960dce3 [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
reed@google.comac10a2d2010-12-22 21:39:39 +000013#include "GrTexture.h"
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000014#include "GrDrawTarget.h"
bsalomonc44be0e2014-07-25 07:32:33 -070015#include "SkPoint.h"
16#include "SkTInternalLList.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017
18class GrGpu;
19class GrRectanizer;
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
robertphillips1d86ee82014-06-24 15:08:49 -070026// GrAtlas can request a new GrPlot via GrAtlas::addToAtlas().
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000027//
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.
robertphillips1d86ee82014-06-24 15:08:49 -070030// GrAtlas::removeUnusedPlots() will free up any finished plots for a given GrAtlas.
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000031
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
robertphillips17dabfc2014-07-16 13:26:24 -070036 // 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.comac10a2d2010-12-22 21:39:39 +000040 GrTexture* texture() const { return fTexture; }
41
robertphillipsd5373412014-06-02 10:20:14 -070042 bool addSubImage(int width, int height, const void*, SkIPoint16*);
reed@google.comac10a2d2010-12-22 21:39:39 +000043
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000044 GrDrawTarget::DrawToken drawToken() const { return fDrawToken; }
45 void setDrawToken(GrDrawTarget::DrawToken draw) { fDrawToken = draw; }
reed@google.comac10a2d2010-12-22 21:39:39 +000046
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000047 void uploadToTexture();
48
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000049 void resetRects();
50
reed@google.comac10a2d2010-12-22 21:39:39 +000051private:
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000052 GrPlot();
53 ~GrPlot(); // does not try to delete the fNext field
robertphillips17dabfc2014-07-16 13:26:24 -070054 void init(GrAtlas* atlas, int id, int offX, int offY, int width, int height, size_t bpp,
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000055 bool batchUploads);
reed@google.comac10a2d2010-12-22 21:39:39 +000056
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000057 // for recycling
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000058 GrDrawTarget::DrawToken fDrawToken;
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000059
robertphillips17dabfc2014-07-16 13:26:24 -070060 int fID;
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000061 unsigned char* fPlotData;
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000062 GrTexture* fTexture;
63 GrRectanizer* fRects;
robertphillips1d86ee82014-06-24 15:08:49 -070064 GrAtlas* fAtlas;
robertphillipsd5373412014-06-02 10:20:14 -070065 SkIPoint16 fOffset; // the offset of the plot in the backing texture
robertphillips@google.com8b169312013-10-15 17:47:36 +000066 size_t fBytesPerPixel;
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +000067 SkIRect fDirtyRect;
68 bool fDirty;
69 bool fBatchUploads;
reed@google.comac10a2d2010-12-22 21:39:39 +000070
robertphillips1d86ee82014-06-24 15:08:49 -070071 friend class GrAtlas;
reed@google.comac10a2d2010-12-22 21:39:39 +000072};
73
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +000074typedef SkTInternalLList<GrPlot> GrPlotList;
75
robertphillips1d86ee82014-06-24 15:08:49 -070076class GrAtlas {
reed@google.comac10a2d2010-12-22 21:39:39 +000077public:
robertphillips1d86ee82014-06-24 15:08:49 -070078 // 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
robertphillips320c9232014-07-29 06:07:19 -070084#ifdef SK_DEBUG
85 bool contains(const GrPlot* plot) const {
86 return fPlots.contains(const_cast<GrPlot*>(plot));
87 }
88#endif
89
robertphillips1d86ee82014-06-24 15:08:49 -070090 private:
91 SkTDArray<GrPlot*> fPlots;
92
93 friend class GrAtlas;
94 };
95
robertphillips952841b2014-06-30 08:26:50 -070096 GrAtlas(GrGpu*, GrPixelConfig, GrTextureFlags flags,
97 const SkISize& backingTextureSize,
robertphillips1d86ee82014-06-24 15:08:49 -070098 int numPlotsX, int numPlotsY, bool batchUploads);
99 ~GrAtlas();
reed@google.comac10a2d2010-12-22 21:39:39 +0000100
robertphillips952841b2014-06-30 08:26:50 -0700101 // 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.com50df4d02013-09-28 07:01:33 +0000107
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000108 // remove reference to this plot
robertphillipsc4f30b12014-07-13 10:09:42 -0700109 static void RemovePlot(ClientPlotUsage* usage, const GrPlot* plot);
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000110
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000111 // 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.com50df4d02013-09-28 07:01:33 +0000114
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +0000115 GrTexture* getTexture() const {
116 return fTexture;
reed@google.com759c16e2011-03-15 19:15:15 +0000117 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000118
commit-bot@chromium.org7801faa2014-05-14 15:14:51 +0000119 void uploadPlotsToTexture();
120
robertphillips320c9232014-07-29 06:07:19 -0700121 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.comac10a2d2010-12-22 21:39:39 +0000133private:
robertphillips1d86ee82014-06-24 15:08:49 -0700134 void makeMRU(GrPlot* plot);
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000135
robertphillips952841b2014-06-30 08:26:50 -0700136 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.com50df4d02013-09-28 07:01:33 +0000144
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000145 // allocated array of GrPlots
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000146 GrPlot* fPlotArray;
robertphillips1d86ee82014-06-24 15:08:49 -0700147 // LRU list of GrPlots (MRU at head - LRU at tail)
commit-bot@chromium.orgc9b2c882014-03-03 14:30:25 +0000148 GrPlotList fPlotList;
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000149};
150
reed@google.comac10a2d2010-12-22 21:39:39 +0000151#endif