From 9e003262982cc656ef74ad053300c17ac15f9d1d Mon Sep 17 00:00:00 2001 From: li-kiao Date: Sat, 20 Jan 2024 13:11:53 +0000 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Eimage=E7=9B=B8=E5=85=B3Drawin?= =?UTF-8?q?g=20C=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: li-kiao --- graphic/graphic_2d/native_drawing/BUILD.gn | 4 + .../graphic_2d/native_drawing/drawing_brush.h | 15 ++- .../native_drawing/drawing_canvas.h | 47 +++++++- .../graphic_2d/native_drawing/drawing_font.h | 31 ++++++ .../graphic_2d/native_drawing/drawing_image.h | 104 ++++++++++++++++++ .../graphic_2d/native_drawing/drawing_pen.h | 19 +++- .../native_drawing/drawing_sampling_options.h | 100 +++++++++++++++++ .../graphic_2d/native_drawing/drawing_types.h | 18 ++- .../native_drawing/libnative_drawing.ndk.json | 13 +++ 9 files changed, 340 insertions(+), 11 deletions(-) create mode 100644 graphic/graphic_2d/native_drawing/drawing_image.h create mode 100644 graphic/graphic_2d/native_drawing/drawing_sampling_options.h diff --git a/graphic/graphic_2d/native_drawing/BUILD.gn b/graphic/graphic_2d/native_drawing/BUILD.gn index b20e52f90..60e24af8d 100644 --- a/graphic/graphic_2d/native_drawing/BUILD.gn +++ b/graphic/graphic_2d/native_drawing/BUILD.gn @@ -25,6 +25,7 @@ ohos_ndk_headers("native_drawing_header") { "//interface/sdk_c/graphic/graphic_2d/native_drawing/drawing_filter.h", "//interface/sdk_c/graphic/graphic_2d/native_drawing/drawing_font.h", "//interface/sdk_c/graphic/graphic_2d/native_drawing/drawing_font_collection.h", + "//interface/sdk_c/graphic/graphic_2d/native_drawing/drawing_image.h", "//interface/sdk_c/graphic/graphic_2d/native_drawing/drawing_mask_filter.h", "//interface/sdk_c/graphic/graphic_2d/native_drawing/drawing_matrix.h", "//interface/sdk_c/graphic/graphic_2d/native_drawing/drawing_memory_stream.h", @@ -35,6 +36,7 @@ ohos_ndk_headers("native_drawing_header") { "//interface/sdk_c/graphic/graphic_2d/native_drawing/drawing_rect.h", "//interface/sdk_c/graphic/graphic_2d/native_drawing/drawing_register_font.h", "//interface/sdk_c/graphic/graphic_2d/native_drawing/drawing_round_rect.h", + "//interface/sdk_c/graphic/graphic_2d/native_drawing/drawing_sampling_options.h", "//interface/sdk_c/graphic/graphic_2d/native_drawing/drawing_shader_effect.h", "//interface/sdk_c/graphic/graphic_2d/native_drawing/drawing_text_blob.h", "//interface/sdk_c/graphic/graphic_2d/native_drawing/drawing_text_declaration.h", @@ -68,6 +70,8 @@ ohos_ndk_library("libnative_drawing_ndk") { "native_drawing/drawing_rect.h", "native_drawing/drawing_register_font.h", "native_drawing/drawing_round_rect.h", + "native_drawing/drawing_image.h", + "native_drawing/drawing_sampling_options.h", "native_drawing/drawing_shader_effect.h", "native_drawing/drawing_text_blob.h", "native_drawing/drawing_text_declaration.h", diff --git a/graphic/graphic_2d/native_drawing/drawing_brush.h b/graphic/graphic_2d/native_drawing/drawing_brush.h index 215a3ee39..dca333f2d 100644 --- a/graphic/graphic_2d/native_drawing/drawing_brush.h +++ b/graphic/graphic_2d/native_drawing/drawing_brush.h @@ -21,7 +21,7 @@ * @{ * * @brief Provides functions such as 2D graphics rendering, text drawing, and image display. - * + * * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing * * @since 8 @@ -81,7 +81,7 @@ bool OH_Drawing_BrushIsAntiAlias(const OH_Drawing_Brush*); * * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing * @param OH_Drawing_Brush Indicates the pointer to an OH_Drawing_Brush object. - * @param bool Specifies whether to enable anti-aliasing. The value true means + * @param bool Specifies whether to enable anti-aliasing. The value true means * to enable anti-aliasing, and false means the opposite. * @since 8 * @version 1.0 @@ -154,6 +154,17 @@ void OH_Drawing_BrushSetShaderEffect(OH_Drawing_Brush*, OH_Drawing_ShaderEffect* */ void OH_Drawing_BrushSetFilter(OH_Drawing_Brush*, OH_Drawing_Filter*); +/** + * @brief Sets a blender that implements the specified blendmode enum for a brush. + * + * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing + * @param OH_Drawing_Pen Indicates the pointer to an OH_Drawing_Brush object. + * @param OH_Drawing_BlendMode Indicates the blend mode. + * @since 12 + * @version 1.0 + */ +void OH_Drawing_BrushSetBlendMode(OH_Drawing_Brush*, OH_Drawing_BlendMode); + #ifdef __cplusplus } #endif diff --git a/graphic/graphic_2d/native_drawing/drawing_canvas.h b/graphic/graphic_2d/native_drawing/drawing_canvas.h index 033effd40..5d2aa9fb2 100644 --- a/graphic/graphic_2d/native_drawing/drawing_canvas.h +++ b/graphic/graphic_2d/native_drawing/drawing_canvas.h @@ -21,7 +21,7 @@ * @{ * * @brief Provides functions such as 2D graphics rendering, text drawing, and image display. - * + * * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing * * @since 8 @@ -64,7 +64,7 @@ OH_Drawing_Canvas* OH_Drawing_CanvasCreate(void); void OH_Drawing_CanvasDestroy(OH_Drawing_Canvas*); /** - * @brief Binds a bitmap to a canvas so that the content drawn on the canvas + * @brief Binds a bitmap to a canvas so that the content drawn on the canvas * is output to the bitmap (this process is called CPU rendering). * * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing @@ -87,7 +87,7 @@ void OH_Drawing_CanvasBind(OH_Drawing_Canvas*, OH_Drawing_Bitmap*); void OH_Drawing_CanvasAttachPen(OH_Drawing_Canvas*, const OH_Drawing_Pen*); /** - * @brief Detaches the pen from a canvas so that the canvas will not use the style + * @brief Detaches the pen from a canvas so that the canvas will not use the style * and color of the pen to outline a shape. * * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing @@ -285,7 +285,7 @@ void OH_Drawing_CanvasDrawTextBlob(OH_Drawing_Canvas*, const OH_Drawing_TextBlob /** * @brief Enumerates clip op. - * + * * @since 11 * @version 1.0 */ @@ -376,6 +376,45 @@ void OH_Drawing_CanvasScale(OH_Drawing_Canvas*, float sx, float sy); */ void OH_Drawing_CanvasClear(OH_Drawing_Canvas*, uint32_t color); +/** + * @brief Sets matrix of canvas. + * + * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing + * @param OH_Drawing_Canvas Indicates the pointer to an OH_Drawing_Canvas object. + * @param OH_Drawing_Matrix Indicates the pointer to an OH_Drawing_Canvas object. + * @since 12 + * @version 1.0 + */ +void OH_Drawing_CanvasSetMatrix(OH_Drawing_Canvas*, OH_Drawing_Matrix*); + +/** + * @brief Draws the specified source rectangle of the image onto the canvas, + * scaled and translated to the destination rectangle. + * + * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing + * @param OH_Drawing_Canvas Indicates the pointer to an OH_Drawing_Canvas object. + * @param OH_Drawing_Matrix Indicates the pointer to an OH_Drawing_Image object. + * @param OH_Drawing_Rect Indicates the pointer to an OH_Drawing_Rect object. + * @param OH_Drawing_SamplingOptions Indicates the pointer to an OH_Drawing_SamplingOptions object. + * @since 12 + * @version 1.0 + */ +void OH_Drawing_CanvasDrawImageRect(OH_Drawing_Canvas*, OH_Drawing_Image*, OH_Drawing_Rect* dst, OH_Drawing_SamplingOptions*); + +/** + * @brief Read pixels data to a bitmap from canvas. + * + * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing + * @param OH_Drawing_Canvas Indicates the pointer to an OH_Drawing_Canvas object. + * @param OH_Drawing_Bitmap Indicates the pointer to an OH_Drawing_Bitmap object. + * @param srcX offset into SkCanvas writable pixels on x-axis. + * @param srcY offset into SkCanvas writable pixels on y-axis. + * @return true if pixels are copied to dstBitmap. + * @since 12 + * @version 1.0 + */ +bool OH_Drawing_CanvasReadPixelsToBitmap(OH_Drawing_Canvas*, OH_Drawing_Bitmap*, int32_t srcX, int32_t srcY); + /** * @brief Get the width of a canvas. * diff --git a/graphic/graphic_2d/native_drawing/drawing_font.h b/graphic/graphic_2d/native_drawing/drawing_font.h index 6949e5f32..437326df7 100644 --- a/graphic/graphic_2d/native_drawing/drawing_font.h +++ b/graphic/graphic_2d/native_drawing/drawing_font.h @@ -140,6 +140,37 @@ void OH_Drawing_FontSetFakeBoldText(OH_Drawing_Font*, bool isFakeBoldText); */ void OH_Drawing_FontDestroy(OH_Drawing_Font*); +/** + * @brief Defines a run, supplies storage for the metrics of an SkFont. + * + * @since 12 + * @version 1.0 + */ +typedef struct { + /** storage for top in font metrics */ + float top; + /** storage for ascent in font metrics */ + float ascent; + /** storage for descent in font metrics */ + float descent; + /** storage for bottom in font metrics */ + float bottom; + /** storage for leading in font metrics */ + float leading; +} OH_Drawing_Font_Metrics; + +/** + * @brief Obtains the metrics of a font. + * + * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing + * @param OH_Drawing_Font Indicates the pointer to an OH_Drawing_Font object. + * @param OH_Drawing_Font_Metrics Indicates the pointer to an OH_Drawing_Font_Metrics object. + * @return Returns a float variable that recommended spacing between lines. + * @since 12 + * @version 1.0 + */ +float OH_Drawing_FontGetMetrics(OH_Drawing_Font*, OH_Drawing_Font_Metrics*); + #ifdef __cplusplus } #endif diff --git a/graphic/graphic_2d/native_drawing/drawing_image.h b/graphic/graphic_2d/native_drawing/drawing_image.h new file mode 100644 index 000000000..bf96b7b34 --- /dev/null +++ b/graphic/graphic_2d/native_drawing/drawing_image.h @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2021-2022 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 C_INCLUDE_DRAWING_IMAGE_H +#define C_INCLUDE_DRAWING_IMAGE_H + +/** + * @addtogroup Drawing + * @{ + * + * @brief Provides functions such as 2D graphics rendering, text drawing, and image display. + * + * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing + * + * @since 12 + * @version 1.0 + */ + +/** + * @file drawing_image.h + * + * @brief Declares functions related to the image object in the drawing module. + * + * @since 12 + * @version 1.0 + */ + +#include "drawing_types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Creates an OH_Drawing_Image object. + * + * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing + * @return Returns the pointer to the OH_Drawing_Image object created. + * @since 12 + * @version 1.0 + */ +OH_Drawing_Image* OH_Drawing_ImageCreate(void); + +/** + * @brief Destroys an OH_Drawing_Image object and reclaims the memory occupied by the object. + * + * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing + * @param OH_Drawing_Image Indicates the pointer to an OH_Drawing_Image object. + * @since 12 + * @version 1.0 + */ +void OH_Drawing_ImageDestroy(OH_Drawing_Image*); + +/** + * @brief Rebuilds an OH_Drawing_Image object, sharing or copying bitmap pixels. + * + * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing + * @param OH_Drawing_Image Indicates the pointer to an OH_Drawing_Image object. + * @param OH_Drawing_Bitmap Indicates the pointer to an OH_Drawing_Bitmap object. + * @return Returns true if successed. + * @since 12 + * @version 1.0 + */ +bool OH_Drawing_ImageBuildFromBitmap(OH_Drawing_Image*, OH_Drawing_Bitmap*); + +/** + * @brief Gets pixel count in each row of image. + * + * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing + * @param OH_Drawing_Image Indicates the pointer to an OH_Drawing_Image object. + * @return Returns the width. + * @since 12 + * @version 1.0 + */ +int32_t OH_Drawing_ImageGetWidth(OH_Drawing_Image*); + +/** + * @brief Gets pixel row count of image. + * + * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing + * @param OH_Drawing_Image Indicates the pointer to an OH_Drawing_Image object. + * @return Returns the height. + * @since 12 + * @version 1.0 + */ +int32_t OH_Drawing_ImageGetHeight(OH_Drawing_Image*); + +#ifdef __cplusplus +} +#endif +/** @} */ +#endif \ No newline at end of file diff --git a/graphic/graphic_2d/native_drawing/drawing_pen.h b/graphic/graphic_2d/native_drawing/drawing_pen.h index 220e5723d..e94fcb24b 100644 --- a/graphic/graphic_2d/native_drawing/drawing_pen.h +++ b/graphic/graphic_2d/native_drawing/drawing_pen.h @@ -21,7 +21,7 @@ * @{ * * @brief Provides functions such as 2D graphics rendering, text drawing, and image display. - * + * * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing * * @since 8 @@ -183,9 +183,9 @@ float OH_Drawing_PenGetMiterLimit(const OH_Drawing_Pen*); void OH_Drawing_PenSetMiterLimit(OH_Drawing_Pen*, float miter); /** - * @brief Enumerates line cap styles of a pen. The line cap style defines + * @brief Enumerates line cap styles of a pen. The line cap style defines * the style of both ends of a line segment drawn by the pen. - * + * * @since 8 * @version 1.0 */ @@ -231,7 +231,7 @@ void OH_Drawing_PenSetCap(OH_Drawing_Pen*, OH_Drawing_PenLineCapStyle); /** * @brief Enumerates pen line join styles. The line join style defines * the shape of the joints of a polyline segment drawn by the pen. - * + * * @since 8 * @version 1.0 */ @@ -291,6 +291,17 @@ void OH_Drawing_PenSetShaderEffect(OH_Drawing_Pen*, OH_Drawing_ShaderEffect*); */ void OH_Drawing_PenSetFilter(OH_Drawing_Pen*, OH_Drawing_Filter*); +/** + * @brief Sets a blender that implements the specified blendmode enum for a pen. + * + * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing + * @param OH_Drawing_Pen Indicates the pointer to an OH_Drawing_Pen object. + * @param OH_Drawing_BlendMode Indicates the blend mode. + * @since 12 + * @version 1.0 + */ +void OH_Drawing_PenSetBlendMode(OH_Drawing_Pen*, OH_Drawing_BlendMode); + #ifdef __cplusplus } #endif diff --git a/graphic/graphic_2d/native_drawing/drawing_sampling_options.h b/graphic/graphic_2d/native_drawing/drawing_sampling_options.h new file mode 100644 index 000000000..d29649408 --- /dev/null +++ b/graphic/graphic_2d/native_drawing/drawing_sampling_options.h @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2021-2022 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 C_INCLUDE_DRAWING_SAMPLING_OPTIONS_H +#define C_INCLUDE_DRAWING_SAMPLING_OPTIONS_H + +/** + * @addtogroup Drawing + * @{ + * + * @brief Provides functions such as 2D graphics rendering, text drawing, and image display. + * + * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing + * + * @since 12 + * @version 1.0 + */ + +/** + * @file drawing_sampling_options.h + * + * @brief Declares functions related to the sampling options object in the drawing module. + * + * @since 12 + * @version 1.0 + */ + +#include "drawing_types.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Enumerates storage filter mode. + * + * @since 12 + * @version 1.0 + */ +typedef enum { + /** single sample point (nearest neighbor) */ + FILTER_MODE_NEAREST, + /** interporate between 2x2 sample points (bilinear interpolation) */ + FILTER_MODE_LINEAR, +} OH_Drawing_FilterMode; + +/** + * @brief Enumerates storage formats mipmap mode. + * + * @since 12 + * @version 1.0 + */ +typedef enum { + /** ignore mipmap levels, sample from the "base" */ + MIPMAP_MODE_NONE, + /** sample from the nearest level */ + MIPMAP_MODE_NEAREST, + /** interpolate between the two nearest levels */ + MIPMAP_MODE_LINEAR, +} OH_Drawing_MipmapMode; + +/** + * @brief Creates an OH_Drawing_SamplingOptions object. + * + * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing + * @param OH_Drawing_FilterMode sampling filter mode. + * @param OH_Drawing_MipmapMode sampling mipmap mode.. + * @return Returns the pointer to the OH_Drawing_SamplingOptions object created. + * @since 12 + * @version 1.0 + */ +OH_Drawing_SamplingOptions* OH_Drawing_SamplingOptionsCreate(OH_Drawing_FilterMode, OH_Drawing_MipmapMode); + +/** + * @brief Destroys an OH_Drawing_SamplingOptions object and reclaims the memory occupied by the object. + * + * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing + * @param OH_Drawing_SamplingOptions Indicates the pointer to an OH_Drawing_SamplingOptions object. + * @since 12 + * @version 1.0 + */ +void OH_Drawing_SamplingOptionsDestroy(OH_Drawing_SamplingOptions*); + +#ifdef __cplusplus +} +#endif +/** @} */ +#endif diff --git a/graphic/graphic_2d/native_drawing/drawing_types.h b/graphic/graphic_2d/native_drawing/drawing_types.h index 4c7b85faf..f3d927d4e 100644 --- a/graphic/graphic_2d/native_drawing/drawing_types.h +++ b/graphic/graphic_2d/native_drawing/drawing_types.h @@ -21,7 +21,7 @@ * @{ * * @brief Provides functions such as 2D graphics rendering, text drawing, and image display. - * + * * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing * * @since 8 @@ -198,6 +198,22 @@ typedef struct OH_Drawing_Typeface OH_Drawing_Typeface; */ typedef struct OH_Drawing_TextBlob OH_Drawing_TextBlob; +/** + * @brief Defines a image, which is used to describe a two dimensional array of pixels to draw. + * + * @since 11 + * @version 1.0 + */ +typedef struct OH_Drawing_Image OH_Drawing_Image; + +/** + * @brief Defines a matrix, which is used to describe the transforming coordinates. + * + * @since 11 + * @version 1.0 + */ +typedef struct OH_Drawing_SamplingOptions OH_Drawing_SamplingOptions; + /** * @brief Defines a textBlobBuilder, which is used to build the textBlob. * diff --git a/graphic/graphic_2d/native_drawing/libnative_drawing.ndk.json b/graphic/graphic_2d/native_drawing/libnative_drawing.ndk.json index 75cfbfbb2..c6caabca9 100644 --- a/graphic/graphic_2d/native_drawing/libnative_drawing.ndk.json +++ b/graphic/graphic_2d/native_drawing/libnative_drawing.ndk.json @@ -9,6 +9,7 @@ { "name": "OH_Drawing_BrushDestroy" }, { "name": "OH_Drawing_BrushGetAlpha" }, { "name": "OH_Drawing_BrushSetAlpha" }, + { "name": "OH_Drawing_BrushSetBlendMode" }, { "name": "OH_Drawing_BrushIsAntiAlias" }, { "name": "OH_Drawing_BrushSetAntiAlias" }, { "name": "OH_Drawing_BrushGetColor" }, @@ -42,6 +43,9 @@ { "name": "OH_Drawing_CanvasTranslate" }, { "name": "OH_Drawing_CanvasScale" }, { "name": "OH_Drawing_CanvasClear" }, + { "name": "OH_Drawing_CanvasSetMatrix" }, + { "name": "OH_Drawing_CanvasDrawImageRect" }, + { "name": "OH_Drawing_CanvasReadPixelsToBitmap" }, { "name": "OH_Drawing_CanvasGetWidth" }, { "name": "OH_Drawing_CanvasGetHeight" }, { "name": "OH_Drawing_CanvasGetLocalClipBounds" }, @@ -69,6 +73,7 @@ { "name": "OH_Drawing_FontSetTextSkewX" }, { "name": "OH_Drawing_FontSetTypeface" }, { "name": "OH_Drawing_FontGetTypeface" }, + { "name": "OH_Drawing_FontGetMetrics" }, { "name": "OH_Drawing_FontSetLinearMetrics" }, { "name": "OH_Drawing_MaskFilterCreateBlur" }, { "name": "OH_Drawing_MaskFilterDestroy" }, @@ -106,6 +111,7 @@ { "name": "OH_Drawing_PenDestroy" }, { "name": "OH_Drawing_PenGetAlpha" }, { "name": "OH_Drawing_PenSetAlpha" }, + { "name": "OH_Drawing_PenSetBlendMode" }, { "name": "OH_Drawing_PenIsAntiAlias" }, { "name": "OH_Drawing_PenSetAntiAlias" }, { "name": "OH_Drawing_PenGetColor" }, @@ -183,6 +189,13 @@ { "name": "OH_Drawing_TypographyGetMaxIntrinsicWidth" }, { "name": "OH_Drawing_TypographyGetAlphabeticBaseline" }, { "name": "OH_Drawing_TypographyGetIdeographicBaseline" }, + { "name": "OH_Drawing_ImageCreate" }, + { "name": "OH_Drawing_ImageDestroy" }, + { "name": "OH_Drawing_ImageBuildFromBitmap" }, + { "name": "OH_Drawing_ImageGetWidth" }, + { "name": "OH_Drawing_ImageGetHeight" }, + { "name": "OH_Drawing_SamplingOptionsCreate" }, + { "name": "OH_Drawing_SamplingOptionsDestroy" }, { "first_introduced": "11", "name": "OH_Drawing_TypographyHandlerAddPlaceholder" -- Gitee