Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 4d71938

Browse files
committed
[[ RefactorGraphics ]] Various fixes to libgraphics
add MCGRectangleTranslate function make MCGRectangleIntersection public - fix so doesn't return negative width / height add MCGImageCreateWithRasterAndRelease change copy param of MCGRasterToSkBitmap to ownership type (take, borrow, or copy)
1 parent 5d26a4d commit 4d71938

File tree

5 files changed

+75
-35
lines changed

5 files changed

+75
-35
lines changed

libgraphics/include/graphics.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,17 @@ inline MCGRectangle MCGRectangleMake(MCGFloat p_x, MCGFloat p_y, MCGFloat p_widt
154154
return t_rect;
155155
}
156156

157+
inline MCGRectangle MCGRectangleTranslate(MCGRectangle p_rect, MCGFloat p_dx, MCGFloat p_dy)
158+
{
159+
MCGRectangle t_rect = p_rect;
160+
t_rect.origin.x += p_dx;
161+
t_rect.origin.y += p_dy;
162+
163+
return t_rect;
164+
}
165+
166+
MCGRectangle MCGRectangleIntersection(MCGRectangle rect_1, MCGRectangle rect_2);
167+
157168
inline MCGPoint MCGPointMake(MCGFloat p_x, MCGFloat p_y)
158169
{
159170
MCGPoint t_point;
@@ -165,6 +176,7 @@ inline MCGPoint MCGPointMake(MCGFloat p_x, MCGFloat p_y)
165176
////////////////////////////////////////////////////////////////////////////////
166177

167178
bool MCGImageCreateWithRaster(const MCGRaster& raster, MCGImageRef& r_image);
179+
bool MCGImageCreateWithRasterAndRelease(const MCGRaster &raster, MCGImageRef &r_image);
168180
bool MCGImageCreateWithData(const void *bytes, uindex_t byte_count, MCGImageRef& r_image);
169181
bool MCGImageCreateWithFilename(const char *filename, MCGImageRef& r_image);
170182

libgraphics/src/context.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ bool MCGContextCreate(uint32_t p_width, uint32_t p_height, bool p_alpha, MCGCont
224224
bool MCGContextCreateWithRaster(MCGRaster &p_raster, MCGContextRef &r_context)
225225
{
226226
SkBitmap t_bitmap;
227-
if (MCGRasterToSkBitmap(p_raster, false, t_bitmap))
227+
if (MCGRasterToSkBitmap(p_raster, kMCGPixelOwnershipTypeBorrow, t_bitmap))
228228
return MCGContextCreateWithBitmap(t_bitmap, r_context);
229229
else
230230
return false;
@@ -1345,7 +1345,7 @@ void MCGContextDrawPixels(MCGContextRef self, const MCGRaster& p_raster, MCGRect
13451345

13461346
SkBitmap t_bitmap;
13471347
if (t_success)
1348-
t_success = MCGRasterToSkBitmap(p_raster, false, t_bitmap);
1348+
t_success = MCGRasterToSkBitmap(p_raster, kMCGPixelOwnershipTypeBorrow, t_bitmap);
13491349

13501350
if (t_success)
13511351
t_success = MCGContextDrawSkBitmap(self, t_bitmap, p_dst, p_filter);

libgraphics/src/graphics-internal.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,16 @@ inline bool MCGRectangleIntersects(MCGRectangle p_rect_1, MCGRectangle p_rect_2)
131131
(p_rect_2 . origin . y < (p_rect_1 . origin . y + p_rect_1 . size . height));
132132
}
133133

134-
MCGRectangle MCGRectangleIntersection(MCGRectangle rect_1, MCGRectangle rect_2);
135-
136134
////////////////////////////////////////////////////////////////////////////////
137135

138-
bool MCGRasterToSkBitmap(const MCGRaster& raster, bool copy_pixels, SkBitmap& bitmap);
136+
enum MCGPixelOwnershipType
137+
{
138+
kMCGPixelOwnershipTypeBorrow,
139+
kMCGPixelOwnershipTypeTake,
140+
kMCGPixelOwnershipTypeCopy,
141+
};
142+
143+
bool MCGRasterToSkBitmap(const MCGRaster& raster, MCGPixelOwnershipType p_ownership, SkBitmap& bitmap);
139144
SkXfermode* MCGBlendModeToSkXfermode(MCGBlendMode mode);
140145
MCGBlendMode MCGBlendModeToSkXfermode(SkXfermode* mode);
141146
void MCGAffineTransformToSkMatrix(const MCGAffineTransform& transform, SkMatrix& r_matrix);

libgraphics/src/image.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ static void MCGImageDestroy(MCGImageRef self)
1313
}
1414
}
1515

16-
bool MCGImageCreateWithRaster(const MCGRaster& p_raster, MCGImageRef& r_image)
16+
bool __MCGImageCreateWithRaster(const MCGRaster &p_raster, MCGImageRef &r_image, MCGPixelOwnershipType p_ownership)
1717
{
1818
bool t_success;
1919
t_success = true;
@@ -31,7 +31,7 @@ bool MCGImageCreateWithRaster(const MCGRaster& p_raster, MCGImageRef& r_image)
3131
}
3232

3333
if (t_success)
34-
t_success = MCGRasterToSkBitmap(p_raster, true, *t_bitmap);
34+
t_success = MCGRasterToSkBitmap(p_raster, p_ownership, *t_bitmap);
3535

3636
if (t_success)
3737
{
@@ -50,6 +50,16 @@ bool MCGImageCreateWithRaster(const MCGRaster& p_raster, MCGImageRef& r_image)
5050
return t_success;
5151
}
5252

53+
bool MCGImageCreateWithRasterAndRelease(const MCGRaster &p_raster, MCGImageRef &r_image)
54+
{
55+
return __MCGImageCreateWithRaster(p_raster, r_image, kMCGPixelOwnershipTypeTake);
56+
}
57+
58+
bool MCGImageCreateWithRaster(const MCGRaster& p_raster, MCGImageRef& r_image)
59+
{
60+
return __MCGImageCreateWithRaster(p_raster, r_image, kMCGPixelOwnershipTypeCopy);
61+
}
62+
5363
bool MCGImageGetRaster(MCGImageRef p_image, MCGRaster &r_raster)
5464
{
5565
if (p_image == nil)
@@ -59,12 +69,18 @@ bool MCGImageGetRaster(MCGImageRef p_image, MCGRaster &r_raster)
5969
if (!t_image->is_valid || t_image->bitmap == nil)
6070
return false;
6171

72+
// IM-2013-06-18: lock pixels here to force skia to update the pixel ptr
73+
// from the bitmap's pixel ref
74+
t_image->bitmap->lockPixels();
75+
6276
r_raster.format = MCGRasterFormatFromSkBitmapConfig(t_image->bitmap->config());
6377
r_raster.width = t_image->bitmap->width();
6478
r_raster.height = t_image->bitmap->height();
6579
r_raster.pixels = t_image->bitmap->getPixels();
6680
r_raster.stride =t_image->bitmap->rowBytes();
6781

82+
t_image->bitmap->unlockPixels();
83+
6884
return true;
6985
}
7086

libgraphics/src/utils.cpp

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <SkShader.h>
55
#include <SkGradientShader.h>
6+
#include <SkMallocPixelRef.h>
67

78
////////////////////////////////////////////////////////////////////////////////
89

@@ -421,13 +422,11 @@ MCGBlendMode MCGBlendModeFromSkXfermode(SkXfermode *p_mode)
421422

422423
////////////////////////////////////////////////////////////////////////////////
423424

424-
bool MCGRasterToSkBitmap(const MCGRaster& p_raster, bool p_copy_pixels, SkBitmap& r_bitmap)
425+
bool MCGRasterToSkBitmap(const MCGRaster& p_raster, MCGPixelOwnershipType p_ownership, SkBitmap& r_bitmap)
425426
{
426427
bool t_success;
427428
t_success = true;
428429

429-
void *t_pixels;
430-
t_pixels = NULL;
431430
if (t_success)
432431
{
433432
SkBitmap::Config t_config = MCGRasterFormatToSkBitmapConfig(p_raster . format);
@@ -438,11 +437,28 @@ bool MCGRasterToSkBitmap(const MCGRaster& p_raster, bool p_copy_pixels, SkBitmap
438437
// for premultiplied bitmaps, just set the pixels in the target directly
439438
// if the copy pixels flag is set, allocate space and copy the pixels from the raster first, then set in target
440439
if (p_raster . format == kMCGRasterFormat_U_ARGB)
441-
t_success = r_bitmap . allocPixels();
442-
else if (p_copy_pixels)
443-
t_success = MCMemoryNew(p_raster . height * p_raster . stride, t_pixels);
444-
else
445-
t_pixels = p_raster . pixels;
440+
p_ownership = kMCGPixelOwnershipTypeCopy;
441+
442+
switch (p_ownership)
443+
{
444+
case kMCGPixelOwnershipTypeBorrow:
445+
r_bitmap . setPixels(p_raster . pixels);
446+
break;
447+
448+
case kMCGPixelOwnershipTypeTake:
449+
SkMallocPixelRef *t_pixelref;
450+
t_success = nil != (t_pixelref = new SkMallocPixelRef(p_raster . pixels, p_raster . stride * p_raster . height, nil));
451+
if (t_success)
452+
{
453+
r_bitmap . setPixelRef(t_pixelref);
454+
t_pixelref -> unref();
455+
}
456+
break;
457+
458+
case kMCGPixelOwnershipTypeCopy:
459+
t_success = r_bitmap . allocPixels();
460+
break;
461+
}
446462
}
447463

448464
if (t_success)
@@ -467,12 +483,8 @@ bool MCGRasterToSkBitmap(const MCGRaster& p_raster, bool p_copy_pixels, SkBitmap
467483
t_row_ptr += p_raster . stride;
468484
}
469485
}
470-
else
471-
{
472-
if (p_copy_pixels)
473-
MCMemoryCopy(t_pixels, p_raster . pixels, p_raster . height * p_raster . stride);
474-
r_bitmap . setPixels(t_pixels);
475-
}
486+
else if (p_ownership == kMCGPixelOwnershipTypeCopy)
487+
MCMemoryCopy(r_bitmap . getPixels(), p_raster . pixels, p_raster . height * p_raster . stride);
476488
}
477489

478490
return t_success;
@@ -483,20 +495,15 @@ bool MCGRasterToSkBitmap(const MCGRaster& p_raster, bool p_copy_pixels, SkBitmap
483495
MCGRectangle MCGRectangleIntersection(MCGRectangle p_rect_1, MCGRectangle p_rect_2)
484496
{
485497
MCGRectangle t_intersection;
486-
t_intersection . origin . x = (p_rect_1 . origin . x > p_rect_2 . origin . x) ? p_rect_1 . origin . x : p_rect_2 . origin . x;
487-
t_intersection . origin . y = (p_rect_1 . origin . y > p_rect_2 . origin . x) ? p_rect_1 . origin . y : p_rect_2 . origin . y;
488-
489-
MCGFloat t_right_1;
490-
t_right_1 = p_rect_1 . origin . x + p_rect_1 . size . width;
491-
MCGFloat t_right_2;
492-
t_right_2 = p_rect_2 . origin . x + p_rect_2 . size . width;
493-
t_intersection . size . width = (t_right_1 < t_right_2) ? t_right_1 - t_intersection . origin . x : t_right_2 - t_intersection . origin . x;
494-
495-
MCGFloat t_bottom_1;
496-
t_bottom_1 = p_rect_1 . origin . y + p_rect_1 . size . height;
497-
MCGFloat t_bottom_2;
498-
t_bottom_2 = p_rect_2 . origin . y + p_rect_2 . size . height;
499-
t_intersection . size . height = (t_bottom_1 < t_bottom_2) ? t_bottom_1 - t_intersection . origin . y : t_bottom_2 - t_intersection . origin . y;
498+
t_intersection . origin . x = MCMax(p_rect_1 . origin . x, p_rect_2 . origin . x);
499+
t_intersection . origin . y = MCMax(p_rect_1 . origin . y, p_rect_2 . origin . y);
500+
501+
MCGFloat t_right, t_bottom;
502+
t_right = MCMin(p_rect_1 . origin . x + p_rect_1 . size . width, p_rect_2 . origin . x + p_rect_2 . size . width);
503+
t_bottom = MCMin(p_rect_1 . origin . y + p_rect_1 . size . height, p_rect_2 . origin . y + p_rect_2 . size . height);
504+
505+
t_intersection . size . width = MCMax(0.0f, t_right - t_intersection . origin . x);
506+
t_intersection . size . height = MCMax(0.0f, t_bottom - t_intersection . origin . y);
500507

501508
return t_intersection;
502509
}

0 commit comments

Comments
 (0)