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 | 11c221a | 2018-10-04 13:55:21 -0400 | [diff] [blame] | 35 | gen->getPixels(gen->getInfo().makeColorSpace(nullptr), dst->getPixels(), dst->rowBytes()); |
halcanary | 30b83d4 | 2014-10-26 05:23:53 -0700 | [diff] [blame] | 36 | } |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 37 | |
Mike Reed | 71f867c | 2017-07-23 13:14:10 -0400 | [diff] [blame] | 38 | std::unique_ptr<SkStreamAsset> GetResourceAsStream(const char* resource) { |
Mike Reed | 0933bc9 | 2017-12-09 01:27:41 +0000 | [diff] [blame] | 39 | auto data = GetResourceAsData(resource); |
| 40 | return data ? std::unique_ptr<SkStreamAsset>(new SkMemoryStream(std::move(data))) |
| 41 | : nullptr; |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 42 | } |
| 43 | |
Hal Canary | a493510 | 2017-12-08 13:35:47 -0500 | [diff] [blame] | 44 | sk_sp<SkData> GetResourceAsData(const char* resource) { |
Hal Canary | fd9bcab | 2018-04-24 11:47:23 -0400 | [diff] [blame] | 45 | if (sk_sp<SkData> data = gResourceFactory |
| 46 | ? gResourceFactory(resource) |
| 47 | : SkData::MakeFromFileName(GetResourcePath(resource).c_str())) { |
Hal Canary | 537d9c0 | 2018-01-30 11:30:48 -0500 | [diff] [blame] | 48 | return data; |
Hal Canary | a493510 | 2017-12-08 13:35:47 -0500 | [diff] [blame] | 49 | } |
Brian Salomon | 5b49d78 | 2018-08-28 10:22:50 -0400 | [diff] [blame] | 50 | SkDebugf("Resource \"%s\" not found.\n", GetResourcePath(resource).c_str()); |
Hal Canary | fd9bcab | 2018-04-24 11:47:23 -0400 | [diff] [blame] | 51 | #ifdef SK_TOOLS_REQUIRE_RESOURCES |
| 52 | SK_ABORT("missing resource"); |
| 53 | #endif |
Hal Canary | a493510 | 2017-12-08 13:35:47 -0500 | [diff] [blame] | 54 | return nullptr; |
| 55 | } |
Mike Reed | 463c848 | 2016-12-21 12:01:12 -0500 | [diff] [blame] | 56 | |
Mike Reed | 271d1d9 | 2018-09-03 21:10:10 -0400 | [diff] [blame] | 57 | sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource, int ttcIndex) { |
| 58 | return SkTypeface::MakeFromStream(GetResourceAsStream(resource), ttcIndex); |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 59 | } |