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