From 33d85b510b4a3c9beadb1553ff2f7cb3cf7543d5 Mon Sep 17 00:00:00 2001 From: Michael Lotz Date: Fri, 13 Jun 2008 13:28:13 +0000 Subject: [PATCH] Add a handy utility class that provides a DrawingEngine directly attached to a server side UtilityBitmap of a certain size. It sets up the DrawingEngine, the UtilityBitmap and the BitmapHWInterface necessary so that one can directly do drawing to a bitmap using the normal DrawingEngine interface. It provides an ExportToBitmap method that allocates an output UtilityBitmap of a specified size and color space where the content is put into, so a single instance of a BitmapDrawingEngine can be reused for various drawing. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25951 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/servers/app/BitmapDrawingEngine.cpp | 85 +++++++++++++++++++++++++ src/servers/app/BitmapDrawingEngine.h | 25 ++++++++ src/servers/app/Jamfile | 1 + 3 files changed, 111 insertions(+) create mode 100644 src/servers/app/BitmapDrawingEngine.cpp create mode 100644 src/servers/app/BitmapDrawingEngine.h diff --git a/src/servers/app/BitmapDrawingEngine.cpp b/src/servers/app/BitmapDrawingEngine.cpp new file mode 100644 index 0000000000..ce7f1d24fa --- /dev/null +++ b/src/servers/app/BitmapDrawingEngine.cpp @@ -0,0 +1,85 @@ +#include "BitmapDrawingEngine.h" +#include "BitmapHWInterface.h" +#include "ServerBitmap.h" +#include + + +BitmapDrawingEngine::BitmapDrawingEngine() + : DrawingEngine(), + fHWInterface(NULL), + fBitmap(NULL) +{ +} + + +BitmapDrawingEngine::~BitmapDrawingEngine() +{ + SetSize(0, 0); +} + + +status_t +BitmapDrawingEngine::SetSize(int32 newWidth, int32 newHeight) +{ + if (fBitmap != NULL && newWidth > 0 && newHeight > 0 + && fBitmap->Bounds().IntegerWidth() >= newWidth + && fBitmap->Bounds().IntegerHeight() >= newHeight) { + return B_OK; + } + + SetHWInterface(NULL); + if (fHWInterface) { + fHWInterface->LockExclusiveAccess(); + fHWInterface->Shutdown(); + fHWInterface->UnlockExclusiveAccess(); + delete fHWInterface; + fHWInterface = NULL; + } + + delete fBitmap; + fBitmap = NULL; + + if (newWidth <= 0 || newHeight <= 0) + return B_OK; + + fBitmap = new(std::nothrow) UtilityBitmap(BRect(0, 0, newWidth - 1, + newHeight - 1), B_RGB32, 0); + if (fBitmap == NULL) + return B_NO_MEMORY; + + fHWInterface = new(std::nothrow) BitmapHWInterface(fBitmap); + if (fHWInterface == NULL) + return B_NO_MEMORY; + + status_t result = fHWInterface->Initialize(); + if (result != B_OK) + return result; + + // we have to set a valid clipping first + fClipping.Set(fBitmap->Bounds()); + ConstrainClippingRegion(&fClipping); + SetHWInterface(fHWInterface); + return B_OK; +} + + +UtilityBitmap * +BitmapDrawingEngine::ExportToBitmap(int32 width, int32 height, + color_space space) +{ + if (width <= 0 || height <= 0) + return NULL; + + UtilityBitmap *result = new(std::nothrow) UtilityBitmap(BRect(0, 0, + width - 1, height - 1), space, 0); + if (result == NULL) + return NULL; + + if (result->ImportBits(fBitmap->Bits(), fBitmap->BitsLength(), + fBitmap->BytesPerRow(), fBitmap->ColorSpace()) != B_OK) { + delete result; + return NULL; + } + + return result; +} diff --git a/src/servers/app/BitmapDrawingEngine.h b/src/servers/app/BitmapDrawingEngine.h new file mode 100644 index 0000000000..b8d98f4ae6 --- /dev/null +++ b/src/servers/app/BitmapDrawingEngine.h @@ -0,0 +1,25 @@ +#ifndef BITMAP_DRAWING_ENGINE_H +#define BITMAP_DRAWING_ENGINE_H + +#include "DrawingEngine.h" +#include + +class BitmapHWInterface; +class UtilityBitmap; + +class BitmapDrawingEngine : public DrawingEngine { +public: + BitmapDrawingEngine(); +virtual ~BitmapDrawingEngine(); + + status_t SetSize(int32 newWidth, int32 newHeight); + UtilityBitmap * ExportToBitmap(int32 width, int32 height, + color_space space); + +private: + BitmapHWInterface * fHWInterface; + UtilityBitmap * fBitmap; + BRegion fClipping; +}; + +#endif // BITMAP_DRAWING_ENGINE_H diff --git a/src/servers/app/Jamfile b/src/servers/app/Jamfile index 76ecae2679..803c791d28 100644 --- a/src/servers/app/Jamfile +++ b/src/servers/app/Jamfile @@ -12,6 +12,7 @@ Server app_server : Angle.cpp AppServer.cpp #BitfieldRegion.cpp + BitmapDrawingEngine.cpp BitmapManager.cpp ClientMemoryAllocator.cpp CursorData.cpp