OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "skia/ext/bitmap_platform_device_skia.h" | 5 #include "skia/ext/bitmap_platform_device_skia.h" |
6 #include "skia/ext/platform_canvas.h" | 6 #include "skia/ext/platform_canvas.h" |
7 | 7 |
8 namespace skia { | 8 namespace skia { |
9 | 9 |
10 BitmapPlatformDevice* BitmapPlatformDevice::Create(int width, int height, | 10 BitmapPlatformDevice* BitmapPlatformDevice::Create(int width, int height, |
11 bool is_opaque) { | 11 bool is_opaque) { |
12 SkBitmap bitmap; | 12 return Create(width, height, is_opaque, nullptr); |
13 if (bitmap.tryAllocN32Pixels(width, height, is_opaque)) { | |
14 // Follow the logic in SkCanvas::createDevice(), initialize the bitmap if it | |
15 // is not opaque. | |
16 if (!is_opaque) | |
17 bitmap.eraseARGB(0, 0, 0, 0); | |
18 return new BitmapPlatformDevice(bitmap); | |
19 } | |
20 return NULL; | |
21 } | 13 } |
22 | 14 |
23 BitmapPlatformDevice* BitmapPlatformDevice::Create(int width, int height, | 15 BitmapPlatformDevice* BitmapPlatformDevice::Create(int width, int height, |
24 bool is_opaque, | 16 bool is_opaque, |
25 uint8_t* data) { | 17 uint8_t* data) { |
26 SkBitmap bitmap; | 18 SkBitmap bitmap; |
27 bitmap.setInfo(SkImageInfo::MakeN32(width, height, | 19 bitmap.setInfo(SkImageInfo::MakeN32(width, height, |
28 is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType)); | 20 is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType)); |
29 if (data) | 21 |
| 22 if (data) { |
30 bitmap.setPixels(data); | 23 bitmap.setPixels(data); |
31 else if (!bitmap.tryAllocPixels()) | 24 } else { |
32 return NULL; | 25 if (!bitmap.tryAllocPixels()) |
| 26 return nullptr; |
| 27 // Follow the logic in SkCanvas::createDevice(), initialize the bitmap if |
| 28 // it is not opaque. |
| 29 if (!is_opaque) |
| 30 bitmap.eraseARGB(0, 0, 0, 0); |
| 31 } |
33 | 32 |
34 return new BitmapPlatformDevice(bitmap); | 33 return new BitmapPlatformDevice(bitmap); |
35 } | 34 } |
36 | 35 |
37 BitmapPlatformDevice::BitmapPlatformDevice(const SkBitmap& bitmap) | 36 BitmapPlatformDevice::BitmapPlatformDevice(const SkBitmap& bitmap) |
38 : SkBitmapDevice(bitmap) { | 37 : SkBitmapDevice(bitmap) { |
39 SetPlatformDevice(this, this); | 38 SetPlatformDevice(this, this); |
40 } | 39 } |
41 | 40 |
42 BitmapPlatformDevice::~BitmapPlatformDevice() { | 41 BitmapPlatformDevice::~BitmapPlatformDevice() { |
43 } | 42 } |
44 | 43 |
45 SkBaseDevice* BitmapPlatformDevice::onCreateDevice(const CreateInfo& info, | 44 SkBaseDevice* BitmapPlatformDevice::onCreateDevice(const CreateInfo& info, |
46 const SkPaint*) { | 45 const SkPaint*) { |
47 SkASSERT(info.fInfo.colorType() == kN32_SkColorType); | 46 SkASSERT(info.fInfo.colorType() == kN32_SkColorType); |
48 return BitmapPlatformDevice::Create(info.fInfo.width(), info.fInfo.height(), | 47 return BitmapPlatformDevice::Create(info.fInfo.width(), info.fInfo.height(), |
49 info.fInfo.isOpaque()); | 48 info.fInfo.isOpaque()); |
50 } | 49 } |
51 | 50 |
52 NativeDrawingContext BitmapPlatformDevice::BeginPlatformPaint( | |
53 const SkMatrix& transform, | |
54 const SkIRect& clip_bounds) { | |
55 // TODO(zhenghao): What should we return? The ptr to the address of the | |
56 // pixels? Maybe this won't be called at all. | |
57 SkPixmap pixmap; | |
58 return accessPixels(&pixmap) ? pixmap.writable_addr() : nullptr; | |
59 } | |
60 | |
61 // PlatformCanvas impl | 51 // PlatformCanvas impl |
62 | 52 |
63 std::unique_ptr<SkCanvas> CreatePlatformCanvasWithPixels( | 53 std::unique_ptr<SkCanvas> CreatePlatformCanvasWithPixels( |
64 int width, | 54 int width, |
65 int height, | 55 int height, |
66 bool is_opaque, | 56 bool is_opaque, |
67 uint8_t* data, | 57 uint8_t* data, |
68 OnFailureType failureType) { | 58 OnFailureType failureType) { |
69 sk_sp<SkBaseDevice> dev( | 59 sk_sp<SkBaseDevice> dev( |
70 BitmapPlatformDevice::Create(width, height, is_opaque, data)); | 60 BitmapPlatformDevice::Create(width, height, is_opaque, data)); |
71 return CreateCanvas(dev, failureType); | 61 return CreateCanvas(dev, failureType); |
72 } | 62 } |
73 | 63 |
74 } // namespace skia | 64 } // namespace skia |
OLD | NEW |