blob: cd8123f140980fdb581ceb9789948098c85d7452 [file] [log] [blame]
joshualitt5bf99f12015-03-13 11:47:42 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef GrBatchAtlas_DEFINED
9#define GrBatchAtlas_DEFINED
10
11#include "GrTexture.h"
12#include "SkPoint.h"
13#include "SkTDArray.h"
14#include "SkTInternalLList.h"
15
16class BatchPlot;
17class GrBatchTarget;
18class GrRectanizer;
19
20typedef SkTInternalLList<BatchPlot> GrBatchPlotList;
21
22class GrBatchAtlas {
23public:
24 typedef uint64_t BatchToken;
25 // An AtlasID is an opaque handle which callers can use to determine if the atlas contains
26 // a specific piece of data
27 typedef uint32_t AtlasID;
joshualitt7c3a2f82015-03-31 13:32:05 -070028 static const uint32_t kInvalidAtlasID = 0;
29 static const uint64_t kInvalidAtlasGeneration = 0;
joshualitt5bf99f12015-03-13 11:47:42 -070030
31 // A function pointer for use as a callback during eviction. Whenever GrBatchAtlas evicts a
32 // specific AtlasID, it will call all of the registered listeners so they can optionally process
33 // the eviction
34 typedef void (*EvictionFunc)(GrBatchAtlas::AtlasID, void*);
35
36 GrBatchAtlas(GrTexture*, int numPlotsX, int numPlotsY);
37 ~GrBatchAtlas();
38
39 // Adds a width x height subimage to the atlas. Upon success it returns
40 // the containing GrPlot and absolute location in the backing texture.
41 // NULL is returned if the subimage cannot fit in the atlas.
42 // If provided, the image data will be written to the CPU-side backing bitmap.
43 bool addToAtlas(AtlasID*, GrBatchTarget*, int width, int height, const void* image,
44 SkIPoint16* loc);
45
46 GrTexture* getTexture() const { return fTexture; }
47
joshualitt7c3a2f82015-03-31 13:32:05 -070048 uint64_t atlasGeneration() const { return fAtlasGeneration; }
joshualitt5bf99f12015-03-13 11:47:42 -070049 bool hasID(AtlasID id);
50 void setLastRefToken(AtlasID id, BatchToken batchToken);
51 void registerEvictionCallback(EvictionFunc func, void* userData) {
52 EvictionData* data = fEvictionCallbacks.append();
53 data->fFunc = func;
54 data->fData = userData;
55 }
56
57private:
58 int getIndexFromID(AtlasID id) {
59 return id & 0xffff;
60 }
61
62 int getGenerationFromID(AtlasID id) {
63 return (id >> 16) & 0xffff;
64 }
65
66 inline void updatePlot(GrBatchTarget*, AtlasID*, BatchPlot*);
67
68 inline void makeMRU(BatchPlot* plot);
69
70 inline void processEviction(AtlasID);
71
72 GrTexture* fTexture;
73 int fNumPlotsX;
74 int fNumPlotsY;
75 int fPlotWidth;
76 int fPlotHeight;
77 size_t fBPP;
joshualitt7c3a2f82015-03-31 13:32:05 -070078 uint64_t fAtlasGeneration;
joshualitt5bf99f12015-03-13 11:47:42 -070079
80 struct EvictionData {
81 EvictionFunc fFunc;
82 void* fData;
83 };
84
85 SkTDArray<EvictionData> fEvictionCallbacks;
86 // allocated array of GrBatchPlots
87 SkAutoTUnref<BatchPlot>* fPlotArray;
88 // LRU list of GrPlots (MRU at head - LRU at tail)
89 GrBatchPlotList fPlotList;
90};
91
92#endif