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 | |
| 8 | #include "Resources.h" |
halcanary | 30b83d4 | 2014-10-26 05:23:53 -0700 | [diff] [blame] | 9 | #include "SkBitmap.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 10 | #include "SkCommandLineFlags.h" |
halcanary | 30b83d4 | 2014-10-26 05:23:53 -0700 | [diff] [blame] | 11 | #include "SkData.h" |
halcanary | 2f0a728 | 2015-08-21 07:47:23 -0700 | [diff] [blame] | 12 | #include "SkImage.h" |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 13 | #include "SkImageGenerator.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 14 | #include "SkOSFile.h" |
Ben Wagner | bf111d7 | 2016-11-07 18:05:29 -0500 | [diff] [blame] | 15 | #include "SkOSPath.h" |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 16 | #include "SkStream.h" |
| 17 | #include "SkTypeface.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 18 | |
| 19 | DEFINE_string2(resourcePath, i, "resources", "Directory with test resources: images, fonts, etc."); |
| 20 | |
| 21 | SkString GetResourcePath(const char* resource) { |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 22 | return SkOSPath::Join(FLAGS_resourcePath[0], resource); |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 23 | } |
caryclark | 936b734 | 2014-07-11 12:14:51 -0700 | [diff] [blame] | 24 | |
| 25 | void SetResourcePath(const char* resource) { |
| 26 | FLAGS_resourcePath.set(0, resource); |
| 27 | } |
halcanary | 30b83d4 | 2014-10-26 05:23:53 -0700 | [diff] [blame] | 28 | |
| 29 | bool GetResourceAsBitmap(const char* resource, SkBitmap* dst) { |
| 30 | SkString resourcePath = GetResourcePath(resource); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 31 | sk_sp<SkData> resourceData(SkData::MakeFromFileName(resourcePath.c_str())); |
Mike Reed | 185130c | 2017-02-15 15:14:16 -0500 | [diff] [blame] | 32 | std::unique_ptr<SkImageGenerator> gen(SkImageGenerator::MakeFromEncoded(resourceData)); |
Brian Osman | 06826d9 | 2016-11-29 15:10:13 -0500 | [diff] [blame] | 33 | if (!gen) { |
| 34 | return false; |
| 35 | } |
Matt Sarett | ebb1b5c | 2017-05-12 11:41:27 -0400 | [diff] [blame] | 36 | return dst->tryAllocPixels(gen->getInfo()) && |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 37 | gen->getPixels(gen->getInfo().makeColorSpace(nullptr), dst->getPixels(), dst->rowBytes(), |
Matt Sarett | ebb1b5c | 2017-05-12 11:41:27 -0400 | [diff] [blame] | 38 | nullptr); |
halcanary | 30b83d4 | 2014-10-26 05:23:53 -0700 | [diff] [blame] | 39 | } |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 40 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 41 | sk_sp<SkImage> GetResourceAsImage(const char* resource) { |
halcanary | 2f0a728 | 2015-08-21 07:47:23 -0700 | [diff] [blame] | 42 | SkString path = GetResourcePath(resource); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 43 | sk_sp<SkData> resourceData(SkData::MakeFromFileName(path.c_str())); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 44 | return SkImage::MakeFromEncoded(resourceData); |
halcanary | 2f0a728 | 2015-08-21 07:47:23 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Mike Reed | 71f867c | 2017-07-23 13:14:10 -0400 | [diff] [blame] | 47 | std::unique_ptr<SkStreamAsset> GetResourceAsStream(const char* resource) { |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 48 | SkString resourcePath = GetResourcePath(resource); |
Mike Reed | 847068c | 2017-07-26 11:35:53 -0400 | [diff] [blame] | 49 | auto stream = SkFILEStream::Make(resourcePath.c_str()); |
| 50 | if (!stream) { |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 51 | SkDebugf("Resource %s not found.\n", resource); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 52 | return nullptr; |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 53 | } |
Mike Reed | 71f867c | 2017-07-23 13:14:10 -0400 | [diff] [blame] | 54 | return std::move(stream); |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 55 | } |
| 56 | |
Mike Reed | 463c848 | 2016-12-21 12:01:12 -0500 | [diff] [blame] | 57 | sk_sp<SkData> GetResourceAsData(const char* resource) { |
| 58 | SkString resourcePath = GetResourcePath(resource); |
Ben Wagner | 1c1c740 | 2017-03-17 17:56:28 -0400 | [diff] [blame] | 59 | return SkData::MakeFromFileName(resourcePath.c_str()); |
Mike Reed | 463c848 | 2016-12-21 12:01:12 -0500 | [diff] [blame] | 60 | } |
| 61 | |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 62 | sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource) { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 63 | std::unique_ptr<SkStreamAsset> stream(GetResourceAsStream(resource)); |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 64 | if (!stream) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 65 | return nullptr; |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 66 | } |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 67 | return SkTypeface::MakeFromStream(stream.release()); |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 68 | } |