1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_DECODING_IMAGE_GENERATOR_H_
27 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_DECODING_IMAGE_GENERATOR_H_
28 
29 #include "base/macros.h"
30 #include "base/memory/scoped_refptr.h"
31 #include "third_party/blink/public/platform/web_vector.h"
32 #include "third_party/blink/renderer/platform/graphics/paint/paint_image.h"
33 #include "third_party/blink/renderer/platform/image-decoders/segment_reader.h"
34 #include "third_party/blink/renderer/platform/platform_export.h"
35 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
36 #include "third_party/skia/include/core/SkImageInfo.h"
37 #include "third_party/skia/include/core/SkYUVAIndex.h"
38 
39 class SkData;
40 
41 namespace blink {
42 
43 class ImageFrameGenerator;
44 
45 // Implements SkImageGenerator, used by SkPixelRef to populate a discardable
46 // memory with a decoded image frame. ImageFrameGenerator does the actual
47 // decoding.
48 class PLATFORM_EXPORT DecodingImageGenerator final
49     : public PaintImageGenerator {
50   USING_FAST_MALLOC(DecodingImageGenerator);
51 
52  public:
53   // Aside from tests, this is used to create a decoder from SkData in Skia
54   // (exported via WebImageGenerator and set via
55   // SkGraphics::SetImageGeneratorFromEncodedDataFactory)
56   static std::unique_ptr<SkImageGenerator> CreateAsSkImageGenerator(
57       sk_sp<SkData>);
58 
59   static sk_sp<DecodingImageGenerator> Create(
60       scoped_refptr<ImageFrameGenerator>,
61       const SkImageInfo&,
62       scoped_refptr<SegmentReader>,
63       WebVector<FrameMetadata>,
64       PaintImage::ContentId,
65       bool all_data_received,
66       bool can_yuv_decode,
67       const cc::ImageHeaderMetadata& image_metadata);
68 
69   ~DecodingImageGenerator() override;
70 
71   // PaintImageGenerator implementation.
72   sk_sp<SkData> GetEncodedData() const override;
73   bool GetPixels(const SkImageInfo&,
74                  void* pixels,
75                  size_t row_bytes,
76                  size_t frame_index,
77                  PaintImage::GeneratorClientId client_id,
78                  uint32_t lazy_pixel_ref) override;
79   bool QueryYUVA8(SkYUVASizeInfo*,
80                   SkYUVAIndex[SkYUVAIndex::kIndexCount],
81                   SkYUVColorSpace*) const override;
82   bool GetYUVA8Planes(const SkYUVASizeInfo&,
83                       const SkYUVAIndex[SkYUVAIndex::kIndexCount],
84                       void* planes[4],
85                       size_t frame_index,
86                       uint32_t lazy_pixel_ref) override;
87   SkISize GetSupportedDecodeSize(const SkISize& requested_size) const override;
88   PaintImage::ContentId GetContentIdForFrame(size_t frame_index) const override;
89   const cc::ImageHeaderMetadata* GetMetadataForDecodeAcceleration()
90       const override;
91 
92  private:
93   DecodingImageGenerator(scoped_refptr<ImageFrameGenerator>,
94                          const SkImageInfo&,
95                          scoped_refptr<SegmentReader>,
96                          WebVector<FrameMetadata>,
97                          PaintImage::ContentId,
98                          bool all_data_received,
99                          bool can_yuv_decode,
100                          const cc::ImageHeaderMetadata& image_metadata);
101 
102   scoped_refptr<ImageFrameGenerator> frame_generator_;
103   const scoped_refptr<SegmentReader> data_;  // Data source.
104   const bool all_data_received_;
105   const bool can_yuv_decode_;
106   const PaintImage::ContentId complete_frame_content_id_;
107 
108   // Image metadata, such as format (e.g. Jpeg or WebP), YUV subsampling factor
109   // (e.g. 444, 422, 420, etc.), size, and format-specific information that is
110   // useful for deciding which kind of decoding can be used (i.e. hardware
111   // acceleration or normal).
112   const cc::ImageHeaderMetadata image_metadata_;
113 
114   DISALLOW_COPY_AND_ASSIGN(DecodingImageGenerator);
115 };
116 
117 }  // namespace blink
118 
119 #endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_DECODING_IMAGE_GENERATOR_H__
120