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" |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 12 | #include "SkImageGenerator.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 13 | #include "SkOSFile.h" |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 14 | #include "SkStream.h" |
| 15 | #include "SkTypeface.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 16 | |
| 17 | DEFINE_string2(resourcePath, i, "resources", "Directory with test resources: images, fonts, etc."); |
| 18 | |
| 19 | SkString GetResourcePath(const char* resource) { |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 20 | return SkOSPath::Join(FLAGS_resourcePath[0], resource); |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 21 | } |
caryclark | 936b734 | 2014-07-11 12:14:51 -0700 | [diff] [blame] | 22 | |
| 23 | void SetResourcePath(const char* resource) { |
| 24 | FLAGS_resourcePath.set(0, resource); |
| 25 | } |
halcanary | 30b83d4 | 2014-10-26 05:23:53 -0700 | [diff] [blame] | 26 | |
| 27 | bool GetResourceAsBitmap(const char* resource, SkBitmap* dst) { |
| 28 | SkString resourcePath = GetResourcePath(resource); |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 29 | SkAutoTUnref<SkData> resourceData(SkData::NewFromFileName(resourcePath.c_str())); |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 30 | return resourceData && SkInstallDiscardablePixelRef(resourceData, dst); |
halcanary | 30b83d4 | 2014-10-26 05:23:53 -0700 | [diff] [blame] | 31 | } |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 32 | |
| 33 | SkStreamAsset* GetResourceAsStream(const char* resource) { |
| 34 | SkString resourcePath = GetResourcePath(resource); |
| 35 | SkAutoTDelete<SkFILEStream> stream(new SkFILEStream(resourcePath.c_str())); |
| 36 | if (stream->isValid()) { |
| 37 | return stream.detach(); |
| 38 | } else { |
| 39 | SkDebugf("Resource %s not found.\n", resource); |
| 40 | return NULL; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | SkTypeface* GetResourceAsTypeface(const char* resource) { |
| 45 | SkAutoTDelete<SkStreamAsset> stream(GetResourceAsStream(resource)); |
| 46 | if (!stream) { |
| 47 | return NULL; |
| 48 | } |
reed | 933d986 | 2015-06-01 12:51:23 -0700 | [diff] [blame] | 49 | return SkTypeface::CreateFromStream(stream.detach()); |
bungeman | 3ffa126 | 2015-04-30 17:12:58 -0400 | [diff] [blame] | 50 | } |