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 | |
Hal Canary | 537d9c0 | 2018-01-30 11:30:48 -0500 | [diff] [blame] | 8 | #include "ResourceFactory.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 9 | #include "Resources.h" |
halcanary | 30b83d4 | 2014-10-26 05:23:53 -0700 | [diff] [blame] | 10 | #include "SkBitmap.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 11 | #include "SkCommandLineFlags.h" |
halcanary | 30b83d4 | 2014-10-26 05:23:53 -0700 | [diff] [blame] | 12 | #include "SkData.h" |
halcanary | 2f0a728 | 2015-08-21 07:47:23 -0700 | [diff] [blame] | 13 | #include "SkImage.h" |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 14 | #include "SkImageGenerator.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 15 | #include "SkOSFile.h" |
Ben Wagner | bf111d7 | 2016-11-07 18:05:29 -0500 | [diff] [blame] | 16 | #include "SkOSPath.h" |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 17 | #include "SkStream.h" |
| 18 | #include "SkTypeface.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 19 | |
| 20 | DEFINE_string2(resourcePath, i, "resources", "Directory with test resources: images, fonts, etc."); |
| 21 | |
Hal Canary | 537d9c0 | 2018-01-30 11:30:48 -0500 | [diff] [blame] | 22 | sk_sp<SkData> (*gResourceFactory)(const char*) = nullptr; |
| 23 | |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 24 | SkString GetResourcePath(const char* resource) { |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 25 | return SkOSPath::Join(FLAGS_resourcePath[0], resource); |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 26 | } |
caryclark | 936b734 | 2014-07-11 12:14:51 -0700 | [diff] [blame] | 27 | |
| 28 | void SetResourcePath(const char* resource) { |
| 29 | FLAGS_resourcePath.set(0, resource); |
| 30 | } |
halcanary | 30b83d4 | 2014-10-26 05:23:53 -0700 | [diff] [blame] | 31 | |
Mike Reed | 0933bc9 | 2017-12-09 01:27:41 +0000 | [diff] [blame] | 32 | bool DecodeDataToBitmap(sk_sp<SkData> data, SkBitmap* dst) { |
| 33 | std::unique_ptr<SkImageGenerator> gen(SkImageGenerator::MakeFromEncoded(std::move(data))); |
| 34 | return gen && dst->tryAllocPixels(gen->getInfo()) && |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 35 | gen->getPixels(gen->getInfo().makeColorSpace(nullptr), dst->getPixels(), dst->rowBytes(), |
Matt Sarett | ebb1b5c | 2017-05-12 11:41:27 -0400 | [diff] [blame] | 36 | nullptr); |
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 | } |
| 51 | SkDebugf("Resource \"%s\" not found.\n", resource); |
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 | |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 58 | sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource) { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 59 | std::unique_ptr<SkStreamAsset> stream(GetResourceAsStream(resource)); |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 60 | if (!stream) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 61 | return nullptr; |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 62 | } |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 63 | return SkTypeface::MakeFromStream(stream.release()); |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 64 | } |