Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 1 | /* |
| 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 III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 18 | #include <SkAndroidCodec.h> |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 19 | #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 III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 28 | namespace android { |
| 29 | |
Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 30 | class Bitmap; |
| 31 | |
| 32 | class ANDROID_API ImageDecoder final { |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 33 | public: |
| 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 III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 39 | ~ImageDecoder(); |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 40 | |
| 41 | bool setTargetSize(int width, int height); |
| 42 | bool setCropRect(const SkIRect*); |
| 43 | |
| 44 | bool setOutColorType(SkColorType outColorType); |
| 45 | |
Leon Scroggins III | 1ade46d | 2020-01-15 05:41:06 -0500 | [diff] [blame] | 46 | bool setUnpremultipliedRequired(bool unpremultipliedRequired); |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 47 | |
Leon Scroggins III | 6eeca5c | 2020-01-30 13:59:50 -0500 | [diff] [blame] | 48 | sk_sp<SkColorSpace> getDefaultColorSpace() const; |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 49 | void setOutColorSpace(sk_sp<SkColorSpace> cs); |
| 50 | |
Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 51 | // The size is the final size after scaling, adjusting for the origin, and |
| 52 | // cropping. |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 53 | SkImageInfo getOutputInfo() const; |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 54 | |
Leon Scroggins III | 139145b | 2020-12-17 15:43:54 -0500 | [diff] [blame] | 55 | int width() const; |
| 56 | int height() const; |
| 57 | |
Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 58 | // True if the current frame is opaque. |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 59 | bool opaque() const; |
Leon Scroggins III | 139145b | 2020-12-17 15:43:54 -0500 | [diff] [blame] | 60 | |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 61 | bool gray() const; |
| 62 | |
| 63 | SkCodec::Result decode(void* pixels, size_t rowBytes); |
| 64 | |
Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 65 | // 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 III | 0621313 | 2020-12-28 15:31:48 -0500 | [diff] [blame^] | 74 | SkCodec::FrameInfo getCurrentFrameInfo(); |
| 75 | |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 76 | private: |
Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 77 | // 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 III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 100 | SkISize mTargetSize; |
| 101 | SkISize mDecodeSize; |
| 102 | SkColorType mOutColorType; |
Leon Scroggins III | 1ade46d | 2020-01-15 05:41:06 -0500 | [diff] [blame] | 103 | bool mUnpremultipliedRequired; |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 104 | sk_sp<SkColorSpace> mOutColorSpace; |
Leon Scroggins III | b26aebc | 2020-12-21 14:55:22 -0500 | [diff] [blame] | 105 | SkAndroidCodec::AndroidOptions mOptions; |
| 106 | bool mCurrentFrameIsIndependent; |
| 107 | bool mCurrentFrameIsOpaque; |
| 108 | RestoreState mRestoreState; |
| 109 | sk_sp<Bitmap> mRestoreFrame; |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 110 | std::optional<SkIRect> mCropRect; |
| 111 | |
| 112 | ImageDecoder(const ImageDecoder&) = delete; |
| 113 | ImageDecoder& operator=(const ImageDecoder&) = delete; |
Leon Scroggins III | 1ade46d | 2020-01-15 05:41:06 -0500 | [diff] [blame] | 114 | |
| 115 | SkAlphaType getOutAlphaType() const; |
Leon Scroggins III | e5ace3f | 2020-01-15 15:31:40 -0500 | [diff] [blame] | 116 | sk_sp<SkColorSpace> getOutputColorSpace() const; |
Leon Scroggins III | 139145b | 2020-12-17 15:43:54 -0500 | [diff] [blame] | 117 | bool swapWidthHeight() const; |
Leon Scroggins III | 753a56f | 2019-12-11 11:02:15 -0500 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | } // namespace android |