Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / skia / ext / bitmap_platform_device_android.cc
blob8a29586abcba39c915f56ab495f84fa56f9de866
1 // Copyright (c) 2012 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_android.h"
6 #include "skia/ext/platform_canvas.h"
8 namespace skia {
10 BitmapPlatformDevice* BitmapPlatformDevice::Create(int width, int height,
11 bool is_opaque) {
12 SkBitmap bitmap;
13 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
14 if (bitmap.allocPixels()) {
15 bitmap.setIsOpaque(is_opaque);
16 // Follow the logic in SkCanvas::createDevice(), initialize the bitmap if it
17 // is not opaque.
18 if (!is_opaque)
19 bitmap.eraseARGB(0, 0, 0, 0);
20 return new BitmapPlatformDevice(bitmap);
22 return NULL;
25 BitmapPlatformDevice* BitmapPlatformDevice::CreateAndClear(int width,
26 int height,
27 bool is_opaque) {
28 BitmapPlatformDevice* device = Create(width, height, is_opaque);
29 if (!is_opaque)
30 device->accessBitmap(true).eraseARGB(0, 0, 0, 0);
31 return device;
34 BitmapPlatformDevice* BitmapPlatformDevice::Create(int width, int height,
35 bool is_opaque,
36 uint8_t* data) {
37 SkBitmap bitmap;
38 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
39 if (data)
40 bitmap.setPixels(data);
41 else if (!bitmap.allocPixels())
42 return NULL;
44 bitmap.setIsOpaque(is_opaque);
45 return new BitmapPlatformDevice(bitmap);
48 BitmapPlatformDevice::BitmapPlatformDevice(const SkBitmap& bitmap)
49 : SkDevice(bitmap) {
50 SetPlatformDevice(this, this);
53 BitmapPlatformDevice::~BitmapPlatformDevice() {
56 SkDevice* BitmapPlatformDevice::onCreateCompatibleDevice(
57 SkBitmap::Config config, int width, int height, bool isOpaque,
58 Usage /*usage*/) {
59 SkASSERT(config == SkBitmap::kARGB_8888_Config);
60 return BitmapPlatformDevice::Create(width, height, isOpaque);
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.
72 SkASSERT(false);
75 // PlatformCanvas impl
77 SkCanvas* CreatePlatformCanvas(int width, int height, bool is_opaque,
78 uint8_t* data, OnFailureType failureType) {
79 skia::RefPtr<SkDevice> 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() {
86 // Nothing to do.
89 bool PlatformBitmap::Allocate(int width, int height, bool is_opaque) {
90 bitmap_.setConfig(SkBitmap::kARGB_8888_Config, width, height);
91 if (!bitmap_.allocPixels())
92 return false;
94 bitmap_.setIsOpaque(is_opaque);
95 surface_ = bitmap_.getPixels();
96 return true;
99 } // namespace skia