blob: 182f87d104d3d72493d7410ea554e1fe5b366e84 [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 Osman11c221a2018-10-04 13:55:21 -040035 gen->getPixels(gen->getInfo().makeColorSpace(nullptr), dst->getPixels(), dst->rowBytes());
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 -050044sk_sp<SkData> GetResourceAsData(const char* resource) {
Hal Canaryfd9bcab2018-04-24 11:47:23 -040045 if (sk_sp<SkData> data = gResourceFactory
46 ? gResourceFactory(resource)
47 : SkData::MakeFromFileName(GetResourcePath(resource).c_str())) {
Hal Canary537d9c02018-01-30 11:30:48 -050048 return data;
Hal Canarya4935102017-12-08 13:35:47 -050049 }
Brian Salomon5b49d782018-08-28 10:22:50 -040050 SkDebugf("Resource \"%s\" not found.\n", GetResourcePath(resource).c_str());
Hal Canaryfd9bcab2018-04-24 11:47:23 -040051 #ifdef SK_TOOLS_REQUIRE_RESOURCES
52 SK_ABORT("missing resource");
53 #endif
Hal Canarya4935102017-12-08 13:35:47 -050054 return nullptr;
55}
Mike Reed463c8482016-12-21 12:01:12 -050056
Mike Reed271d1d92018-09-03 21:10:10 -040057sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource, int ttcIndex) {
58 return SkTypeface::MakeFromStream(GetResourceAsStream(resource), ttcIndex);
bungeman3ffa1262015-04-30 17:12:58 -040059}