blob: 89f857e7f23b2234e5c232bd63d168018911a9fc [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
9#include "include/core/SkData.h"
10#include "include/core/SkImage.h"
11#include "include/core/SkImageGenerator.h"
12#include "include/core/SkStream.h"
13#include "include/core/SkTypeface.h"
14#include "src/core/SkOSFile.h"
15#include "src/utils/SkOSPath.h"
16#include "tools/ResourceFactory.h"
17#include "tools/Resources.h"
18#include "tools/flags/CommandLineFlags.h"
tfarinabcbc1782014-06-18 14:32:48 -070019
Mike Kleinc6142d82019-03-25 10:54:59 -050020static DEFINE_string2(resourcePath, i, "resources",
21 "Directory with test resources: images, fonts, etc.");
tfarinabcbc1782014-06-18 14:32:48 -070022
Hal Canary537d9c02018-01-30 11:30:48 -050023sk_sp<SkData> (*gResourceFactory)(const char*) = nullptr;
24
tfarinabcbc1782014-06-18 14:32:48 -070025SkString GetResourcePath(const char* resource) {
tfarinaa8e2e152014-07-28 19:26:58 -070026 return SkOSPath::Join(FLAGS_resourcePath[0], resource);
tfarinabcbc1782014-06-18 14:32:48 -070027}
caryclark936b7342014-07-11 12:14:51 -070028
29void SetResourcePath(const char* resource) {
30 FLAGS_resourcePath.set(0, resource);
31}
halcanary30b83d42014-10-26 05:23:53 -070032
Mike Reed0933bc92017-12-09 01:27:41 +000033bool DecodeDataToBitmap(sk_sp<SkData> data, SkBitmap* dst) {
34 std::unique_ptr<SkImageGenerator> gen(SkImageGenerator::MakeFromEncoded(std::move(data)));
35 return gen && dst->tryAllocPixels(gen->getInfo()) &&
Brian Osman11c221a2018-10-04 13:55:21 -040036 gen->getPixels(gen->getInfo().makeColorSpace(nullptr), dst->getPixels(), dst->rowBytes());
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 }
Brian Salomon5b49d782018-08-28 10:22:50 -040051 SkDebugf("Resource \"%s\" not found.\n", GetResourcePath(resource).c_str());
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
Mike Reed271d1d92018-09-03 21:10:10 -040058sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource, int ttcIndex) {
59 return SkTypeface::MakeFromStream(GetResourceAsStream(resource), ttcIndex);
bungeman3ffa1262015-04-30 17:12:58 -040060}