1 | /* |
2 | * Copyright 2019 Google LLC |
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 "modules/skresources/include/SkResources.h" |
9 | |
10 | #include "include/codec/SkCodec.h" |
11 | #include "include/core/SkData.h" |
12 | #include "include/core/SkImage.h" |
13 | #include "include/utils/SkAnimCodecPlayer.h" |
14 | #include "include/utils/SkBase64.h" |
15 | #include "src/core/SkOSFile.h" |
16 | #include "src/utils/SkOSPath.h" |
17 | |
18 | namespace skresources { |
19 | |
20 | sk_sp<MultiFrameImageAsset> MultiFrameImageAsset::Make(sk_sp<SkData> data, bool predecode) { |
21 | if (auto codec = SkCodec::MakeFromData(std::move(data))) { |
22 | return sk_sp<MultiFrameImageAsset>( |
23 | new MultiFrameImageAsset(std::make_unique<SkAnimCodecPlayer>(std::move(codec)), |
24 | predecode)); |
25 | } |
26 | |
27 | return nullptr; |
28 | } |
29 | |
30 | MultiFrameImageAsset::MultiFrameImageAsset(std::unique_ptr<SkAnimCodecPlayer> player, |
31 | bool predecode) |
32 | : fPlayer(std::move(player)) |
33 | , fPreDecode(predecode) { |
34 | SkASSERT(fPlayer); |
35 | } |
36 | |
37 | bool MultiFrameImageAsset::isMultiFrame() { |
38 | return fPlayer->duration() > 0; |
39 | } |
40 | |
41 | sk_sp<SkImage> MultiFrameImageAsset::getFrame(float t) { |
42 | auto decode = [](sk_sp<SkImage> image) { |
43 | SkASSERT(image->isLazyGenerated()); |
44 | |
45 | static constexpr size_t kMaxArea = 2048 * 2048; |
46 | const auto image_area = SkToSizeT(image->width() * image->height()); |
47 | |
48 | if (image_area > kMaxArea) { |
49 | // When the image is too large, decode and scale down to a reasonable size. |
50 | const auto scale = std::sqrt(static_cast<float>(kMaxArea) / image_area); |
51 | const auto info = SkImageInfo::MakeN32Premul(scale * image->width(), |
52 | scale * image->height()); |
53 | SkBitmap bm; |
54 | if (bm.tryAllocPixels(info, info.minRowBytes()) && |
55 | image->scalePixels(bm.pixmap(), |
56 | SkFilterQuality::kMedium_SkFilterQuality, |
57 | SkImage::kDisallow_CachingHint)) { |
58 | image = SkImage::MakeFromBitmap(bm); |
59 | } |
60 | } else { |
61 | // When the image size is OK, just force-decode. |
62 | image = image->makeRasterImage(); |
63 | } |
64 | |
65 | return image; |
66 | }; |
67 | |
68 | fPlayer->seek(static_cast<uint32_t>(t * 1000)); |
69 | auto frame = fPlayer->getFrame(); |
70 | |
71 | if (fPreDecode && frame && frame->isLazyGenerated()) { |
72 | frame = decode(std::move(frame)); |
73 | } |
74 | |
75 | return frame; |
76 | } |
77 | |
78 | sk_sp<FileResourceProvider> FileResourceProvider::Make(SkString base_dir, bool predecode) { |
79 | return sk_isdir(base_dir.c_str()) |
80 | ? sk_sp<FileResourceProvider>(new FileResourceProvider(std::move(base_dir), predecode)) |
81 | : nullptr; |
82 | } |
83 | |
84 | FileResourceProvider::FileResourceProvider(SkString base_dir, bool predecode) |
85 | : fDir(std::move(base_dir)) |
86 | , fPredecode(predecode) {} |
87 | |
88 | sk_sp<SkData> FileResourceProvider::load(const char resource_path[], |
89 | const char resource_name[]) const { |
90 | const auto full_dir = SkOSPath::Join(fDir.c_str() , resource_path), |
91 | full_path = SkOSPath::Join(full_dir.c_str(), resource_name); |
92 | return SkData::MakeFromFileName(full_path.c_str()); |
93 | } |
94 | |
95 | sk_sp<ImageAsset> FileResourceProvider::loadImageAsset(const char resource_path[], |
96 | const char resource_name[], |
97 | const char[]) const { |
98 | return MultiFrameImageAsset::Make(this->load(resource_path, resource_name), fPredecode); |
99 | } |
100 | |
101 | ResourceProviderProxyBase::ResourceProviderProxyBase(sk_sp<ResourceProvider> rp) |
102 | : fProxy(std::move(rp)) {} |
103 | |
104 | sk_sp<SkData> ResourceProviderProxyBase::load(const char resource_path[], |
105 | const char resource_name[]) const { |
106 | return fProxy ? fProxy->load(resource_path, resource_name) |
107 | : nullptr; |
108 | } |
109 | |
110 | sk_sp<ImageAsset> ResourceProviderProxyBase::loadImageAsset(const char rpath[], |
111 | const char rname[], |
112 | const char rid[]) const { |
113 | return fProxy ? fProxy->loadImageAsset(rpath, rname, rid) |
114 | : nullptr; |
115 | } |
116 | |
117 | sk_sp<SkData> ResourceProviderProxyBase::loadFont(const char name[], const char url[]) const { |
118 | return fProxy ? fProxy->loadFont(name, url) |
119 | : nullptr; |
120 | } |
121 | |
122 | CachingResourceProvider::CachingResourceProvider(sk_sp<ResourceProvider> rp) |
123 | : INHERITED(std::move(rp)) {} |
124 | |
125 | sk_sp<ImageAsset> CachingResourceProvider::loadImageAsset(const char resource_path[], |
126 | const char resource_name[], |
127 | const char resource_id[]) const { |
128 | SkAutoMutexExclusive amx(fMutex); |
129 | |
130 | const SkString key(resource_id); |
131 | if (const auto* asset = fImageCache.find(key)) { |
132 | return *asset; |
133 | } |
134 | |
135 | auto asset = this->INHERITED::loadImageAsset(resource_path, resource_name, resource_id); |
136 | fImageCache.set(key, asset); |
137 | |
138 | return asset; |
139 | } |
140 | |
141 | sk_sp<DataURIResourceProviderProxy> DataURIResourceProviderProxy::Make(sk_sp<ResourceProvider> rp, |
142 | bool predecode) { |
143 | return sk_sp<DataURIResourceProviderProxy>( |
144 | new DataURIResourceProviderProxy(std::move(rp), predecode)); |
145 | } |
146 | |
147 | DataURIResourceProviderProxy::DataURIResourceProviderProxy(sk_sp<ResourceProvider> rp, |
148 | bool predecode) |
149 | : INHERITED(std::move(rp)) |
150 | , fPredecode(predecode) {} |
151 | |
152 | sk_sp<ImageAsset> DataURIResourceProviderProxy::loadImageAsset(const char rpath[], |
153 | const char rname[], |
154 | const char rid[]) const { |
155 | // We only handle B64 encoded image dataURIs: data:image/<type>;base64,<data> |
156 | // (https://en.wikipedia.org/wiki/Data_URI_scheme) |
157 | static constexpr char kDataURIImagePrefix[] = "data:image/" , |
158 | kDataURIEncodingStr[] = ";base64," ; |
159 | |
160 | if (!strncmp(rname, kDataURIImagePrefix, SK_ARRAY_COUNT(kDataURIImagePrefix) - 1)) { |
161 | const char* encoding_start = strstr(rname + SK_ARRAY_COUNT(kDataURIImagePrefix) - 1, |
162 | kDataURIEncodingStr); |
163 | if (encoding_start) { |
164 | const char* data_start = encoding_start + SK_ARRAY_COUNT(kDataURIEncodingStr) - 1; |
165 | |
166 | // TODO: SkBase64::decode ergonomics are... interesting. |
167 | SkBase64 b64; |
168 | if (SkBase64::kNoError == b64.decode(data_start, strlen(data_start))) { |
169 | return MultiFrameImageAsset::Make(SkData::MakeWithProc(b64.getData(), |
170 | b64.getDataSize(), |
171 | [](const void* ptr, void*) { |
172 | delete[] static_cast<const char*>(ptr); |
173 | }, /*ctx=*/nullptr), |
174 | fPredecode); |
175 | } |
176 | } |
177 | } |
178 | |
179 | return this->INHERITED::loadImageAsset(rpath, rname, rid); |
180 | } |
181 | |
182 | } // namespace skresources |
183 | |