/src/skia/tools/Resources.cpp
Line | Count | Source (jump to first uncovered line) |
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 "tools/Resources.h" |
9 | | |
10 | | #include "include/core/SkData.h" |
11 | | #include "include/core/SkStream.h" |
12 | | #include "include/private/base/SkDebug.h" |
13 | | #include "src/utils/SkOSPath.h" |
14 | | #include "tools/flags/CommandLineFlags.h" |
15 | | |
16 | | #include <utility> |
17 | | |
18 | | static DEFINE_string2(resourcePath, i, "resources", |
19 | | "Directory with test resources: images, fonts, etc."); |
20 | | |
21 | | sk_sp<SkData> (*gResourceFactory)(const char*) = nullptr; |
22 | | |
23 | 22 | SkString GetResourcePath(const char* resource) { |
24 | 22 | return SkOSPath::Join(FLAGS_resourcePath[0], resource); |
25 | 22 | } |
26 | | |
27 | 0 | void SetResourcePath(const char* resource) { |
28 | 0 | FLAGS_resourcePath.set(0, resource); |
29 | 0 | } |
30 | | |
31 | 11 | std::unique_ptr<SkStreamAsset> GetResourceAsStream(const char* resource, bool useFileStream) { |
32 | 11 | if (useFileStream) { |
33 | 0 | auto path = GetResourcePath(resource); |
34 | 0 | return SkFILEStream::Make(path.c_str()); |
35 | 11 | } else { |
36 | 11 | auto data = GetResourceAsData(resource); |
37 | 11 | return data ? std::unique_ptr<SkStreamAsset>(new SkMemoryStream(std::move(data))) |
38 | 11 | : nullptr; |
39 | 11 | } |
40 | 11 | } |
41 | | |
42 | 11 | sk_sp<SkData> GetResourceAsData(const char* resource) { |
43 | 11 | if (sk_sp<SkData> data = gResourceFactory |
44 | 11 | ? gResourceFactory(resource) |
45 | 11 | : SkData::MakeFromFileName(GetResourcePath(resource).c_str())) { |
46 | 0 | return data; |
47 | 0 | } |
48 | 11 | SkDebugf("Resource \"%s\" not found.\n", GetResourcePath(resource).c_str()); |
49 | | #ifdef SK_TOOLS_REQUIRE_RESOURCES |
50 | | SK_ABORT("missing resource"); |
51 | | #endif |
52 | 11 | return nullptr; |
53 | 11 | } |