1 /*
   2  * Copyright (c) 2011, 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 #include "config.h"
  27 
  28 #include "BitmapImage.h"
  29 #include "NotImplemented.h"
  30 #include "GraphicsContext.h"
  31 #include "ImageObserver.h"
  32 #include <wtf/java/JavaEnv.h>
  33 #include "GraphicsContextJava.h"
  34 #include "PlatformContextJava.h"
  35 #include "ImageDecoderJava.h"
  36 #include "RenderingQueue.h"
  37 #include "SharedBuffer.h"
  38 
  39 namespace WebCore {
  40 
  41 void BitmapImage::invalidatePlatformData()
  42 {
  43 }
  44 
  45 Ref<Image> BitmapImage::createFromName(const char* name)
  46 {
  47     Ref<BitmapImage> img(create());
  48 
  49     WC_GETJAVAENV_CHKRET(env, WTFMove(img));
  50 
  51 #if USE(IMAGEIO)
  52     static jmethodID midLoadFromResource = env->GetMethodID(
  53         PG_GetGraphicsImageDecoderClass(env),
  54         "loadFromResource",
  55         "(Ljava/lang/String;)V");
  56     ASSERT(midLoadFromResource);
  57 
  58     RefPtr<SharedBuffer> dataBuffer(SharedBuffer::create());
  59     img->m_source->ensureDecoderAvailable(dataBuffer.get());
  60     env->CallVoidMethod(
  61         static_cast<ImageDecoderJava*>(img->m_source->m_decoder.get())->nativeDecoder(),
  62         midLoadFromResource,
  63         (jstring)String(name).toJavaString(env));
  64     CheckAndClearException(env);
  65 
  66     // we have to make this call in order to initialize
  67     // internal flags that indicates the image readiness
  68     img->encodedDataStatus();
  69 
  70     // Absence if the image size indicates some problem with
  71     // the availability of the resource referred by the name.
  72     // It should never happen if resources are set up correctly,
  73     // however it does happen after OOME
  74 //    ASSERT(isSizeAvailable);
  75 #else
  76     static jmethodID midLoadFromResource = env->GetMethodID(
  77        PG_GetGraphicsManagerClass(env),
  78        "fwkLoadFromResource",
  79        "(Ljava/lang/String;J)V");
  80     ASSERT(midLoadFromResource);
  81 
  82     RefPtr<SharedBuffer> dataBuffer(SharedBuffer::create());
  83     JLString resourceName(String(name).toJavaString(env));
  84     ASSERT(resourceName);
  85 
  86     env->CallVoidMethod(
  87         PL_GetGraphicsManager(env),
  88         midLoadFromResource,
  89         (jstring)resourceName,
  90         ptr_to_jlong(dataBuffer.get()));
  91     CheckAndClearException(env);
  92     //From the upper call we got a callback [Java_com_sun_webkit_graphics_WCGraphicsManager_append]
  93     //that fills the buffer.
  94     img->setData(WTFMove(dataBuffer), true);
  95 #endif
  96     return WTFMove(img);
  97 }
  98 
  99 
 100 } // namespace WebCore
 101 
 102 using namespace WebCore;
 103 extern "C" {
 104 
 105 JNIEXPORT void JNICALL Java_com_sun_webkit_graphics_WCGraphicsManager_append
 106     (JNIEnv *env, jclass, jlong sharedBufferPtr, jbyteArray jbits, jint count)
 107 {
 108     ASSERT(sharedBufferPtr);
 109     SharedBuffer* pBuffer = static_cast<SharedBuffer*>jlong_to_ptr(sharedBufferPtr);
 110 
 111     void *cbits = env->GetPrimitiveArrayCritical(jbits, 0);
 112     pBuffer->append(static_cast<char*>(cbits), count);
 113     env->ReleasePrimitiveArrayCritical(jbits, cbits, JNI_ABORT);
 114 }
 115 
 116 }//extern "C"
 117