diff options
author | brettw@google.com <brettw@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-21 20:32:45 +0000 |
---|---|---|
committer | brettw@google.com <brettw@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-21 20:32:45 +0000 |
commit | 98abd85558e19788d971df47690bcb4eb4dd8d98 (patch) | |
tree | 3639cbd4ce1324ce34be603ed36fec0022a438d7 /skia | |
parent | 21ba9918905c5f3f66e3a075944587618da2d52d (diff) | |
download | chromium_src-98abd85558e19788d971df47690bcb4eb4dd8d98.zip chromium_src-98abd85558e19788d971df47690bcb4eb4dd8d98.tar.gz chromium_src-98abd85558e19788d971df47690bcb4eb4dd8d98.tar.bz2 |
Move the platform files form port to skia for Mac only. Hopefully this won't affect other platforms.
Review URL: http://codereview.chromium.org/11357
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@5845 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'skia')
-rw-r--r-- | skia/SConscript | 5 | ||||
-rwxr-xr-x | skia/ext/bitmap_platform_device_mac.cc | 290 | ||||
-rwxr-xr-x | skia/ext/bitmap_platform_device_mac.h | 95 | ||||
-rwxr-xr-x | skia/ext/platform_canvas_mac.cc | 80 | ||||
-rwxr-xr-x | skia/ext/platform_canvas_mac.h | 88 | ||||
-rwxr-xr-x | skia/ext/platform_device_mac.cc | 161 | ||||
-rwxr-xr-x | skia/ext/platform_device_mac.h | 86 | ||||
-rw-r--r-- | skia/skia.xcodeproj/project.pbxproj | 30 |
8 files changed, 835 insertions, 0 deletions
diff --git a/skia/SConscript b/skia/SConscript index 386a219..95fa1b1 100644 --- a/skia/SConscript +++ b/skia/SConscript @@ -167,6 +167,11 @@ if env['PLATFORM'] == 'posix': if env['PLATFORM'] in ('darwin', 'posix'): input_files.append('ports/SkThread_pthread.cpp') +if env['PLATFORM'] == 'darwin': + input_files.append('ext/bitmap_platform_device_mac.cc') + input_files.append('ext/platform_canvas_mac.cc') + input_files.append('ext/platform_device_mac.cc') + if env['PLATFORM'] == 'win32': input_files.append('ports/SkThread_win.cpp') diff --git a/skia/ext/bitmap_platform_device_mac.cc b/skia/ext/bitmap_platform_device_mac.cc new file mode 100755 index 0000000..d4458d7 --- /dev/null +++ b/skia/ext/bitmap_platform_device_mac.cc @@ -0,0 +1,290 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "skia/ext/bitmap_platform_device_mac.h" + +#include <time.h> + +#include "SkMatrix.h" +#include "SkRegion.h" +#include "SkUtils.h" + +#include "base/gfx/skia_utils_mac.h" +#include "base/logging.h" + +namespace gfx { + +namespace { + +// Constrains position and size to fit within available_size. If |size| is -1, +// all the |available_size| is used. Returns false if the position is out of +// |available_size|. +bool Constrain(int available_size, int* position, int *size) { + if (*size < -2) + return false; + + if (*position < 0) { + if (*size != -1) + *size += *position; + *position = 0; + } + if (*size == 0 || *position >= available_size) + return false; + + if (*size > 0) { + int overflow = (*position + *size) - available_size; + if (overflow > 0) { + *size -= overflow; + } + } else { + // Fill up available size. + *size = available_size - *position; + } + return true; +} + +} // namespace + +class BitmapPlatformDeviceMac::BitmapPlatformDeviceMacData + : public base::RefCounted<BitmapPlatformDeviceMacData> { + public: + explicit BitmapPlatformDeviceMacData(CGContextRef bitmap); + + // Create/destroy CoreGraphics context for our bitmap data. + CGContextRef GetBitmapContext() { + LoadConfig(); + return bitmap_context_; + } + + void ReleaseBitmapContext() { + DCHECK(bitmap_context_); + CGContextRelease(bitmap_context_); + bitmap_context_ = NULL; + } + + // Sets the transform and clip operations. This will not update the CGContext, + // but will mark the config as dirty. The next call of LoadConfig will + // pick up these changes. + void SetMatrixClip(const SkMatrix& transform, const SkRegion& region); + + // Loads the current transform and clip into the DC. Can be called even when + // |bitmap_context_| is NULL (will be a NOP). + void LoadConfig(); + + // Lazily-created graphics context used to draw into the bitmap. + CGContextRef bitmap_context_; + + // True when there is a transform or clip that has not been set to the + // CGContext. The CGContext is retrieved for every text operation, and the + // transform and clip do not change as much. We can save time by not loading + // the clip and transform for every one. + bool config_dirty_; + + // Translation assigned to the CGContext: we need to keep track of this + // separately so it can be updated even if the CGContext isn't created yet. + SkMatrix transform_; + + // The current clipping + SkRegion clip_region_; + + private: + friend class base::RefCounted<BitmapPlatformDeviceMacData>; + ~BitmapPlatformDeviceMacData() { + if (bitmap_context_) + CGContextRelease(bitmap_context_); + } + + DISALLOW_COPY_AND_ASSIGN(BitmapPlatformDeviceMacData); +}; + +BitmapPlatformDeviceMac::\ + BitmapPlatformDeviceMacData::BitmapPlatformDeviceMacData( + CGContextRef bitmap) + : bitmap_context_(bitmap), + config_dirty_(true) { // Want to load the config next time. + DCHECK(bitmap_context_); + // Initialize the clip region to the entire bitmap. + + SkIRect rect; + rect.set(0, 0, + CGBitmapContextGetWidth(bitmap_context_), + CGBitmapContextGetHeight(bitmap_context_)); + clip_region_ = SkRegion(rect); + transform_.reset(); + CGContextRetain(bitmap_context_); +} + +void BitmapPlatformDeviceMac::BitmapPlatformDeviceMacData::SetMatrixClip( + const SkMatrix& transform, + const SkRegion& region) { + transform_ = transform; + clip_region_ = region; + config_dirty_ = true; +} + +void BitmapPlatformDeviceMac::BitmapPlatformDeviceMacData::LoadConfig() { + if (!config_dirty_ || !bitmap_context_) + return; // Nothing to do. + config_dirty_ = false; + + // Transform. + SkMatrix t(transform_); + LoadTransformToCGContext(bitmap_context_, t); + t.setTranslateX(-t.getTranslateX()); + t.setTranslateY(-t.getTranslateY()); + LoadClippingRegionToCGContext(bitmap_context_, clip_region_, t); +} + + +// We use this static factory function instead of the regular constructor so +// that we can create the pixel data before calling the constructor. This is +// required so that we can call the base class' constructor with the pixel +// data. +BitmapPlatformDeviceMac* BitmapPlatformDeviceMac::Create(CGContextRef context, + int width, + int height, + bool is_opaque) { + void* data = malloc(height * width * 4); + if (!data) return NULL; + + SkBitmap bitmap; + bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); + 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); + + if (is_opaque) { +#ifndef NDEBUG + // To aid in finding bugs, we set the background color to something + // obviously wrong so it will be noticable when it is not cleared + bitmap.eraseARGB(255, 0, 255, 128); // bright bluish green +#endif + } + + CGColorSpaceRef color_space = + CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); + // allocate a bitmap context with 4 components per pixel (RGBA): + CGContextRef bitmap_context = + CGBitmapContextCreate(data, width, height, 8, width*4, + color_space, kCGImageAlphaPremultipliedLast); + + // Change the coordinate system to match WebCore's + CGContextTranslateCTM(bitmap_context, 0, height); + CGContextScaleCTM(bitmap_context, 1.0, -1.0); + CGColorSpaceRelease(color_space); + + // The device object will take ownership of the graphics context. + return new BitmapPlatformDeviceMac( + new BitmapPlatformDeviceMacData(bitmap_context), bitmap); +} + +// The device will own the bitmap, which corresponds to also owning the pixel +// data. Therefore, we do not transfer ownership to the SkDevice's bitmap. +BitmapPlatformDeviceMac::BitmapPlatformDeviceMac( + BitmapPlatformDeviceMacData* data, const SkBitmap& bitmap) + : PlatformDeviceMac(bitmap), + data_(data) { +} + +// The copy constructor just adds another reference to the underlying data. +// We use a const cast since the default Skia definitions don't define the +// proper constedness that we expect (accessBitmap should really be const). +BitmapPlatformDeviceMac::BitmapPlatformDeviceMac( + const BitmapPlatformDeviceMac& other) + : PlatformDeviceMac( + const_cast<BitmapPlatformDeviceMac&>(other).accessBitmap(true)), + data_(other.data_) { +} + +BitmapPlatformDeviceMac::~BitmapPlatformDeviceMac() { +} + +BitmapPlatformDeviceMac& BitmapPlatformDeviceMac::operator=( + const BitmapPlatformDeviceMac& other) { + data_ = other.data_; + return *this; +} + +CGContextRef BitmapPlatformDeviceMac::GetBitmapContext() { + return data_->GetBitmapContext(); +} + +void BitmapPlatformDeviceMac::setMatrixClip(const SkMatrix& transform, + const SkRegion& region) { + data_->SetMatrixClip(transform, region); +} + +void BitmapPlatformDeviceMac::DrawToContext(CGContextRef context, int x, int y, + const CGRect* src_rect) { + bool created_dc = false; + if (!data_->bitmap_context_) { + created_dc = true; + GetBitmapContext(); + } + + // this should not make a copy of the bits, since we're not doing + // anything to trigger copy on write + CGImageRef image = CGBitmapContextCreateImage(data_->bitmap_context_); + CGRect bounds; + if (src_rect) { + bounds = *src_rect; + bounds.origin.x = x; + bounds.origin.y = y; + CGImageRef sub_image = CGImageCreateWithImageInRect(image, *src_rect); + CGContextDrawImage(context, bounds, sub_image); + CGImageRelease(sub_image); + } else { + bounds.origin.x = 0; + bounds.origin.y = 0; + bounds.size.width = width(); + bounds.size.height = height(); + CGContextDrawImage(context, bounds, image); + } + CGImageRelease(image); + + if (created_dc) + data_->ReleaseBitmapContext(); +} + +// Returns the color value at the specified location. +SkColor BitmapPlatformDeviceMac::getColorAt(int x, int y) { + const SkBitmap& bitmap = accessBitmap(true); + SkAutoLockPixels lock(bitmap); + uint32_t* data = bitmap.getAddr32(0, 0); + return static_cast<SkColor>(data[x + y * width()]); +} + +void BitmapPlatformDeviceMac::onAccessBitmap(SkBitmap*) { + // Not needed in CoreGraphics +} + +void BitmapPlatformDeviceMac::processPixels(int x, int y, + int width, int height, + adjustAlpha adjustor) { + const SkBitmap& bitmap = accessBitmap(true); + SkMatrix& matrix = data_->transform_; + int bitmap_start_x = SkScalarRound(matrix.getTranslateX()) + x; + int bitmap_start_y = SkScalarRound(matrix.getTranslateY()) + y; + + SkAutoLockPixels lock(bitmap); + if (Constrain(bitmap.width(), &bitmap_start_x, &width) && + Constrain(bitmap.height(), &bitmap_start_y, &height)) { + uint32_t* data = bitmap.getAddr32(0, 0); + size_t row_words = bitmap.rowBytes() / 4; + for (int i = 0; i < height; i++) { + size_t offset = (i + bitmap_start_y) * row_words + bitmap_start_x; + for (int j = 0; j < width; j++) { + adjustor(data + offset + j); + } + } + } +} + +} // namespace gfx + diff --git a/skia/ext/bitmap_platform_device_mac.h b/skia/ext/bitmap_platform_device_mac.h new file mode 100755 index 0000000..db798eb --- /dev/null +++ b/skia/ext/bitmap_platform_device_mac.h @@ -0,0 +1,95 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SKIA_EXT_BITMAP_PLATFORM_DEVICE_MAC_H_ +#define SKIA_EXT_BITMAP_PLATFORM_DEVICE_MAC_H_ + +#include "base/ref_counted.h" +#include "skia/ext/platform_device_mac.h" + +namespace gfx { + +// A device is basically a wrapper around SkBitmap that provides a surface for +// SkCanvas to draw into. Our device provides a surface CoreGraphics can also +// write to. BitmapPlatformDeviceMac creates a bitmap using +// CGCreateBitmapContext() in a format that Skia supports and can then use this +// to draw text into, etc. This pixel data is provided to the bitmap that the +// device contains so that it can be shared. +// +// The device owns the pixel data, when the device goes away, the pixel data +// also becomes invalid. THIS IS DIFFERENT THAN NORMAL SKIA which uses +// reference counting for the pixel data. In normal Skia, you could assign +// another bitmap to this device's bitmap and everything will work properly. +// For us, that other bitmap will become invalid as soon as the device becomes +// invalid, which may lead to subtle bugs. Therefore, DO NOT ASSIGN THE +// DEVICE'S PIXEL DATA TO ANOTHER BITMAP, make sure you copy instead. +class BitmapPlatformDeviceMac : public PlatformDeviceMac { + public: + // Factory function. The screen DC is used to create the bitmap, and will not + // be stored beyond this function. is_opaque should be set if the caller + // knows the bitmap will be completely opaque and allows some optimizations. + // + // The shared_section parameter is optional (pass NULL for default behavior). + // If shared_section is non-null, then it must be a handle to a file-mapping + // object returned by CreateFileMapping. See CreateDIBSection for details. + static BitmapPlatformDeviceMac* Create(CGContextRef context, + int width, + int height, + bool is_opaque); + + // Copy constructor. When copied, devices duplicate their internal data, so + // stay linked. This is because their implementation is very heavyweight + // (lots of memory and CoreGraphics state). If a device has been copied, both + // clip rects and other state will stay in sync. + // + // This means it will NOT work to duplicate a device and assign it to a + // canvas, because the two canvases will each set their own clip rects, and + // the resulting CoreGraphics drawing state will be unpredictable. + // + // Copy constucting and "=" is designed for saving the device or passing it + // around to another routine willing to deal with the bitmap data directly. + BitmapPlatformDeviceMac(const BitmapPlatformDeviceMac& other); + virtual ~BitmapPlatformDeviceMac(); + + // See warning for copy constructor above. + BitmapPlatformDeviceMac& operator=(const BitmapPlatformDeviceMac& other); + + virtual CGContextRef GetBitmapContext(); + virtual void setMatrixClip(const SkMatrix& transform, const SkRegion& region); + + virtual void DrawToContext(CGContextRef context, int x, int y, + const CGRect* src_rect); + virtual bool IsVectorial() { return false; } + virtual void fixupAlphaBeforeCompositing() { }; + + // Returns the color value at the specified location. This does not + // consider any transforms that may be set on the device. + SkColor getColorAt(int x, int y); + + protected: + // Reference counted data that can be shared between multiple devices. This + // allows copy constructors and operator= for devices to work properly. The + // bitmaps used by the base device class are already refcounted and copyable. + class BitmapPlatformDeviceMacData; + + BitmapPlatformDeviceMac(BitmapPlatformDeviceMacData* data, + const SkBitmap& bitmap); + + // Flushes the CoreGraphics context so that the pixel data can be accessed + // directly by Skia. Overridden from SkDevice, this is called when Skia + // starts accessing pixel data. + virtual void onAccessBitmap(SkBitmap*); + + // Data associated with this device, guaranteed non-null. + scoped_refptr<BitmapPlatformDeviceMacData> data_; + + virtual void processPixels(int x, int y, + int width, int height, + adjustAlpha adjustor); +}; + +} // namespace gfx + +#endif // SKIA_EXT_BITMAP_PLATFORM_DEVICE_MAC_H_ + diff --git a/skia/ext/platform_canvas_mac.cc b/skia/ext/platform_canvas_mac.cc new file mode 100755 index 0000000..0bb56f4 --- /dev/null +++ b/skia/ext/platform_canvas_mac.cc @@ -0,0 +1,80 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "skia/ext/platform_canvas_mac.h" + +#include "base/logging.h" +#include "skia/ext/bitmap_platform_device_mac.h" + +namespace gfx { + +PlatformCanvasMac::PlatformCanvasMac() : SkCanvas() { +} + +PlatformCanvasMac::PlatformCanvasMac(int width, int height, bool is_opaque) + : SkCanvas() { + initialize(width, height, is_opaque); +} + +PlatformCanvasMac::PlatformCanvasMac(int width, + int height, + bool is_opaque, + CGContextRef context) + : SkCanvas() { + initialize(width, height, is_opaque); +} + +PlatformCanvasMac::~PlatformCanvasMac() { +} + +bool PlatformCanvasMac::initialize(int width, + int height, + bool is_opaque) { + SkDevice* device = createPlatformDevice(width, height, is_opaque, NULL); + if (!device) + return false; + + setDevice(device); + device->unref(); // was created with refcount 1, and setDevice also refs + return true; +} + +CGContextRef PlatformCanvasMac::beginPlatformPaint() { + return getTopPlatformDevice().GetBitmapContext(); +} + +void PlatformCanvasMac::endPlatformPaint() { + // flushing will be done in onAccessBitmap +} + +PlatformDeviceMac& PlatformCanvasMac::getTopPlatformDevice() const { + // All of our devices should be our special PlatformDeviceMac. + SkCanvas::LayerIter iter(const_cast<PlatformCanvasMac*>(this), false); + return *static_cast<PlatformDeviceMac*>(iter.device()); +} + +SkDevice* PlatformCanvasMac::createDevice(SkBitmap::Config config, + int width, + int height, + bool is_opaque, bool isForLayer) { + DCHECK(config == SkBitmap::kARGB_8888_Config); + return createPlatformDevice(width, height, is_opaque, NULL); +} + +SkDevice* PlatformCanvasMac::createPlatformDevice(int width, + int height, + bool is_opaque, + CGContextRef context) { + SkDevice* device = BitmapPlatformDeviceMac::Create(context, width, height, + is_opaque); + return device; +} + +SkDevice* PlatformCanvasMac::setBitmapDevice(const SkBitmap&) { + NOTREACHED(); + return NULL; +} + +} // namespace gfx + diff --git a/skia/ext/platform_canvas_mac.h b/skia/ext/platform_canvas_mac.h new file mode 100755 index 0000000..3f43fb2 --- /dev/null +++ b/skia/ext/platform_canvas_mac.h @@ -0,0 +1,88 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SKIA_EXT_PLATFORM_CANVAS_MAC_H_ +#define SKIA_EXT_PLATFORM_CANVAS_MAC_H_ + +#include "skia/ext/platform_device_mac.h" + +#include "SkCanvas.h" + +namespace gfx { + +// This class is a specialization of the regular SkCanvas that is designed to +// work with a gfx::PlatformDevice to manage platform-specific drawing. It +// allows using both Skia operations and platform-specific operations. +class PlatformCanvasMac : public SkCanvas { + public: + // Set is_opaque if you are going to erase the bitmap and not use + // tranparency: this will enable some optimizations. The shared_section + // parameter is passed to gfx::PlatformDevice::create. See it for details. + // + // If you use the version with no arguments, you MUST call initialize() + PlatformCanvasMac(); + PlatformCanvasMac(int width, int height, bool is_opaque); + PlatformCanvasMac(int width, int height, bool is_opaque, CGContextRef context); + virtual ~PlatformCanvasMac(); + + // For two-part init, call if you use the no-argument constructor above + bool initialize(int width, int height, bool is_opaque); + + // These calls should surround calls to platform drawing routines. The CG + // context returned by beginPlatformPaint is the one that can be used to + // draw into. + // Call endPlatformPaint when you are done and want to use Skia operations + // again; this will synchronize the bitmap. + virtual CGContextRef beginPlatformPaint(); + virtual void endPlatformPaint(); + + // Returns the platform device pointer of the topmost rect with a non-empty + // clip. In practice, this is usually either the top layer or nothing, since + // we usually set the clip to new layers when we make them. + // + // If there is no layer that is not all clipped out, this will return a + // dummy device so callers do not have to check. If you are concerned about + // performance, check the clip before doing any painting. + // + // This is different than SkCanvas' getDevice, because that returns the + // bottommost device. + // + // Danger: the resulting device should not be saved. It will be invalidated + // by the next call to save() or restore(). + PlatformDeviceMac& getTopPlatformDevice() const; + + // Allow callers to see the non-virtual function even though we have an + // override of a virtual one. + using SkCanvas::clipRect; + + protected: + // Creates a device store for use by the canvas. We override this so that + // the device is always our own so we know that we can use GDI operations + // on it. Simply calls into createPlatformDevice(). + virtual SkDevice* createDevice(SkBitmap::Config, int width, int height, + bool is_opaque, bool isForLayer); + + // Creates a device store for use by the canvas. By default, it creates a + // BitmapPlatformDevice object. Can be overridden to change the object type. + virtual SkDevice* createPlatformDevice(int width, int height, bool is_opaque, + CGContextRef context); + + private: + // Unimplemented. This is to try to prevent people from calling this function + // on SkCanvas. SkCanvas' version is not virtual, so we can't prevent this + // 100%, but hopefully this will make people notice and not use the function. + // Calling SkCanvas' version will create a new device which is not compatible + // with us and we will crash if somebody tries to draw into it with + // CoreGraphics. + SkDevice* setBitmapDevice(const SkBitmap& bitmap); + + // Disallow copy and assign. + PlatformCanvasMac(const PlatformCanvasMac&); + PlatformCanvasMac& operator=(const PlatformCanvasMac&); +}; + +} // namespace gfx + +#endif // SKIA_EXT_PLATFORM_CANVAS_MAC_H_ + diff --git a/skia/ext/platform_device_mac.cc b/skia/ext/platform_device_mac.cc new file mode 100755 index 0000000..9539e0f --- /dev/null +++ b/skia/ext/platform_device_mac.cc @@ -0,0 +1,161 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "skia/ext/bitmap_platform_device_mac.h" + +#include "base/logging.h" +#include "base/gfx/skia_utils_mac.h" +#include "SkMatrix.h" +#include "SkPath.h" +#include "SkUtils.h" + +namespace gfx { + +namespace { + +// Constrains position and size to fit within available_size. +bool constrain(int available_size, int* position, int *size) { + if (*position < 0) { + *size += *position; + *position = 0; + } + if (*size > 0 && *position < available_size) { + int overflow = (*position + *size) - available_size; + if (overflow > 0) { + *size -= overflow; + } + return true; + } + return false; +} + +// Sets the opacity of the specified value to 0xFF. +void makeOpaqueAlphaAdjuster(uint32_t* pixel) { + *pixel |= 0xFF000000; +} + +} // namespace + +PlatformDeviceMac::PlatformDeviceMac(const SkBitmap& bitmap) + : SkDevice(bitmap) { +} + +void PlatformDeviceMac::makeOpaque(int x, int y, int width, int height) { + processPixels(x, y, width, height, makeOpaqueAlphaAdjuster); +} + +// Set up the CGContextRef for peaceful coexistence with Skia +void PlatformDeviceMac::InitializeCGContext(CGContextRef context) { + // CG defaults to the same settings as Skia +} + +// static +void PlatformDeviceMac::LoadPathToCGContext(CGContextRef context, + const SkPath& path) { + // instead of a persistent attribute of the context, CG specifies the fill + // type per call, so we just have to load up the geometry. + CGContextBeginPath(context); + + SkPoint points[4] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} }; + SkPath::Iter iter(path, false); + for (SkPath::Verb verb = iter.next(points); verb != SkPath::kDone_Verb; + verb = iter.next(points)) { + switch (verb) { + case SkPath::kMove_Verb: { // iter.next returns 1 point + CGContextMoveToPoint(context, points[0].fX, points[0].fY); + break; + } + case SkPath::kLine_Verb: { // iter.next returns 2 points + CGContextAddLineToPoint(context, points[1].fX, points[1].fY); + break; + } + case SkPath::kQuad_Verb: { // iter.next returns 3 points + CGContextAddQuadCurveToPoint(context, points[1].fX, points[1].fY, + points[2].fX, points[2].fY); + break; + } + case SkPath::kCubic_Verb: { // iter.next returns 4 points + CGContextAddCurveToPoint(context, points[1].fX, points[1].fY, + points[2].fX, points[2].fY, + points[3].fX, points[3].fY); + break; + } + case SkPath::kClose_Verb: { // iter.next returns 1 point (the last point) + break; + } + case SkPath::kDone_Verb: // iter.next returns 0 points + default: { + NOTREACHED(); + break; + } + } + } + CGContextClosePath(context); +} + +// static +void PlatformDeviceMac::LoadTransformToCGContext(CGContextRef context, + const SkMatrix& matrix) { + // CoreGraphics can concatenate transforms, but not reset the current one. + // So in order to get the required behavior here, we need to first make + // the current transformation matrix identity and only then load the new one. + + // Reset matrix to identity. + CGAffineTransform orig_cg_matrix = CGContextGetCTM(context); + CGAffineTransform orig_cg_matrix_inv = CGAffineTransformInvert(orig_cg_matrix); + CGContextConcatCTM(context, orig_cg_matrix_inv); + + // assert that we have indeed returned to the identity Matrix. + DCHECK(CGAffineTransformIsIdentity(CGContextGetCTM(context))); + + // Convert xform to CG-land. + // Our coordinate system is flipped to match WebKit's so we need to modify + // the xform to match that. + SkMatrix transformed_matrix = matrix; + SkScalar sy = matrix.getScaleY() * (SkScalar)-1; + transformed_matrix.setScaleY(sy); + size_t height = CGBitmapContextGetHeight(context); + SkScalar ty = -matrix.getTranslateY(); // y axis is flipped. + transformed_matrix.setTranslateY(ty + (SkScalar)height); + + CGAffineTransform cg_matrix = SkMatrixToCGAffineTransform(transformed_matrix); + + // Load final transform into context. + CGContextConcatCTM(context, cg_matrix); +} + +// static +void PlatformDeviceMac::LoadClippingRegionToCGContext( + CGContextRef context, + const SkRegion& region, + const SkMatrix& transformation) { + if (region.isEmpty()) { + // region can be empty, in which case everything will be clipped. + SkRect rect; + rect.setEmpty(); + CGContextClipToRect(context, SkRectToCGRect(rect)); + } else if (region.isRect()) { + // Do the transformation. + SkRect rect; + rect.set(region.getBounds()); + transformation.mapRect(&rect); + SkIRect irect; + rect.round(&irect); + CGContextClipToRect(context, SkIRectToCGRect(irect)); + } else { + // It is complex. + SkPath path; + region.getBoundaryPath(&path); + // Clip. Note that windows clipping regions are not affected by the + // transform so apply it manually. + path.transform(transformation); + // TODO(playmobil): Implement. + NOTREACHED(); + // LoadPathToDC(context, path); + // hrgn = PathToRegion(context); + } +} + +} // namespace gfx + diff --git a/skia/ext/platform_device_mac.h b/skia/ext/platform_device_mac.h new file mode 100755 index 0000000..589448c --- /dev/null +++ b/skia/ext/platform_device_mac.h @@ -0,0 +1,86 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SKIA_EXT_PLATFORM_DEVICE_MAC_H_ +#define SKIA_EXT_PLATFORM_DEVICE_MAC_H_ + +#import <ApplicationServices/ApplicationServices.h> +#include "SkDevice.h" + +class SkMatrix; +class SkPath; +class SkRegion; + +namespace gfx { + +// A device is basically a wrapper around SkBitmap that provides a surface for +// SkCanvas to draw into. Our device provides a surface CoreGraphics can also +// write to. It also provides functionality to play well with CG drawing +// functions. +// This class is abstract and must be subclassed. It provides the basic +// interface to implement it either with or without a bitmap backend. +class PlatformDeviceMac : public SkDevice { + public: + // The CGContext that corresponds to the bitmap, used for CoreGraphics + // operations drawing into the bitmap. This is possibly heavyweight, so it + // should exist only during one pass of rendering. + virtual CGContextRef GetBitmapContext() = 0; + + // Draws to the given graphics context. If the bitmap context doesn't exist, + // this will temporarily create it. However, if you have created the bitmap + // context, it will be more efficient if you don't free it until after this + // call so it doesn't have to be created twice. If src_rect is null, then + // the entirety of the source device will be copied. + virtual void DrawToContext(CGContextRef context, int x, int y, + const CGRect* src_rect) = 0; + + // Sets the opacity of each pixel in the specified region to be opaque. + void makeOpaque(int x, int y, int width, int height); + + // Returns if the preferred rendering engine is vectorial or bitmap based. + virtual bool IsVectorial() = 0; + + // On platforms where the native rendering API does not support rendering + // into bitmaps with a premultiplied alpha channel, this call is responsible + // for doing any fixup necessary. It is not used on the Mac, since + // CoreGraphics can handle premultiplied alpha just fine. + virtual void fixupAlphaBeforeCompositing() = 0; + + // Initializes the default settings and colors in a device context. + static void InitializeCGContext(CGContextRef context); + + // Loads a SkPath into the CG context. The path can there after be used for + // clipping or as a stroke. + static void LoadPathToCGContext(CGContextRef context, const SkPath& path); + + // Loads a SkRegion into the CG context. + static void LoadClippingRegionToCGContext(CGContextRef context, + const SkRegion& region, + const SkMatrix& transformation); + + protected: + // Forwards |bitmap| to SkDevice's constructor. + PlatformDeviceMac(const SkBitmap& bitmap); + + // Loads the specified Skia transform into the device context + static void LoadTransformToCGContext(CGContextRef context, + const SkMatrix& matrix); + + // Function pointer used by the processPixels method for setting the alpha + // value of a particular pixel. + typedef void (*adjustAlpha)(uint32_t* pixel); + + // Loops through each of the pixels in the specified range, invoking + // adjustor for the alpha value of each pixel. + virtual void processPixels(int x, + int y, + int width, + int height, + adjustAlpha adjustor) = 0; +}; + +} // namespace gfx + +#endif // SKIA_EXT_PLATFORM_DEVICE_MAC_H_ + diff --git a/skia/skia.xcodeproj/project.pbxproj b/skia/skia.xcodeproj/project.pbxproj index cd45e94..7ecdb3b 100644 --- a/skia/skia.xcodeproj/project.pbxproj +++ b/skia/skia.xcodeproj/project.pbxproj @@ -10,6 +10,9 @@ 7B4DF47E0E5B5FE0004D7619 /* SkNinePatch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7B4DF47D0E5B5FE0004D7619 /* SkNinePatch.cpp */; }; 7B4DF4800E5B6005004D7619 /* SkScaledBitmapSampler.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7B4DF47F0E5B6005004D7619 /* SkScaledBitmapSampler.cpp */; }; 7B4DF48A0E5B609B004D7619 /* SkTypeface_fake.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7B4DF4820E5B6087004D7619 /* SkTypeface_fake.cpp */; }; + A70A3BAE0ED7385F00C31871 /* bitmap_platform_device_mac.cc in Sources */ = {isa = PBXBuildFile; fileRef = A70A3BA80ED7385F00C31871 /* bitmap_platform_device_mac.cc */; }; + A70A3BAF0ED7385F00C31871 /* platform_canvas_mac.cc in Sources */ = {isa = PBXBuildFile; fileRef = A70A3BAA0ED7385F00C31871 /* platform_canvas_mac.cc */; }; + A70A3BB00ED7385F00C31871 /* platform_device_mac.cc in Sources */ = {isa = PBXBuildFile; fileRef = A70A3BAC0ED7385F00C31871 /* platform_device_mac.cc */; }; E48EE4D20E34E873009DE966 /* Sk1DPathEffect.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AB4C46860DAE9C4F00FC0DB7 /* Sk1DPathEffect.cpp */; }; E48EE4D30E34E873009DE966 /* Sk2DPathEffect.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AB4C46870DAE9C4F00FC0DB7 /* Sk2DPathEffect.cpp */; }; E48EE4D40E34E873009DE966 /* Sk64.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AB4C465C0DAE9C4A00FC0DB7 /* Sk64.cpp */; }; @@ -144,6 +147,12 @@ 7B4DF47F0E5B6005004D7619 /* SkScaledBitmapSampler.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SkScaledBitmapSampler.cpp; sourceTree = "<group>"; }; 7B4DF4810E5B6087004D7619 /* SkDrawProcs.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SkDrawProcs.h; sourceTree = "<group>"; }; 7B4DF4820E5B6087004D7619 /* SkTypeface_fake.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SkTypeface_fake.cpp; sourceTree = "<group>"; }; + A70A3BA80ED7385F00C31871 /* bitmap_platform_device_mac.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = bitmap_platform_device_mac.cc; path = ext/bitmap_platform_device_mac.cc; sourceTree = "<group>"; }; + A70A3BA90ED7385F00C31871 /* bitmap_platform_device_mac.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = bitmap_platform_device_mac.h; path = ext/bitmap_platform_device_mac.h; sourceTree = "<group>"; }; + A70A3BAA0ED7385F00C31871 /* platform_canvas_mac.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = platform_canvas_mac.cc; path = ext/platform_canvas_mac.cc; sourceTree = "<group>"; }; + A70A3BAB0ED7385F00C31871 /* platform_canvas_mac.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = platform_canvas_mac.h; path = ext/platform_canvas_mac.h; sourceTree = "<group>"; }; + A70A3BAC0ED7385F00C31871 /* platform_device_mac.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = platform_device_mac.cc; path = ext/platform_device_mac.cc; sourceTree = "<group>"; }; + A70A3BAD0ED7385F00C31871 /* platform_device_mac.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = platform_device_mac.h; path = ext/platform_device_mac.h; sourceTree = "<group>"; }; AB4C45B00DAE9C3700FC0DB7 /* SkTime.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SkTime.cpp; sourceTree = "<group>"; }; AB4C465C0DAE9C4A00FC0DB7 /* Sk64.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Sk64.cpp; sourceTree = "<group>"; }; AB4C465D0DAE9C4A00FC0DB7 /* SkBuffer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SkBuffer.cpp; sourceTree = "<group>"; }; @@ -476,12 +485,26 @@ path = ../build; sourceTree = "<group>"; }; + A70A3BA70ED7385100C31871 /* ext */ = { + isa = PBXGroup; + children = ( + A70A3BA80ED7385F00C31871 /* bitmap_platform_device_mac.cc */, + A70A3BA90ED7385F00C31871 /* bitmap_platform_device_mac.h */, + A70A3BAA0ED7385F00C31871 /* platform_canvas_mac.cc */, + A70A3BAB0ED7385F00C31871 /* platform_canvas_mac.h */, + A70A3BAC0ED7385F00C31871 /* platform_device_mac.cc */, + A70A3BAD0ED7385F00C31871 /* platform_device_mac.h */, + ); + name = ext; + sourceTree = "<group>"; + }; AB4C450D0DAE9B9100FC0DB7 /* Source */ = { isa = PBXGroup; children = ( AB4C45130DAE9C3700FC0DB7 /* animator */, AB4C46580DAE9C4A00FC0DB7 /* corecg */, AB4C46850DAE9C4F00FC0DB7 /* effects */, + A70A3BA70ED7385100C31871 /* ext */, AB4C46C00DAE9C7000FC0DB7 /* images */, AB4C46DF0DAE9C7900FC0DB7 /* include */, E48EE5A20E34F13C009DE966 /* picture */, @@ -896,6 +919,9 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + A70A3BAE0ED7385F00C31871 /* bitmap_platform_device_mac.cc in Sources */, + A70A3BAF0ED7385F00C31871 /* platform_canvas_mac.cc in Sources */, + A70A3BB00ED7385F00C31871 /* platform_device_mac.cc in Sources */, E48EE4D20E34E873009DE966 /* Sk1DPathEffect.cpp in Sources */, E48EE4D30E34E873009DE966 /* Sk2DPathEffect.cpp in Sources */, E48EE4D40E34E873009DE966 /* Sk64.cpp in Sources */, @@ -1041,12 +1067,14 @@ isa = XCBuildConfiguration; baseConfigurationReference = 7B4DF44D0E5B5E5C004D7619 /* staticlib.xcconfig */; buildSettings = { + FRAMEWORK_SEARCH_PATHS = "$(SYSTEM_LIBRARY_DIR)/Frameworks/ApplicationServices.framework/Versions/A/Frameworks"; GCC_PREFIX_HEADER = skia_Prefix.pch; GCC_PREPROCESSOR_DEFINITIONS = ( "$(GCC_PREPROCESSOR_DEFINITIONS)", SK_BUILD_FOR_MAC, ); HEADER_SEARCH_PATHS = ( + .., corecg, include, include/corecg, @@ -1061,12 +1089,14 @@ isa = XCBuildConfiguration; baseConfigurationReference = 7B4DF44D0E5B5E5C004D7619 /* staticlib.xcconfig */; buildSettings = { + FRAMEWORK_SEARCH_PATHS = "$(SYSTEM_LIBRARY_DIR)/Frameworks/ApplicationServices.framework/Versions/A/Frameworks"; GCC_PREFIX_HEADER = skia_Prefix.pch; GCC_PREPROCESSOR_DEFINITIONS = ( "$(GCC_PREPROCESSOR_DEFINITIONS)", SK_BUILD_FOR_MAC, ); HEADER_SEARCH_PATHS = ( + .., corecg, include, include/corecg, |