• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 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 #include "src/codec/SkHeifCodec.h"
8 
9 #include "include/codec/SkCodec.h"
10 #include "include/codec/SkEncodedImageFormat.h"
11 #include "include/core/SkStream.h"
12 #include "include/core/SkTypes.h"
13 #include "include/private/SkColorData.h"
14 #include "include/private/base/SkTemplates.h"
15 #include "src/base/SkEndian.h"
16 #include "src/codec/SkCodecPriv.h"
17 
18 #if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32__)
19 #include <dlfcn.h>
20 #endif
21 
22 #define FOURCC(c1, c2, c3, c4) \
23     ((c1) << 24 | (c2) << 16 | (c3) << 8 | (c4))
24 
25 const static char *HEIF_IMPL_SO_NAME = "libheifimpl.z.so";
26 
IsSupported(const void * buffer,size_t bytesRead,SkEncodedImageFormat * format)27 bool SkHeifCodec::IsSupported(const void* buffer, size_t bytesRead,
28                               SkEncodedImageFormat* format) {
29     // Parse the ftyp box up to bytesRead to determine if this is HEIF or AVIF.
30     // Any valid ftyp box should have at least 8 bytes.
31     if (bytesRead < 8) {
32         return false;
33     }
34 
35     const uint32_t* ptr = (const uint32_t*)buffer;
36     uint64_t chunkSize = SkEndian_SwapBE32(ptr[0]);
37     uint32_t chunkType = SkEndian_SwapBE32(ptr[1]);
38 
39     if (chunkType != FOURCC('f', 't', 'y', 'p')) {
40         return false;
41     }
42 
43     int64_t offset = 8;
44     if (chunkSize == 1) {
45         // This indicates that the next 8 bytes represent the chunk size,
46         // and chunk data comes after that.
47         if (bytesRead < 16) {
48             return false;
49         }
50         auto* chunkSizePtr = SkTAddOffset<const uint64_t>(buffer, offset);
51         chunkSize = SkEndian_SwapBE64(*chunkSizePtr);
52         if (chunkSize < 16) {
53             // The smallest valid chunk is 16 bytes long in this case.
54             return false;
55         }
56         offset += 8;
57     } else if (chunkSize < 8) {
58         // The smallest valid chunk is 8 bytes long.
59         return false;
60     }
61 
62     if (chunkSize > bytesRead) {
63         chunkSize = bytesRead;
64     }
65     int64_t chunkDataSize = chunkSize - offset;
66     // It should at least have major brand (4-byte) and minor version (4-bytes).
67     // The rest of the chunk (if any) is a list of (4-byte) compatible brands.
68     if (chunkDataSize < 8) {
69         return false;
70     }
71 
72     uint32_t numCompatibleBrands = (chunkDataSize - 8) / 4;
73     bool isHeif = false;
74     for (size_t i = 0; i < numCompatibleBrands + 2; ++i) {
75         if (i == 1) {
76             // Skip this index, it refers to the minorVersion,
77             // not a brand.
78             continue;
79         }
80         auto* brandPtr = SkTAddOffset<const uint32_t>(buffer, offset + 4 * i);
81         uint32_t brand = SkEndian_SwapBE32(*brandPtr);
82         if (brand == FOURCC('m', 'i', 'f', '1') || brand == FOURCC('h', 'e', 'i', 'c')
83          || brand == FOURCC('m', 's', 'f', '1') || brand == FOURCC('h', 'e', 'v', 'c')
84          || brand == FOURCC('a', 'v', 'i', 'f') || brand == FOURCC('a', 'v', 'i', 's')) {
85             // AVIF files could have "mif1" as the major brand. So we cannot
86             // distinguish whether the image is AVIF or HEIC just based on the
87             // "mif1" brand. So wait until we see a specific avif brand to
88             // determine whether it is AVIF or HEIC.
89             isHeif = true;
90             if (brand == FOURCC('a', 'v', 'i', 'f')
91               || brand == FOURCC('a', 'v', 'i', 's')) {
92                 if (format != nullptr) {
93                     *format = SkEncodedImageFormat::kAVIF;
94                 }
95                 return true;
96             }
97         }
98     }
99     if (isHeif) {
100         if (format != nullptr) {
101             *format = SkEncodedImageFormat::kHEIF;
102         }
103         return true;
104     }
105     return false;
106 }
107 
get_orientation(const HeifFrameInfo & frameInfo)108 static SkEncodedOrigin get_orientation(const HeifFrameInfo& frameInfo) {
109     switch (frameInfo.mRotationAngle) {
110         case 0:   return kTopLeft_SkEncodedOrigin;
111         case 90:  return kRightTop_SkEncodedOrigin;
112         case 180: return kBottomRight_SkEncodedOrigin;
113         case 270: return kLeftBottom_SkEncodedOrigin;
114     }
115     return kDefault_SkEncodedOrigin;
116 }
117 
118 struct SkHeifStreamWrapper : public HeifStream {
SkHeifStreamWrapperSkHeifStreamWrapper119     SkHeifStreamWrapper(SkStream* stream) : fStream(stream) {}
120 
~SkHeifStreamWrapperSkHeifStreamWrapper121     ~SkHeifStreamWrapper() override {}
122 
readSkHeifStreamWrapper123     size_t read(void* buffer, size_t size) override {
124         return fStream->read(buffer, size);
125     }
126 
rewindSkHeifStreamWrapper127     bool rewind() override {
128         return fStream->rewind();
129     }
130 
seekSkHeifStreamWrapper131     bool seek(size_t position) override {
132         return fStream->seek(position);
133     }
134 
hasLengthSkHeifStreamWrapper135     bool hasLength() const override {
136         return fStream->hasLength();
137     }
138 
getLengthSkHeifStreamWrapper139     size_t getLength() const override {
140         return fStream->getLength();
141     }
142 
hasPositionSkHeifStreamWrapper143     bool hasPosition() const override {
144         return fStream->hasPosition();
145     }
146 
getPositionSkHeifStreamWrapper147     size_t getPosition() const override {
148         return fStream->getPosition();
149     }
150 
151 private:
152     std::unique_ptr<SkStream> fStream;
153 };
154 
releaseProc(const void * ptr,void * context)155 static void releaseProc(const void* ptr, void* context) {
156     delete reinterpret_cast<std::vector<uint8_t>*>(context);
157 }
158 
MakeFromStream(std::unique_ptr<SkStream> stream,SkCodec::SelectionPolicy selectionPolicy,Result * result)159 std::unique_ptr<SkCodec> SkHeifCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
160         SkCodec::SelectionPolicy selectionPolicy, Result* result) {
161     SkASSERT(result);
162     if (!stream) {
163         *result = SkCodec::kInvalidInput;
164         return nullptr;
165     }
166     std::unique_ptr<HeifDecoder> heifDecoder(createHeifDecoder());
167     if (heifDecoder == nullptr) {
168         *result = SkCodec::kInternalError;
169         return nullptr;
170     }
171 
172     constexpr size_t bytesToRead = MinBufferedBytesNeeded();
173     char buffer[bytesToRead];
174     size_t bytesRead = stream->peek(buffer, bytesToRead);
175     if (0 == bytesRead) {
176         // It is possible the stream does not support peeking, but does support rewinding.
177         // Attempt to read() and pass the actual amount read to the decoder.
178         bytesRead = stream->read(buffer, bytesToRead);
179         if (!stream->rewind()) {
180             SkCodecPrintf("Encoded image data could not peek or rewind to determine format!\n");
181             *result = kCouldNotRewind;
182             return nullptr;
183         }
184     }
185 
186     SkEncodedImageFormat format;
187     if (!SkHeifCodec::IsSupported(buffer, bytesRead, &format)) {
188         SkCodecPrintf("Failed to get format despite earlier detecting it");
189         *result = SkCodec::kInternalError;
190         return nullptr;
191     }
192 
193     HeifFrameInfo heifInfo;
194     if (!heifDecoder->init(new SkHeifStreamWrapper(stream.release()), &heifInfo)) {
195         *result = SkCodec::kInvalidInput;
196         return nullptr;
197     }
198 
199     size_t frameCount = 1;
200     if (selectionPolicy == SkCodec::SelectionPolicy::kPreferAnimation) {
201         HeifFrameInfo sequenceInfo;
202         if (heifDecoder->getSequenceInfo(&sequenceInfo, &frameCount) &&
203                 frameCount > 1) {
204             heifInfo = std::move(sequenceInfo);
205         }
206     }
207 
208     std::unique_ptr<SkEncodedInfo::ICCProfile> profile = nullptr;
209     if (heifInfo.mIccData.size() > 0) {
210         auto iccData = new std::vector<uint8_t>(std::move(heifInfo.mIccData));
211         auto icc = SkData::MakeWithProc(iccData->data(), iccData->size(), releaseProc, iccData);
212         profile = SkEncodedInfo::ICCProfile::Make(std::move(icc));
213     }
214     if (profile && profile->profile()->data_color_space != skcms_Signature_RGB) {
215         // This will result in sRGB.
216         profile = nullptr;
217     }
218 
219     uint8_t colorDepth = heifDecoder->getColorDepth();
220 
221     SkEncodedInfo info = SkEncodedInfo::Make(heifInfo.mWidth, heifInfo.mHeight,
222             SkEncodedInfo::kYUV_Color, SkEncodedInfo::kOpaque_Alpha,
223             /*bitsPerComponent*/ 8, std::move(profile), colorDepth);
224     SkEncodedOrigin orientation = get_orientation(heifInfo);
225 
226     *result = SkCodec::kSuccess;
227     return std::unique_ptr<SkCodec>(new SkHeifCodec(
228             std::move(info), heifDecoder.release(), orientation, frameCount > 1, format));
229 }
230 
SkHeifCodec(SkEncodedInfo && info,HeifDecoder * heifDecoder,SkEncodedOrigin origin,bool useAnimation,SkEncodedImageFormat format)231 SkHeifCodec::SkHeifCodec(
232         SkEncodedInfo&& info,
233         HeifDecoder* heifDecoder,
234         SkEncodedOrigin origin,
235         bool useAnimation,
236         SkEncodedImageFormat format)
237     : INHERITED(std::move(info), skcms_PixelFormat_RGBA_8888, nullptr, origin)
238     , fHeifDecoder(heifDecoder)
239     , fSwizzleSrcRow(nullptr)
240     , fColorXformSrcRow(nullptr)
241     , fUseAnimation(useAnimation)
242     , fFormat(format)
243 {}
244 
conversionSupported(const SkImageInfo & dstInfo,bool srcIsOpaque,bool needsColorXform)245 bool SkHeifCodec::conversionSupported(const SkImageInfo& dstInfo, bool srcIsOpaque,
246                                       bool needsColorXform) {
247     SkASSERT(srcIsOpaque);
248 
249     if (kUnknown_SkAlphaType == dstInfo.alphaType()) {
250         return false;
251     }
252 
253     if (kOpaque_SkAlphaType != dstInfo.alphaType()) {
254         SkCodecPrintf("Warning: an opaque image should be decoded as opaque "
255                 "- it is being decoded as non-opaque, which will draw slower\n");
256     }
257 
258     uint8_t colorDepth = fHeifDecoder->getColorDepth();
259     switch (dstInfo.colorType()) {
260         case kRGBA_8888_SkColorType:
261             this->setSrcXformFormat(skcms_PixelFormat_RGBA_8888);
262             return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888);
263 
264         case kBGRA_8888_SkColorType:
265             this->setSrcXformFormat(skcms_PixelFormat_RGBA_8888);
266             return fHeifDecoder->setOutputColor(kHeifColorFormat_BGRA_8888);
267 
268         case kRGB_565_SkColorType:
269             this->setSrcXformFormat(skcms_PixelFormat_RGBA_8888);
270             if (needsColorXform) {
271                 return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888);
272             } else {
273                 return fHeifDecoder->setOutputColor(kHeifColorFormat_RGB565);
274             }
275 
276         case kRGBA_1010102_SkColorType:
277             this->setSrcXformFormat(skcms_PixelFormat_RGBA_1010102);
278             return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_1010102);
279 
280         case kRGBA_F16_SkColorType:
281             SkASSERT(needsColorXform);
282             if (srcIsOpaque && colorDepth == 10) {
283                 this->setSrcXformFormat(skcms_PixelFormat_RGBA_1010102);
284                 return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_1010102);
285             } else {
286                 this->setSrcXformFormat(skcms_PixelFormat_RGBA_8888);
287                 return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888);
288             }
289 
290         default:
291             return false;
292     }
293 }
294 
readRows(const SkImageInfo & dstInfo,void * dst,size_t rowBytes,int count,const Options & opts)295 int SkHeifCodec::readRows(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, int count,
296                           const Options& opts) {
297     // When fSwizzleSrcRow is non-null, it means that we need to swizzle.  In this case,
298     // we will always decode into fSwizzlerSrcRow before swizzling into the next buffer.
299     // We can never swizzle "in place" because the swizzler may perform sampling and/or
300     // subsetting.
301     // When fColorXformSrcRow is non-null, it means that we need to color xform and that
302     // we cannot color xform "in place" (many times we can, but not when the dst is F16).
303     // In this case, we will color xform from fColorXformSrcRow into the dst.
304     uint8_t* decodeDst = (uint8_t*) dst;
305     uint32_t* swizzleDst = (uint32_t*) dst;
306     size_t decodeDstRowBytes = rowBytes;
307     size_t swizzleDstRowBytes = rowBytes;
308     int dstWidth = opts.fSubset ? opts.fSubset->width() : dstInfo.width();
309     if (fSwizzleSrcRow && fColorXformSrcRow) {
310         decodeDst = fSwizzleSrcRow;
311         swizzleDst = fColorXformSrcRow;
312         decodeDstRowBytes = 0;
313         swizzleDstRowBytes = 0;
314         dstWidth = fSwizzler->swizzleWidth();
315     } else if (fColorXformSrcRow) {
316         decodeDst = (uint8_t*) fColorXformSrcRow;
317         swizzleDst = fColorXformSrcRow;
318         decodeDstRowBytes = 0;
319         swizzleDstRowBytes = 0;
320     } else if (fSwizzleSrcRow) {
321         decodeDst = fSwizzleSrcRow;
322         decodeDstRowBytes = 0;
323         dstWidth = fSwizzler->swizzleWidth();
324     }
325 
326     for (int y = 0; y < count; y++) {
327         if (!fHeifDecoder->getScanline(decodeDst)) {
328             return y;
329         }
330 
331         if (fSwizzler) {
332             fSwizzler->swizzle(swizzleDst, decodeDst);
333         }
334 
335         if (this->colorXform()) {
336             this->applyColorXform(dst, swizzleDst, dstWidth);
337             dst = SkTAddOffset<void>(dst, rowBytes);
338         }
339 
340         decodeDst = SkTAddOffset<uint8_t>(decodeDst, decodeDstRowBytes);
341         swizzleDst = SkTAddOffset<uint32_t>(swizzleDst, swizzleDstRowBytes);
342     }
343 
344     return count;
345 }
346 
onGetFrameCount()347 int SkHeifCodec::onGetFrameCount() {
348     if (!fUseAnimation) {
349         return 1;
350     }
351 
352     if (fFrameHolder.size() == 0) {
353         size_t frameCount;
354         HeifFrameInfo frameInfo;
355         if (!fHeifDecoder->getSequenceInfo(&frameInfo, &frameCount)
356                 || frameCount <= 1) {
357             fUseAnimation = false;
358             return 1;
359         }
360         fFrameHolder.reserve(frameCount);
361         for (size_t i = 0; i < frameCount; i++) {
362             Frame* frame = fFrameHolder.appendNewFrame();
363             frame->setXYWH(0, 0, frameInfo.mWidth, frameInfo.mHeight);
364             frame->setDisposalMethod(SkCodecAnimation::DisposalMethod::kKeep);
365             // Currently we don't know the duration until the frame is actually
366             // decoded (onGetFrameInfo is also called before frame is decoded).
367             // For now, fill it base on the value reported for the sequence.
368             frame->setDuration(frameInfo.mDurationUs / 1000);
369             frame->setRequiredFrame(SkCodec::kNoFrame);
370             frame->setHasAlpha(false);
371         }
372     }
373 
374     return fFrameHolder.size();
375 }
376 
onGetFrame(int i) const377 const SkFrame* SkHeifCodec::FrameHolder::onGetFrame(int i) const {
378     return static_cast<const SkFrame*>(this->frame(i));
379 }
380 
appendNewFrame()381 SkHeifCodec::Frame* SkHeifCodec::FrameHolder::appendNewFrame() {
382     const int i = this->size();
383     fFrames.emplace_back(i); // TODO: need to handle frame duration here
384     return &fFrames[i];
385 }
386 
frame(int i) const387 const SkHeifCodec::Frame* SkHeifCodec::FrameHolder::frame(int i) const {
388     SkASSERT(i >= 0 && i < this->size());
389     return &fFrames[i];
390 }
391 
editFrameAt(int i)392 SkHeifCodec::Frame* SkHeifCodec::FrameHolder::editFrameAt(int i) {
393     SkASSERT(i >= 0 && i < this->size());
394     return &fFrames[i];
395 }
396 
onGetFrameInfo(int i,FrameInfo * frameInfo) const397 bool SkHeifCodec::onGetFrameInfo(int i, FrameInfo* frameInfo) const {
398     if (i >= fFrameHolder.size()) {
399         return false;
400     }
401 
402     const Frame* frame = fFrameHolder.frame(i);
403     if (!frame) {
404         return false;
405     }
406 
407     if (frameInfo) {
408         frame->fillIn(frameInfo, true);
409     }
410 
411     return true;
412 }
413 
onGetRepetitionCount()414 int SkHeifCodec::onGetRepetitionCount() {
415     return kRepetitionCountInfinite;
416 }
417 
418 /*
419  * Performs the heif decode
420  */
onGetPixels(const SkImageInfo & dstInfo,void * dst,size_t dstRowBytes,const Options & options,int * rowsDecoded)421 SkCodec::Result SkHeifCodec::onGetPixels(const SkImageInfo& dstInfo,
422                                          void* dst, size_t dstRowBytes,
423                                          const Options& options,
424                                          int* rowsDecoded) {
425     if (options.fSubset) {
426         // Not supporting subsets on this path for now.
427         // TODO: if the heif has tiles, we can support subset here, but
428         // need to retrieve tile config from metadata retriever first.
429         return kUnimplemented;
430     }
431 
432     bool success;
433     if (fUseAnimation) {
434         success = fHeifDecoder->decodeSequence(options.fFrameIndex, &fFrameInfo);
435         fFrameHolder.editFrameAt(options.fFrameIndex)->setDuration(
436                 fFrameInfo.mDurationUs / 1000);
437     } else {
438         fHeifDecoder->setDstBuffer((uint8_t *)dst, dstRowBytes, nullptr);
439         success = fHeifDecoder->decode(&fFrameInfo);
440     }
441 
442     if (!success) {
443         return kInvalidInput;
444     }
445 
446     fSwizzler.reset(nullptr);
447     this->allocateStorage(dstInfo);
448 
449     int rows = this->readRows(dstInfo, dst, dstRowBytes, dstInfo.height(), options);
450     if (rows < dstInfo.height()) {
451         *rowsDecoded = rows;
452         return kIncompleteInput;
453     }
454 
455     return kSuccess;
456 }
457 
allocateStorage(const SkImageInfo & dstInfo)458 void SkHeifCodec::allocateStorage(const SkImageInfo& dstInfo) {
459     int dstWidth = dstInfo.width();
460 
461     size_t swizzleBytes = 0;
462     if (fSwizzler) {
463         swizzleBytes = fFrameInfo.mBytesPerPixel * fFrameInfo.mWidth;
464         dstWidth = fSwizzler->swizzleWidth();
465         SkASSERT(!this->colorXform() || SkIsAlign4(swizzleBytes));
466     }
467 
468     size_t xformBytes = 0;
469     if (this->colorXform() && (kRGBA_F16_SkColorType == dstInfo.colorType() ||
470                                kRGB_565_SkColorType == dstInfo.colorType())) {
471         xformBytes = dstWidth * sizeof(uint32_t);
472     }
473 
474     size_t totalBytes = swizzleBytes + xformBytes;
475     fStorage.reset(totalBytes);
476     if (totalBytes > 0) {
477         fSwizzleSrcRow = (swizzleBytes > 0) ? fStorage.get() : nullptr;
478         fColorXformSrcRow = (xformBytes > 0) ?
479                 SkTAddOffset<uint32_t>(fStorage.get(), swizzleBytes) : nullptr;
480     }
481 }
482 
initializeSwizzler(const SkImageInfo & dstInfo,const Options & options)483 void SkHeifCodec::initializeSwizzler(
484         const SkImageInfo& dstInfo, const Options& options) {
485     SkImageInfo swizzlerDstInfo = dstInfo;
486     switch (this->getSrcXformFormat()) {
487         case skcms_PixelFormat_RGBA_8888:
488             swizzlerDstInfo = swizzlerDstInfo.makeColorType(kRGBA_8888_SkColorType);
489             break;
490         case skcms_PixelFormat_RGBA_1010102:
491             swizzlerDstInfo = swizzlerDstInfo.makeColorType(kRGBA_1010102_SkColorType);
492             break;
493         default:
494             SkASSERT(false);
495     }
496 
497     int srcBPP = 4;
498     if (dstInfo.colorType() == kRGB_565_SkColorType && !this->colorXform()) {
499         srcBPP = 2;
500     }
501 
502     fSwizzler = SkSwizzler::MakeSimple(srcBPP, swizzlerDstInfo, options);
503     SkASSERT(fSwizzler);
504 }
505 
getSampler(bool createIfNecessary)506 SkSampler* SkHeifCodec::getSampler(bool createIfNecessary) {
507     if (!createIfNecessary || fSwizzler) {
508         SkASSERT(!fSwizzler || (fSwizzleSrcRow && fStorage.get() == fSwizzleSrcRow));
509         return fSwizzler.get();
510     }
511 
512     this->initializeSwizzler(this->dstInfo(), this->options());
513     this->allocateStorage(this->dstInfo());
514     return fSwizzler.get();
515 }
516 
onRewind()517 bool SkHeifCodec::onRewind() {
518     fSwizzler.reset(nullptr);
519     fSwizzleSrcRow = nullptr;
520     fColorXformSrcRow = nullptr;
521     fStorage.reset();
522 
523     return true;
524 }
525 
onStartScanlineDecode(const SkImageInfo & dstInfo,const Options & options)526 SkCodec::Result SkHeifCodec::onStartScanlineDecode(
527         const SkImageInfo& dstInfo, const Options& options) {
528     // TODO: For now, just decode the whole thing even when there is a subset.
529     // If the heif image has tiles, we could potentially do this much faster,
530     // but the tile configuration needs to be retrieved from the metadata.
531     if (!fHeifDecoder->decode(&fFrameInfo)) {
532         return kInvalidInput;
533     }
534 
535     if (options.fSubset) {
536         this->initializeSwizzler(dstInfo, options);
537     } else {
538         fSwizzler.reset(nullptr);
539     }
540 
541     this->allocateStorage(dstInfo);
542 
543     return kSuccess;
544 }
545 
onGetScanlines(void * dst,int count,size_t dstRowBytes)546 int SkHeifCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
547     return this->readRows(this->dstInfo(), dst, dstRowBytes, count, this->options());
548 }
549 
onSkipScanlines(int count)550 bool SkHeifCodec::onSkipScanlines(int count) {
551     return count == (int) fHeifDecoder->skipScanlines(count);
552 }
553 
554 void *SkHeifCodec::heifImplHandle = nullptr;
createHeifDecoder()555 HeifDecoder* SkHeifCodec::createHeifDecoder() {
556 #if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32__)
557     if (!heifImplHandle) {
558         heifImplHandle = dlopen(HEIF_IMPL_SO_NAME, RTLD_LAZY);
559     }
560     if (!heifImplHandle) {
561         return new StubHeifDecoder();
562     }
563     typedef HeifDecoder* (*CreateHeifDecoderImplT)();
564     HeifDecoder* decoder = nullptr;
565     CreateHeifDecoderImplT func = (CreateHeifDecoderImplT)dlsym(heifImplHandle, "CreateHeifDecoderImpl");
566     if (func) {
567         decoder = func();
568     }
569     return decoder != nullptr ? decoder : new StubHeifDecoder();
570 #else
571     return new StubHeifDecoder();
572 #endif
573 }
574 
575 namespace SkHeifDecoder {
IsHeif(const void * data,size_t len)576 bool IsHeif(const void* data, size_t len) {
577     return SkHeifCodec::IsSupported(data, len, nullptr);
578 }
579 
Decode(std::unique_ptr<SkStream> stream,SkCodec::Result * outResult,SkCodecs::DecodeContext ctx)580 std::unique_ptr<SkCodec> Decode(std::unique_ptr<SkStream> stream,
581                                 SkCodec::Result* outResult,
582                                 SkCodecs::DecodeContext ctx) {
583     SkASSERT(ctx);
584     SkCodec::Result resultStorage;
585     if (!outResult) {
586         outResult = &resultStorage;
587     }
588     auto policy = static_cast<SkCodec::SelectionPolicy*>(ctx);
589     return SkHeifCodec::MakeFromStream(std::move(stream), *policy, outResult);
590 }
591 
Decode(sk_sp<SkData> data,SkCodec::Result * outResult,SkCodecs::DecodeContext ctx)592 std::unique_ptr<SkCodec> Decode(sk_sp<SkData> data,
593                                 SkCodec::Result* outResult,
594                                 SkCodecs::DecodeContext ctx) {
595     if (!data) {
596         if (outResult) {
597             *outResult = SkCodec::kInvalidInput;
598         }
599         return nullptr;
600     }
601     return Decode(SkMemoryStream::Make(std::move(data)), outResult, ctx);
602 }
603 }  // namespace SkHeifDecoder
604 
605