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 bitmap
.setConfig(SkBitmap::kARGB_8888_Config
, width
, height
, 0,
14 is_opaque
? kOpaque_SkAlphaType
: kPremul_SkAlphaType
);
15 if (bitmap
.allocPixels()) {
16 // Follow the logic in SkCanvas::createDevice(), initialize the bitmap if it
19 bitmap
.eraseARGB(0, 0, 0, 0);
20 return new BitmapPlatformDevice(bitmap
);
25 BitmapPlatformDevice
* BitmapPlatformDevice::CreateAndClear(int width
,
28 BitmapPlatformDevice
* device
= Create(width
, height
, is_opaque
);
34 BitmapPlatformDevice
* BitmapPlatformDevice::Create(int width
, int height
,
38 bitmap
.setConfig(SkBitmap::kARGB_8888_Config
, width
, height
, 0,
39 is_opaque
? kOpaque_SkAlphaType
: kPremul_SkAlphaType
);
41 bitmap
.setPixels(data
);
42 else if (!bitmap
.allocPixels())
45 return new BitmapPlatformDevice(bitmap
);
48 BitmapPlatformDevice::BitmapPlatformDevice(const SkBitmap
& bitmap
)
49 : SkBitmapDevice(bitmap
) {
50 SetPlatformDevice(this, this);
53 BitmapPlatformDevice::~BitmapPlatformDevice() {
56 SkBaseDevice
* BitmapPlatformDevice::onCreateDevice(const SkImageInfo
& info
,
58 SkASSERT(info
.colorType() == kPMColor_SkColorType
);
59 return BitmapPlatformDevice::Create(info
.width(), info
.height(),
63 PlatformSurface
BitmapPlatformDevice::BeginPlatformPaint() {
64 // TODO(zhenghao): What should we return? The ptr to the address of the
65 // pixels? Maybe this won't be called at all.
66 return accessBitmap(true).getPixels();
69 void BitmapPlatformDevice::DrawToNativeContext(
70 PlatformSurface surface
, int x
, int y
, const PlatformRect
* src_rect
) {
71 // Should never be called on Android.
75 // PlatformCanvas impl
77 SkCanvas
* CreatePlatformCanvas(int width
, int height
, bool is_opaque
,
78 uint8_t* data
, OnFailureType failureType
) {
79 skia::RefPtr
<SkBaseDevice
> dev
= skia::AdoptRef(
80 BitmapPlatformDevice::Create(width
, height
, is_opaque
, data
));
81 return CreateCanvas(dev
, failureType
);
84 // Port of PlatformBitmap to android
85 PlatformBitmap::~PlatformBitmap() {
89 bool PlatformBitmap::Allocate(int width
, int height
, bool is_opaque
) {
90 bitmap_
.setConfig(SkBitmap::kARGB_8888_Config
, width
, height
, 0,
91 is_opaque
? kOpaque_SkAlphaType
: kPremul_SkAlphaType
);
92 if (!bitmap_
.allocPixels())
95 surface_
= bitmap_
.getPixels();