Skip to content

Commit

Permalink
Remove the definition of NativeImageRef in OHOS, and provide methods …
Browse files Browse the repository at this point in the history
…to convert PixelMap into Image.
  • Loading branch information
Hparty committed Jul 12, 2024
1 parent 80403e4 commit 27c2ce9
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 217 deletions.
10 changes: 5 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,11 @@ if (WEB)
list(APPEND TGFX_FILES ${GPU_PLATFORM_FILES})
endif ()
elseif (OHOS)
find_library(pixelmap_ndk pixelmap_ndk.z)
find_library(libimage_source_ndk image_source_ndk.z)
find_library(image_source image_source)
find_library(pixelmap pixelmap)
list(APPEND TGFX_SHARED_LIBS ${pixelmap_ndk} ${libimage_source_ndk} ${image_source} ${pixelmap})
find_library(PIXELMAP_NDK pixelmap_ndk.z)
find_library(IMAGE_SOURCE_NDK image_source_ndk.z)
find_library(PIXELMAP pixelmap)
find_library(IMAGE_SOURCE image_source)
list(APPEND TGFX_SHARED_LIBS ${PIXELMAP_NDK} ${IMAGE_SOURCE_NDK} ${PIXELMAP} ${IMAGE_SOURCE})
file(GLOB_RECURSE PLATFORM_FILES src/platform/ohos/*.*)
list(APPEND TGFX_FILES ${PLATFORM_FILES})
if (TGFX_USE_NATIVE_GL)
Expand Down
10 changes: 2 additions & 8 deletions include/tgfx/platform/NativeImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ class _jobject;
struct CGImage;

#elif defined(__OHOS__)
#include <multimedia/image_framework/image_pixel_map_mdk.h>
#include "tgfx/core/AlphaType.h"
#include "tgfx/core/Orientation.h"
#endif

namespace tgfx {
Expand All @@ -49,11 +46,8 @@ typedef CGImage* NativeImageRef;

#elif defined(__OHOS__)

struct NativeImage {
NativePixelMap* pixelMap = nullptr;
Orientation orientation = Orientation::TopLeft;
AlphaType alphaType = AlphaType::Unpremultiplied;
};
struct NativeImage {};

typedef NativeImage* NativeImageRef;

#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,26 @@
/////////////////////////////////////////////////////////////////////////////////////////////////

#pragma once
#include "tgfx/core/ImageCodec.h"

#include <memory>
#include <napi/native_api.h>
#include "tgfx/core/Image.h"

namespace tgfx {
class NativeImageCodec : public ImageCodec {
/**
* OHOSBitmap provides a utility to access an OpenHarmony PixelMap object.
*/
class OHOSPixelMap {
public:
bool readPixels(const ImageInfo& dstInfo, void* dstPixels) const override;

private:
NativeImageCodec(int width, int height, Orientation orientation, std::shared_ptr<Data> data,
const ImageInfo& info)
: ImageCodec(width, height, orientation), imageData(std::move(data)), imageInfo(info) {
}

std::shared_ptr<Data> imageData;
ImageInfo imageInfo;
/**
* Returns an ImageInfo describing the width, height, color type, alpha type, and row bytes of the
* specified OpenHarmony PixelMap object. Only AlphaType::Opaque or AlphaType::Premultiplied PixelMap is supported.
*/
static ImageInfo GetInfo(napi_env env, napi_value value);

friend class ImageCodec;
/**
* Copy PixelMap into an Image. Only AlphaType::Opaque or AlphaType::Premultiplied PixelMap is supported.
*/
static std::shared_ptr<Image> CopyImage(napi_env env, napi_value value);
};
} // namespace tgfx
83 changes: 3 additions & 80 deletions ohos/hello2d/src/main/cpp/napi_init.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#include "napi/native_api.h"
#include <ace/xcomponent/native_interface_xcomponent.h>
#include <multimedia/image_framework/image_source_mdk.h>
#include "tgfx/core/AlphaType.h"
#include "tgfx/platform/ohos/HarmonyImage.h"
#include "tgfx/opengl/egl/EGLWindow.h"
#include "tgfx/platform/NativeImage.h"
#include "drawers/AppHost.h"
#include "drawers/Drawer.h"
#include "tgfx/platform/ohos/OHOSPixelMap.h"

static float screenDensity = 1.0f;
static std::shared_ptr<drawers::AppHost> appHost = nullptr;
Expand All @@ -24,91 +21,19 @@ static napi_value OnUpdateDensity(napi_env env, napi_callback_info info) {
return nullptr;
}

static napi_value AddImageFromSource(napi_env env, napi_callback_info info) {
static napi_value AddImageFromPixelMap(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2] = {nullptr};
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
size_t strSize;
char srcBuf[2048];
napi_get_value_string_utf8(env, args[0], srcBuf, sizeof(srcBuf), &strSize);
std::string name(srcBuf, strSize);
ImageSourceNative* imageSource = OH_ImageSource_InitNative(env, args[1]);
if (!imageSource) {
return nullptr;
}
OhosImageSourceInfo sourceInfo;
auto errorCode = OH_ImageSource_GetImageInfo(imageSource, 0, &sourceInfo);
if (errorCode != IMAGE_RESULT_SUCCESS || sourceInfo.size.width < 1 ||
sourceInfo.size.height < 1) {
OH_ImageSource_Release(imageSource);
return nullptr;
}
OhosImageSourceProperty target;
char exifKey[] = "Orientation";
target.size = strlen(exifKey);
target.value = exifKey;

OhosImageSourceProperty response{};
char responseValue[20];
response.size = 20;
response.value = responseValue;
errorCode = OH_ImageSource_GetImageProperty(imageSource, &target, &response);

tgfx::Orientation orientation = tgfx::Orientation::TopLeft;
if (errorCode == IMAGE_RESULT_SUCCESS) {
orientation = tgfx::HarmonyImage::ToTGFXOrientation(response.value, response.size);
}

napi_value pixelMap;
errorCode = OH_ImageSource_CreatePixelMap(imageSource, nullptr, &pixelMap);
if (errorCode != IMAGE_RESULT_SUCCESS) {
OH_ImageSource_Release(imageSource);
return nullptr;
}
NativePixelMap* nativePixelMap = OH_PixelMap_InitNativePixelMap(env, pixelMap);
if (nativePixelMap == nullptr) {
OH_ImageSource_Release(imageSource);
return nullptr;
}
if (appHost == nullptr) {
appHost = CreateAppHost();
}
tgfx::AlphaType alphaType = tgfx::HarmonyImage::ToTGFXAlphaType(sourceInfo.alphaType);
alphaType = alphaType == tgfx::AlphaType::Unknown ? tgfx::AlphaType::Unpremultiplied : alphaType;
tgfx::NativeImage image{nativePixelMap, orientation, alphaType};
appHost->addImage(name, tgfx::Image::MakeFrom(&image));
OH_ImageSource_Release(imageSource);
return nullptr;
}

static napi_value AddImageFromPixelMap(napi_env env, napi_callback_info info) {
size_t argc = 4;
napi_value args[4] = {nullptr};
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
size_t strSize;
char srcBuf[2048];
napi_get_value_string_utf8(env, args[0], srcBuf, sizeof(srcBuf), &strSize);
std::string name(srcBuf, strSize);
NativePixelMap* pixelmap = OH_PixelMap_InitNativePixelMap(env, args[1]);
if (pixelmap == nullptr) {
return nullptr;
}
if (appHost == nullptr) {
appHost = CreateAppHost();
}
size_t orientationSize;
char orientation[2048];
napi_get_value_string_utf8(env, args[2], orientation, sizeof(orientation), &orientationSize);

int alphaType = 0;
napi_get_value_int32(env, args[3], &alphaType);
tgfx::NativeImage nativeImage{};
nativeImage.pixelMap = pixelmap;
nativeImage.orientation = tgfx::HarmonyImage::ToTGFXOrientation(orientation, orientationSize);
auto tgfxAlphaType = tgfx::HarmonyImage::ToTGFXAlphaType(alphaType);
nativeImage.alphaType =
tgfxAlphaType == tgfx::AlphaType::Unknown ? tgfx::AlphaType::Unpremultiplied : tgfxAlphaType;
appHost->addImage(name, tgfx::Image::MakeFrom(&nativeImage));
appHost->addImage(name, tgfx::OHOSPixelMap::CopyImage(env, args[1]));
return nullptr;
}

Expand Down Expand Up @@ -273,8 +198,6 @@ static napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor desc[] = {
{"draw", nullptr, OnDraw, nullptr, nullptr, nullptr, napi_default, nullptr},
{"updateDensity", nullptr, OnUpdateDensity, nullptr, nullptr, nullptr, napi_default, nullptr},
{"addImageFromSource", nullptr, AddImageFromSource, nullptr, nullptr, nullptr, napi_default,
nullptr},
{"addImageFromPixelMap", nullptr, AddImageFromPixelMap, nullptr, nullptr, nullptr,
napi_default, nullptr},
{"addImageFromPath", nullptr, AddImageFromPath, nullptr, nullptr, nullptr, napi_default,
Expand Down
5 changes: 1 addition & 4 deletions ohos/hello2d/src/main/cpp/types/libhello2d/Index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ export const draw: (index: number) => void;

export const updateDensity: (density: number) => void;

export const addImageFromSource: (name: string, image: image.ImageSource) => void;

export const addImageFromPixelMap: (name: string, image: image.PixelMap, orientation: string,
alphaType: number) => void;
export const addImageFromPixelMap: (name: string, image: image.PixelMap) => void;

export const addImageFromPath: (name: string, imagePath: string) => void;

Expand Down
26 changes: 9 additions & 17 deletions ohos/hello2d/src/main/ets/pages/Index.ets
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,21 @@ struct Index {
aboutToAppear(): void {
let displayInfo = display.getDefaultDisplaySync();
hello2d.updateDensity(displayInfo.densityPixels);
getContext(this).resourceManager.getRawFileContent("rotation.jpg", async (error, value) => {
getContext(this).resourceManager.getRawFileContent("bridge.jpg", async (error, value) => {
if (error) {
return;
}
let name = "bridge";
//from pixelmap
// const buffer = value.buffer.slice(0);
// let img = image.createImageSource(buffer);
// let pixelmap = img.createPixelMapSync();
// let imgInfo = await img.getImageInfo(0);
// let orientation = await img.getImageProperty(image.PropertyKey.ORIENTATION);
// if (orientation) {
// hello2d.addImageFromPixelMap(name, pixelmap, orientation, imgInfo.alphaType);
// } else {
// hello2d.addImageFromPixelMap(name, pixelmap, "Top-left", imgInfo.alphaType);
// }
// pixelmap.release();
const buffer = value.buffer.slice(0);
let img = image.createImageSource(buffer);
let pixelmap = img.createPixelMapSync();
hello2d.addImageFromPixelMap(name, pixelmap);
pixelmap.release();

// from data
// const buffer = value.buffer.slice(0);
// hello2d.addImageFromEncoded(name, buffer);

// from source
const buffer = value.buffer.slice(0);
let img = image.createImageSource(buffer);
hello2d.addImageFromSource(name, img);
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/platform/ohos/HardwareBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ ImageInfo HardwareBufferGetInfo(HardwareBufferRef) {
PixelFormat HardwareBufferGetPixelFormat(HardwareBufferRef) {
return PixelFormat::Unknown;
}
} // namespace tgfx
} // namespace tgfx
34 changes: 24 additions & 10 deletions src/platform/ohos/NativeCodec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@
#include <cstdint>
#include "tgfx/core/AlphaType.h"
#include "tgfx/core/ColorType.h"
#include "tgfx/platform/ohos/HarmonyImage.h"
#include "tgfx/core/Image.h"
#include "OHOSConverter.h"
#include "utils/Log.h"

namespace tgfx {

std::shared_ptr<ImageCodec> ImageCodec::MakeFrom(NativeImageRef) {
return nullptr;
}

Orientation GetOrientation(OH_ImageSourceNative* imageSource) {
char targetData[] = "Orientation";
Image_String target;
Expand All @@ -36,7 +41,7 @@ Orientation GetOrientation(OH_ImageSourceNative* imageSource) {

auto errorCode = OH_ImageSourceNative_GetImageProperty(imageSource, &target, &response);
if (errorCode == IMAGE_SUCCESS) {
auto result = HarmonyImage::ToTGFXOrientation(response.data, response.size);
auto result = OHOSConverter::ToTGFXOrientation(response.data, response.size);
free(response.data);
return result;
}
Expand Down Expand Up @@ -73,14 +78,17 @@ std::shared_ptr<ImageCodec> ImageCodec::MakeNativeCodec(std::shared_ptr<Data> im

bool NativeCodec::readPixels(const ImageInfo& dstInfo, void* dstPixels) const {
auto image = CreateImageSource();
if (!image) {
return false;
}
OH_DecodingOptions* options;
auto errorCode = OH_DecodingOptions_Create(&options);
if (errorCode != Image_ErrorCode::IMAGE_SUCCESS) {
LOGE("NativeCodec::readPixels() Failed to Create Decode Option");
OH_ImageSourceNative_Release(image);
return false;
}
OH_DecodingOptions_SetPixelFormat(options, HarmonyImage::ToOhPixelFormat(dstInfo.colorType()));
OH_DecodingOptions_SetPixelFormat(options, OHOSConverter::ToOhPixelFormat(dstInfo.colorType()));
// decode
OH_PixelmapNative* pixelmap = nullptr;
errorCode = OH_ImageSourceNative_CreatePixelmap(image, options, &pixelmap);
Expand All @@ -90,13 +98,19 @@ bool NativeCodec::readPixels(const ImageInfo& dstInfo, void* dstPixels) const {
return false;
}
auto info = GetPixelmapInfo(pixelmap);
uint8_t* pixels = new uint8_t[info.byteSize()];
size_t bufferSize = info.byteSize();
OH_PixelmapNative_ReadPixels(pixelmap, pixels, &bufferSize);
auto result = Pixmap(info, pixels).readPixels(dstInfo, dstPixels);
bool result = true;
if (info == dstInfo) {
size_t bufferSize = info.byteSize();
OH_PixelmapNative_ReadPixels(pixelmap, static_cast<uint8_t*>(dstPixels), &bufferSize);
} else {
uint8_t* pixels = new uint8_t[info.byteSize()];
size_t bufferSize = info.byteSize();
OH_PixelmapNative_ReadPixels(pixelmap, pixels, &bufferSize);
result = Pixmap(info, pixels).readPixels(dstInfo, dstPixels);
delete[] pixels;
}
OH_ImageSourceNative_Release(image);
OH_PixelmapNative_Release(pixelmap);
delete[] pixels;
return result;
}

Expand Down Expand Up @@ -150,11 +164,11 @@ ImageInfo NativeCodec::GetPixelmapInfo(OH_PixelmapNative* pixelmap) {

int32_t pixelFormat = 0;
OH_PixelmapImageInfo_GetPixelFormat(currentInfo, &pixelFormat);
ColorType colorType = HarmonyImage::ToTGFXColorType(pixelFormat);
ColorType colorType = OHOSConverter::ToTGFXColorType(pixelFormat);

int32_t alpha = 0;
OH_PixelmapImageInfo_GetAlphaType(currentInfo, &alpha);
AlphaType alphaType = HarmonyImage::ToTGFXAlphaType(alpha);
AlphaType alphaType = OHOSConverter::ToTGFXAlphaType(alpha);
if (alphaType == AlphaType::Unknown) {
alphaType = AlphaType::Unpremultiplied;
}
Expand Down
2 changes: 1 addition & 1 deletion src/platform/ohos/NativeCodec.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ class NativeCodec : public ImageCodec {

friend class ImageCodec;
};
} // namespace tgfx
} // namespace tgfx
Loading

0 comments on commit 27c2ce9

Please sign in to comment.