blob: 183a8d807d69d19c25d6ca346d0d561852b53ff6 [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 Canary537d9c02018-01-30 11:30:48 -05008#include "ResourceFactory.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
Hal Canary537d9c02018-01-30 11:30:48 -050022sk_sp<SkData> (*gResourceFactory)(const char*) = nullptr;
23
tfarinabcbc1782014-06-18 14:32:48 -070024SkString GetResourcePath(const char* resource) {
tfarinaa8e2e152014-07-28 19:26:58 -070025 return SkOSPath::Join(FLAGS_resourcePath[0], resource);
tfarinabcbc1782014-06-18 14:32:48 -070026}
caryclark936b7342014-07-11 12:14:51 -070027
28void SetResourcePath(const char* resource) {
29 FLAGS_resourcePath.set(0, resource);
30}
halcanary30b83d42014-10-26 05:23:53 -070031
Mike Reed0933bc92017-12-09 01:27:41 +000032bool 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 Osman7992da32016-11-18 11:28:24 -050035 gen->getPixels(gen->getInfo().makeColorSpace(nullptr), dst->getPixels(), dst->rowBytes(),
Matt Sarettebb1b5c2017-05-12 11:41:27 -040036 nullptr);
halcanary30b83d42014-10-26 05:23:53 -070037}
bungeman3ffa1262015-04-30 17:12:58 -040038
Mike Reed71f867c2017-07-23 13:14:10 -040039std::unique_ptr<SkStreamAsset> GetResourceAsStream(const char* resource) {
Mike Reed0933bc92017-12-09 01:27:41 +000040 auto data = GetResourceAsData(resource);
41 return data ? std::unique_ptr<SkStreamAsset>(new SkMemoryStream(std::move(data)))
42 : nullptr;
bungeman3ffa1262015-04-30 17:12:58 -040043}
44
Hal Canarya4935102017-12-08 13:35:47 -050045sk_sp<SkData> GetResourceAsData(const char* resource) {
Hal Canaryfd9bcab2018-04-24 11:47:23 -040046 if (sk_sp<SkData> data = gResourceFactory
47 ? gResourceFactory(resource)
48 : SkData::MakeFromFileName(GetResourcePath(resource).c_str())) {
Hal Canary537d9c02018-01-30 11:30:48 -050049 return data;
Hal Canarya4935102017-12-08 13:35:47 -050050 }
51 SkDebugf("Resource \"%s\" not found.\n", resource);
Hal Canaryfd9bcab2018-04-24 11:47:23 -040052 #ifdef SK_TOOLS_REQUIRE_RESOURCES
53 SK_ABORT("missing resource");
54 #endif
Hal Canarya4935102017-12-08 13:35:47 -050055 return nullptr;
56}
Mike Reed463c8482016-12-21 12:01:12 -050057
bungeman13b9c952016-05-12 10:09:30 -070058sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource) {
Ben Wagner145dbcd2016-11-03 14:40:50 -040059 std::unique_ptr<SkStreamAsset> stream(GetResourceAsStream(resource));
bungeman3ffa1262015-04-30 17:12:58 -040060 if (!stream) {
halcanary96fcdcc2015-08-27 07:41:13 -070061 return nullptr;
bungeman3ffa1262015-04-30 17:12:58 -040062 }
bungeman13b9c952016-05-12 10:09:30 -070063 return SkTypeface::MakeFromStream(stream.release());
bungeman3ffa1262015-04-30 17:12:58 -040064}