blob: 0ba3996ecf4ea4702a009274c95dfcd66f838cea [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"
cdalton855d83f2014-09-18 13:51:53 -070013#include "SkTArray.h"
cdaltonb85a0aa2014-07-21 15:32:44 -070014
15class SkPath;
cdalton855d83f2014-09-18 13:51:53 -070016class SkDescriptor;
cdaltonb85a0aa2014-07-21 15:32:44 -070017
18/**
kkinnunen50b58e62015-05-18 23:02:07 -070019 * Represents a contiguous range of GPU path objects.
cdalton855d83f2014-09-18 13:51:53 -070020 * This object is immutable with the exception that individual paths may be
21 * initialized lazily.
cdaltonb85a0aa2014-07-21 15:32:44 -070022 */
cdalton855d83f2014-09-18 13:51:53 -070023
bsalomon6d3fe022014-07-25 08:35:45 -070024class GrPathRange : public GrGpuResource {
cdaltonb85a0aa2014-07-21 15:32:44 -070025public:
mtklein2766c002015-06-26 11:45:03 -070026
cdaltonb85a0aa2014-07-21 15:32:44 -070027
cdalton55b24af2014-11-25 11:00:56 -080028 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
cdaltonb85a0aa2014-07-21 15:32:44 -070045 /**
cdalton55b24af2014-11-25 11:00:56 -080046 * Class that generates the paths for a specific range.
cdaltonb85a0aa2014-07-21 15:32:44 -070047 */
cdalton855d83f2014-09-18 13:51:53 -070048 class PathGenerator : public SkRefCnt {
49 public:
50 virtual int getNumPaths() = 0;
51 virtual void generatePath(int index, SkPath* out) = 0;
kkinnunen50b58e62015-05-18 23:02:07 -070052#ifdef SK_DEBUG
cdalton855d83f2014-09-18 13:51:53 -070053 virtual bool isEqualTo(const SkDescriptor&) const { return false; }
kkinnunen50b58e62015-05-18 23:02:07 -070054#endif
cdalton855d83f2014-09-18 13:51:53 -070055 virtual ~PathGenerator() {}
56 };
cdaltonb85a0aa2014-07-21 15:32:44 -070057
58 /**
cdalton855d83f2014-09-18 13:51:53 -070059 * 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.
cdaltonb85a0aa2014-07-21 15:32:44 -070061 */
kkinnunen50b58e62015-05-18 23:02:07 -070062 GrPathRange(GrGpu*, PathGenerator*);
cdalton855d83f2014-09-18 13:51:53 -070063
64 /**
65 * Initialize an eager-loaded path range. The subclass is responsible for ensuring all
66 * the paths are initialized up front.
67 */
kkinnunen50b58e62015-05-18 23:02:07 -070068 GrPathRange(GrGpu*, int numPaths);
cdalton855d83f2014-09-18 13:51:53 -070069
kkinnunen50b58e62015-05-18 23:02:07 -070070 int getNumPaths() const { return fNumPaths; }
71 const PathGenerator* getPathGenerator() const { return fPathGenerator.get(); }
72
73#ifdef SK_DEBUG
cdalton855d83f2014-09-18 13:51:53 -070074 virtual bool isEqualTo(const SkDescriptor& desc) const {
75 return NULL != fPathGenerator.get() && fPathGenerator->isEqualTo(desc);
76 }
kkinnunen50b58e62015-05-18 23:02:07 -070077#endif
cdaltonb85a0aa2014-07-21 15:32:44 -070078protected:
cdalton855d83f2014-09-18 13:51:53 -070079 // 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;
cdaltonb85a0aa2014-07-21 15:32:44 -070083
84private:
cdalton855d83f2014-09-18 13:51:53 -070085 // Notify when paths will be drawn in case this is a lazy-loaded path range.
kkinnunencabe20c2015-06-01 01:37:26 -070086 friend class GrPathRendering;
cdalton55b24af2014-11-25 11:00:56 -080087 void willDrawPaths(const void* indices, PathIndexType, int count) const;
88 template<typename IndexType> void willDrawPaths(const void* indices, int count) const;
cdalton855d83f2014-09-18 13:51:53 -070089
90 mutable SkAutoTUnref<PathGenerator> fPathGenerator;
91 mutable SkTArray<uint8_t, true /*MEM_COPY*/> fGeneratedPaths;
92 const int fNumPaths;
cdalton855d83f2014-09-18 13:51:53 -070093
bsalomon6d3fe022014-07-25 08:35:45 -070094 typedef GrGpuResource INHERITED;
cdaltonb85a0aa2014-07-21 15:32:44 -070095};
96
97#endif