From b992766b440e5de50091cab1fededdddb44a0088 Mon Sep 17 00:00:00 2001 From: xingyanan Date: Tue, 19 Jul 2022 14:26:52 +0800 Subject: [PATCH] fix starting window draw bug Signed-off-by: xingyanan Change-Id: I05da606596f856c1c57c14bebba907beedfcdb45 Signed-off-by: xingyanan --- utils/include/surface_draw.h | 4 ++-- utils/src/surface_draw.cpp | 27 +++++++++++++++++---------- wmserver/src/starting_window.cpp | 8 ++++---- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/utils/include/surface_draw.h b/utils/include/surface_draw.h index 2b740766..8c57e77c 100644 --- a/utils/include/surface_draw.h +++ b/utils/include/surface_draw.h @@ -49,8 +49,8 @@ private: int32_t bufferHeight); static void DrawPixelmap(Drawing::Canvas &canvas, const std::string& imagePath); static std::unique_ptr DecodeImageToPixelMap(const std::string &imagePath); - static bool DoDrawImageRect(uint8_t *addr, int32_t winWidth, int32_t winHeight, sptr pixelMap, - uint32_t color); + static bool DoDrawImageRect(uint8_t *addr, const Rect rect, sptr pixelMap, + uint32_t color, int32_t bufferStride); }; } // Rosen } // OHOS diff --git a/utils/src/surface_draw.cpp b/utils/src/surface_draw.cpp index 90a543f8..e8d0678b 100644 --- a/utils/src/surface_draw.cpp +++ b/utils/src/surface_draw.cpp @@ -276,7 +276,7 @@ bool SurfaceDraw::DrawImageRect(std::shared_ptr surfaceNode, Rect return false; } auto addr = static_cast(buffer->GetVirAddr()); - if (!DoDrawImageRect(addr, winWidth, winHeight, pixelMap, color)) { + if (!DoDrawImageRect(addr, rect, pixelMap, color, buffer->GetStride())) { WLOGE("draw image rect failed."); return false; } @@ -294,9 +294,15 @@ bool SurfaceDraw::DrawImageRect(std::shared_ptr surfaceNode, Rect return true; } -bool SurfaceDraw::DoDrawImageRect(uint8_t *addr, int32_t winWidth, int32_t winHeight, - sptr pixelMap, uint32_t color) +bool SurfaceDraw::DoDrawImageRect(uint8_t *addr, const Rect rect, sptr pixelMap, + uint32_t color, int32_t bufferStride) { + int32_t winWidth = static_cast(rect.width_); + int32_t winHeight = static_cast(rect.height_); + // actual width of the surface buffer after alignment + int32_t alignWidth = bufferStride / static_cast(IMAGE_BYTES_STRIDE); + WLOGFD("drawing image rect win width: %{public}d win height: %{public}d align width:%{public}d.", + winWidth, winHeight, alignWidth); if (pixelMap == nullptr) { WLOGFE("drawing pixel map failed, because pixel map is nullptr."); return false; @@ -305,7 +311,7 @@ bool SurfaceDraw::DoDrawImageRect(uint8_t *addr, int32_t winWidth, int32_t winHe WLOGFE("drawing pixel map failed, because width or height is invalid."); return false; } - float xAxis = static_cast(winWidth) / pixelMap->GetWidth(); + float xAxis = static_cast(alignWidth) / pixelMap->GetWidth(); float yAxis = static_cast(winHeight) / pixelMap->GetHeight(); float axis = std::min(xAxis, yAxis); if (axis < 1.0) { @@ -313,21 +319,22 @@ bool SurfaceDraw::DoDrawImageRect(uint8_t *addr, int32_t winWidth, int32_t winHe // use axis to scale equally pixelMap->scale(axis, axis); } - int left = (winWidth - pixelMap->GetWidth()) / 2; // 2 is the left and right boundaries of the window + int left = (alignWidth - pixelMap->GetWidth()) / 2; // 2 is the left and right boundaries of the window int top = (winHeight - pixelMap->GetHeight()) / 2; // 2 is the top and bottom boundaries of the window Drawing::Bitmap bitmap; Drawing::BitmapFormat format { Drawing::ColorType::COLORTYPE_RGBA_8888, Drawing::AlphaType::ALPHATYPE_OPAQUE }; - bitmap.Build(winWidth, winHeight, format); + bitmap.Build(alignWidth, winHeight, format); Drawing::Canvas canvas; canvas.Bind(bitmap); canvas.Clear(color); canvas.DrawBitmap(*pixelMap, left, top); - - uint32_t addrSize = winWidth * winHeight * IMAGE_BYTES_STRIDE; - errno_t ret = memcpy_s(addr, addrSize, bitmap.GetPixels(), addrSize); + // bufferSize is actual size of the surface buffer after alignment + int32_t bufferSize = bufferStride * winHeight; + uint8_t* bitmapAddr = static_cast(bitmap.GetPixels()); + errno_t ret = memcpy_s(addr, bufferSize, bitmapAddr, bufferSize); if (ret != EOK) { - WLOGFE("draw failed"); + WLOGFE("draw image rect failed, because copy bitmap to buffer failed."); return false; } return true; diff --git a/wmserver/src/starting_window.cpp b/wmserver/src/starting_window.cpp index 4feb660b..ff0c636a 100644 --- a/wmserver/src/starting_window.cpp +++ b/wmserver/src/starting_window.cpp @@ -108,6 +108,10 @@ void StartingWindow::DrawStartingWindow(sptr& node, { // using snapshot to support hot start since node destroy when hide HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "wms:DrawStartingWindow(%u)", node->GetWindowId()); + Rect rect = node->GetWindowRect(); + if (RemoteAnimation::CheckAnimationController() && node->leashWinSurfaceNode_) { + node->leashWinSurfaceNode_->SetBounds(rect.posX_, rect.posY_, -1, -1); + } if (!isColdStart) { return; } @@ -115,10 +119,6 @@ void StartingWindow::DrawStartingWindow(sptr& node, WLOGFE("no starting Window SurfaceNode!"); return; } - Rect rect = node->GetWindowRect(); - if (RemoteAnimation::CheckAnimationController() && node->leashWinSurfaceNode_) { - node->leashWinSurfaceNode_->SetBounds(rect.posX_, rect.posY_, -1, -1); - } if (pixelMap == nullptr) { SurfaceDraw::DrawColor(node->startingWinSurfaceNode_, rect.width_, rect.height_, bkgColor); return;