| cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 11 | #include "GrGpuResource.h" |
| cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 12 | #include "SkRefCnt.h" |
| cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 13 | #include "SkStrokeRec.h" |
| cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 14 | #include "SkTArray.h" |
| cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 15 | |
| 16 | class SkPath; |
| cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 17 | class SkDescriptor; |
| cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 18 | |
| 19 | /** |
| cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 20 | * 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. |
| cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 23 | */ |
| cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 24 | |
| bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 25 | class GrPathRange : public GrGpuResource { |
| cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 26 | public: |
| 27 | SK_DECLARE_INST_COUNT(GrPathRange); |
| 28 | |
| 29 | static const bool kIsWrapped = false; |
| 30 | |
| cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 31 | /** |
| 32 | * Return the resourceType intended for cache lookups involving GrPathRange. |
| 33 | */ |
| cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 34 | static GrResourceKey::ResourceType resourceType() { |
| 35 | static const GrResourceKey::ResourceType type = GrResourceKey::GenerateResourceType(); |
| 36 | return type; |
| 37 | } |
| 38 | |
| cdalton | 55b24af | 2014-11-25 11:00:56 -0800 | [diff] [blame^] | 39 | 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 | |
| cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 56 | /** |
| cdalton | 55b24af | 2014-11-25 11:00:56 -0800 | [diff] [blame^] | 57 | * Class that generates the paths for a specific range. |
| cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 58 | */ |
| cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 59 | 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 | }; |
| cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 66 | |
| 67 | /** |
| cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 68 | * 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. |
| cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 70 | */ |
| cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 71 | 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(); } |
| cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 86 | |
| 87 | protected: |
| cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 88 | // 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; |
| cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 92 | |
| 93 | private: |
| cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 94 | // Notify when paths will be drawn in case this is a lazy-loaded path range. |
| 95 | friend class GrGpu; |
| cdalton | 55b24af | 2014-11-25 11:00:56 -0800 | [diff] [blame^] | 96 | void willDrawPaths(const void* indices, PathIndexType, int count) const; |
| 97 | template<typename IndexType> void willDrawPaths(const void* indices, int count) const; |
| cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 98 | |
| 99 | mutable SkAutoTUnref<PathGenerator> fPathGenerator; |
| 100 | mutable SkTArray<uint8_t, true /*MEM_COPY*/> fGeneratedPaths; |
| 101 | const int fNumPaths; |
| 102 | const SkStrokeRec fStroke; |
| 103 | |
| bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 104 | typedef GrGpuResource INHERITED; |
| cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | #endif |