cdalton | 855d83f | 2014-09-18 13:51:53 -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 | #include "GrPathRange.h" |
| 9 | #include "SkPath.h" |
| 10 | |
| 11 | enum { |
| 12 | kPathsPerGroup = 16 // Paths get tracked in groups of 16 for lazy loading. |
| 13 | }; |
| 14 | |
| 15 | GrPathRange::GrPathRange(GrGpu* gpu, |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame^] | 16 | PathGenerator* pathGenerator) |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 17 | : INHERITED(gpu, kCached_LifeCycle), |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 18 | fPathGenerator(SkRef(pathGenerator)), |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame^] | 19 | fNumPaths(fPathGenerator->getNumPaths()) { |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 20 | const int numGroups = (fNumPaths + kPathsPerGroup - 1) / kPathsPerGroup; |
| 21 | fGeneratedPaths.reset((numGroups + 7) / 8); // 1 bit per path group. |
| 22 | memset(&fGeneratedPaths.front(), 0, fGeneratedPaths.count()); |
| 23 | } |
| 24 | |
| 25 | GrPathRange::GrPathRange(GrGpu* gpu, |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame^] | 26 | int numPaths) |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 27 | : INHERITED(gpu, kCached_LifeCycle), |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame^] | 28 | fNumPaths(numPaths) { |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 29 | } |
| 30 | |
cdalton | 55b24af | 2014-11-25 11:00:56 -0800 | [diff] [blame] | 31 | void GrPathRange::willDrawPaths(const void* indices, PathIndexType indexType, int count) const { |
| 32 | if (!fPathGenerator) { |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 33 | return; |
| 34 | } |
| 35 | |
cdalton | 55b24af | 2014-11-25 11:00:56 -0800 | [diff] [blame] | 36 | switch (indexType) { |
| 37 | case kU8_PathIndexType: return this->willDrawPaths<uint8_t>(indices, count); |
| 38 | case kU16_PathIndexType: return this->willDrawPaths<uint16_t>(indices, count); |
| 39 | case kU32_PathIndexType: return this->willDrawPaths<uint32_t>(indices, count); |
| 40 | default: SkFAIL("Unknown path index type"); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | template<typename IndexType> void GrPathRange::willDrawPaths(const void* indices, int count) const { |
| 45 | SkASSERT(fPathGenerator); |
| 46 | |
| 47 | const IndexType* indexArray = reinterpret_cast<const IndexType*>(indices); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 48 | bool didLoadPaths = false; |
| 49 | |
| 50 | for (int i = 0; i < count; ++i) { |
cdalton | 55b24af | 2014-11-25 11:00:56 -0800 | [diff] [blame] | 51 | SkASSERT(indexArray[i] < static_cast<uint32_t>(fNumPaths)); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 52 | |
cdalton | 55b24af | 2014-11-25 11:00:56 -0800 | [diff] [blame] | 53 | const int groupIndex = indexArray[i] / kPathsPerGroup; |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 54 | const int groupByte = groupIndex / 8; |
| 55 | const uint8_t groupBit = 1 << (groupIndex % 8); |
| 56 | |
| 57 | const bool hasPath = SkToBool(fGeneratedPaths[groupByte] & groupBit); |
| 58 | if (!hasPath) { |
| 59 | // We track which paths are loaded in groups of kPathsPerGroup. To |
| 60 | // mark a path as loaded we need to load the entire group. |
| 61 | const int groupFirstPath = groupIndex * kPathsPerGroup; |
| 62 | const int groupLastPath = SkTMin(groupFirstPath + kPathsPerGroup, fNumPaths) - 1; |
| 63 | |
| 64 | SkPath path; |
| 65 | for (int pathIdx = groupFirstPath; pathIdx <= groupLastPath; ++pathIdx) { |
| 66 | fPathGenerator->generatePath(pathIdx, &path); |
| 67 | this->onInitPath(pathIdx, path); |
| 68 | } |
| 69 | |
| 70 | fGeneratedPaths[groupByte] |= groupBit; |
| 71 | didLoadPaths = true; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (didLoadPaths) { |
| 76 | this->didChangeGpuMemorySize(); |
| 77 | } |
| 78 | } |