Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement CachedBitmap functionality #654

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions src/GdiPlusFlat.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ extern "C"
typedef void GpAdjustableArrowCap;
typedef void GpBitmap;
typedef void GpBrush;
typedef void GpCachedBitmap;
typedef void GpCustomLineCap;
typedef void GpFont;
typedef void GpFontCollection;
Expand Down Expand Up @@ -87,6 +88,7 @@ typedef void GpTexture;
#include "adjustablearrowcap.h"
#include "bitmap.h"
#include "brush.h"
#include "cachedbitmap.h"
#include "customlinecap.h"
#include "font.h"
#include "fontcollection.h"
Expand Down
3 changes: 3 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ libgdiplus_la_SOURCES = \
brush.c \
brush.h \
brush-private.h \
cachedbitmap.c \
cachedbitmap.h \
cachedbitmap-private.h \
carbon-private.c \
carbon-private.h \
codecs.h \
Expand Down
14 changes: 14 additions & 0 deletions src/cachedbitmap-private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#ifndef __CACHEDBITMAP_PRIVATE_H__
#define __CACHEDBITMAP_PRIVATE_H__

typedef struct _CachedBitmap {
cairo_surface_t *surface;
} CachedBitmap;

#include "cachedbitmap.h"

#endif
63 changes: 63 additions & 0 deletions src/cachedbitmap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#include "general-private.h"
#include "bitmap-private.h"
#include "graphics-private.h"
#include "cachedbitmap-private.h"

GpStatus WINGDIPAPI
GdipCreateCachedBitmap (GpBitmap *bitmap, GpGraphics *graphics, GpCachedBitmap **cachedBitmap)
{
cairo_t *ct;
cairo_surface_t *surface;
GpCachedBitmap *newCachedBitmap;
cairo_status_t status;

if (!bitmap || !graphics || !cachedBitmap)
return InvalidParameter;
if (bitmap->type != ImageTypeBitmap)
return InvalidParameter;

gdip_bitmap_ensure_surface (bitmap);

surface = cairo_surface_create_similar (bitmap->surface, CAIRO_CONTENT_COLOR_ALPHA, bitmap->active_bitmap->width, bitmap->active_bitmap->height);

ct = cairo_create (surface);

cairo_set_source_surface (ct, bitmap->surface, 0, 0);
cairo_paint (ct);

cairo_destroy (ct);

status = cairo_surface_status (surface);
if (status != CAIRO_STATUS_SUCCESS) {
cairo_surface_destroy (surface);
return gdip_get_status (status);
}

newCachedBitmap = GdipAlloc (sizeof (GpCachedBitmap));
if (!newCachedBitmap)
{
cairo_surface_destroy(surface);
return OutOfMemory;
reflectronic marked this conversation as resolved.
Show resolved Hide resolved
}

newCachedBitmap->surface = surface;
*cachedBitmap = newCachedBitmap;

return Ok;
}

GpStatus WINGDIPAPI
GdipDeleteCachedBitmap (GpCachedBitmap *cachedBitmap)
{
if (!cachedBitmap)
return InvalidParameter;

cairo_surface_destroy (cachedBitmap->surface);
GdipFree (cachedBitmap);

return Ok;
}
12 changes: 12 additions & 0 deletions src/cachedbitmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#ifndef __CACHEDBITMAP_H__
#define __CACHEDBITMAP_H__

GpStatus WINGDIPAPI GdipCreateCachedBitmap (GpBitmap *bitmap, GpGraphics *graphics, GpCachedBitmap **cachedBitmap);

GpStatus WINGDIPAPI GdipDeleteCachedBitmap (GpCachedBitmap *cachedBitmap);

#endif
1 change: 1 addition & 0 deletions src/gdiplus-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@

typedef struct _AdjustableArrowCap GpAdjustableArrowCap;
typedef struct _Brush GpBrush;
typedef struct _CachedBitmap GpCachedBitmap;
typedef struct _CustomLineCap GpCustomLineCap;
typedef struct _Font GpFont;
typedef struct _FontCollection GpFontCollection;
Expand Down
25 changes: 25 additions & 0 deletions src/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "brush-private.h"
#include "matrix-private.h"
#include "bitmap-private.h"
#include "cachedbitmap-private.h"
#include "metafile-private.h"

#include <cairo-features.h>
Expand Down Expand Up @@ -1361,6 +1362,30 @@ GdipDrawCurve3I (GpGraphics *graphics, GpPen* pen, GDIPCONST GpPoint *points, IN
return status;
}

GpStatus WINGDIPAPI
GdipDrawCachedBitmap (GpGraphics *graphics, GpCachedBitmap *cachedBitmap, INT x, INT y)
{
if (!graphics || !cachedBitmap)
return InvalidParameter;
if (graphics->state == GraphicsStateBusy)
return ObjectBusy;

// For compat with Windows, only support translation matrices.
// Return WrongState otherwise.

cairo_matrix_t matrix;
cairo_get_matrix (graphics->ct, &matrix);
if (matrix.xx != 1 || matrix.yx != 0 || matrix.xy != 0 || matrix.yy != 1)
{
return WrongState;
}

cairo_set_source_surface (graphics->ct, cachedBitmap->surface, x, y);
cairo_paint (graphics->ct);

return Ok;
}

/*
* Fills
*/
Expand Down
2 changes: 2 additions & 0 deletions src/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ GpStatus WINGDIPAPI GdipFillPieI( GpGraphics *graphics, GpBrush *brush, INT x, I
GpStatus WINGDIPAPI GdipFillRegion (GpGraphics *graphics, GpBrush *brush, GpRegion *region);
GpStatus WINGDIPAPI GdipGraphicsClear (GpGraphics *graphics, ARGB color);

GpStatus WINGDIPAPI GdipDrawCachedBitmap (GpGraphics *graphics, GpCachedBitmap *cachedBitmap, INT x, INT y);

GpStatus WINGDIPAPI GdipGetDpiX( GpGraphics *graphics, REAL *dpi);
GpStatus WINGDIPAPI GdipGetDpiY (GpGraphics *graphics, REAL *dpi);
GpStatus WINGDIPAPI GdipGetNearestColor (GpGraphics *graphics, ARGB *argb);
Expand Down
10 changes: 10 additions & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ noinst_PROGRAMS = \
testbits \
testbmpcodec \
testbrush \
testcachedbitmap \
testclip \
testcodecs \
testcustomlinecap \
Expand Down Expand Up @@ -91,6 +92,13 @@ testbrush_SOURCES = \
testbrush_DEPENDENCIES = $(TEST_DEPS)
testbrush_LDADD = $(LDADDS)


testcachedbitmap_SOURCES = \
testcachedbitmap.c

testcachedbitmap_DEPENDENCIES = $(TEST_DEPS)
testcachedbitmap_LDADD = $(LDADDS)

testclip_SOURCES = \
testclip.c

Expand Down Expand Up @@ -283,6 +291,7 @@ EXTRA_DIST = \
$(testbits_SOURCES) \
$(testbmpcodec_SOURCES) \
$(testbrush_SOURCES) \
$(testcachedbitmap_SOURCES) \
$(testclip_SOURCES) \
$(test_codecs_SOURCES) \
$(testcustomlinecap_SOURCES) \
Expand Down Expand Up @@ -339,6 +348,7 @@ TESTS = \
testbits \
testbmpcodec \
testbrush \
testcachedbitmap \
testclip \
testcodecs \
testcustomlinecap \
Expand Down
Binary file added tests/test-clipped.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/test-translated.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading