0

Add platform_canvas_unittest.cc

Also fixes issue 2977.
Review URL: http://codereview.chromium.org/5644

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@2777 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
jeremy@chromium.org
2008-10-01 21:40:28 +00:00
parent a2914d54e7
commit dc4f63c80c
6 changed files with 58 additions and 39 deletions

@@ -162,6 +162,7 @@
ABF4B9BE0DC2BD1500A6E319 /* sha512.cc in Sources */ = {isa = PBXBuildFile; fileRef = 825403700D92D2840006B936 /* sha512.cc */; }; ABF4B9BE0DC2BD1500A6E319 /* sha512.cc in Sources */ = {isa = PBXBuildFile; fileRef = 825403700D92D2840006B936 /* sha512.cc */; };
ABF4B9C30DC2BD6C00A6E319 /* values.cc in Sources */ = {isa = PBXBuildFile; fileRef = 825403880D92D2CF0006B936 /* values.cc */; }; ABF4B9C30DC2BD6C00A6E319 /* values.cc in Sources */ = {isa = PBXBuildFile; fileRef = 825403880D92D2CF0006B936 /* values.cc */; };
ABFBD3E60DC793C600E164CB /* md5.cc in Sources */ = {isa = PBXBuildFile; fileRef = 825403290D92D2090006B936 /* md5.cc */; }; ABFBD3E60DC793C600E164CB /* md5.cc in Sources */ = {isa = PBXBuildFile; fileRef = 825403290D92D2090006B936 /* md5.cc */; };
B5EF235C0E89ABF500E1E114 /* platform_canvas_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = ABE1BA0C0E756EC4009041DA /* platform_canvas_unittest.cc */; };
BA0F69870E79D7980079A8A1 /* thread_local_storage_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = BA0F69860E79D7980079A8A1 /* thread_local_storage_unittest.cc */; }; BA0F69870E79D7980079A8A1 /* thread_local_storage_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = BA0F69860E79D7980079A8A1 /* thread_local_storage_unittest.cc */; };
BA5CC5840E788093004EDD45 /* shared_memory_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = BA5CC5830E788093004EDD45 /* shared_memory_unittest.cc */; }; BA5CC5840E788093004EDD45 /* shared_memory_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = BA5CC5830E788093004EDD45 /* shared_memory_unittest.cc */; };
BA739A020E5E3242009842A7 /* tracked_objects_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = BA739A000E5E3242009842A7 /* tracked_objects_unittest.cc */; }; BA739A020E5E3242009842A7 /* tracked_objects_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = BA739A000E5E3242009842A7 /* tracked_objects_unittest.cc */; };
@@ -1432,6 +1433,7 @@
7B78D3950E54FE0100609465 /* observer_list_unittest.cc in Sources */, 7B78D3950E54FE0100609465 /* observer_list_unittest.cc in Sources */,
7B78D3960E54FE0100609465 /* path_service_unittest.cc in Sources */, 7B78D3960E54FE0100609465 /* path_service_unittest.cc in Sources */,
7B78D3970E54FE0100609465 /* pickle_unittest.cc in Sources */, 7B78D3970E54FE0100609465 /* pickle_unittest.cc in Sources */,
B5EF235C0E89ABF500E1E114 /* platform_canvas_unittest.cc in Sources */,
A5CB82980E5C74E300FD6825 /* platform_test_mac.mm in Sources */, A5CB82980E5C74E300FD6825 /* platform_test_mac.mm in Sources */,
7B8505D50E5B441000730B43 /* png_codec_unittest.cc in Sources */, 7B8505D50E5B441000730B43 /* png_codec_unittest.cc in Sources */,
7B78D3980E54FE0100609465 /* pr_time_unittest.cc in Sources */, 7B78D3980E54FE0100609465 /* pr_time_unittest.cc in Sources */,

@@ -130,7 +130,6 @@ void BitmapPlatformDeviceMac::BitmapPlatformDeviceMacData::LoadConfig() {
// Transform. // Transform.
SkMatrix t(transform_); SkMatrix t(transform_);
LoadTransformToCGContext(bitmap_context_, t); LoadTransformToCGContext(bitmap_context_, t);
t.setTranslateX(-t.getTranslateX()); t.setTranslateX(-t.getTranslateX());
t.setTranslateY(-t.getTranslateY()); t.setTranslateY(-t.getTranslateY());
LoadClippingRegionToCGContext(bitmap_context_, clip_region_, t); LoadClippingRegionToCGContext(bitmap_context_, clip_region_, t);
@@ -145,15 +144,18 @@ BitmapPlatformDeviceMac* BitmapPlatformDeviceMac::Create(CGContextRef context,
int width, int width,
int height, int height,
bool is_opaque) { bool is_opaque) {
// TODO(playmobil): remove debug code.
//printf("BitmapPlatformDeviceMac::Create(%d,%d)\n", width, height);
// each pixel is 4 bytes (RGBA):
void* data = malloc(height * width * 4); void* data = malloc(height * width * 4);
if (!data) return NULL; if (!data) return NULL;
SkBitmap bitmap; SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
bitmap.setPixels(data); bitmap.setPixels(data);
// Note: The Windows implementation clears the Bitmap later on.
// This bears mentioning since removal of this line makes the
// unit tests only fail periodically (or when MallocPreScribble is set).
bitmap.eraseARGB(0, 0, 0, 0);
bitmap.setIsOpaque(is_opaque); bitmap.setIsOpaque(is_opaque);
if (is_opaque) { if (is_opaque) {
@@ -170,7 +172,8 @@ BitmapPlatformDeviceMac* BitmapPlatformDeviceMac::Create(CGContextRef context,
CGContextRef bitmap_context = CGContextRef bitmap_context =
CGBitmapContextCreate(data, width, height, 8, width*4, CGBitmapContextCreate(data, width, height, 8, width*4,
color_space, kCGImageAlphaPremultipliedLast); color_space, kCGImageAlphaPremultipliedLast);
// change the coordinate system to match WebCore's
// Change the coordinate system to match WebCore's
CGContextTranslateCTM(bitmap_context, 0, height); CGContextTranslateCTM(bitmap_context, 0, height);
CGContextScaleCTM(bitmap_context, 1.0, -1.0); CGContextScaleCTM(bitmap_context, 1.0, -1.0);
CGColorSpaceRelease(color_space); CGColorSpaceRelease(color_space);

@@ -78,8 +78,10 @@ void DrawNativeRect(PlatformCanvas& canvas, int x, int y, int w, int h) {
CGContextRef context = canvas.beginPlatformPaint(); CGContextRef context = canvas.beginPlatformPaint();
CGRect inner_rc = CGRectMake(x, y, w, h); CGRect inner_rc = CGRectMake(x, y, w, h);
CGFloat black[] = { 0.0, 0.0, 0.0, 1.0 }; // RGBA opaque black // RGBA opaque black
CGContextSetFillColor(context, black); CGColorRef black = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1.0);
CGContextSetFillColorWithColor(context, black);
CGColorRelease(black);
CGContextFillRect(context, inner_rc); CGContextFillRect(context, inner_rc);
canvas.endPlatformPaint(); canvas.endPlatformPaint();

@@ -97,37 +97,32 @@ void PlatformDeviceMac::LoadPathToCGContext(CGContextRef context,
// static // static
void PlatformDeviceMac::LoadTransformToCGContext(CGContextRef context, void PlatformDeviceMac::LoadTransformToCGContext(CGContextRef context,
const SkMatrix& matrix) { const SkMatrix& matrix) {
// TODO: CoreGraphics can concatenate transforms, but not reset the current // CoreGraphics can concatenate transforms, but not reset the current one.
// one. Either find a workaround or remove this function if it turns out // So in order to get the required behavior here, we need to first make
// to be unneeded on the Mac. // the current transformation matrix identity and only then load the new one.
// For now, just load the translation.
// First reset the Transforms. // Reset matrix to identity.
// TODO(playmobil): no need to call CGContextTranslateCTM() twice CGAffineTransform orig_cg_matrix = CGContextGetCTM(context);
// just add up the numbers and call through. CGAffineTransform orig_cg_matrix_inv = CGAffineTransformInvert(orig_cg_matrix);
CGAffineTransform orig_transform = CGContextGetCTM(context); CGContextConcatCTM(context, orig_cg_matrix_inv);
CGContextTranslateCTM(context,
-orig_transform.tx,
orig_transform.ty); // y axis is flipped.
// TODO(playmobil): remove debug code.
// CGAffineTransform temp_transform = CGContextGetCTM(context);
// Now set the new transform. // assert that we have indeed returned to the identity Matrix.
int tx = matrix.getTranslateX(); DCHECK(CGAffineTransformIsIdentity(CGContextGetCTM(context)));
int ty = -matrix.getTranslateY();
int height = CGBitmapContextGetHeight(context); // Convert xform to CG-land.
CGContextTranslateCTM(context, // Our coordinate system is flipped to match WebKit's so we need to modify
tx, // the xform to match that.
-(ty+height)); SkMatrix transformed_matrix = matrix;
CGAffineTransform new_transform = CGContextGetCTM(context); SkScalar sy = matrix.getScaleY() * (SkScalar)-1;
// TODO(playmobil): remove debug code. transformed_matrix.setScaleY(sy);
// printf("tx_matrix (%lf,%lf)->(%lf,%lf)->(%lf,%lf) (%d, %d) height=%d\n", orig_transform.tx, size_t height = CGBitmapContextGetHeight(context);
// orig_transform.ty, SkScalar ty = -matrix.getTranslateY(); // y axis is flipped.
// foo_transform.tx, transformed_matrix.setTranslateY(ty + (SkScalar)height);
// foo_transform.ty,
// new_transform.tx, CGAffineTransform cg_matrix = SkMatrixToCGAffineTransform(transformed_matrix);
// new_transform.ty, tx, ty, height);
// Load final transform into context.
CGContextConcatCTM(context, cg_matrix);
} }
// static // static
@@ -147,9 +142,6 @@ void PlatformDeviceMac::LoadClippingRegionToCGContext(
transformation.mapRect(&rect); transformation.mapRect(&rect);
SkIRect irect; SkIRect irect;
rect.round(&irect); rect.round(&irect);
// TODO(playmobil): remove debug code.
// printf("Clipping to (%d,%d) (%d,%d)\n", irect.fLeft, irect.fTop,
// irect.fRight, irect.fBottom);
CGContextClipToRect(context, SkIRectToCGRect(irect)); CGContextClipToRect(context, SkIRectToCGRect(irect));
} else { } else {
// It is complex. // It is complex.

@@ -5,10 +5,26 @@
#include "base/gfx/skia_utils_mac.h" #include "base/gfx/skia_utils_mac.h"
#include "base/logging.h" #include "base/logging.h"
#include "SkMatrix.h"
#include "SkRect.h" #include "SkRect.h"
namespace gfx { namespace gfx {
CGAffineTransform SkMatrixToCGAffineTransform(const SkMatrix& matrix) {
// CGAffineTransforms don't support perspective transforms, so make sure
// we don't get those.
DCHECK(matrix[SkMatrix::kMPersp0] == 0.0f);
DCHECK(matrix[SkMatrix::kMPersp1] == 0.0f);
DCHECK(matrix[SkMatrix::kMPersp2] == 1.0f);
return CGAffineTransformMake(matrix[SkMatrix::kMScaleX],
matrix[SkMatrix::kMSkewY],
matrix[SkMatrix::kMSkewX],
matrix[SkMatrix::kMScaleY],
matrix[SkMatrix::kMTransX],
matrix[SkMatrix::kMTransY]);
}
SkIRect CGRectToSkIRect(const CGRect& rect) { SkIRect CGRectToSkIRect(const CGRect& rect) {
SkIRect sk_rect = { SkIRect sk_rect = {
SkScalarRound(rect.origin.x), SkScalarRound(rect.origin.x),

@@ -9,6 +9,7 @@
#include "SkColor.h" #include "SkColor.h"
#include <CoreGraphics/CGColor.h> #include <CoreGraphics/CGColor.h>
struct SkMatrix;
struct SkIRect; struct SkIRect;
struct SkPoint; struct SkPoint;
struct SkRect; struct SkRect;
@@ -26,6 +27,9 @@ inline const CGPoint& SkPointToCGPoint(const SkPoint& point) {
inline const SkPoint& CGPointToSkPoint(const CGPoint& point) { inline const SkPoint& CGPointToSkPoint(const CGPoint& point) {
return reinterpret_cast<const SkPoint&>(point); return reinterpret_cast<const SkPoint&>(point);
} }
// Matrix converters.
CGAffineTransform SkMatrixToCGAffineTransform(const SkMatrix& matrix);
// Rectangle converters. // Rectangle converters.
SkRect CGRectToSkRect(const CGRect& rect); SkRect CGRectToSkRect(const CGRect& rect);