| 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 "modules/skresources/src/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 |
|
| 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(SkImages::DeferredFromGenerator( |
| 50 |
SkCodecImageGenerator::MakeFromCodec(std::move(fCodec)))); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
SkAnimCodecPlayer::~SkAnimCodecPlayer() {} |
| 55 |
|
| 56 |
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 |
|
| 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 |
canvas->concat(*originMatrix.invert()); |
| 106 |
} |
| 107 |
canvas->drawImage(requiredImage, 0, 0, SkSamplingOptions(), &paint); |
| 108 |
opts.fPriorFrame = requiredFrame; |
| 109 |
} |
| 110 |
|
| 111 |
if (SkCodec::kSuccess != fCodec->getPixels(imageInfo, data->writable_data(), rb, &opts)) { |
| 112 |
return nullptr; |
| 113 |
} |
| 114 |
|
| 115 |
auto image = SkImages::RasterFromData(imageInfo, std::move(data), rb); |
| 116 |
if (origin != kDefault_SkEncodedOrigin) { |
| 117 |
imageInfo = imageInfo.makeDimensions(orientedDims); |
| 118 |
rb = imageInfo.minRowBytes(); |
| 119 |
size = imageInfo.computeByteSize(rb); |
| 120 |
data = SkData::MakeUninitialized(size); |
| 121 |
auto canvas = SkCanvas::MakeRasterDirect(imageInfo, data->writable_data(), rb); |
| 122 |
canvas->concat(originMatrix); |
| 123 |
canvas->drawImage(image, 0, 0, SkSamplingOptions(), &paint); |
| 124 |
image = SkImages::RasterFromData(imageInfo, std::move(data), rb); |
| 125 |
} |
| 126 |
return fImages[index] = image; |
| 127 |
} |
| 128 |
|
| 129 |
sk_sp<SkImage> SkAnimCodecPlayer::getFrame() { |
| 130 |
SkASSERT(fTotalDuration > 0 || fImages.size() == 1); |
| 131 |
|
| 132 |
return fTotalDuration > 0 |
| 133 |
? this->getFrameAt(fCurrIndex) |
| 134 |
: fImages.front(); |
| 135 |
} |
| 136 |
|
| 137 |
bool SkAnimCodecPlayer::seek(uint32_t msec) { |
| 138 |
if (!fTotalDuration) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
msec %= fTotalDuration; |
| 143 |
|
| 144 |
auto lower = std::lower_bound(fFrameInfos.begin(), fFrameInfos.end(), msec, |
| 145 |
[](const SkCodec::FrameInfo& info, uint32_t msec) { |
| 146 |
return (uint32_t)info.fDuration <= msec; |
| 147 |
}); |
| 148 |
int prevIndex = fCurrIndex; |
| 149 |
fCurrIndex = lower - fFrameInfos.begin(); |
| 150 |
return fCurrIndex != prevIndex; |
| 151 |
} |