1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "skia/ext/bitmap_platform_device_skia.h"
6 #include "skia/ext/platform_canvas.h"
10 BitmapPlatformDevice
* BitmapPlatformDevice::Create(int width
, int height
,
13 if (bitmap
.allocN32Pixels(width
, height
, is_opaque
)) {
14 // Follow the logic in SkCanvas::createDevice(), initialize the bitmap if it
17 bitmap
.eraseARGB(0, 0, 0, 0);
18 return new BitmapPlatformDevice(bitmap
);
23 BitmapPlatformDevice
* BitmapPlatformDevice::CreateAndClear(int width
,
26 BitmapPlatformDevice
* device
= Create(width
, height
, is_opaque
);
32 BitmapPlatformDevice
* BitmapPlatformDevice::Create(int width
, int height
,
36 bitmap
.setInfo(SkImageInfo::MakeN32(width
, height
,
37 is_opaque
? kOpaque_SkAlphaType
: kPremul_SkAlphaType
));
39 bitmap
.setPixels(data
);
40 else if (!bitmap
.allocPixels())
43 return new BitmapPlatformDevice(bitmap
);
46 BitmapPlatformDevice::BitmapPlatformDevice(const SkBitmap
& bitmap
)
47 : SkBitmapDevice(bitmap
) {
48 SetPlatformDevice(this, this);
51 BitmapPlatformDevice::~BitmapPlatformDevice() {
54 SkBaseDevice
* BitmapPlatformDevice::onCreateDevice(const SkImageInfo
& info
,
56 SkASSERT(info
.colorType() == kPMColor_SkColorType
);
57 return BitmapPlatformDevice::Create(info
.width(), info
.height(),
61 PlatformSurface
BitmapPlatformDevice::BeginPlatformPaint() {
62 // TODO(zhenghao): What should we return? The ptr to the address of the
63 // pixels? Maybe this won't be called at all.
64 return accessBitmap(true).getPixels();
67 void BitmapPlatformDevice::DrawToNativeContext(
68 PlatformSurface surface
, int x
, int y
, const PlatformRect
* src_rect
) {
69 // Should never be called on Android.
73 // PlatformCanvas impl
75 SkCanvas
* CreatePlatformCanvas(int width
, int height
, bool is_opaque
,
76 uint8_t* data
, OnFailureType failureType
) {
77 skia::RefPtr
<SkBaseDevice
> dev
= skia::AdoptRef(
78 BitmapPlatformDevice::Create(width
, height
, is_opaque
, data
));
79 return CreateCanvas(dev
, failureType
);
82 // Port of PlatformBitmap to android
83 PlatformBitmap::~PlatformBitmap() {
87 bool PlatformBitmap::Allocate(int width
, int height
, bool is_opaque
) {
88 if (!bitmap_
.allocN32Pixels(width
, height
, is_opaque
))
91 surface_
= bitmap_
.getPixels();