| 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 |
SkString GetResourcePath(const char* resource) { |
| 24 |
return SkOSPath::Join(FLAGS_resourcePath[0], resource); |
| 25 |
} |
| 26 |
|
| 27 |
void SetResourcePath(const char* resource) { |
| 28 |
FLAGS_resourcePath.set(0, resource); |
| 29 |
} |
| 30 |
|
| 31 |
std::unique_ptr<SkStreamAsset> GetResourceAsStream(const char* resource, bool useFileStream) { |
| 32 |
if (useFileStream) { |
| 33 |
auto path = GetResourcePath(resource); |
| 34 |
return SkFILEStream::Make(path.c_str()); |
| 35 |
} else { |
| 36 |
auto data = GetResourceAsData(resource); |
| 37 |
return data ? std::unique_ptr<SkStreamAsset>(new SkMemoryStream(std::move(data))) |
| 38 |
: nullptr; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
sk_sp<SkData> GetResourceAsData(const char* resource) { |
| 43 |
if (sk_sp<SkData> data = gResourceFactory |
| 44 |
? gResourceFactory(resource) |
| 45 |
: SkData::MakeFromFileName(GetResourcePath(resource).c_str())) { |
| 46 |
return data; |
| 47 |
} |
| 48 |
SkDebugf("Resource \"%s\" not found.\n", GetResourcePath(resource).c_str()); |
| 49 |
#ifdef SK_TOOLS_REQUIRE_RESOURCES |
| 50 |
SK_ABORT("missing resource"); |
| 51 |
#endif |
| 52 |
return nullptr; |
| 53 |
} |