• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
8 #include "ResourceFactory.h"
9 #include "Resources.h"
10 #include "SkBitmap.h"
11 #include "SkCommandLineFlags.h"
12 #include "SkData.h"
13 #include "SkImage.h"
14 #include "SkImageGenerator.h"
15 #include "SkOSFile.h"
16 #include "SkOSPath.h"
17 #include "SkStream.h"
18 #include "SkTypeface.h"
19 
20 DEFINE_string2(resourcePath, i, "resources", "Directory with test resources: images, fonts, etc.");
21 
22 sk_sp<SkData> (*gResourceFactory)(const char*) = nullptr;
23 
GetResourcePath(const char * resource)24 SkString GetResourcePath(const char* resource) {
25     return SkOSPath::Join(FLAGS_resourcePath[0], resource);
26 }
27 
SetResourcePath(const char * resource)28 void SetResourcePath(const char* resource) {
29     FLAGS_resourcePath.set(0, resource);
30 }
31 
DecodeDataToBitmap(sk_sp<SkData> data,SkBitmap * dst)32 bool 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()) &&
35         gen->getPixels(gen->getInfo().makeColorSpace(nullptr), dst->getPixels(), dst->rowBytes(),
36                        nullptr);
37 }
38 
GetResourceAsStream(const char * resource)39 std::unique_ptr<SkStreamAsset> GetResourceAsStream(const char* resource) {
40     auto data = GetResourceAsData(resource);
41     return data ? std::unique_ptr<SkStreamAsset>(new SkMemoryStream(std::move(data)))
42                 : nullptr;
43 }
44 
GetResourceAsData(const char * resource)45 sk_sp<SkData> GetResourceAsData(const char* resource) {
46     if (gResourceFactory) {
47         if (auto data = gResourceFactory(resource)) {
48             return data;
49         }
50         SkDebugf("Resource \"%s\" not found.\n", resource);
51         SK_ABORT("missing resource");
52     }
53     if (auto data = SkData::MakeFromFileName(GetResourcePath(resource).c_str())) {
54         return data;
55     }
56     SkDebugf("Resource \"%s\" not found.\n", resource);
57     return nullptr;
58 }
59 
MakeResourceAsTypeface(const char * resource)60 sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource) {
61     std::unique_ptr<SkStreamAsset> stream(GetResourceAsStream(resource));
62     if (!stream) {
63         return nullptr;
64     }
65     return SkTypeface::MakeFromStream(stream.release());
66 }
67