blob: 3d6869817d2a386f42eab7011df5d3eb659cd944 [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 "GrPoint.h"
14#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.org7d330eb2013-09-27 19:39:38 +000034 int getOffsetX() const { return fOffset.fX; }
35 int getOffsetY() const { return fOffset.fY; }
reed@google.comac10a2d2010-12-22 21:39:39 +000036
37 GrTexture* texture() const { return fTexture; }
38
39 bool addSubImage(int width, int height, const void*, GrIPoint16*);
40
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000041 GrDrawTarget::DrawToken drawToken() const { return fDrawToken; }
42 void setDrawToken(GrDrawTarget::DrawToken draw) { fDrawToken = draw; }
reed@google.comac10a2d2010-12-22 21:39:39 +000043
44private:
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000045 GrPlot();
46 ~GrPlot(); // does not try to delete the fNext field
reed@google.comac10a2d2010-12-22 21:39:39 +000047
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000048 // for recycling
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000049 GrDrawTarget::DrawToken fDrawToken;
commit-bot@chromium.org67ed64e2013-08-05 19:42:56 +000050
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000051 GrPlot* fNext;
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000052
53 GrTexture* fTexture;
54 GrRectanizer* fRects;
55 GrAtlasMgr* fAtlasMgr;
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000056 GrIPoint16 fOffset;
robertphillips@google.com8b169312013-10-15 17:47:36 +000057 size_t fBytesPerPixel;
reed@google.comac10a2d2010-12-22 21:39:39 +000058
59 friend class GrAtlasMgr;
60};
61
reed@google.comac10a2d2010-12-22 21:39:39 +000062class GrAtlasMgr {
63public:
commit-bot@chromium.org95294412013-09-26 15:28:40 +000064 GrAtlasMgr(GrGpu*, GrPixelConfig);
reed@google.comac10a2d2010-12-22 21:39:39 +000065 ~GrAtlasMgr();
66
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000067 // 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*);
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +000070
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000071 // free up any plots that are not waiting on a draw call
72 bool removeUnusedPlots(GrAtlas* atlas);
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +000073
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000074 // to be called by ~GrAtlas()
75 void deletePlotList(GrPlot* plot);
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +000076
commit-bot@chromium.org3fddf0e2013-09-26 12:57:19 +000077 GrTexture* getTexture() const {
78 return fTexture;
reed@google.com759c16e2011-03-15 19:15:15 +000079 }
reed@google.comac10a2d2010-12-22 21:39:39 +000080
reed@google.comac10a2d2010-12-22 21:39:39 +000081private:
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000082 GrPlot* allocPlot();
83 void freePlot(GrPlot* plot);
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +000084
commit-bot@chromium.org95294412013-09-26 15:28:40 +000085 GrGpu* fGpu;
86 GrPixelConfig fPixelConfig;
87 GrTexture* fTexture;
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +000088
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000089 // allocated array of GrPlots
90 GrPlot* fPlots;
91 // linked list of free GrPlots
92 GrPlot* fFreePlots;
93};
94
95class GrAtlas {
96public:
97 GrAtlas(GrAtlasMgr* mgr) : fPlots(NULL), fAtlasMgr(mgr) { }
98 ~GrAtlas() { fAtlasMgr->deletePlotList(fPlots); }
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +000099
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000100 bool isEmpty() { return NULL == fPlots; }
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000101
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000102private:
103 GrPlot* fPlots;
104 GrAtlasMgr* fAtlasMgr;
skia.committer@gmail.com50df4d02013-09-28 07:01:33 +0000105
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +0000106 friend class GrAtlasMgr;
reed@google.comac10a2d2010-12-22 21:39:39 +0000107};
108
109#endif