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 | a493510 | 2017-12-08 13:35:47 -0500 | [diff] [blame] | 8 | #include "BinaryAsset.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 | |
| 22 | SkString GetResourcePath(const char* resource) { |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 23 | return SkOSPath::Join(FLAGS_resourcePath[0], resource); |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 24 | } |
caryclark | 936b734 | 2014-07-11 12:14:51 -0700 | [diff] [blame] | 25 | |
| 26 | void SetResourcePath(const char* resource) { |
| 27 | FLAGS_resourcePath.set(0, resource); |
| 28 | } |
halcanary | 30b83d4 | 2014-10-26 05:23:53 -0700 | [diff] [blame] | 29 | |
Mike Reed | 0933bc9 | 2017-12-09 01:27:41 +0000 | [diff] [blame] | 30 | |
| 31 | bool DecodeDataToBitmap(sk_sp<SkData> data, SkBitmap* dst) { |
| 32 | std::unique_ptr<SkImageGenerator> gen(SkImageGenerator::MakeFromEncoded(std::move(data))); |
| 33 | return gen && dst->tryAllocPixels(gen->getInfo()) && |
Brian Osman | 7992da3 | 2016-11-18 11:28:24 -0500 | [diff] [blame] | 34 | gen->getPixels(gen->getInfo().makeColorSpace(nullptr), dst->getPixels(), dst->rowBytes(), |
Matt Sarett | ebb1b5c | 2017-05-12 11:41:27 -0400 | [diff] [blame] | 35 | nullptr); |
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 | #ifdef SK_EMBED_RESOURCES |
| 45 | extern BinaryAsset gResources[]; |
| 46 | sk_sp<SkData> GetResourceAsData(const char* resource) { |
| 47 | for (const BinaryAsset* ptr = gResources; ptr->name; ++ptr) { |
| 48 | if (0 == strcmp(resource, ptr->name)) { |
| 49 | return SkData::MakeWithoutCopy(ptr->data, ptr->len); |
| 50 | } |
| 51 | } |
| 52 | SkDebugf("Resource \"%s\" not found.\n", resource); |
| 53 | SK_ABORT("missing resource"); |
| 54 | return nullptr; |
| 55 | } |
| 56 | #else |
Mike Reed | 463c848 | 2016-12-21 12:01:12 -0500 | [diff] [blame] | 57 | sk_sp<SkData> GetResourceAsData(const char* resource) { |
Mike Reed | 0933bc9 | 2017-12-09 01:27:41 +0000 | [diff] [blame] | 58 | auto data = SkData::MakeFromFileName(GetResourcePath(resource).c_str()); |
| 59 | if (!data) { |
| 60 | SkDebugf("Resource \"%s\" not found.\n", resource); |
| 61 | } |
| 62 | return data; |
Mike Reed | 463c848 | 2016-12-21 12:01:12 -0500 | [diff] [blame] | 63 | } |
Hal Canary | a493510 | 2017-12-08 13:35:47 -0500 | [diff] [blame] | 64 | #endif |
Mike Reed | 463c848 | 2016-12-21 12:01:12 -0500 | [diff] [blame] | 65 | |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 66 | sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource) { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 67 | std::unique_ptr<SkStreamAsset> stream(GetResourceAsStream(resource)); |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 68 | if (!stream) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 69 | return nullptr; |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 70 | } |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 71 | return SkTypeface::MakeFromStream(stream.release()); |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 72 | } |