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 | 55b24af | 2014-11-25 11:00:56 -0800 | [diff] [blame] | 31 | enum PathIndexType { |
| 32 | kU8_PathIndexType, //!< uint8_t |
| 33 | kU16_PathIndexType, //!< uint16_t |
| 34 | kU32_PathIndexType, //!< uint32_t |
| 35 | |
| 36 | kLast_PathIndexType = kU32_PathIndexType |
| 37 | }; |
| 38 | |
| 39 | static inline int PathIndexSizeInBytes(PathIndexType type) { |
| 40 | GR_STATIC_ASSERT(0 == kU8_PathIndexType); |
| 41 | GR_STATIC_ASSERT(1 == kU16_PathIndexType); |
| 42 | GR_STATIC_ASSERT(2 == kU32_PathIndexType); |
| 43 | GR_STATIC_ASSERT(kU32_PathIndexType == kLast_PathIndexType); |
| 44 | |
| 45 | return 1 << type; |
| 46 | } |
| 47 | |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 48 | /** |
cdalton | 55b24af | 2014-11-25 11:00:56 -0800 | [diff] [blame] | 49 | * Class that generates the paths for a specific range. |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 50 | */ |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 51 | class PathGenerator : public SkRefCnt { |
| 52 | public: |
| 53 | virtual int getNumPaths() = 0; |
| 54 | virtual void generatePath(int index, SkPath* out) = 0; |
| 55 | virtual bool isEqualTo(const SkDescriptor&) const { return false; } |
| 56 | virtual ~PathGenerator() {} |
| 57 | }; |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 58 | |
| 59 | /** |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 60 | * Initialize a lazy-loaded path range. This class will generate an SkPath and call |
| 61 | * 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] | 62 | */ |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 63 | GrPathRange(GrGpu*, PathGenerator*, const SkStrokeRec& stroke); |
| 64 | |
| 65 | /** |
| 66 | * Initialize an eager-loaded path range. The subclass is responsible for ensuring all |
| 67 | * the paths are initialized up front. |
| 68 | */ |
| 69 | GrPathRange(GrGpu*, int numPaths, const SkStrokeRec& stroke); |
| 70 | |
| 71 | virtual bool isEqualTo(const SkDescriptor& desc) const { |
| 72 | return NULL != fPathGenerator.get() && fPathGenerator->isEqualTo(desc); |
| 73 | } |
| 74 | |
| 75 | int getNumPaths() const { return fNumPaths; } |
| 76 | const SkStrokeRec& getStroke() const { return fStroke; } |
| 77 | const PathGenerator* getPathGenerator() const { return fPathGenerator.get(); } |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 78 | |
| 79 | protected: |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 80 | // Initialize a path in the range before drawing. This is only called when |
| 81 | // fPathGenerator is non-null. The child class need not call didChangeGpuMemorySize(), |
| 82 | // GrPathRange will take care of that after the call is complete. |
| 83 | virtual void onInitPath(int index, const SkPath&) const = 0; |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 84 | |
| 85 | private: |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 86 | // Notify when paths will be drawn in case this is a lazy-loaded path range. |
| 87 | friend class GrGpu; |
cdalton | 55b24af | 2014-11-25 11:00:56 -0800 | [diff] [blame] | 88 | void willDrawPaths(const void* indices, PathIndexType, int count) const; |
| 89 | template<typename IndexType> void willDrawPaths(const void* indices, int count) const; |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 90 | |
| 91 | mutable SkAutoTUnref<PathGenerator> fPathGenerator; |
| 92 | mutable SkTArray<uint8_t, true /*MEM_COPY*/> fGeneratedPaths; |
| 93 | const int fNumPaths; |
| 94 | const SkStrokeRec fStroke; |
| 95 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 96 | typedef GrGpuResource INHERITED; |
cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | #endif |