• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkBitmap.h"
12 #include "include/core/SkData.h"
13 #include "include/core/SkImage.h"
14 #include "include/private/SkTPin.h"
15 #include "include/utils/SkAnimCodecPlayer.h"
16 #include "include/utils/SkBase64.h"
17 #include "src/core/SkOSFile.h"
18 #include "src/utils/SkOSPath.h"
19 
20 #if defined(HAVE_VIDEO_DECODER)
21     #include "experimental/ffmpeg/SkVideoDecoder.h"
22 #endif
23 
24 namespace skresources {
25 namespace  {
26 
27 #if defined(HAVE_VIDEO_DECODER)
28 
29 class VideoAsset final : public ImageAsset {
30 public:
Make(sk_sp<SkData> data)31     static sk_sp<VideoAsset> Make(sk_sp<SkData> data) {
32         auto decoder = std::make_unique<SkVideoDecoder>();
33 
34         if (!decoder->loadStream(SkMemoryStream::Make(std::move(data))) ||
35             decoder->duration() <= 0) {
36             return nullptr;
37         }
38 
39         return sk_sp<VideoAsset>(new VideoAsset(std::move(decoder)));
40     }
41 
42 private:
VideoAsset(std::unique_ptr<SkVideoDecoder> decoder)43     explicit VideoAsset(std::unique_ptr<SkVideoDecoder> decoder)
44         : fDecoder(std::move(decoder)) {
45     }
46 
isMultiFrame()47     bool isMultiFrame() override { return true; }
48 
49     // Each frame has a presentation timestamp
50     //   => the timespan for frame N is [stamp_N .. stamp_N+1)
51     //   => we use a two-frame sliding window to track the current interval.
advance()52     void advance() {
53         fWindow[0] = std::move(fWindow[1]);
54         fWindow[1].frame = fDecoder->nextImage(&fWindow[1].stamp);
55         fEof = !fWindow[1].frame;
56     }
57 
getFrame(float t_float)58     sk_sp<SkImage> getFrame(float t_float) override {
59         const auto t = SkTPin(static_cast<double>(t_float), 0.0, fDecoder->duration());
60 
61         if (t < fWindow[0].stamp) {
62             // seeking back requires a full rewind
63             fDecoder->rewind();
64             fWindow[0].stamp = fWindow[1].stamp = 0;
65             fEof = 0;
66         }
67 
68         while (!fEof && t >= fWindow[1].stamp) {
69             this->advance();
70         }
71 
72         SkASSERT(fWindow[0].stamp <= t && (fEof || t < fWindow[1].stamp));
73 
74         return fWindow[0].frame;
75     }
76 
77     const std::unique_ptr<SkVideoDecoder> fDecoder;
78 
79     struct FrameRec {
80         sk_sp<SkImage> frame;
81         double         stamp = 0;
82     };
83 
84     FrameRec fWindow[2];
85     bool     fEof = false;
86 };
87 
88 #endif // defined(HAVE_VIDEO_DECODER)
89 
90 } // namespace
91 
getFrame(float t)92 sk_sp<SkImage> ImageAsset::getFrame(float t) {
93     return nullptr;
94 }
95 
getFrameData(float t)96 ImageAsset::FrameData ImageAsset::getFrameData(float t) {
97     // legacy behavior
98     return {
99         this->getFrame(t),
100         SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kNearest),
101         SkMatrix::I(),
102     };
103 }
104 
Make(sk_sp<SkData> data,bool predecode)105 sk_sp<MultiFrameImageAsset> MultiFrameImageAsset::Make(sk_sp<SkData> data, bool predecode) {
106     if (auto codec = SkCodec::MakeFromData(std::move(data))) {
107         return sk_sp<MultiFrameImageAsset>(
108               new MultiFrameImageAsset(std::make_unique<SkAnimCodecPlayer>(std::move(codec)),
109                                                                              predecode));
110     }
111 
112     return nullptr;
113 }
114 
MultiFrameImageAsset(std::unique_ptr<SkAnimCodecPlayer> player,bool predecode)115 MultiFrameImageAsset::MultiFrameImageAsset(std::unique_ptr<SkAnimCodecPlayer> player,
116                                            bool predecode)
117     : fPlayer(std::move(player))
118     , fPreDecode(predecode) {
119     SkASSERT(fPlayer);
120 }
121 
isMultiFrame()122 bool MultiFrameImageAsset::isMultiFrame() {
123     return fPlayer->duration() > 0;
124 }
125 
generateFrame(float t)126 sk_sp<SkImage> MultiFrameImageAsset::generateFrame(float t) {
127     auto decode = [](sk_sp<SkImage> image) {
128         SkASSERT(image->isLazyGenerated());
129 
130         static constexpr size_t kMaxArea = 2048 * 2048;
131         const auto image_area = SkToSizeT(image->width() * image->height());
132 
133         if (image_area > kMaxArea) {
134             // When the image is too large, decode and scale down to a reasonable size.
135             const auto scale = std::sqrt(static_cast<float>(kMaxArea) / image_area);
136             const auto info  = SkImageInfo::MakeN32Premul(scale * image->width(),
137                                                           scale * image->height());
138             SkBitmap bm;
139             if (bm.tryAllocPixels(info, info.minRowBytes()) &&
140                     image->scalePixels(bm.pixmap(),
141                                        SkSamplingOptions(SkFilterMode::kLinear,
142                                                          SkMipmapMode::kNearest),
143                                        SkImage::kDisallow_CachingHint)) {
144                 image = bm.asImage();
145             }
146         } else {
147             // When the image size is OK, just force-decode.
148             image = image->makeRasterImage();
149         }
150 
151         return image;
152     };
153 
154     fPlayer->seek(static_cast<uint32_t>(t * 1000));
155     auto frame = fPlayer->getFrame();
156 
157     if (fPreDecode && frame && frame->isLazyGenerated()) {
158         // The multi-frame decoder should never return lazy images.
159         SkASSERT(!this->isMultiFrame());
160         frame = decode(std::move(frame));
161     }
162 
163     return frame;
164 }
165 
getFrame(float t)166 sk_sp<SkImage> MultiFrameImageAsset::getFrame(float t) {
167     // For static images we can reuse the cached frame
168     // (which includes the optional pre-decode step).
169     if (!fCachedFrame || this->isMultiFrame()) {
170         fCachedFrame = this->generateFrame(t);
171     }
172 
173     return fCachedFrame;
174 }
175 
Make(SkString base_dir,bool predecode)176 sk_sp<FileResourceProvider> FileResourceProvider::Make(SkString base_dir, bool predecode) {
177     return sk_isdir(base_dir.c_str())
178         ? sk_sp<FileResourceProvider>(new FileResourceProvider(std::move(base_dir), predecode))
179         : nullptr;
180 }
181 
FileResourceProvider(SkString base_dir,bool predecode)182 FileResourceProvider::FileResourceProvider(SkString base_dir, bool predecode)
183     : fDir(std::move(base_dir))
184     , fPredecode(predecode) {}
185 
load(const char resource_path[],const char resource_name[]) const186 sk_sp<SkData> FileResourceProvider::load(const char resource_path[],
187                                          const char resource_name[]) const {
188     const auto full_dir  = SkOSPath::Join(fDir.c_str()    , resource_path),
189                full_path = SkOSPath::Join(full_dir.c_str(), resource_name);
190     return SkData::MakeFromFileName(full_path.c_str());
191 }
192 
loadImageAsset(const char resource_path[],const char resource_name[],const char[]) const193 sk_sp<ImageAsset> FileResourceProvider::loadImageAsset(const char resource_path[],
194                                                        const char resource_name[],
195                                                        const char[]) const {
196     auto data = this->load(resource_path, resource_name);
197 
198     if (auto image = MultiFrameImageAsset::Make(data, fPredecode)) {
199         return std::move(image);
200     }
201 
202 #if defined(HAVE_VIDEO_DECODER)
203     if (auto video = VideoAsset::Make(data)) {
204         return std::move(video);
205     }
206 #endif
207 
208     return nullptr;
209 }
210 
ResourceProviderProxyBase(sk_sp<ResourceProvider> rp)211 ResourceProviderProxyBase::ResourceProviderProxyBase(sk_sp<ResourceProvider> rp)
212     : fProxy(std::move(rp)) {}
213 
load(const char resource_path[],const char resource_name[]) const214 sk_sp<SkData> ResourceProviderProxyBase::load(const char resource_path[],
215                                               const char resource_name[]) const {
216     return fProxy ? fProxy->load(resource_path, resource_name)
217                   : nullptr;
218 }
219 
loadImageAsset(const char rpath[],const char rname[],const char rid[]) const220 sk_sp<ImageAsset> ResourceProviderProxyBase::loadImageAsset(const char rpath[],
221                                                             const char rname[],
222                                                             const char rid[]) const {
223     return fProxy ? fProxy->loadImageAsset(rpath, rname, rid)
224                   : nullptr;
225 }
226 
loadTypeface(const char name[],const char url[]) const227 sk_sp<SkTypeface> ResourceProviderProxyBase::loadTypeface(const char name[],
228                                                           const char url[]) const {
229     return fProxy ? fProxy->loadTypeface(name, url)
230                   : nullptr;
231 }
232 
loadFont(const char name[],const char url[]) const233 sk_sp<SkData> ResourceProviderProxyBase::loadFont(const char name[], const char url[]) const {
234     return fProxy ? fProxy->loadFont(name, url)
235                   : nullptr;
236 }
237 
loadAudioAsset(const char path[],const char name[],const char id[])238 sk_sp<ExternalTrackAsset> ResourceProviderProxyBase::loadAudioAsset(const char path[],
239                                                                     const char name[],
240                                                                     const char id[]) {
241     return fProxy ? fProxy->loadAudioAsset(path, name, id)
242                   : nullptr;
243 }
244 
CachingResourceProvider(sk_sp<ResourceProvider> rp)245 CachingResourceProvider::CachingResourceProvider(sk_sp<ResourceProvider> rp)
246     : INHERITED(std::move(rp)) {}
247 
loadImageAsset(const char resource_path[],const char resource_name[],const char resource_id[]) const248 sk_sp<ImageAsset> CachingResourceProvider::loadImageAsset(const char resource_path[],
249                                                           const char resource_name[],
250                                                           const char resource_id[]) const {
251     SkAutoMutexExclusive amx(fMutex);
252 
253     const SkString key(resource_id);
254     if (const auto* asset = fImageCache.find(key)) {
255         return *asset;
256     }
257 
258     auto asset = this->INHERITED::loadImageAsset(resource_path, resource_name, resource_id);
259     fImageCache.set(key, asset);
260 
261     return asset;
262 }
263 
Make(sk_sp<ResourceProvider> rp,bool predecode)264 sk_sp<DataURIResourceProviderProxy> DataURIResourceProviderProxy::Make(sk_sp<ResourceProvider> rp,
265                                                                        bool predecode) {
266     return sk_sp<DataURIResourceProviderProxy>(
267             new DataURIResourceProviderProxy(std::move(rp), predecode));
268 }
269 
DataURIResourceProviderProxy(sk_sp<ResourceProvider> rp,bool predecode)270 DataURIResourceProviderProxy::DataURIResourceProviderProxy(sk_sp<ResourceProvider> rp,
271                                                            bool predecode)
272     : INHERITED(std::move(rp))
273     , fPredecode(predecode) {}
274 
decode_datauri(const char prefix[],const char uri[])275 static sk_sp<SkData> decode_datauri(const char prefix[], const char uri[]) {
276     // We only handle B64 encoded image dataURIs: data:image/<type>;base64,<data>
277     // (https://en.wikipedia.org/wiki/Data_URI_scheme)
278     static constexpr char kDataURIEncodingStr[] = ";base64,";
279 
280     const size_t prefixLen = strlen(prefix);
281     if (strncmp(uri, prefix, prefixLen) != 0) {
282         return nullptr;
283     }
284 
285     const char* encoding = strstr(uri + prefixLen, kDataURIEncodingStr);
286     if (!encoding) {
287         return nullptr;
288     }
289 
290     const char* b64Data = encoding + SK_ARRAY_COUNT(kDataURIEncodingStr) - 1;
291     size_t b64DataLen = strlen(b64Data);
292     size_t dataLen;
293     if (SkBase64::Decode(b64Data, b64DataLen, nullptr, &dataLen) != SkBase64::kNoError) {
294         return nullptr;
295     }
296 
297     sk_sp<SkData> data = SkData::MakeUninitialized(dataLen);
298     void* rawData = data->writable_data();
299     if (SkBase64::Decode(b64Data, b64DataLen, rawData, &dataLen) != SkBase64::kNoError) {
300         return nullptr;
301     }
302 
303     return data;
304 }
305 
loadImageAsset(const char rpath[],const char rname[],const char rid[]) const306 sk_sp<ImageAsset> DataURIResourceProviderProxy::loadImageAsset(const char rpath[],
307                                                                const char rname[],
308                                                                const char rid[]) const {
309     if (auto data = decode_datauri("data:image/", rname)) {
310         return MultiFrameImageAsset::Make(std::move(data), fPredecode);
311     }
312 
313     return this->INHERITED::loadImageAsset(rpath, rname, rid);
314 }
315 
loadTypeface(const char name[],const char url[]) const316 sk_sp<SkTypeface> DataURIResourceProviderProxy::loadTypeface(const char name[],
317                                                              const char url[]) const {
318     if (auto data = decode_datauri("data:font/", url)) {
319         return SkTypeface::MakeFromData(std::move(data));
320     }
321 
322     return this->INHERITED::loadTypeface(name, url);
323 }
324 
325 } // namespace skresources
326