Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2012 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 SkTileGrid_DEFINED
      9 #define SkTileGrid_DEFINED
     10 
     11 #include "SkBBHFactory.h"
     12 #include "SkBBoxHierarchy.h"
     13 
     14 /**
     15  * Subclass of SkBBoxHierarchy that stores elements in buckets that correspond
     16  * to tile regions, disposed in a regular grid.  This is useful when the tile
     17  * structure that will be use in search() calls is known prior to insertion.
     18  */
     19 class SkTileGrid : public SkBBoxHierarchy {
     20 public:
     21     SkTileGrid(int xTiles, int yTiles, const SkTileGridFactory::TileGridInfo& info);
     22 
     23     virtual ~SkTileGrid();
     24 
     25     /**
     26      * Insert a data pointer and corresponding bounding box
     27      * @param data   An arbitrary data pointer, may be NULL.
     28      * @param bounds The bounding box, should not be empty.
     29      * @param defer  Ignored; SkTileGrid does not defer insertions.
     30      */
     31     virtual void insert(void* data, const SkRect& bounds, bool) SK_OVERRIDE;
     32 
     33     virtual void flushDeferredInserts() SK_OVERRIDE {};
     34 
     35     /**
     36      * Populate 'results' with data pointers corresponding to bounding boxes that intersect 'query'.
     37      * This will be fastest if the query is an exact match to a single grid tile.
     38      */
     39     virtual void search(const SkRect& query, SkTDArray<void*>* results) const SK_OVERRIDE;
     40 
     41     virtual void clear() SK_OVERRIDE;
     42 
     43     virtual int getCount() const SK_OVERRIDE { return fCount; }
     44 
     45     virtual int getDepth() const SK_OVERRIDE { return -1; }
     46 
     47     virtual void rewindInserts() SK_OVERRIDE;
     48 
     49     // For testing.
     50     int tileCount(int x, int y) { return fTiles[y * fXTiles + x].count(); }
     51 
     52 private:
     53     struct Entry {
     54         size_t order;  // Insertion order.  Used to preserve order when merging multiple tiles.
     55         void*  data;
     56     };
     57 
     58     const int fXTiles, fYTiles;
     59     SkTileGridFactory::TileGridInfo fInfo;
     60     size_t fCount;
     61 
     62     // (fXTiles * fYTiles) SkTDArrays, each listing data overlapping that tile in insertion order.
     63     SkTDArray<Entry>* fTiles;
     64 
     65     typedef SkBBoxHierarchy INHERITED;
     66 };
     67 
     68 #endif
     69