This source file includes following definitions.
- CGContextForData
- ReleaseBitmapContext
- SetMatrixClip
- LoadConfig
- Create
- CreateAndClear
- CreateWithData
- transform_
- GetBitmapContext
- setMatrixClip
- DrawToNativeContext
- onCreateDevice
- CreatePlatformCanvas
- CreatePlatformCanvas
- Allocate
#include "skia/ext/bitmap_platform_device_mac.h"
#import <ApplicationServices/ApplicationServices.h>
#include <time.h>
#include "base/mac/mac_util.h"
#include "base/memory/ref_counted.h"
#include "skia/ext/bitmap_platform_device.h"
#include "skia/ext/platform_canvas.h"
#include "skia/ext/skia_utils_mac.h"
#include "third_party/skia/include/core/SkMatrix.h"
#include "third_party/skia/include/core/SkRegion.h"
#include "third_party/skia/include/core/SkTypes.h"
#include "third_party/skia/include/core/SkUtils.h"
namespace skia {
namespace {
static CGContextRef CGContextForData(void* data, int width, int height) {
#define HAS_ARGB_SHIFTS(a, r, g, b) \
(SK_A32_SHIFT == (a) && SK_R32_SHIFT == (r) \
&& SK_G32_SHIFT == (g) && SK_B32_SHIFT == (b))
#if defined(SK_CPU_LENDIAN) && HAS_ARGB_SHIFTS(24, 16, 8, 0)
width = SkMax32(1, width);
height = SkMax32(1, height);
CGContextRef context =
CGBitmapContextCreate(data, width, height, 8, width * 4,
base::mac::GetSystemColorSpace(),
kCGImageAlphaPremultipliedFirst |
kCGBitmapByteOrder32Host);
#else
#error We require that Skia's and CoreGraphics's recommended \
image memory layout match.
#endif
#undef HAS_ARGB_SHIFTS
if (!context)
return NULL;
CGContextTranslateCTM(context, 0, height);
CGContextScaleCTM(context, 1.0, -1.0);
return context;
}
}
void BitmapPlatformDevice::ReleaseBitmapContext() {
SkASSERT(bitmap_context_);
CGContextRelease(bitmap_context_);
bitmap_context_ = NULL;
}
void BitmapPlatformDevice::SetMatrixClip(
const SkMatrix& transform,
const SkRegion& region) {
transform_ = transform;
clip_region_ = region;
config_dirty_ = true;
}
void BitmapPlatformDevice::LoadConfig() {
if (!config_dirty_ || !bitmap_context_)
return;
config_dirty_ = false;
CGContextRestoreGState(bitmap_context_);
CGContextSaveGState(bitmap_context_);
LoadTransformToCGContext(bitmap_context_, transform_);
LoadClippingRegionToCGContext(bitmap_context_, clip_region_, transform_);
}
BitmapPlatformDevice* BitmapPlatformDevice::Create(CGContextRef context,
int width,
int height,
bool is_opaque) {
if (RasterDeviceTooBigToAllocate(width, height))
return NULL;
SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height, 0,
is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
void* data;
if (context) {
data = CGBitmapContextGetData(context);
bitmap.setPixels(data);
} else {
if (!bitmap.allocPixels())
return NULL;
data = bitmap.getPixels();
}
#ifndef NDEBUG
if (!context && is_opaque) {
bitmap.eraseARGB(255, 0, 255, 128);
}
#endif
if (!context) {
context = CGContextForData(data, width, height);
if (!context)
return NULL;
} else
CGContextRetain(context);
BitmapPlatformDevice* rv = new BitmapPlatformDevice(context, bitmap);
CGContextRelease(context);
return rv;
}
BitmapPlatformDevice* BitmapPlatformDevice::CreateAndClear(int width,
int height,
bool is_opaque) {
BitmapPlatformDevice* device = Create(NULL, width, height, is_opaque);
if (!is_opaque)
device->clear(0);
return device;
}
BitmapPlatformDevice* BitmapPlatformDevice::CreateWithData(uint8_t* data,
int width,
int height,
bool is_opaque) {
CGContextRef context = NULL;
if (data)
context = CGContextForData(data, width, height);
BitmapPlatformDevice* rv = Create(context, width, height, is_opaque);
if (context)
CGContextRelease(context);
return rv;
}
BitmapPlatformDevice::BitmapPlatformDevice(
CGContextRef context, const SkBitmap& bitmap)
: SkBitmapDevice(bitmap),
bitmap_context_(context),
config_dirty_(true),
transform_(SkMatrix::I()) {
SetPlatformDevice(this, this);
SkASSERT(bitmap_context_);
SkIRect rect;
rect.set(0, 0,
CGBitmapContextGetWidth(bitmap_context_),
CGBitmapContextGetHeight(bitmap_context_));
clip_region_ = SkRegion(rect);
CGContextRetain(bitmap_context_);
CGContextSaveGState(bitmap_context_);
}
BitmapPlatformDevice::~BitmapPlatformDevice() {
if (bitmap_context_)
CGContextRelease(bitmap_context_);
}
CGContextRef BitmapPlatformDevice::GetBitmapContext() {
LoadConfig();
return bitmap_context_;
}
void BitmapPlatformDevice::setMatrixClip(const SkMatrix& transform,
const SkRegion& region,
const SkClipStack&) {
SetMatrixClip(transform, region);
}
void BitmapPlatformDevice::DrawToNativeContext(CGContextRef context, int x,
int y, const CGRect* src_rect) {
bool created_dc = false;
if (!bitmap_context_) {
created_dc = true;
GetBitmapContext();
}
CGImageRef image = CGBitmapContextCreateImage(bitmap_context_);
CGRect bounds;
bounds.origin.x = x;
bounds.origin.y = y;
if (src_rect) {
bounds.size.width = src_rect->size.width;
bounds.size.height = src_rect->size.height;
CGImageRef sub_image = CGImageCreateWithImageInRect(image, *src_rect);
CGContextDrawImage(context, bounds, sub_image);
CGImageRelease(sub_image);
} else {
bounds.size.width = width();
bounds.size.height = height();
CGContextDrawImage(context, bounds, image);
}
CGImageRelease(image);
if (created_dc)
ReleaseBitmapContext();
}
SkBaseDevice* BitmapPlatformDevice::onCreateDevice(const SkImageInfo& info,
Usage ) {
SkASSERT(info.colorType() == kPMColor_SkColorType);
return BitmapPlatformDevice::CreateAndClear(info.width(), info.height(),
info.isOpaque());
}
SkCanvas* CreatePlatformCanvas(CGContextRef ctx, int width, int height,
bool is_opaque, OnFailureType failureType) {
skia::RefPtr<SkBaseDevice> dev = skia::AdoptRef(
BitmapPlatformDevice::Create(ctx, width, height, is_opaque));
return CreateCanvas(dev, failureType);
}
SkCanvas* CreatePlatformCanvas(int width, int height, bool is_opaque,
uint8_t* data, OnFailureType failureType) {
skia::RefPtr<SkBaseDevice> dev = skia::AdoptRef(
BitmapPlatformDevice::CreateWithData(data, width, height, is_opaque));
return CreateCanvas(dev, failureType);
}
PlatformBitmap::~PlatformBitmap() {
if (surface_)
CGContextRelease(surface_);
}
bool PlatformBitmap::Allocate(int width, int height, bool is_opaque) {
if (RasterDeviceTooBigToAllocate(width, height))
return false;
bitmap_.setConfig(SkBitmap::kARGB_8888_Config, width, height, width * 4,
is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
if (!bitmap_.allocPixels())
return false;
if (!is_opaque)
bitmap_.eraseColor(0);
surface_ = CGContextForData(bitmap_.getPixels(), bitmap_.width(),
bitmap_.height());
return true;
}
}