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