blob: eb956fbf658e77574dec939d65f198e61ce90f21 [file] [log] [blame]
robertphillips@google.com7fa18762012-09-11 13:02:31 +00001/*
2 * Copyright 2012 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 "GrPath.h"
9
cdaltonb85a0aa2014-07-21 15:32:44 -070010template<int NumBits> static uint64_t get_top_n_float_bits(float f) {
11 char* floatData = reinterpret_cast<char*>(&f);
12 uint32_t floatBits = *reinterpret_cast<uint32_t*>(floatData);
13 return floatBits >> (32 - NumBits);
14}
15
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +000016GrResourceKey GrPath::ComputeKey(const SkPath& path, const SkStrokeRec& stroke) {
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +000017 static const GrCacheID::Domain gPathDomain = GrCacheID::GenerateDomain();
18
19 GrCacheID::Key key;
cdaltonb85a0aa2014-07-21 15:32:44 -070020 uint64_t* keyData = key.fData64;
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +000021 keyData[0] = path.getGenerationID();
cdaltonb85a0aa2014-07-21 15:32:44 -070022 keyData[1] = ComputeStrokeKey(stroke);
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +000023
bsalomon7775c852014-12-30 12:50:52 -080024 return GrResourceKey(GrCacheID(gPathDomain, key), 0);
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +000025}
cdaltonb85a0aa2014-07-21 15:32:44 -070026
27uint64_t GrPath::ComputeStrokeKey(const SkStrokeRec& stroke) {
28 enum {
29 kStyleBits = 2,
30 kJoinBits = 2,
31 kCapBits = 2,
32 kWidthBits = 29,
33 kMiterBits = 29,
34
35 kJoinShift = kStyleBits,
36 kCapShift = kJoinShift + kJoinBits,
37 kWidthShift = kCapShift + kCapBits,
38 kMiterShift = kWidthShift + kWidthBits,
39
40 kBitCount = kMiterShift + kMiterBits
41 };
42
43 SK_COMPILE_ASSERT(SkStrokeRec::kStyleCount <= (1 << kStyleBits), style_shift_will_be_wrong);
44 SK_COMPILE_ASSERT(SkPaint::kJoinCount <= (1 << kJoinBits), cap_shift_will_be_wrong);
45 SK_COMPILE_ASSERT(SkPaint::kCapCount <= (1 << kCapBits), miter_shift_will_be_wrong);
46 SK_COMPILE_ASSERT(kBitCount == 64, wrong_stroke_key_size);
47
48 if (!stroke.needToApply()) {
49 return SkStrokeRec::kFill_Style;
50 }
51
52 uint64_t key = stroke.getStyle();
53 key |= stroke.getJoin() << kJoinShift;
54 key |= stroke.getCap() << kCapShift;
55 key |= get_top_n_float_bits<kWidthBits>(stroke.getWidth()) << kWidthShift;
56 key |= get_top_n_float_bits<kMiterBits>(stroke.getMiter()) << kMiterShift;
57
58 return key;
59}