blob: 62b22d80e61f5ca8f64946aab6faf39e4d2e3494 [file] [log] [blame]
rileya@google.com1f45e932012-09-05 16:10:59 +00001
2/*
3 * Copyright 2012 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.
7 */
8
9#ifndef SkBBoxHierarchy_DEFINED
10#define SkBBoxHierarchy_DEFINED
11
12#include "SkRect.h"
13#include "SkTDArray.h"
rileya@google.com48134582012-09-11 15:41:50 +000014#include "SkRefCnt.h"
rileya@google.com1f45e932012-09-05 16:10:59 +000015
16/**
commit-bot@chromium.org4b32bd52013-03-15 15:06:03 +000017 * Interface for a client class that implements utility methods needed
skia.committer@gmail.comac2c82c2013-03-16 07:01:18 +000018 * by SkBBoxHierarchy that require intrinsic knowledge of the data
commit-bot@chromium.org4b32bd52013-03-15 15:06:03 +000019 * object type that is stored in the bounding box hierarchy.
20 */
21class SkBBoxHierarchyClient {
22public:
23 virtual ~SkBBoxHierarchyClient() {}
24
25 /**
26 * Implements a rewind stop condition used by rewindInserts
27 * Must returns true if 'data' points to an object that should be re-wound
28 * by rewinfInserts.
29 */
30 virtual bool shouldRewind(void* data) = 0;
31};
32
33/**
rileya@google.com1f45e932012-09-05 16:10:59 +000034 * Interface for a spatial data structure that associates user data pointers with axis-aligned
35 * bounding boxes, and allows efficient retrieval of intersections with query rectangles.
36 */
rileya@google.com48134582012-09-11 15:41:50 +000037class SkBBoxHierarchy : public SkRefCnt {
rileya@google.com1f45e932012-09-05 16:10:59 +000038public:
rileya@google.com48134582012-09-11 15:41:50 +000039 SK_DECLARE_INST_COUNT(SkBBoxHierarchy)
rileya@google.com1f45e932012-09-05 16:10:59 +000040
commit-bot@chromium.org4b32bd52013-03-15 15:06:03 +000041 SkBBoxHierarchy() : fClient(NULL) {}
42
rileya@google.com1f45e932012-09-05 16:10:59 +000043 /**
44 * Insert a data pointer and corresponding bounding box
45 * @param data The data pointer, may be NULL
46 * @param bounds The bounding box, should not be empty
47 * @param defer Whether or not it is acceptable to delay insertion of this element (building up
48 * an entire spatial data structure at once is often faster and produces better
49 * structures than repeated inserts) until flushDeferredInserts is called or the first
50 * search.
51 */
52 virtual void insert(void* data, const SkIRect& bounds, bool defer = false) = 0;
53
54 /**
55 * If any insertions have been deferred, this forces them to be inserted
56 */
57 virtual void flushDeferredInserts() = 0;
58
59 /**
60 * Populate 'results' with data pointers corresponding to bounding boxes that intersect 'query'
61 */
62 virtual void search(const SkIRect& query, SkTDArray<void*>* results) = 0;
63
64 virtual void clear() = 0;
65
66 /**
67 * Gets the number of insertions
68 */
69 virtual int getCount() const = 0;
rileya@google.com48134582012-09-11 15:41:50 +000070
commit-bot@chromium.org4b32bd52013-03-15 15:06:03 +000071 /**
72 * Rewinds all the most recently inserted data elements until an element
73 * is encountered for which client->shouldRewind(data) returns false. May
74 * not rewind elements that were inserted prior to the last call to
75 * flushDeferredInserts.
76 */
77 virtual void rewindInserts() = 0;
78
79 void setClient(SkBBoxHierarchyClient* client) { fClient = client; }
80
81protected:
82 SkBBoxHierarchyClient* fClient;
83
rileya@google.com48134582012-09-11 15:41:50 +000084private:
85 typedef SkRefCnt INHERITED;
rileya@google.com1f45e932012-09-05 16:10:59 +000086};
87
88#endif