1 /*
   2  * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #pragma once
  27 
  28 #include "ImageDecoder.h"
  29 #include "ImageSource.h"
  30 #include "IntSize.h"
  31 #include "SharedBuffer.h"
  32 #include "RQRef.h"
  33 #include <wtf/Optional.h>
  34 
  35 #include <jni.h>
  36 
  37 namespace WebCore {
  38 
  39 class ImageDecoderJava : public ImageDecoder {
  40     WTF_MAKE_FAST_ALLOCATED;
  41 public:
  42     ImageDecoderJava();
  43     ~ImageDecoderJava();
  44 
  45     static bool supportsMediaType(MediaType type) { return type == MediaType::Image; }
  46 
  47     static Ref<ImageDecoder> create(const SharedBuffer&, AlphaOption, GammaAndColorProfileOption)
  48     {
  49         return adoptRef(*new ImageDecoderJava());
  50     }
  51 
  52     size_t bytesDecodedToDetermineProperties() const final;
  53 
  54     String filenameExtension() const final;
  55     bool isSizeAvailable() const final;
  56 
  57     EncodedDataStatus encodedDataStatus() const final;
  58     // Always original size, without subsampling.
  59     IntSize size() const final;
  60     size_t frameCount() const final;
  61 
  62     RepetitionCount repetitionCount() const final;
  63     std::optional<IntPoint> hotSpot() const final;
  64 
  65     IntSize frameSizeAtIndex(size_t, SubsamplingLevel = SubsamplingLevel::Default) const final;
  66     bool frameIsCompleteAtIndex(size_t) const final;
  67     ImageOrientation frameOrientationAtIndex(size_t) const final;
  68 
  69     WTF::Seconds frameDurationAtIndex(size_t) const final;
  70     bool frameHasAlphaAtIndex(size_t) const final;
  71     bool frameAllowSubsamplingAtIndex(size_t) const final;
  72     unsigned frameBytesAtIndex(size_t, SubsamplingLevel = SubsamplingLevel::Default) const final;
  73 
  74     NativeImagePtr createFrameImageAtIndex(size_t, SubsamplingLevel = SubsamplingLevel::Default, const DecodingOptions& = DecodingOptions(DecodingMode::Synchronous)) final;
  75 
  76     void setData(SharedBuffer&, bool allDataReceived) final;
  77     bool isAllDataReceived() const final { return m_isAllDataReceived;}
  78     void clearFrameBufferCache(size_t) final {  }
  79 
  80     JLObject nativeDecoder() const { return m_nativeDecoder; }
  81 
  82 protected:
  83     bool m_isAllDataReceived { false };
  84     size_t m_receivedDataSize { 0 };
  85     mutable EncodedDataStatus m_encodedDataStatus { EncodedDataStatus::Unknown };
  86     // Native Handle for Java object.
  87     JGObject m_nativeDecoder;
  88     mutable IntSize m_size;
  89 };
  90 
  91 } // namespace WebCore