1ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2010 Google Inc.
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
7ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com */
8ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
9ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com#ifndef GrAtlas_DEFINED
10ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com#define GrAtlas_DEFINED
11ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
127d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.org
13d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillips#include "SkPoint.h"
14ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com#include "GrTexture.h"
15a8916ffd90c04dc6cc1fb9ba94af2ff950284fadcommit-bot@chromium.org#include "GrDrawTarget.h"
16ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
17ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.comclass GrGpu;
18ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.comclass GrRectanizer;
19ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.comclass GrAtlasMgr;
207d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.orgclass GrAtlas;
217d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.org
227d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.org// The backing GrTexture for a set of GrAtlases is broken into a spatial grid of GrPlots. When
237d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.org// a GrAtlas needs space on the texture, it requests a GrPlot. Each GrAtlas can claim one
247d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.org// or more GrPlots. The GrPlots keep track of subimage placement via their GrRectanizer. Once a
257d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.org// GrPlot is "full" (i.e. there is no room for the new subimage according to the GrRectanizer), the
267d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.org// GrAtlas can request a new GrPlot via GrAtlasMgr::addToAtlas().
277d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.org//
287d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.org// If all GrPlots are allocated, the replacement strategy is up to the client. The drawToken is
297d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.org// available to ensure that all draw calls are finished for that particular GrPlot.
307d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.org// GrAtlasMgr::removeUnusedPlots() will free up any finished plots for a given GrAtlas.
317d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.org
327d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.orgclass GrPlot {
33ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.compublic:
34c9b2c885be8d2bb39f1d75cc316278fa8d0fa9f0commit-bot@chromium.org    SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrPlot);
35ade9a3485e78d471f5f0902e9e50a2ec74c88e76skia.committer@gmail.com
36ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    GrTexture* texture() const { return fTexture; }
37ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
38d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillips    bool addSubImage(int width, int height, const void*, SkIPoint16*);
39ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
40a8916ffd90c04dc6cc1fb9ba94af2ff950284fadcommit-bot@chromium.org    GrDrawTarget::DrawToken drawToken() const { return fDrawToken; }
41a8916ffd90c04dc6cc1fb9ba94af2ff950284fadcommit-bot@chromium.org    void setDrawToken(GrDrawTarget::DrawToken draw) { fDrawToken = draw; }
42ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
437801faaab9bf7dd0ac67e859c4e284e74f7bd46fcommit-bot@chromium.org    void uploadToTexture();
447801faaab9bf7dd0ac67e859c4e284e74f7bd46fcommit-bot@chromium.org
45c9b2c885be8d2bb39f1d75cc316278fa8d0fa9f0commit-bot@chromium.org    void resetRects();
46c9b2c885be8d2bb39f1d75cc316278fa8d0fa9f0commit-bot@chromium.org
47ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.comprivate:
487d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.org    GrPlot();
497d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.org    ~GrPlot(); // does not try to delete the fNext field
507801faaab9bf7dd0ac67e859c4e284e74f7bd46fcommit-bot@chromium.org    void init(GrAtlasMgr* mgr, int offX, int offY, int width, int height, size_t bpp,
517801faaab9bf7dd0ac67e859c4e284e74f7bd46fcommit-bot@chromium.org              bool batchUploads);
52ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
5367ed64e9aa70f5a95a2d309f9b73dc0009f3ed8ccommit-bot@chromium.org    // for recycling
54a8916ffd90c04dc6cc1fb9ba94af2ff950284fadcommit-bot@chromium.org    GrDrawTarget::DrawToken fDrawToken;
55a8916ffd90c04dc6cc1fb9ba94af2ff950284fadcommit-bot@chromium.org
567801faaab9bf7dd0ac67e859c4e284e74f7bd46fcommit-bot@chromium.org    unsigned char*          fPlotData;
57a8916ffd90c04dc6cc1fb9ba94af2ff950284fadcommit-bot@chromium.org    GrTexture*              fTexture;
58a8916ffd90c04dc6cc1fb9ba94af2ff950284fadcommit-bot@chromium.org    GrRectanizer*           fRects;
59a8916ffd90c04dc6cc1fb9ba94af2ff950284fadcommit-bot@chromium.org    GrAtlasMgr*             fAtlasMgr;
60d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillips    SkIPoint16              fOffset;        // the offset of the plot in the backing texture
618b169311b59ab84e8ca6f3630a1e960cc1be751erobertphillips@google.com    size_t                  fBytesPerPixel;
627801faaab9bf7dd0ac67e859c4e284e74f7bd46fcommit-bot@chromium.org    SkIRect                 fDirtyRect;
637801faaab9bf7dd0ac67e859c4e284e74f7bd46fcommit-bot@chromium.org    bool                    fDirty;
647801faaab9bf7dd0ac67e859c4e284e74f7bd46fcommit-bot@chromium.org    bool                    fBatchUploads;
65ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
66ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    friend class GrAtlasMgr;
67ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com};
68ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
69c9b2c885be8d2bb39f1d75cc316278fa8d0fa9f0commit-bot@chromium.orgtypedef SkTInternalLList<GrPlot> GrPlotList;
70c9b2c885be8d2bb39f1d75cc316278fa8d0fa9f0commit-bot@chromium.org
71ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.comclass GrAtlasMgr {
72ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.compublic:
7353e1e4d88a06db62898a3bf75751c042729d7160commit-bot@chromium.org    GrAtlasMgr(GrGpu*, GrPixelConfig, const SkISize& backingTextureSize,
747801faaab9bf7dd0ac67e859c4e284e74f7bd46fcommit-bot@chromium.org               int numPlotsX, int numPlotsY, bool batchUploads);
75ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com    ~GrAtlasMgr();
76ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
777d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.org    // add subimage of width, height dimensions to atlas
787d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.org    // returns the containing GrPlot and location relative to the backing texture
79d537341e16524d1e22ac5e6c8b9c8f274ba1833crobertphillips    GrPlot* addToAtlas(GrAtlas*, int width, int height, const void*, SkIPoint16*);
8050df4d013f840749f70d1759c23c4217e727fd54skia.committer@gmail.com
81c9b2c885be8d2bb39f1d75cc316278fa8d0fa9f0commit-bot@chromium.org    // remove reference to this plot
82c9b2c885be8d2bb39f1d75cc316278fa8d0fa9f0commit-bot@chromium.org    bool removePlot(GrAtlas* atlas, const GrPlot* plot);
8350df4d013f840749f70d1759c23c4217e727fd54skia.committer@gmail.com
84c9b2c885be8d2bb39f1d75cc316278fa8d0fa9f0commit-bot@chromium.org    // get a plot that's not being used by the current draw
85c9b2c885be8d2bb39f1d75cc316278fa8d0fa9f0commit-bot@chromium.org    // this allows us to overwrite this plot without flushing
86c9b2c885be8d2bb39f1d75cc316278fa8d0fa9f0commit-bot@chromium.org    GrPlot* getUnusedPlot();
8750df4d013f840749f70d1759c23c4217e727fd54skia.committer@gmail.com
883fddf0eed6dc2873bcc8e584f435c6cd34964518commit-bot@chromium.org    GrTexture* getTexture() const {
893fddf0eed6dc2873bcc8e584f435c6cd34964518commit-bot@chromium.org        return fTexture;
90759c16e20dc42577226c8805bfea92d8bacb14d8reed@google.com    }
91ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
927801faaab9bf7dd0ac67e859c4e284e74f7bd46fcommit-bot@chromium.org    void uploadPlotsToTexture();
937801faaab9bf7dd0ac67e859c4e284e74f7bd46fcommit-bot@chromium.org
94ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.comprivate:
95c9b2c885be8d2bb39f1d75cc316278fa8d0fa9f0commit-bot@chromium.org    void moveToHead(GrPlot* plot);
9650df4d013f840749f70d1759c23c4217e727fd54skia.committer@gmail.com
97952944144758dd3f9a8a010ec1d99cb4bd035ae4commit-bot@chromium.org    GrGpu*        fGpu;
98952944144758dd3f9a8a010ec1d99cb4bd035ae4commit-bot@chromium.org    GrPixelConfig fPixelConfig;
99952944144758dd3f9a8a010ec1d99cb4bd035ae4commit-bot@chromium.org    GrTexture*    fTexture;
10053e1e4d88a06db62898a3bf75751c042729d7160commit-bot@chromium.org    SkISize       fBackingTextureSize;
10153e1e4d88a06db62898a3bf75751c042729d7160commit-bot@chromium.org    int           fNumPlotsX;
10253e1e4d88a06db62898a3bf75751c042729d7160commit-bot@chromium.org    int           fNumPlotsY;
1037801faaab9bf7dd0ac67e859c4e284e74f7bd46fcommit-bot@chromium.org    bool          fBatchUploads;
10450df4d013f840749f70d1759c23c4217e727fd54skia.committer@gmail.com
1057d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.org    // allocated array of GrPlots
106c9b2c885be8d2bb39f1d75cc316278fa8d0fa9f0commit-bot@chromium.org    GrPlot*       fPlotArray;
107c9b2c885be8d2bb39f1d75cc316278fa8d0fa9f0commit-bot@chromium.org    // LRU list of GrPlots
108c9b2c885be8d2bb39f1d75cc316278fa8d0fa9f0commit-bot@chromium.org    GrPlotList    fPlotList;
1097d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.org};
1107d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.org
1117d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.orgclass GrAtlas {
1127d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.orgpublic:
113c9b2c885be8d2bb39f1d75cc316278fa8d0fa9f0commit-bot@chromium.org    GrAtlas() { }
114c9b2c885be8d2bb39f1d75cc316278fa8d0fa9f0commit-bot@chromium.org    ~GrAtlas() { }
11550df4d013f840749f70d1759c23c4217e727fd54skia.committer@gmail.com
116c9b2c885be8d2bb39f1d75cc316278fa8d0fa9f0commit-bot@chromium.org    bool isEmpty() { return 0 == fPlots.count(); }
11750df4d013f840749f70d1759c23c4217e727fd54skia.committer@gmail.com
1187d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.orgprivate:
119c9b2c885be8d2bb39f1d75cc316278fa8d0fa9f0commit-bot@chromium.org    SkTDArray<GrPlot*> fPlots;
12050df4d013f840749f70d1759c23c4217e727fd54skia.committer@gmail.com
1217d330eb19cd3c9278abce68ca0e3efabf2ec8f87commit-bot@chromium.org    friend class GrAtlasMgr;
122ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com};
123ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com
124ac10a2d039c5d52eed66e27cbbc503ab523c1cd5reed@google.com#endif
125