tfarina | bcbc178 | 2014-06-18 14:32:48 -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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" |
| 9 | #include "include/core/SkData.h" |
| 10 | #include "include/core/SkImage.h" |
| 11 | #include "include/core/SkImageGenerator.h" |
| 12 | #include "include/core/SkStream.h" |
| 13 | #include "include/core/SkTypeface.h" |
| 14 | #include "src/core/SkOSFile.h" |
| 15 | #include "src/utils/SkOSPath.h" |
| 16 | #include "tools/ResourceFactory.h" |
| 17 | #include "tools/Resources.h" |
| 18 | #include "tools/flags/CommandLineFlags.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 19 | |
Mike Klein | c6142d8 | 2019-03-25 10:54:59 -0500 | [diff] [blame] | 20 | static DEFINE_string2(resourcePath, i, "resources", |
| 21 | "Directory with test resources: images, fonts, etc."); |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 22 | |
Hal Canary | 537d9c0 | 2018-01-30 11:30:48 -0500 | [diff] [blame] | 23 | sk_sp<SkData> (*gResourceFactory)(const char*) = nullptr; |
| 24 | |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 25 | SkString GetResourcePath(const char* resource) { |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 26 | return SkOSPath::Join(FLAGS_resourcePath[0], resource); |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 27 | } |
caryclark | 936b734 | 2014-07-11 12:14:51 -0700 | [diff] [blame] | 28 | |
| 29 | void SetResourcePath(const char* resource) { |
| 30 | FLAGS_resourcePath.set(0, resource); |
| 31 | } |
halcanary | 30b83d4 | 2014-10-26 05:23:53 -0700 | [diff] [blame] | 32 | |
Mike Reed | 0933bc9 | 2017-12-09 01:27:41 +0000 | [diff] [blame] | 33 | bool DecodeDataToBitmap(sk_sp<SkData> data, SkBitmap* dst) { |
| 34 | std::unique_ptr<SkImageGenerator> gen(SkImageGenerator::MakeFromEncoded(std::move(data))); |
| 35 | return gen && dst->tryAllocPixels(gen->getInfo()) && |
Brian Osman | 11c221a | 2018-10-04 13:55:21 -0400 | [diff] [blame] | 36 | gen->getPixels(gen->getInfo().makeColorSpace(nullptr), dst->getPixels(), dst->rowBytes()); |
halcanary | 30b83d4 | 2014-10-26 05:23:53 -0700 | [diff] [blame] | 37 | } |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 38 | |
Mike Reed | 71f867c | 2017-07-23 13:14:10 -0400 | [diff] [blame] | 39 | std::unique_ptr<SkStreamAsset> GetResourceAsStream(const char* resource) { |
Mike Reed | 0933bc9 | 2017-12-09 01:27:41 +0000 | [diff] [blame] | 40 | auto data = GetResourceAsData(resource); |
| 41 | return data ? std::unique_ptr<SkStreamAsset>(new SkMemoryStream(std::move(data))) |
| 42 | : nullptr; |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 43 | } |
| 44 | |
Hal Canary | a493510 | 2017-12-08 13:35:47 -0500 | [diff] [blame] | 45 | sk_sp<SkData> GetResourceAsData(const char* resource) { |
Hal Canary | fd9bcab | 2018-04-24 11:47:23 -0400 | [diff] [blame] | 46 | if (sk_sp<SkData> data = gResourceFactory |
| 47 | ? gResourceFactory(resource) |
| 48 | : SkData::MakeFromFileName(GetResourcePath(resource).c_str())) { |
Hal Canary | 537d9c0 | 2018-01-30 11:30:48 -0500 | [diff] [blame] | 49 | return data; |
Hal Canary | a493510 | 2017-12-08 13:35:47 -0500 | [diff] [blame] | 50 | } |
Brian Salomon | 5b49d78 | 2018-08-28 10:22:50 -0400 | [diff] [blame] | 51 | SkDebugf("Resource \"%s\" not found.\n", GetResourcePath(resource).c_str()); |
Hal Canary | fd9bcab | 2018-04-24 11:47:23 -0400 | [diff] [blame] | 52 | #ifdef SK_TOOLS_REQUIRE_RESOURCES |
| 53 | SK_ABORT("missing resource"); |
| 54 | #endif |
Hal Canary | a493510 | 2017-12-08 13:35:47 -0500 | [diff] [blame] | 55 | return nullptr; |
| 56 | } |
Mike Reed | 463c848 | 2016-12-21 12:01:12 -0500 | [diff] [blame] | 57 | |
Mike Reed | 271d1d9 | 2018-09-03 21:10:10 -0400 | [diff] [blame] | 58 | sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource, int ttcIndex) { |
| 59 | return SkTypeface::MakeFromStream(GetResourceAsStream(resource), ttcIndex); |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 60 | } |