blob: 90261b16e57efa4fca6466ae82f2341cf7abda63 [file] [log] [blame]
Leon Scroggins III753a56f2019-12-11 11:02:15 -05001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#pragma once
17
Leon Scroggins IIIb26aebc2020-12-21 14:55:22 -050018#include <SkAndroidCodec.h>
Leon Scroggins III753a56f2019-12-11 11:02:15 -050019#include <SkCodec.h>
20#include <SkImageInfo.h>
21#include <SkPngChunkReader.h>
22#include <SkRect.h>
23#include <SkSize.h>
24#include <cutils/compiler.h>
25
26#include <optional>
27
Leon Scroggins III753a56f2019-12-11 11:02:15 -050028namespace android {
29
Leon Scroggins IIIb26aebc2020-12-21 14:55:22 -050030class Bitmap;
31
32class ANDROID_API ImageDecoder final {
Leon Scroggins III753a56f2019-12-11 11:02:15 -050033public:
34 std::unique_ptr<SkAndroidCodec> mCodec;
35 sk_sp<SkPngChunkReader> mPeeker;
36
37 ImageDecoder(std::unique_ptr<SkAndroidCodec> codec,
38 sk_sp<SkPngChunkReader> peeker = nullptr);
Leon Scroggins IIIb26aebc2020-12-21 14:55:22 -050039 ~ImageDecoder();
Leon Scroggins III753a56f2019-12-11 11:02:15 -050040
41 bool setTargetSize(int width, int height);
42 bool setCropRect(const SkIRect*);
43
44 bool setOutColorType(SkColorType outColorType);
45
Leon Scroggins III1ade46d2020-01-15 05:41:06 -050046 bool setUnpremultipliedRequired(bool unpremultipliedRequired);
Leon Scroggins III753a56f2019-12-11 11:02:15 -050047
Leon Scroggins III6eeca5c2020-01-30 13:59:50 -050048 sk_sp<SkColorSpace> getDefaultColorSpace() const;
Leon Scroggins III753a56f2019-12-11 11:02:15 -050049 void setOutColorSpace(sk_sp<SkColorSpace> cs);
50
Leon Scroggins IIIb26aebc2020-12-21 14:55:22 -050051 // The size is the final size after scaling, adjusting for the origin, and
52 // cropping.
Leon Scroggins III753a56f2019-12-11 11:02:15 -050053 SkImageInfo getOutputInfo() const;
Leon Scroggins III753a56f2019-12-11 11:02:15 -050054
Leon Scroggins III139145b2020-12-17 15:43:54 -050055 int width() const;
56 int height() const;
57
Leon Scroggins IIIb26aebc2020-12-21 14:55:22 -050058 // True if the current frame is opaque.
Leon Scroggins III753a56f2019-12-11 11:02:15 -050059 bool opaque() const;
Leon Scroggins III139145b2020-12-17 15:43:54 -050060
Leon Scroggins III753a56f2019-12-11 11:02:15 -050061 bool gray() const;
62
63 SkCodec::Result decode(void* pixels, size_t rowBytes);
64
Leon Scroggins IIIb26aebc2020-12-21 14:55:22 -050065 // Return true if the decoder has advanced beyond all frames.
66 bool finished() const;
67
68 bool advanceFrame();
69 bool rewind();
70
71 bool isAnimated();
72 int currentFrame() const;
73
Leon Scroggins III06213132020-12-28 15:31:48 -050074 SkCodec::FrameInfo getCurrentFrameInfo();
75
Leon Scroggins III753a56f2019-12-11 11:02:15 -050076private:
Leon Scroggins IIIb26aebc2020-12-21 14:55:22 -050077 // State machine for keeping track of how to handle RestorePrevious (RP)
78 // frames in decode().
79 enum class RestoreState {
80 // Neither this frame nor the prior is RP, so there is no need to cache
81 // or restore.
82 kDoNothing,
83
84 // This is the first in a sequence of one or more RP frames. decode()
85 // needs to cache the provided pixels.
86 kFirstRPFrame,
87
88 // This is the second (or later) in a sequence of multiple RP frames.
89 // decode() needs to restore the cached frame that preceded the first RP
90 // frame in the sequence.
91 kRPFrame,
92
93 // This is the first non-RP frame after a sequence of one or more RP
94 // frames. decode() still needs to restore the cached frame. Separate
95 // from kRPFrame because if the following frame is RP the state will
96 // change to kFirstRPFrame.
97 kNeedsRestore,
98 };
99
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500100 SkISize mTargetSize;
101 SkISize mDecodeSize;
102 SkColorType mOutColorType;
Leon Scroggins III1ade46d2020-01-15 05:41:06 -0500103 bool mUnpremultipliedRequired;
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500104 sk_sp<SkColorSpace> mOutColorSpace;
Leon Scroggins IIIb26aebc2020-12-21 14:55:22 -0500105 SkAndroidCodec::AndroidOptions mOptions;
106 bool mCurrentFrameIsIndependent;
107 bool mCurrentFrameIsOpaque;
108 RestoreState mRestoreState;
109 sk_sp<Bitmap> mRestoreFrame;
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500110 std::optional<SkIRect> mCropRect;
111
112 ImageDecoder(const ImageDecoder&) = delete;
113 ImageDecoder& operator=(const ImageDecoder&) = delete;
Leon Scroggins III1ade46d2020-01-15 05:41:06 -0500114
115 SkAlphaType getOutAlphaType() const;
Leon Scroggins IIIe5ace3f2020-01-15 15:31:40 -0500116 sk_sp<SkColorSpace> getOutputColorSpace() const;
Leon Scroggins III139145b2020-12-17 15:43:54 -0500117 bool swapWidthHeight() const;
Leon Scroggins III753a56f2019-12-11 11:02:15 -0500118};
119
120} // namespace android