• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "skia/ext/bitmap_platform_device_skia.h"
6 #include "skia/ext/platform_canvas.h"
7 
8 namespace skia {
9 
Create(int width,int height,bool is_opaque)10 BitmapPlatformDevice* BitmapPlatformDevice::Create(int width, int height,
11                                                    bool is_opaque) {
12   SkBitmap bitmap;
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 }
22 
CreateAndClear(int width,int height,bool is_opaque)23 BitmapPlatformDevice* BitmapPlatformDevice::CreateAndClear(int width,
24                                                            int height,
25                                                            bool is_opaque) {
26   BitmapPlatformDevice* device = Create(width, height, is_opaque);
27   if (!is_opaque)
28     device->clear(0);
29   return device;
30 }
31 
Create(int width,int height,bool is_opaque,uint8_t * data)32 BitmapPlatformDevice* BitmapPlatformDevice::Create(int width, int height,
33                                                    bool is_opaque,
34                                                    uint8_t* data) {
35   SkBitmap bitmap;
36   bitmap.setInfo(SkImageInfo::MakeN32(width, height,
37       is_opaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType));
38   if (data)
39     bitmap.setPixels(data);
40   else if (!bitmap.tryAllocPixels())
41     return NULL;
42 
43   return new BitmapPlatformDevice(bitmap);
44 }
45 
BitmapPlatformDevice(const SkBitmap & bitmap)46 BitmapPlatformDevice::BitmapPlatformDevice(const SkBitmap& bitmap)
47     : SkBitmapDevice(bitmap) {
48   SetPlatformDevice(this, this);
49 }
50 
~BitmapPlatformDevice()51 BitmapPlatformDevice::~BitmapPlatformDevice() {
52 }
53 
onCreateDevice(const SkImageInfo & info,Usage)54 SkBaseDevice* BitmapPlatformDevice::onCreateDevice(const SkImageInfo& info,
55                                                    Usage /*usage*/) {
56   SkASSERT(info.colorType() == kN32_SkColorType);
57   return BitmapPlatformDevice::Create(info.width(), info.height(),
58                                       info.isOpaque());
59 }
60 
BeginPlatformPaint()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();
65 }
66 
DrawToNativeContext(PlatformSurface surface,int x,int y,const PlatformRect * src_rect)67 void BitmapPlatformDevice::DrawToNativeContext(
68     PlatformSurface surface, int x, int y, const PlatformRect* src_rect) {
69   // Should never be called on Android.
70   SkASSERT(false);
71 }
72 
73 // PlatformCanvas impl
74 
CreatePlatformCanvas(int width,int height,bool is_opaque,uint8_t * data,OnFailureType failureType)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);
80 }
81 
82 // Port of PlatformBitmap to android
~PlatformBitmap()83 PlatformBitmap::~PlatformBitmap() {
84   // Nothing to do.
85 }
86 
Allocate(int width,int height,bool is_opaque)87 bool PlatformBitmap::Allocate(int width, int height, bool is_opaque) {
88   if (!bitmap_.tryAllocN32Pixels(width, height, is_opaque))
89     return false;
90 
91   surface_ = bitmap_.getPixels();
92   return true;
93 }
94 
95 }  // namespace skia
96