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
.tryAllocN32Pixels(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::Create(int width
, int height
,
27 bitmap
.setInfo(SkImageInfo::MakeN32(width
, height
,
28 is_opaque
? kOpaque_SkAlphaType
: kPremul_SkAlphaType
));
30 bitmap
.setPixels(data
);
31 else if (!bitmap
.tryAllocPixels())
34 return new BitmapPlatformDevice(bitmap
);
37 BitmapPlatformDevice::BitmapPlatformDevice(const SkBitmap
& bitmap
)
38 : SkBitmapDevice(bitmap
) {
39 SetPlatformDevice(this, this);
42 BitmapPlatformDevice::~BitmapPlatformDevice() {
45 SkBaseDevice
* BitmapPlatformDevice::onCreateDevice(const CreateInfo
& info
,
47 SkASSERT(info
.fInfo
.colorType() == kN32_SkColorType
);
48 return BitmapPlatformDevice::Create(info
.fInfo
.width(), info
.fInfo
.height(),
49 info
.fInfo
.isOpaque());
52 PlatformSurface
BitmapPlatformDevice::BeginPlatformPaint() {
53 // TODO(zhenghao): What should we return? The ptr to the address of the
54 // pixels? Maybe this won't be called at all.
55 return accessBitmap(true).getPixels();
58 // PlatformCanvas impl
60 SkCanvas
* CreatePlatformCanvas(int width
, int height
, bool is_opaque
,
61 uint8_t* data
, OnFailureType failureType
) {
62 skia::RefPtr
<SkBaseDevice
> dev
= skia::AdoptRef(
63 BitmapPlatformDevice::Create(width
, height
, is_opaque
, data
));
64 return CreateCanvas(dev
, failureType
);
67 // Port of PlatformBitmap to android
68 PlatformBitmap::~PlatformBitmap() {
72 bool PlatformBitmap::Allocate(int width
, int height
, bool is_opaque
) {
73 if (!bitmap_
.tryAllocN32Pixels(width
, height
, is_opaque
))
76 surface_
= bitmap_
.getPixels();