2014-06-18 21:32:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkData.h"
|
|
|
|
#include "include/core/SkImage.h"
|
|
|
|
#include "include/core/SkImageGenerator.h"
|
|
|
|
#include "include/core/SkStream.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "src/core/SkOSFile.h"
|
|
|
|
#include "src/utils/SkOSPath.h"
|
|
|
|
#include "tools/ResourceFactory.h"
|
|
|
|
#include "tools/Resources.h"
|
|
|
|
#include "tools/flags/CommandLineFlags.h"
|
2014-06-18 21:32:48 +00:00
|
|
|
|
2019-03-25 15:54:59 +00:00
|
|
|
static DEFINE_string2(resourcePath, i, "resources",
|
|
|
|
"Directory with test resources: images, fonts, etc.");
|
2014-06-18 21:32:48 +00:00
|
|
|
|
2018-01-30 16:30:48 +00:00
|
|
|
sk_sp<SkData> (*gResourceFactory)(const char*) = nullptr;
|
|
|
|
|
2014-06-18 21:32:48 +00:00
|
|
|
SkString GetResourcePath(const char* resource) {
|
2014-07-29 02:26:58 +00:00
|
|
|
return SkOSPath::Join(FLAGS_resourcePath[0], resource);
|
2014-06-18 21:32:48 +00:00
|
|
|
}
|
2014-07-11 19:14:51 +00:00
|
|
|
|
|
|
|
void SetResourcePath(const char* resource) {
|
|
|
|
FLAGS_resourcePath.set(0, resource);
|
|
|
|
}
|
2014-10-26 12:23:53 +00:00
|
|
|
|
2017-12-09 01:27:41 +00:00
|
|
|
bool DecodeDataToBitmap(sk_sp<SkData> data, SkBitmap* dst) {
|
|
|
|
std::unique_ptr<SkImageGenerator> gen(SkImageGenerator::MakeFromEncoded(std::move(data)));
|
|
|
|
return gen && dst->tryAllocPixels(gen->getInfo()) &&
|
2018-10-04 17:55:21 +00:00
|
|
|
gen->getPixels(gen->getInfo().makeColorSpace(nullptr), dst->getPixels(), dst->rowBytes());
|
2014-10-26 12:23:53 +00:00
|
|
|
}
|
2015-04-30 21:12:58 +00:00
|
|
|
|
2017-07-23 17:14:10 +00:00
|
|
|
std::unique_ptr<SkStreamAsset> GetResourceAsStream(const char* resource) {
|
2017-12-09 01:27:41 +00:00
|
|
|
auto data = GetResourceAsData(resource);
|
|
|
|
return data ? std::unique_ptr<SkStreamAsset>(new SkMemoryStream(std::move(data)))
|
|
|
|
: nullptr;
|
2015-04-30 21:12:58 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 18:35:47 +00:00
|
|
|
sk_sp<SkData> GetResourceAsData(const char* resource) {
|
2018-04-24 15:47:23 +00:00
|
|
|
if (sk_sp<SkData> data = gResourceFactory
|
|
|
|
? gResourceFactory(resource)
|
|
|
|
: SkData::MakeFromFileName(GetResourcePath(resource).c_str())) {
|
2018-01-30 16:30:48 +00:00
|
|
|
return data;
|
2017-12-08 18:35:47 +00:00
|
|
|
}
|
2018-08-28 14:22:50 +00:00
|
|
|
SkDebugf("Resource \"%s\" not found.\n", GetResourcePath(resource).c_str());
|
2018-04-24 15:47:23 +00:00
|
|
|
#ifdef SK_TOOLS_REQUIRE_RESOURCES
|
|
|
|
SK_ABORT("missing resource");
|
|
|
|
#endif
|
2017-12-08 18:35:47 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-12-21 17:01:12 +00:00
|
|
|
|
2018-09-04 01:10:10 +00:00
|
|
|
sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource, int ttcIndex) {
|
|
|
|
return SkTypeface::MakeFromStream(GetResourceAsStream(resource), ttcIndex);
|
2015-04-30 21:12:58 +00:00
|
|
|
}
|