From b16882ce2aad9fe62a97c8687083dc42921046b1 Mon Sep 17 00:00:00 2001 From: wangyihui Date: Mon, 20 Mar 2023 15:30:33 +0000 Subject: [PATCH] =?UTF-8?q?image=E6=94=AF=E6=8C=81drawable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangyihui Change-Id: Ie1a5f392a161d284b9cb4719d28bff78c9c689a7 --- adapter/ohos/osal/pixel_map_ohos.cpp | 23 ++- adapter/preview/osal/pixel_map_preview.cpp | 5 + frameworks/base/image/pixel_map.h | 3 +- .../declarative_frontend/jsview/js_image.cpp | 34 +++- .../declarative_frontend/jsview/js_image.h | 2 + .../declarative_frontend/jsview/js_utils.cpp | 36 ++-- .../declarative_frontend/jsview/js_utils.h | 1 + .../inner_api/drawable_descriptor/BUILD.gn | 1 + .../drawable_descriptor.cpp | 173 ++++++------------ .../drawable_descriptor/drawable_descriptor.h | 19 +- .../drawable_descriptor/image_converter.cpp | 80 ++++++++ .../drawable_descriptor/image_converter.h | 36 ++++ .../js_drawable_descriptor.cpp | 14 +- .../js_drawable_descriptor.h | 2 +- 14 files changed, 250 insertions(+), 179 deletions(-) create mode 100644 interfaces/inner_api/drawable_descriptor/image_converter.cpp create mode 100644 interfaces/inner_api/drawable_descriptor/image_converter.h diff --git a/adapter/ohos/osal/pixel_map_ohos.cpp b/adapter/ohos/osal/pixel_map_ohos.cpp index 125b27bb55b..4bb919e511f 100644 --- a/adapter/ohos/osal/pixel_map_ohos.cpp +++ b/adapter/ohos/osal/pixel_map_ohos.cpp @@ -17,9 +17,10 @@ #include +#include "drawable_descriptor.h" + #include "base/log/log_wrapper.h" #include "base/utils/utils.h" -#include "drawable_descriptor.h" namespace OHOS::Ace { @@ -71,15 +72,19 @@ AlphaType PixelMapOhos::AlphaTypeConverter(Media::AlphaType alphaType) RefPtr PixelMap::CreatePixelMap(void* rawPtr) { - auto pixmapPtr = reinterpret_cast*>(rawPtr); - auto drawablePtr = reinterpret_cast*>(rawPtr); - if (pixmapPtr && *pixmapPtr) { - return AceType::MakeRefPtr(*pixmapPtr); - } - if (drawablePtr && *drawablePtr) { - return AceType::MakeRefPtr((*drawablePtr)->GetPixelMap()); + std::shared_ptr* pixmapPtr = reinterpret_cast*>(rawPtr); + if (pixmapPtr == nullptr || *pixmapPtr == nullptr) { + LOGW("pixmap pointer is nullptr when CreatePixelMap."); + return nullptr; } - return nullptr; + return AceType::MakeRefPtr(*pixmapPtr); +} + +RefPtr PixelMap::GetFromDrawable(void* ptr) +{ + CHECK_NULL_RETURN(ptr, nullptr); + auto* drawable = reinterpret_cast(ptr); + return AceType::MakeRefPtr(drawable->GetPixelMap()); } RefPtr PixelMap::CreatePixelMapFromDataAbility(void* uniquePtr) diff --git a/adapter/preview/osal/pixel_map_preview.cpp b/adapter/preview/osal/pixel_map_preview.cpp index cc6ccc13c5f..112ffefba42 100644 --- a/adapter/preview/osal/pixel_map_preview.cpp +++ b/adapter/preview/osal/pixel_map_preview.cpp @@ -25,6 +25,11 @@ RefPtr PixelMap::CreatePixelMap(void* rawPtr) return nullptr; } +RefPtr PixelMap::GetFromDrawable(void* ptr) +{ + return nullptr; +} + RefPtr PixelMap::CreatePixelMapFromDataAbility(void* uniquePtr) { return nullptr; diff --git a/frameworks/base/image/pixel_map.h b/frameworks/base/image/pixel_map.h index 5258e7de2d2..6efeec36ed3 100644 --- a/frameworks/base/image/pixel_map.h +++ b/frameworks/base/image/pixel_map.h @@ -52,6 +52,7 @@ class ACE_EXPORT PixelMap : public AceType { public: static RefPtr CreatePixelMap(void* sptrAddr); + static RefPtr GetFromDrawable(void* ptr); static RefPtr CreatePixelMapFromDataAbility(void* uniquePtr); static RefPtr ConvertSkImageToPixmap(const uint32_t *colors, uint32_t colorLength, int32_t width, int32_t height); @@ -79,4 +80,4 @@ public: } // namespace Ace } // namespace OHOS -#endif // FOUNDATION_ACE_FRAMEWORKS_BASE_IMAGE_ACE_PIXEL_MAP_H \ No newline at end of file +#endif // FOUNDATION_ACE_FRAMEWORKS_BASE_IMAGE_ACE_PIXEL_MAP_H diff --git a/frameworks/bridge/declarative_frontend/jsview/js_image.cpp b/frameworks/bridge/declarative_frontend/jsview/js_image.cpp index 7284e20a7a2..f22dcc22dec 100644 --- a/frameworks/bridge/declarative_frontend/jsview/js_image.cpp +++ b/frameworks/bridge/declarative_frontend/jsview/js_image.cpp @@ -177,28 +177,31 @@ void JSImage::Create(const JSCallbackInfo& info) std::string bundleName; std::string moduleName; std::string src; - auto noPixMap = ParseJsMedia(info[0], src); + auto noPixmap = ParseJsMedia(info[0], src); if (context->IsFormRender() && info[0]->IsString()) { SrcType srcType = ImageSourceInfo::ResolveURIType(src); - bool notSupport = ( - srcType == SrcType::NETWORK || srcType == SrcType::FILE || srcType == SrcType::DATA_ABILITY); + bool notSupport = (srcType == SrcType::NETWORK || srcType == SrcType::FILE || srcType == SrcType::DATA_ABILITY); if (notSupport) { LOGE("Not supported src : %{public}s when form render", src.c_str()); src.clear(); } } GetJsMediaBundleInfo(info[0], bundleName, moduleName); - RefPtr pixMap = nullptr; + RefPtr pixmap = nullptr; #if defined(PIXEL_MAP_SUPPORTED) - if (!noPixMap) { + if (!noPixmap) { if (context->IsFormRender()) { - LOGE("Not supported pixMap when form render"); + LOGE("Not supported pixmap when form render"); } else { - pixMap = CreatePixelMapFromNapiValue(info[0]); + if (IsDrawable(info[0])) { + pixmap = GetDrawablePixmap(info[0]); + } else { + pixmap = CreatePixelMapFromNapiValue(info[0]); + } } } #endif - ImageModel::GetInstance()->Create(src, noPixMap, pixMap, bundleName, moduleName); + ImageModel::GetInstance()->Create(src, noPixmap, pixmap, bundleName, moduleName); } // Interim programme @@ -218,6 +221,21 @@ void JSImage::GetJsMediaBundleInfo(const JSRef& jsValue, std::string& bun } } +bool JSImage::IsDrawable(const JSRef& jsValue) +{ + if (!jsValue->IsObject()) { + return false; + } + JSRef jsObj = JSRef::Cast(jsValue); + if (jsObj->IsUndefined()) { + return false; + } + + // if jsObject has function getPixelMap, it's a DrawableDescriptor object + JSRef func = jsObj->GetProperty("getPixelMap"); + return (!func->IsNull() && func->IsFunction()); +} + void JSImage::JsBorder(const JSCallbackInfo& info) { JSViewAbstract::JsBorder(info); diff --git a/frameworks/bridge/declarative_frontend/jsview/js_image.h b/frameworks/bridge/declarative_frontend/jsview/js_image.h index 4087e2201a5..d813302c395 100644 --- a/frameworks/bridge/declarative_frontend/jsview/js_image.h +++ b/frameworks/bridge/declarative_frontend/jsview/js_image.h @@ -63,6 +63,8 @@ public: static void SetBorder(const Border& border); static void SetAutoResize(bool autoResize); + + static bool IsDrawable(const JSRef& jsValue); }; class JSColorFilter : public AceType { diff --git a/frameworks/bridge/declarative_frontend/jsview/js_utils.cpp b/frameworks/bridge/declarative_frontend/jsview/js_utils.cpp index 77f19cbd705..46f797be321 100644 --- a/frameworks/bridge/declarative_frontend/jsview/js_utils.cpp +++ b/frameworks/bridge/declarative_frontend/jsview/js_utils.cpp @@ -86,23 +86,18 @@ RefPtr CreatePixelMapFromNapiValue(JSRef obj) return PixelMap::CreatePixelMap(pixmapPtrAddr); } -const std::shared_ptr CreateRSNodeFromNapiValue(JSRef obj) +namespace { +void* UnwrapNapiValue(const JSRef& obj) { #ifdef ENABLE_ROSEN_BACKEND if (!obj->IsObject()) { - LOGE("info[0] is not an object when try CreateRSNodeFromNapiValue"); + LOGE("info[0] is not an object when try CreateFromNapiValue"); return nullptr; } auto engine = EngineHelper::GetCurrentEngine(); - if (!engine) { - LOGE("CreateRSNodeFromNapiValue engine is null"); - return nullptr; - } + CHECK_NULL_RETURN(engine, nullptr); auto nativeEngine = engine->GetNativeEngine(); - if (nativeEngine == nullptr) { - LOGE("nativeEngine is nullptr."); - return nullptr; - } + CHECK_NULL_RETURN(nativeEngine, nullptr); #ifdef USE_ARK_ENGINE panda::Local value = obj.Get().GetLocalHandle(); #endif @@ -110,16 +105,21 @@ const std::shared_ptr CreateRSNodeFromNapiValue(JSRef obj) ScopeRAII scope(nativeEngine->GetScopeManager()); NativeValue* nativeValue = nativeEngine->ValueToNativeValue(valueWrapper); - if (nativeValue == nullptr) { - LOGE("nativeValue is nullptr."); - return nullptr; - } + CHECK_NULL_RETURN(nativeValue, nullptr); NativeObject* object = static_cast(nativeValue->GetInterface(NativeObject::INTERFACE_ID)); - if (object == nullptr) { - return nullptr; - } + CHECK_NULL_RETURN(object, nullptr); + return object->GetNativePointer(); +} +} // namespace - auto nodePtr = static_cast*>(object->GetNativePointer()); +RefPtr GetDrawablePixmap(JSRef obj) +{ + return PixelMap::GetFromDrawable(UnwrapNapiValue(obj)); +} + +const std::shared_ptr CreateRSNodeFromNapiValue(JSRef obj) +{ + auto nodePtr = static_cast*>(UnwrapNapiValue(obj)); if (nodePtr == nullptr) { return nullptr; } diff --git a/frameworks/bridge/declarative_frontend/jsview/js_utils.h b/frameworks/bridge/declarative_frontend/jsview/js_utils.h index d712cc7221c..aa724b6fa9d 100644 --- a/frameworks/bridge/declarative_frontend/jsview/js_utils.h +++ b/frameworks/bridge/declarative_frontend/jsview/js_utils.h @@ -36,6 +36,7 @@ class WantWrap; namespace OHOS::Ace::Framework { RefPtr CreatePixelMapFromNapiValue(JSRef obj); const std::shared_ptr CreateRSNodeFromNapiValue(JSRef obj); + RefPtr GetDrawablePixmap(JSRef obj); RefPtr CreateWantWrapFromNapiValue(JSRef obj); } // namespace OHOS::Ace::Framework #endif diff --git a/interfaces/inner_api/drawable_descriptor/BUILD.gn b/interfaces/inner_api/drawable_descriptor/BUILD.gn index 933f26941d4..11e20b8f8c4 100644 --- a/interfaces/inner_api/drawable_descriptor/BUILD.gn +++ b/interfaces/inner_api/drawable_descriptor/BUILD.gn @@ -34,6 +34,7 @@ ohos_shared_library("drawable_descriptor") { sources = [ "drawable_descriptor.cpp", + "image_converter.cpp", "js_drawable_descriptor.cpp", ] diff --git a/interfaces/inner_api/drawable_descriptor/drawable_descriptor.cpp b/interfaces/inner_api/drawable_descriptor/drawable_descriptor.cpp index ce01793736e..2feed1ebf1f 100644 --- a/interfaces/inner_api/drawable_descriptor/drawable_descriptor.cpp +++ b/interfaces/inner_api/drawable_descriptor/drawable_descriptor.cpp @@ -18,20 +18,19 @@ #include #include +#include "SkBlendMode.h" +#include "SkCanvas.h" +#include "SkImage.h" +#include "SkPaint.h" +#include "SkRect.h" #include "cJSON.h" +#include "image_source.h" -#include "foundation/graphic/graphic_2d/rosen/modules/2d_graphics/include/draw/blend_mode.h" -#include "foundation/graphic/graphic_2d/rosen/modules/2d_graphics/include/draw/brush.h" -#include "foundation/graphic/graphic_2d/rosen/modules/2d_graphics/include/draw/canvas.h" -#include "foundation/graphic/graphic_2d/rosen/modules/2d_graphics/include/image/image.h" -#include "foundation/graphic/graphic_2d/rosen/modules/2d_graphics/include/utils/rect.h" -#include "foundation/graphic/graphic_2d/rosen/modules/2d_graphics/include/utils/sampling_options.h" -#include "platform/image_native/image_type.h" namespace OHOS::Ace::Napi { namespace { const char DRAWABLEDESCRIPTOR_JSON_KEY_BACKGROUND[] = "background"; const char DRAWABLEDESCRIPTOR_JSON_KEY_FOREGROUND[] = "foreground"; -} +} // namespace bool DrawableDescriptor::GetPixelMapFromBuffer() { @@ -44,6 +43,7 @@ bool DrawableDescriptor::GetPixelMapFromBuffer() return false; } Media::DecodeOptions decodeOpts; + decodeOpts.desiredPixelFormat = Media::PixelFormat::BGRA_8888; auto pixelMapPtr = imageSource->CreatePixelMap(decodeOpts, errorCode); pixelMap_ = std::shared_ptr(pixelMapPtr.release()); if (errorCode != 0 || !pixelMap_) { @@ -57,14 +57,12 @@ std::shared_ptr DrawableDescriptor::GetPixelMap() { if (pixelMap_.has_value()) { return pixelMap_.value(); - } else { - if (GetPixelMapFromBuffer()) { - return pixelMap_.value(); - } else { - HILOG_ERROR("Failed to GetPixelMap"); - return nullptr; - } } + if (GetPixelMapFromBuffer()) { + return pixelMap_.value(); + } + HILOG_ERROR("Failed to GetPixelMap!"); + return nullptr; } std::unique_ptr LayeredDrawableDescriptor::CreateImageSource(const char* item, uint32_t& errorCode) @@ -73,13 +71,13 @@ std::unique_ptr LayeredDrawableDescriptor::CreateImageSource std::string idStr = itemStr.substr(itemStr.find(':') + 1); size_t len = 0; std::unique_ptr data; - + auto state = resourceMgr_->GetMediaDataById(static_cast(std::stoul(idStr)), len, data); if (state != Global::Resource::SUCCESS) { HILOG_ERROR("GetMediaDataById failed"); return nullptr; } - + Media::SourceOptions opts; return Media::ImageSource::CreateImageSource(data.get(), len, opts, errorCode); } @@ -125,7 +123,7 @@ bool LayeredDrawableDescriptor::GetPixelMapFromJsonBuf(bool isBackground) foreground_ = std::shared_ptr(pixelMapPtr.release()); } } else { - HILOG_ERROR("GetPixelMapFromJsonBuf from json buffer failed"); + HILOG_ERROR("Get background from json buffer failed"); return false; } return true; @@ -142,6 +140,7 @@ bool LayeredDrawableDescriptor::GetDefaultMask() std::unique_ptr imageSource = Media::ImageSource::CreateImageSource(data.get(), len, opts, errorCode); Media::DecodeOptions decodeOpts; + decodeOpts.desiredPixelFormat = Media::PixelFormat::BGRA_8888; auto pixelMapPtr = imageSource->CreatePixelMap(decodeOpts, errorCode); mask_ = std::shared_ptr(pixelMapPtr.release()); if (errorCode != 0 || !mask_) { @@ -161,6 +160,7 @@ bool LayeredDrawableDescriptor::GetMaskByName(const std::string& name) std::unique_ptr imageSource = Media::ImageSource::CreateImageSource(data.get(), len, opts, errorCode); Media::DecodeOptions decodeOpts; + decodeOpts.desiredPixelFormat = Media::PixelFormat::BGRA_8888; auto pixelMapPtr = imageSource->CreatePixelMap(decodeOpts, errorCode); mask_ = std::shared_ptr(pixelMapPtr.release()); if (errorCode != 0 || !mask_) { @@ -193,7 +193,6 @@ std::unique_ptr LayeredDrawableDescriptor::GetBackground() if (GetPixelMapFromJsonBuf(true)) { return std::make_unique(background_.value()); } - HILOG_ERROR("GetForeground failed"); return nullptr; } @@ -212,124 +211,62 @@ std::unique_ptr LayeredDrawableDescriptor::GetMask() return nullptr; } -Rosen::Drawing::ColorType LayeredDrawableDescriptor::PixelFormatToColorType(Media::PixelFormat pixelFormat) -{ - switch (pixelFormat) { - case Media::PixelFormat::RGB_565: - return Rosen::Drawing::ColorType::COLORTYPE_RGB_565; - case Media::PixelFormat::RGBA_8888: - return Rosen::Drawing::ColorType::COLORTYPE_RGBA_8888; - case Media::PixelFormat::BGRA_8888: - return Rosen::Drawing::ColorType::COLORTYPE_BGRA_8888; - case Media::PixelFormat::ALPHA_8: - return Rosen::Drawing::ColorType::COLORTYPE_ALPHA_8; - case Media::PixelFormat::ARGB_8888: - case Media::PixelFormat::RGB_888: - case Media::PixelFormat::RGBA_F16: - case Media::PixelFormat::NV21: - case Media::PixelFormat::NV12: - case Media::PixelFormat::CMYK: - case Media::PixelFormat::UNKNOWN: - default: - return Rosen::Drawing::ColorType::COLORTYPE_UNKNOWN; - } -} - -Rosen::Drawing::AlphaType LayeredDrawableDescriptor::AlphaTypeToRSAlphaType(Media::AlphaType alphaType) -{ - switch (alphaType) { - case Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE: - return Rosen::Drawing::AlphaType::ALPHATYPE_OPAQUE; - case Media::AlphaType::IMAGE_ALPHA_TYPE_PREMUL: - return Rosen::Drawing::AlphaType::ALPHATYPE_PREMUL; - case Media::AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL: - return Rosen::Drawing::AlphaType::ALPHATYPE_UNPREMUL; - case Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN: - default: - return Rosen::Drawing::AlphaType::ALPHATYPE_UNKNOWN; - } -} - -std::shared_ptr LayeredDrawableDescriptor::PixelMapToBitMap( - const std::shared_ptr& pixelMap) -{ - Rosen::Drawing::Bitmap bitmap; - Rosen::Drawing::ColorType colorType = LayeredDrawableDescriptor::PixelFormatToColorType(pixelMap->GetPixelFormat()); - Rosen::Drawing::AlphaType rAlphaType = LayeredDrawableDescriptor::AlphaTypeToRSAlphaType(pixelMap->GetAlphaType()); - Rosen::Drawing::BitmapFormat format { colorType, rAlphaType }; - auto data = pixelMap->GetPixels(); - bitmap.Build(pixelMap->GetWidth(), pixelMap->GetHeight(), format); - bitmap.SetPixels(const_cast(data)); - return std::make_shared(bitmap); -} - -std::shared_ptr LayeredDrawableDescriptor::BitMapToPixelMap( - const std::shared_ptr& bitMap, Media::InitializationOptions& opts) -{ - auto data = bitMap->GetPixels(); - opts.size.width = bitMap->GetWidth(); - opts.size.height = bitMap->GetHeight(); - pixelMap_ = Media::PixelMap::Create(reinterpret_cast(data), opts.size.width * opts.size.height, opts); - return pixelMap_.value(); -} - bool LayeredDrawableDescriptor::CreatePixelMap() { - // get pixelMap - std::shared_ptr foreground; - bool isBackground = false; + std::shared_ptr foreground; if (foreground_.has_value()) { - foreground = PixelMapToBitMap(foreground_.value()); - } else if (GetPixelMapFromJsonBuf(isBackground)) { - foreground = PixelMapToBitMap(foreground_.value()); + foreground = ImageConverter::PixelMapToBitmap(foreground_.value()); + } else if (GetPixelMapFromJsonBuf(false)) { + foreground = ImageConverter::PixelMapToBitmap(foreground_.value()); } else { - HILOG_ERROR("Get piexelmap of foreground faied."); + HILOG_INFO("Get pixelMap of foreground failed."); return false; } - - std::shared_ptr background; - isBackground = true; + + std::shared_ptr background; if (background_.has_value()) { - background = PixelMapToBitMap(background_.value()); - } else if (GetPixelMapFromJsonBuf(isBackground)) { - background = PixelMapToBitMap(background_.value()); + background = ImageConverter::PixelMapToBitmap(background_.value()); + } else if (GetPixelMapFromJsonBuf(true)) { + background = ImageConverter::PixelMapToBitmap(background_.value()); } else { - HILOG_ERROR("Get piexelmap of background faied."); + HILOG_ERROR("Get pixelMap of background failed."); return false; } - std::shared_ptr mask; + std::shared_ptr mask; if (mask_.has_value()) { - mask = PixelMapToBitMap(mask_.value()); + mask = ImageConverter::PixelMapToBitmap(mask_.value()); } else if (GetDefaultMask()) { - mask = PixelMapToBitMap(mask_.value()); + mask = ImageConverter::PixelMapToBitmap(mask_.value()); } else { - HILOG_ERROR("Get piexelmap of mask faied."); + HILOG_ERROR("Get pixelMap of mask failed."); return false; } - Rosen::Drawing::Canvas canvas; - canvas.Bind(static_cast(*(background.get()))); - Rosen::Drawing::Brush brush; - brush.SetAntiAlias(true); - canvas.AttachBrush(brush); - Rosen::Drawing::Image image; - image.BuildFromBitmap(static_cast(*(foreground.get()))); - Rosen::Drawing::RectF dst = Rosen::Drawing::RectF( - 0.0f, static_cast(foreground->GetWidth()), 0.0f, static_cast(foreground->GetHeight())); - Rosen::Drawing::SamplingOptions ops; - canvas.DrawImageRect(image, dst, ops); - - brush.SetBlendMode(Rosen::Drawing::BlendMode::DST_IN); - canvas.AttachBrush(brush); - image.BuildFromBitmap(static_cast(*(mask.get()))); - canvas.DrawImageRect(image, dst, ops); - - // convert bitMap back to pixelMap + SkPaint paint; + paint.setAntiAlias(true); + SkCanvas bitmapCanvas(*background); + auto rect1 = SkRect::MakeWH(static_cast(mask->width()), static_cast(mask->height())); + auto rect2 = SkRect::MakeXYWH(0, 0, static_cast(background->width()), + static_cast(background->height())); + paint.setBlendMode(SkBlendMode::kDstATop); + bitmapCanvas.drawImageRect(SkImage::MakeFromBitmap(*mask), rect1, rect2, &paint); + + rect1 = SkRect::MakeWH(static_cast(foreground->width()), static_cast(foreground->height())); + auto x = static_cast((background->width() - foreground->width()) / 2); + auto y = static_cast((background->height() - foreground->height()) / 2); + rect2 = SkRect::MakeXYWH(x, y, static_cast(foreground->width()), static_cast(foreground->height())); + paint.setBlendMode(SkBlendMode::kSrcATop); + bitmapCanvas.drawImageRect(SkImage::MakeFromBitmap(*foreground), rect1, rect2, &paint); + SkBitmap result; + result.allocPixels(background->info()); + bitmapCanvas.readPixels(result, 0, 0); + + // convert bitmap back to pixelMap Media::InitializationOptions opts; opts.alphaType = background_.value()->GetAlphaType(); - opts.pixelFormat = background_.value()->GetPixelFormat(); - pixelMap_ = BitMapToPixelMap(background, opts); + opts.pixelFormat = Media::PixelFormat::BGRA_8888; + pixelMap_ = ImageConverter::BitmapToPixelMap(std::make_shared(result), opts); return true; } diff --git a/interfaces/inner_api/drawable_descriptor/drawable_descriptor.h b/interfaces/inner_api/drawable_descriptor/drawable_descriptor.h index 3852fe53e98..0d6a8277f82 100644 --- a/interfaces/inner_api/drawable_descriptor/drawable_descriptor.h +++ b/interfaces/inner_api/drawable_descriptor/drawable_descriptor.h @@ -28,13 +28,11 @@ #include #include + +#include "resource_manager.h" +#include "image_converter.h" #include "base/memory/ace_type.h" #include "foundation/arkui/ace_engine/interfaces/inner_api/form_render/include/form_renderer_hilog.h" -#include "image_source.h" -#include "foundation/graphic/graphic_2d/rosen/modules/2d_graphics/include/draw/color.h" -#include "foundation/graphic/graphic_2d/rosen/modules/2d_graphics/include/image/bitmap.h" -#include "pixel_map.h" -#include "resource_manager.h" namespace OHOS::Ace::Napi { @@ -48,7 +46,7 @@ public: private: bool GetPixelMapFromBuffer(); - + std::unique_ptr jsonBuf_; size_t len_ = 0; std::optional> pixelMap_; @@ -66,18 +64,11 @@ public: std::shared_ptr GetPixelMap() override; private: + friend class ImageConverter; std::unique_ptr CreateImageSource(const char* item, uint32_t& errorCode); bool GetPixelMapFromJsonBuf(bool isBackground); bool GetDefaultMask(); bool GetMaskByName(const std::string& name); - static Rosen::Drawing::ColorType PixelFormatToColorType(Media::PixelFormat pixelFormat); - static Rosen::Drawing::AlphaType AlphaTypeToRSAlphaType(Media::AlphaType alphaType); - static Media::PixelFormat ColorTypeToPixelFormat(Rosen::Drawing::ColorType colorType); - static Media::AlphaType RSAlphaTypeToAlphaType(Rosen::Drawing::AlphaType alphaType); - - static std::shared_ptr PixelMapToBitMap(const std::shared_ptr& pixelMap); - std::shared_ptr BitMapToPixelMap( - const std::shared_ptr& bitMap, Media::InitializationOptions& opts); bool CreatePixelMap(); std::unique_ptr jsonBuf_; diff --git a/interfaces/inner_api/drawable_descriptor/image_converter.cpp b/interfaces/inner_api/drawable_descriptor/image_converter.cpp new file mode 100644 index 00000000000..d38edd97147 --- /dev/null +++ b/interfaces/inner_api/drawable_descriptor/image_converter.cpp @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "image_converter.h" + +#include "platform/image_native/image_type.h" +#include "image_utils.h" + +namespace OHOS::Ace::Napi { +SkColorType ImageConverter::PixelFormatToSkColorType(Media::PixelFormat pixelFormat) +{ + switch (pixelFormat) { + case Media::PixelFormat::BGRA_8888: + return SkColorType::kBGRA_8888_SkColorType; + case Media::PixelFormat::ARGB_8888: + case Media::PixelFormat::ALPHA_8: + case Media::PixelFormat::RGBA_8888: + case Media::PixelFormat::RGB_565: + case Media::PixelFormat::RGB_888: + case Media::PixelFormat::RGBA_F16: + case Media::PixelFormat::NV21: + case Media::PixelFormat::NV12: + case Media::PixelFormat::CMYK: + case Media::PixelFormat::UNKNOWN: + default: + return SkColorType::kUnknown_SkColorType; + } +} + +SkAlphaType ImageConverter::AlphaTypeToSkAlphaType(Media::AlphaType alphaType) +{ + switch (alphaType) { + case Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE: + return SkAlphaType::kOpaque_SkAlphaType; + case Media::AlphaType::IMAGE_ALPHA_TYPE_PREMUL: + return SkAlphaType::kPremul_SkAlphaType; + case Media::AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL: + return SkAlphaType::kUnpremul_SkAlphaType; + case Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN: + default: + return SkAlphaType::kUnknown_SkAlphaType; + } +} + +std::shared_ptr ImageConverter::PixelMapToBitmap( + const std::shared_ptr& pixelMap) +{ + auto data = pixelMap->GetPixels(); + SkBitmap bitmap; + SkColorType colorType = ImageConverter::PixelFormatToSkColorType(pixelMap->GetPixelFormat()); + SkAlphaType alphaType = ImageConverter::AlphaTypeToSkAlphaType(pixelMap->GetAlphaType()); + auto imageInfo = SkImageInfo::Make(pixelMap->GetWidth(), pixelMap->GetHeight(), colorType, alphaType); + bitmap.setInfo(imageInfo); + bitmap.setPixels(const_cast(data)); + return std::make_shared(bitmap); +} + +std::shared_ptr ImageConverter::BitmapToPixelMap( + const std::shared_ptr& bitMap, Media::InitializationOptions& opts) +{ + auto data = bitMap->getPixels(); + opts.size.width = static_cast(bitMap->width()); + opts.size.height = static_cast(bitMap->height()); + auto pixelMap = Media::PixelMap::Create(reinterpret_cast(data), + opts.size.width * opts.size.height, opts); + return pixelMap; +} +} // namespace OHOS::Ace::Napi diff --git a/interfaces/inner_api/drawable_descriptor/image_converter.h b/interfaces/inner_api/drawable_descriptor/image_converter.h new file mode 100644 index 00000000000..9d2ca6bea6f --- /dev/null +++ b/interfaces/inner_api/drawable_descriptor/image_converter.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef FOUNDATION_ACE_INTERFACE_INNERKITS_IMAGE_CONVERTER_H +#define FOUNDATION_ACE_INTERFACE_INNERKITS_IMAGE_CONVERTER_H + +#include "SkBitmap.h" +#include "SkImageInfo.h" +#include "pixel_map.h" +#include "image_source.h" + +namespace OHOS::Ace::Napi { +class ImageConverter { +public: + std::unique_ptr CreateImageSource(const char* item, uint32_t& errorCode); + static SkColorType PixelFormatToSkColorType(Media::PixelFormat pixelFormat); + static SkAlphaType AlphaTypeToSkAlphaType(Media::AlphaType alphaType); + static std::shared_ptr PixelMapToBitmap( + const std::shared_ptr& pixelMap); + static std::shared_ptr BitmapToPixelMap( + const std::shared_ptr& bitMap, Media::InitializationOptions& opts); +}; +} // namespace OHOS::Ace::Napi +#endif // #define FOUNDATION_ACE_INTERFACE_INNERKITS_IMAGE_CONVERTER_H + diff --git a/interfaces/inner_api/drawable_descriptor/js_drawable_descriptor.cpp b/interfaces/inner_api/drawable_descriptor/js_drawable_descriptor.cpp index b1efc954ac0..b99916847b3 100644 --- a/interfaces/inner_api/drawable_descriptor/js_drawable_descriptor.cpp +++ b/interfaces/inner_api/drawable_descriptor/js_drawable_descriptor.cpp @@ -31,8 +31,8 @@ constexpr char DRAWABLE_LAYERED[] = "LayeredDrawableDescriptor"; } // namespace namespace OHOS::Ace::Napi { -thread_local napi_ref JsDrawableDescriptor::baseConstructor_ = nullptr; -thread_local napi_ref JsDrawableDescriptor::layeredConstructor_ = nullptr; +thread_local napi_ref JsDrawableDescriptor::baseConstructor_; +thread_local napi_ref JsDrawableDescriptor::layeredConstructor_; napi_value JsDrawableDescriptor::Constructor(napi_env env, napi_callback_info info) { @@ -87,7 +87,7 @@ napi_value JsDrawableDescriptor::ToNapi(napi_env env, DrawableDescriptor* drawab if (!drawable) { return nullptr; } - if (!baseConstructor_) { + if (!layeredConstructor_) { // init js class constructor by importing module manually napi_value globalValue; napi_get_global(env, &globalValue); @@ -102,13 +102,7 @@ napi_value JsDrawableDescriptor::ToNapi(napi_env env, DrawableDescriptor* drawab napi_value constructor = nullptr; napi_value result = nullptr; - napi_status status; - if (static_cast(drawable)) { - status = napi_get_reference_value(env, layeredConstructor_, &constructor); - } else { - status = napi_get_reference_value(env, baseConstructor_, &constructor); - } - + napi_status status = napi_get_reference_value(env, layeredConstructor_, &constructor); if (status == napi_status::napi_ok) { NAPI_CALL(env, napi_new_instance(env, constructor, 0, nullptr, &result)); NAPI_CALL(env, napi_wrap(env, result, drawable, Destructor, nullptr, nullptr)); diff --git a/interfaces/inner_api/drawable_descriptor/js_drawable_descriptor.h b/interfaces/inner_api/drawable_descriptor/js_drawable_descriptor.h index 1ea571919a0..331ed141429 100644 --- a/interfaces/inner_api/drawable_descriptor/js_drawable_descriptor.h +++ b/interfaces/inner_api/drawable_descriptor/js_drawable_descriptor.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 Huawei Device Co., Ltd. + * Copyright (C) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at -- Gitee