Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkAnimCodecPlayer.cpp
Go to the documentation of this file.
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
9
15#include "include/core/SkData.h"
22#include "include/core/SkSize.h"
25
26#include <algorithm>
27#include <cstddef>
28#include <memory>
29#include <utility>
30#include <vector>
31
32SkAnimCodecPlayer::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
55
57 if (!fCodec) {
58 auto image = fImages.front();
60 }
61 if (SkEncodedOriginSwapsWidthHeight(fCodec->getOrigin())) {
62 return { fImageInfo.height(), fImageInfo.width() };
63 }
64 return { fImageInfo.width(), fImageInfo.height() };
65}
66
67sk_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);
77
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
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 = SkImages::RasterFromData(imageInfo, std::move(data), rb);
118 if (origin != kDefault_SkEncodedOrigin) {
119 imageInfo = imageInfo.makeDimensions(orientedDims);
120 rb = imageInfo.minRowBytes();
121 size = imageInfo.computeByteSize(rb);
123 auto canvas = SkCanvas::MakeRasterDirect(imageInfo, data->writable_data(), rb);
124 canvas->concat(originMatrix);
125 canvas->drawImage(image, 0, 0, SkSamplingOptions(), &paint);
126 image = SkImages::RasterFromData(imageInfo, std::move(data), rb);
127 }
128 return fImages[index] = image;
129}
130
132 SkASSERT(fTotalDuration > 0 || fImages.size() == 1);
133
134 return fTotalDuration > 0
135 ? this->getFrameAt(fCurrIndex)
136 : fImages.front();
137}
138
139bool 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
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
@ kOpaque_SkAlphaType
pixel is opaque
Definition SkAlphaType.h:28
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
#define SkAssertResult(cond)
Definition SkAssert.h:123
#define SkASSERT(cond)
Definition SkAssert.h:116
@ kDefault_SkEncodedOrigin
static bool SkEncodedOriginSwapsWidthHeight(SkEncodedOrigin origin)
static SkMatrix SkEncodedOriginToMatrix(SkEncodedOrigin origin, int w, int h)
sk_sp< SkImage > getFrame()
SkAnimCodecPlayer(std::unique_ptr< SkCodec > codec)
SkISize dimensions() const
bool seek(uint32_t msec)
static std::unique_ptr< SkCanvas > MakeRasterDirect(const SkImageInfo &info, void *pixels, size_t rowBytes, const SkSurfaceProps *props=nullptr)
static std::unique_ptr< SkImageGenerator > MakeFromCodec(std::unique_ptr< SkCodec >, std::optional< SkAlphaType >=std::nullopt)
@ kSuccess
Definition SkCodec.h:80
static constexpr int kNoFrame
Definition SkCodec.h:650
static sk_sp< SkData > MakeUninitialized(size_t length)
Definition SkData.cpp:116
SkISize dimensions() const
Definition SkImage.h:297
const Paint & paint
sk_sp< SkImage > image
Definition examples.cpp:29
SK_API sk_sp< SkImage > RasterFromData(const SkImageInfo &info, sk_sp< SkData > pixels, size_t rowBytes)
SK_API sk_sp< SkImage > DeferredFromGenerator(std::unique_ptr< SkImageGenerator > imageGenerator)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition switches.h:41
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition switches.h:259
Definition ref_ptr.h:256
static constexpr SkISize MakeEmpty()
Definition SkSize.h:22
SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const
size_t minRowBytes() const
size_t computeByteSize(size_t rowBytes) const
int width() const
int height() const