• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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 "include/utils/SkAnimCodecPlayer.h"
9 
10 #include "include/codec/SkCodec.h"
11 #include "include/codec/SkEncodedOrigin.h"
12 #include "include/core/SkAlphaType.h"
13 #include "include/core/SkBlendMode.h"
14 #include "include/core/SkCanvas.h"
15 #include "include/core/SkData.h"
16 #include "include/core/SkImage.h"
17 #include "include/core/SkImageInfo.h"
18 #include "include/core/SkMatrix.h"
19 #include "include/core/SkPaint.h"
20 #include "include/core/SkRefCnt.h"
21 #include "include/core/SkSamplingOptions.h"
22 #include "include/core/SkSize.h"
23 #include "include/core/SkTypes.h"
24 #include "src/codec/SkCodecImageGenerator.h"
25 
26 #include <algorithm>
27 #include <cstddef>
28 #include <memory>
29 #include <utility>
30 #include <vector>
31 
SkAnimCodecPlayer(std::unique_ptr<SkCodec> codec)32 SkAnimCodecPlayer::SkAnimCodecPlayer(std::unique_ptr<SkCodec> codec) : fCodec(std::move(codec)) {
33     fImageInfo = fCodec->getInfo();
34     fFrameInfos = fCodec->getFrameInfo();
35     fImages.resize(fFrameInfos.size());
36 
37     // change the interpretation of fDuration to a end-time for that frame
38     size_t dur = 0;
39     for (auto& f : fFrameInfos) {
40         dur += f.fDuration;
41         f.fDuration = dur;
42     }
43     fTotalDuration = dur;
44 
45     if (!fTotalDuration) {
46         // Static image -- may or may not have returned a single frame info.
47         fFrameInfos.clear();
48         fImages.clear();
49         fImages.push_back(SkImage::MakeFromGenerator(
50                               SkCodecImageGenerator::MakeFromCodec(std::move(fCodec))));
51     }
52 }
53 
~SkAnimCodecPlayer()54 SkAnimCodecPlayer::~SkAnimCodecPlayer() {}
55 
dimensions() const56 SkISize SkAnimCodecPlayer::dimensions() const {
57     if (!fCodec) {
58         auto image = fImages.front();
59         return image ? image->dimensions() : SkISize::MakeEmpty();
60     }
61     if (SkEncodedOriginSwapsWidthHeight(fCodec->getOrigin())) {
62         return { fImageInfo.height(), fImageInfo.width() };
63     }
64     return { fImageInfo.width(), fImageInfo.height() };
65 }
66 
getFrameAt(int index)67 sk_sp<SkImage> SkAnimCodecPlayer::getFrameAt(int index) {
68     SkASSERT((unsigned)index < fFrameInfos.size());
69 
70     if (fImages[index]) {
71         return fImages[index];
72     }
73 
74     size_t rb = fImageInfo.minRowBytes();
75     size_t size = fImageInfo.computeByteSize(rb);
76     auto data = SkData::MakeUninitialized(size);
77 
78     SkCodec::Options opts;
79     opts.fFrameIndex = index;
80 
81     const auto origin = fCodec->getOrigin();
82     const auto orientedDims = this->dimensions();
83     const auto originMatrix = SkEncodedOriginToMatrix(origin, orientedDims.width(),
84                                                               orientedDims.height());
85 
86     SkPaint paint;
87     paint.setBlendMode(SkBlendMode::kSrc);
88 
89     auto imageInfo = fImageInfo;
90     if (fFrameInfos[index].fAlphaType != kOpaque_SkAlphaType && imageInfo.isOpaque()) {
91         imageInfo = imageInfo.makeAlphaType(kPremul_SkAlphaType);
92     }
93     const int requiredFrame = fFrameInfos[index].fRequiredFrame;
94     if (requiredFrame != SkCodec::kNoFrame && fImages[requiredFrame]) {
95         auto requiredImage = fImages[requiredFrame];
96         auto canvas = SkCanvas::MakeRasterDirect(imageInfo, data->writable_data(), rb);
97         if (origin != kDefault_SkEncodedOrigin) {
98             // The required frame is stored after applying the origin. Undo that,
99             // because the codec decodes prior to applying the origin.
100             // FIXME: Another approach would be to decode the frame's delta on top
101             // of transparent black, and then draw that through the origin matrix
102             // onto the required frame. To do that, SkCodec needs to expose the
103             // rectangle of the delta and the blend mode, so we can handle
104             // kRestoreBGColor frames and Blend::kSrc.
105             SkMatrix inverse;
106             SkAssertResult(originMatrix.invert(&inverse));
107             canvas->concat(inverse);
108         }
109         canvas->drawImage(requiredImage, 0, 0, SkSamplingOptions(), &paint);
110         opts.fPriorFrame = requiredFrame;
111     }
112 
113     if (SkCodec::kSuccess != fCodec->getPixels(imageInfo, data->writable_data(), rb, &opts)) {
114         return nullptr;
115     }
116 
117     auto image = SkImage::MakeRasterData(imageInfo, std::move(data), rb);
118     if (origin != kDefault_SkEncodedOrigin) {
119         imageInfo = imageInfo.makeDimensions(orientedDims);
120         rb = imageInfo.minRowBytes();
121         size = imageInfo.computeByteSize(rb);
122         data = SkData::MakeUninitialized(size);
123         auto canvas = SkCanvas::MakeRasterDirect(imageInfo, data->writable_data(), rb);
124         canvas->concat(originMatrix);
125         canvas->drawImage(image, 0, 0, SkSamplingOptions(), &paint);
126         image = SkImage::MakeRasterData(imageInfo, std::move(data), rb);
127     }
128     return fImages[index] = image;
129 }
130 
getFrame()131 sk_sp<SkImage> SkAnimCodecPlayer::getFrame() {
132     SkASSERT(fTotalDuration > 0 || fImages.size() == 1);
133 
134     return fTotalDuration > 0
135         ? this->getFrameAt(fCurrIndex)
136         : fImages.front();
137 }
138 
seek(uint32_t msec)139 bool SkAnimCodecPlayer::seek(uint32_t msec) {
140     if (!fTotalDuration) {
141         return false;
142     }
143 
144     msec %= fTotalDuration;
145 
146     auto lower = std::lower_bound(fFrameInfos.begin(), fFrameInfos.end(), msec,
147                                   [](const SkCodec::FrameInfo& info, uint32_t msec) {
148                                       return (uint32_t)info.fDuration <= msec;
149                                   });
150     int prevIndex = fCurrIndex;
151     fCurrIndex = lower - fFrameInfos.begin();
152     return fCurrIndex != prevIndex;
153 }
154 
155 
156