michael@0: michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef SkBBoxHierarchy_DEFINED michael@0: #define SkBBoxHierarchy_DEFINED michael@0: michael@0: #include "SkRect.h" michael@0: #include "SkTDArray.h" michael@0: #include "SkRefCnt.h" michael@0: michael@0: /** michael@0: * Interface for a client class that implements utility methods needed michael@0: * by SkBBoxHierarchy that require intrinsic knowledge of the data michael@0: * object type that is stored in the bounding box hierarchy. michael@0: */ michael@0: class SkBBoxHierarchyClient { michael@0: public: michael@0: virtual ~SkBBoxHierarchyClient() {} michael@0: michael@0: /** michael@0: * Implements a rewind stop condition used by rewindInserts michael@0: * Must returns true if 'data' points to an object that should be re-wound michael@0: * by rewinfInserts. michael@0: */ michael@0: virtual bool shouldRewind(void* data) = 0; michael@0: }; michael@0: michael@0: /** michael@0: * Interface for a spatial data structure that associates user data pointers with axis-aligned michael@0: * bounding boxes, and allows efficient retrieval of intersections with query rectangles. michael@0: */ michael@0: class SkBBoxHierarchy : public SkRefCnt { michael@0: public: michael@0: SK_DECLARE_INST_COUNT(SkBBoxHierarchy) michael@0: michael@0: SkBBoxHierarchy() : fClient(NULL) {} michael@0: michael@0: /** michael@0: * Insert a data pointer and corresponding bounding box michael@0: * @param data The data pointer, may be NULL michael@0: * @param bounds The bounding box, should not be empty michael@0: * @param defer Whether or not it is acceptable to delay insertion of this element (building up michael@0: * an entire spatial data structure at once is often faster and produces better michael@0: * structures than repeated inserts) until flushDeferredInserts is called or the first michael@0: * search. michael@0: */ michael@0: virtual void insert(void* data, const SkIRect& bounds, bool defer = false) = 0; michael@0: michael@0: /** michael@0: * If any insertions have been deferred, this forces them to be inserted michael@0: */ michael@0: virtual void flushDeferredInserts() = 0; michael@0: michael@0: /** michael@0: * Populate 'results' with data pointers corresponding to bounding boxes that intersect 'query' michael@0: */ michael@0: virtual void search(const SkIRect& query, SkTDArray* results) = 0; michael@0: michael@0: virtual void clear() = 0; michael@0: michael@0: /** michael@0: * Gets the number of insertions actually made (does not include deferred insertions) michael@0: */ michael@0: virtual int getCount() const = 0; michael@0: michael@0: /** michael@0: * Returns the depth of the currently allocated tree. The root node counts for 1 level, michael@0: * so it should be 1 or more if there's a root node. This information provides details michael@0: * about the underlying structure, which is useful mainly for testing purposes. michael@0: * michael@0: * Returns 0 if there are currently no nodes in the tree. michael@0: * Returns -1 if the structure isn't a tree. michael@0: */ michael@0: virtual int getDepth() const = 0; michael@0: michael@0: /** michael@0: * Rewinds all the most recently inserted data elements until an element michael@0: * is encountered for which client->shouldRewind(data) returns false. May michael@0: * not rewind elements that were inserted prior to the last call to michael@0: * flushDeferredInserts. michael@0: */ michael@0: virtual void rewindInserts() = 0; michael@0: michael@0: void setClient(SkBBoxHierarchyClient* client) { fClient = client; } michael@0: michael@0: protected: michael@0: SkBBoxHierarchyClient* fClient; michael@0: michael@0: private: michael@0: typedef SkRefCnt INHERITED; michael@0: }; michael@0: michael@0: #endif