blob: 014b7ece74caa597ce044b984f0b3882e8978663 [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
cdalton55b24af2014-11-25 11:00:56 -080031 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
cdaltonb85a0aa2014-07-21 15:32:44 -070048 /**
cdalton55b24af2014-11-25 11:00:56 -080049 * Class that generates the paths for a specific range.
cdaltonb85a0aa2014-07-21 15:32:44 -070050 */
cdalton855d83f2014-09-18 13:51:53 -070051 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 };
cdaltonb85a0aa2014-07-21 15:32:44 -070058
59 /**
cdalton855d83f2014-09-18 13:51:53 -070060 * 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.
cdaltonb85a0aa2014-07-21 15:32:44 -070062 */
cdalton855d83f2014-09-18 13:51:53 -070063 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(); }
cdaltonb85a0aa2014-07-21 15:32:44 -070078
79protected:
cdalton855d83f2014-09-18 13:51:53 -070080 // 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;
cdaltonb85a0aa2014-07-21 15:32:44 -070084
85private:
cdalton855d83f2014-09-18 13:51:53 -070086 // Notify when paths will be drawn in case this is a lazy-loaded path range.
87 friend class GrGpu;
cdalton55b24af2014-11-25 11:00:56 -080088 void willDrawPaths(const void* indices, PathIndexType, int count) const;
89 template<typename IndexType> void willDrawPaths(const void* indices, int count) const;
cdalton855d83f2014-09-18 13:51:53 -070090
91 mutable SkAutoTUnref<PathGenerator> fPathGenerator;
92 mutable SkTArray<uint8_t, true /*MEM_COPY*/> fGeneratedPaths;
93 const int fNumPaths;
94 const SkStrokeRec fStroke;
95
bsalomon6d3fe022014-07-25 08:35:45 -070096 typedef GrGpuResource INHERITED;
cdaltonb85a0aa2014-07-21 15:32:44 -070097};
98
99#endif