blob: 9d540269869bdfab246d9766de9b660789d2dee7 [file] [log] [blame]
tfarinabcbc1782014-06-18 14:32:48 -07001/*
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 Canarya4935102017-12-08 13:35:47 -05008#include "BinaryAsset.h"
tfarinabcbc1782014-06-18 14:32:48 -07009#include "Resources.h"
halcanary30b83d42014-10-26 05:23:53 -070010#include "SkBitmap.h"
tfarinabcbc1782014-06-18 14:32:48 -070011#include "SkCommandLineFlags.h"
halcanary30b83d42014-10-26 05:23:53 -070012#include "SkData.h"
halcanary2f0a7282015-08-21 07:47:23 -070013#include "SkImage.h"
reed5965c8a2015-01-07 18:04:45 -080014#include "SkImageGenerator.h"
tfarinabcbc1782014-06-18 14:32:48 -070015#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050016#include "SkOSPath.h"
bungeman3ffa1262015-04-30 17:12:58 -040017#include "SkStream.h"
18#include "SkTypeface.h"
tfarinabcbc1782014-06-18 14:32:48 -070019
20DEFINE_string2(resourcePath, i, "resources", "Directory with test resources: images, fonts, etc.");
21
22SkString GetResourcePath(const char* resource) {
tfarinaa8e2e152014-07-28 19:26:58 -070023 return SkOSPath::Join(FLAGS_resourcePath[0], resource);
tfarinabcbc1782014-06-18 14:32:48 -070024}
caryclark936b7342014-07-11 12:14:51 -070025
26void SetResourcePath(const char* resource) {
27 FLAGS_resourcePath.set(0, resource);
28}
halcanary30b83d42014-10-26 05:23:53 -070029
Mike Reed0933bc92017-12-09 01:27:41 +000030
31bool 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 Osman7992da32016-11-18 11:28:24 -050034 gen->getPixels(gen->getInfo().makeColorSpace(nullptr), dst->getPixels(), dst->rowBytes(),
Matt Sarettebb1b5c2017-05-12 11:41:27 -040035 nullptr);
halcanary30b83d42014-10-26 05:23:53 -070036}
bungeman3ffa1262015-04-30 17:12:58 -040037
Mike Reed71f867c2017-07-23 13:14:10 -040038std::unique_ptr<SkStreamAsset> GetResourceAsStream(const char* resource) {
Mike Reed0933bc92017-12-09 01:27:41 +000039 auto data = GetResourceAsData(resource);
40 return data ? std::unique_ptr<SkStreamAsset>(new SkMemoryStream(std::move(data)))
41 : nullptr;
bungeman3ffa1262015-04-30 17:12:58 -040042}
43
Hal Canarya4935102017-12-08 13:35:47 -050044#ifdef SK_EMBED_RESOURCES
45extern BinaryAsset gResources[];
46sk_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 Reed463c8482016-12-21 12:01:12 -050057sk_sp<SkData> GetResourceAsData(const char* resource) {
Mike Reed0933bc92017-12-09 01:27:41 +000058 auto data = SkData::MakeFromFileName(GetResourcePath(resource).c_str());
59 if (!data) {
60 SkDebugf("Resource \"%s\" not found.\n", resource);
61 }
62 return data;
Mike Reed463c8482016-12-21 12:01:12 -050063}
Hal Canarya4935102017-12-08 13:35:47 -050064#endif
Mike Reed463c8482016-12-21 12:01:12 -050065
bungeman13b9c952016-05-12 10:09:30 -070066sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource) {
Ben Wagner145dbcd2016-11-03 14:40:50 -040067 std::unique_ptr<SkStreamAsset> stream(GetResourceAsStream(resource));
bungeman3ffa1262015-04-30 17:12:58 -040068 if (!stream) {
halcanary96fcdcc2015-08-27 07:41:13 -070069 return nullptr;
bungeman3ffa1262015-04-30 17:12:58 -040070 }
bungeman13b9c952016-05-12 10:09:30 -070071 return SkTypeface::MakeFromStream(stream.release());
bungeman3ffa1262015-04-30 17:12:58 -040072}