1 /* 2 * Copyright 2015 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 #ifndef SkWebpCodec_DEFINED 9 #define SkWebpCodec_DEFINED 10 11 #include "SkCodec.h" 12 #include "SkEncodedImageFormat.h" 13 #include "SkFrameHolder.h" 14 #include "SkImageInfo.h" 15 #include "SkTypes.h" 16 17 #include <vector> 18 19 class SkStream; 20 extern "C" { 21 struct WebPDemuxer; 22 void WebPDemuxDelete(WebPDemuxer* dmux); 23 } 24 25 class SkWebpCodec final : public SkCodec { 26 public: 27 // Assumes IsWebp was called and returned true. 28 static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result*); 29 static bool IsWebp(const void*, size_t); 30 protected: 31 Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, int*) override; onGetEncodedFormat()32 SkEncodedImageFormat onGetEncodedFormat() const override { return SkEncodedImageFormat::kWEBP; } 33 34 SkISize onGetScaledDimensions(float desiredScale) const override; 35 36 bool onDimensionsSupported(const SkISize&) override; 37 38 bool onGetValidSubset(SkIRect* /* desiredSubset */) const override; 39 40 int onGetFrameCount() override; 41 bool onGetFrameInfo(int, FrameInfo*) const override; 42 int onGetRepetitionCount() override; 43 getFrameHolder()44 const SkFrameHolder* getFrameHolder() const override { 45 return &fFrameHolder; 46 } 47 48 private: 49 SkWebpCodec(SkEncodedInfo&&, std::unique_ptr<SkStream>, WebPDemuxer*, sk_sp<SkData>, 50 SkEncodedOrigin); 51 52 SkAutoTCallVProc<WebPDemuxer, WebPDemuxDelete> fDemux; 53 54 // fDemux has a pointer into this data. 55 // This should not be freed until the decode is completed. 56 sk_sp<SkData> fData; 57 58 class Frame : public SkFrame { 59 public: Frame(int i,SkEncodedInfo::Alpha alpha)60 Frame(int i, SkEncodedInfo::Alpha alpha) 61 : INHERITED(i) 62 , fReportedAlpha(alpha) 63 {} 64 65 protected: onReportedAlpha()66 SkEncodedInfo::Alpha onReportedAlpha() const override { 67 return fReportedAlpha; 68 } 69 70 private: 71 const SkEncodedInfo::Alpha fReportedAlpha; 72 73 typedef SkFrame INHERITED; 74 }; 75 76 class FrameHolder : public SkFrameHolder { 77 public: ~FrameHolder()78 ~FrameHolder() override {} setScreenSize(int w,int h)79 void setScreenSize(int w, int h) { 80 fScreenWidth = w; 81 fScreenHeight = h; 82 } 83 Frame* appendNewFrame(bool hasAlpha); 84 const Frame* frame(int i) const; size()85 int size() const { 86 return static_cast<int>(fFrames.size()); 87 } reserve(int size)88 void reserve(int size) { 89 fFrames.reserve(size); 90 } 91 92 protected: 93 const SkFrame* onGetFrame(int i) const override; 94 95 private: 96 std::vector<Frame> fFrames; 97 }; 98 99 FrameHolder fFrameHolder; 100 // Set to true if WebPDemuxGetFrame fails. This only means 101 // that we will cap the frame count to the frames that 102 // succeed. 103 bool fFailed; 104 105 typedef SkCodec INHERITED; 106 }; 107 #endif // SkWebpCodec_DEFINED 108