1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_IMAGE_DECODER_WRAPPER_H_
6 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_IMAGE_DECODER_WRAPPER_H_
7 
8 #include "cc/paint/paint_image.h"
9 #include "third_party/blink/renderer/platform/image-decoders/image_decoder.h"
10 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
11 #include "third_party/skia/include/core/SkSize.h"
12 
13 namespace blink {
14 class ImageDecoderFactory;
15 class ImageFrameGenerator;
16 class SegmentReader;
17 
18 class ImageDecoderWrapper {
19   STACK_ALLOCATED();
20 
21  public:
22   ImageDecoderWrapper(ImageFrameGenerator* generator,
23                       SegmentReader* data,
24                       const SkISize& scaled_size,
25                       ImageDecoder::AlphaOption alpha_option,
26                       ColorBehavior decoder_color_behavior,
27                       ImageDecoder::HighBitDepthDecodingOption decoding_option,
28                       size_t index,
29                       const SkImageInfo& info,
30                       void* pixels,
31                       size_t row_bytes,
32                       bool all_data_received,
33                       cc::PaintImage::GeneratorClientId client_id);
34   ~ImageDecoderWrapper();
35 
36   // Returns true if the decode succeeded.
37   bool Decode(ImageDecoderFactory* factory,
38               size_t* frame_count,
39               bool* has_alpha);
40 
41   // Indicates that the decode failed due to a corrupt image.
decode_failed()42   bool decode_failed() const { return decode_failed_; }
43 
44  private:
45   bool ShouldDecodeToExternalMemory(size_t frame_count,
46                                     bool has_cached_decoder) const;
47   bool ShouldRemoveDecoder(bool frame_was_completely_decoded,
48                            bool decoded_to_external_memory) const;
49   void PurgeAllFramesIfNecessary(ImageDecoder* decoder,
50                                  bool frame_was_completely_decoded,
51                                  size_t frame_count) const;
52   std::unique_ptr<ImageDecoder> CreateDecoderWithData(
53       ImageDecoderFactory* factory) const;
54 
55   const ImageFrameGenerator* const generator_;
56   SegmentReader* data_;
57   const SkISize scaled_size_;
58   const ImageDecoder::AlphaOption alpha_option_;
59   const ColorBehavior decoder_color_behavior_;
60   const ImageDecoder::HighBitDepthDecodingOption decoding_option_;
61   const size_t frame_index_;
62   const SkImageInfo info_;
63   void* pixels_;
64   const size_t row_bytes_;
65   const bool all_data_received_;
66   const cc::PaintImage::GeneratorClientId client_id_;
67 
68   bool decode_failed_ = false;
69 };
70 
71 }  // namespace blink
72 
73 #endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_IMAGE_DECODER_WRAPPER_H_
74