blob: a1431b93c768b6598d38ecbd162800286c1d62ab [file] [log] [blame]
cdaltonb85a0aa2014-07-21 15:32:44 -07001/*
2 * Copyright 2014 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 GrPathRange_DEFINED
9#define GrPathRange_DEFINED
10
bsalomon6d3fe022014-07-25 08:35:45 -070011#include "GrGpuResource.h"
cdalton855d83f2014-09-18 13:51:53 -070012#include "SkRefCnt.h"
cdaltonb85a0aa2014-07-21 15:32:44 -070013#include "SkStrokeRec.h"
cdalton855d83f2014-09-18 13:51:53 -070014#include "SkTArray.h"
cdaltonb85a0aa2014-07-21 15:32:44 -070015
16class SkPath;
cdalton855d83f2014-09-18 13:51:53 -070017class SkDescriptor;
cdaltonb85a0aa2014-07-21 15:32:44 -070018
19/**
cdalton855d83f2014-09-18 13:51:53 -070020 * Represents a contiguous range of GPU path objects, all with a common stroke.
21 * This object is immutable with the exception that individual paths may be
22 * initialized lazily.
cdaltonb85a0aa2014-07-21 15:32:44 -070023 */
cdalton855d83f2014-09-18 13:51:53 -070024
bsalomon6d3fe022014-07-25 08:35:45 -070025class GrPathRange : public GrGpuResource {
cdaltonb85a0aa2014-07-21 15:32:44 -070026public:
27 SK_DECLARE_INST_COUNT(GrPathRange);
28
29 static const bool kIsWrapped = false;
30
cdalton855d83f2014-09-18 13:51:53 -070031 /**
32 * Return the resourceType intended for cache lookups involving GrPathRange.
33 */
cdaltonb85a0aa2014-07-21 15:32:44 -070034 static GrResourceKey::ResourceType resourceType() {
35 static const GrResourceKey::ResourceType type = GrResourceKey::GenerateResourceType();
36 return type;
37 }
38
cdalton55b24af2014-11-25 11:00:56 -080039 enum PathIndexType {
40 kU8_PathIndexType, //!< uint8_t
41 kU16_PathIndexType, //!< uint16_t
42 kU32_PathIndexType, //!< uint32_t
43
44 kLast_PathIndexType = kU32_PathIndexType
45 };
46
47 static inline int PathIndexSizeInBytes(PathIndexType type) {
48 GR_STATIC_ASSERT(0 == kU8_PathIndexType);
49 GR_STATIC_ASSERT(1 == kU16_PathIndexType);
50 GR_STATIC_ASSERT(2 == kU32_PathIndexType);
51 GR_STATIC_ASSERT(kU32_PathIndexType == kLast_PathIndexType);
52
53 return 1 << type;
54 }
55
cdaltonb85a0aa2014-07-21 15:32:44 -070056 /**
cdalton55b24af2014-11-25 11:00:56 -080057 * Class that generates the paths for a specific range.
cdaltonb85a0aa2014-07-21 15:32:44 -070058 */
cdalton855d83f2014-09-18 13:51:53 -070059 class PathGenerator : public SkRefCnt {
60 public:
61 virtual int getNumPaths() = 0;
62 virtual void generatePath(int index, SkPath* out) = 0;
63 virtual bool isEqualTo(const SkDescriptor&) const { return false; }
64 virtual ~PathGenerator() {}
65 };
cdaltonb85a0aa2014-07-21 15:32:44 -070066
67 /**
cdalton855d83f2014-09-18 13:51:53 -070068 * Initialize a lazy-loaded path range. This class will generate an SkPath and call
69 * onInitPath() for each path within the range before it is drawn for the first time.
cdaltonb85a0aa2014-07-21 15:32:44 -070070 */
cdalton855d83f2014-09-18 13:51:53 -070071 GrPathRange(GrGpu*, PathGenerator*, const SkStrokeRec& stroke);
72
73 /**
74 * Initialize an eager-loaded path range. The subclass is responsible for ensuring all
75 * the paths are initialized up front.
76 */
77 GrPathRange(GrGpu*, int numPaths, const SkStrokeRec& stroke);
78
79 virtual bool isEqualTo(const SkDescriptor& desc) const {
80 return NULL != fPathGenerator.get() && fPathGenerator->isEqualTo(desc);
81 }
82
83 int getNumPaths() const { return fNumPaths; }
84 const SkStrokeRec& getStroke() const { return fStroke; }
85 const PathGenerator* getPathGenerator() const { return fPathGenerator.get(); }
cdaltonb85a0aa2014-07-21 15:32:44 -070086
87protected:
cdalton855d83f2014-09-18 13:51:53 -070088 // Initialize a path in the range before drawing. This is only called when
89 // fPathGenerator is non-null. The child class need not call didChangeGpuMemorySize(),
90 // GrPathRange will take care of that after the call is complete.
91 virtual void onInitPath(int index, const SkPath&) const = 0;
cdaltonb85a0aa2014-07-21 15:32:44 -070092
93private:
cdalton855d83f2014-09-18 13:51:53 -070094 // Notify when paths will be drawn in case this is a lazy-loaded path range.
95 friend class GrGpu;
cdalton55b24af2014-11-25 11:00:56 -080096 void willDrawPaths(const void* indices, PathIndexType, int count) const;
97 template<typename IndexType> void willDrawPaths(const void* indices, int count) const;
cdalton855d83f2014-09-18 13:51:53 -070098
99 mutable SkAutoTUnref<PathGenerator> fPathGenerator;
100 mutable SkTArray<uint8_t, true /*MEM_COPY*/> fGeneratedPaths;
101 const int fNumPaths;
102 const SkStrokeRec fStroke;
103
bsalomon6d3fe022014-07-25 08:35:45 -0700104 typedef GrGpuResource INHERITED;
cdaltonb85a0aa2014-07-21 15:32:44 -0700105};
106
107#endif