| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_linux.h" | 5 #include "skia/ext/bitmap_platform_device_linux.h" |
| 6 | 6 |
| 7 #include <cairo/cairo.h> | 7 #include <cairo/cairo.h> |
| 8 | 8 |
| 9 namespace skia { | 9 namespace skia { |
| 10 | 10 |
| 11 // ----------------------------------------------------------------------------- | 11 // ----------------------------------------------------------------------------- |
| 12 // These objects are reference counted and own a Cairo surface. The surface is | 12 // These objects are reference counted and own a Cairo surface. The surface is |
| 13 // the backing store for a Skia bitmap and we reference count it so that we can | 13 // the backing store for a Skia bitmap and we reference count it so that we can |
| 14 // copy BitmapPlatformDeviceLinux objects without having to copy all the image | 14 // copy BitmapPlatformDevice objects without having to copy all the image |
| 15 // data. | 15 // data. |
| 16 // ----------------------------------------------------------------------------- | 16 // ----------------------------------------------------------------------------- |
| 17 class BitmapPlatformDeviceLinux::BitmapPlatformDeviceLinuxData | 17 class BitmapPlatformDevice::BitmapPlatformDeviceData |
| 18 : public base::RefCounted<BitmapPlatformDeviceLinuxData> { | 18 : public base::RefCounted<BitmapPlatformDeviceData> { |
| 19 public: | 19 public: |
| 20 explicit BitmapPlatformDeviceLinuxData(cairo_surface_t* surface) | 20 explicit BitmapPlatformDeviceData(cairo_surface_t* surface) |
| 21 : surface_(surface) { } | 21 : surface_(surface) { } |
| 22 | 22 |
| 23 cairo_surface_t* surface() const { return surface_; } | 23 cairo_surface_t* surface() const { return surface_; } |
| 24 | 24 |
| 25 protected: | 25 protected: |
| 26 cairo_surface_t *const surface_; | 26 cairo_surface_t *const surface_; |
| 27 | 27 |
| 28 friend class base::RefCounted<BitmapPlatformDeviceLinuxData>; | 28 friend class base::RefCounted<BitmapPlatformDeviceData>; |
| 29 ~BitmapPlatformDeviceLinuxData() { | 29 ~BitmapPlatformDeviceData() { |
| 30 cairo_surface_destroy(surface_); | 30 cairo_surface_destroy(surface_); |
| 31 } | 31 } |
| 32 | 32 |
| 33 // Disallow copy & assign. | 33 // Disallow copy & assign. |
| 34 BitmapPlatformDeviceLinuxData(const BitmapPlatformDeviceLinuxData&); | 34 BitmapPlatformDeviceData(const BitmapPlatformDeviceData&); |
| 35 BitmapPlatformDeviceLinuxData& operator=( | 35 BitmapPlatformDeviceData& operator=( |
| 36 const BitmapPlatformDeviceLinuxData&); | 36 const BitmapPlatformDeviceData&); |
| 37 }; | 37 }; |
| 38 | 38 |
| 39 // We use this static factory function instead of the regular constructor so | 39 // We use this static factory function instead of the regular constructor so |
| 40 // that we can create the pixel data before calling the constructor. This is | 40 // that we can create the pixel data before calling the constructor. This is |
| 41 // required so that we can call the base class' constructor with the pixel | 41 // required so that we can call the base class' constructor with the pixel |
| 42 // data. | 42 // data. |
| 43 BitmapPlatformDeviceLinux* BitmapPlatformDeviceLinux::Create( | 43 BitmapPlatformDevice* BitmapPlatformDevice::Create(int width, int height, |
| 44 int width, int height, bool is_opaque, cairo_surface_t* surface) { | 44 bool is_opaque, |
| 45 cairo_surface_t* surface) { |
| 45 SkBitmap bitmap; | 46 SkBitmap bitmap; |
| 46 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height, | 47 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height, |
| 47 cairo_image_surface_get_stride(surface)); | 48 cairo_image_surface_get_stride(surface)); |
| 48 bitmap.setPixels(cairo_image_surface_get_data(surface)); | 49 bitmap.setPixels(cairo_image_surface_get_data(surface)); |
| 49 bitmap.setIsOpaque(is_opaque); | 50 bitmap.setIsOpaque(is_opaque); |
| 50 | 51 |
| 51 #ifndef NDEBUG | 52 #ifndef NDEBUG |
| 52 if (is_opaque) { | 53 if (is_opaque) { |
| 53 bitmap.eraseARGB(255, 0, 255, 128); // bright bluish green | 54 bitmap.eraseARGB(255, 0, 255, 128); // bright bluish green |
| 54 } | 55 } |
| 55 #endif | 56 #endif |
| 56 | 57 |
| 57 // The device object will take ownership of the graphics context. | 58 // The device object will take ownership of the graphics context. |
| 58 return new BitmapPlatformDeviceLinux | 59 return new BitmapPlatformDevice |
| 59 (bitmap, new BitmapPlatformDeviceLinuxData(surface)); | 60 (bitmap, new BitmapPlatformDeviceData(surface)); |
| 60 } | 61 } |
| 61 | 62 |
| 62 BitmapPlatformDeviceLinux* BitmapPlatformDeviceLinux::Create( | 63 BitmapPlatformDevice* BitmapPlatformDevice::Create(int width, int height, |
| 63 int width, int height, bool is_opaque) { | 64 bool is_opaque) { |
| 64 cairo_surface_t* surface = | 65 cairo_surface_t* surface = |
| 65 cairo_image_surface_create(CAIRO_FORMAT_ARGB32, | 66 cairo_image_surface_create(CAIRO_FORMAT_ARGB32, |
| 66 width, height); | 67 width, height); |
| 67 | 68 |
| 68 return Create(width, height, is_opaque, surface); | 69 return Create(width, height, is_opaque, surface); |
| 69 } | 70 } |
| 70 | 71 |
| 71 BitmapPlatformDeviceLinux* BitmapPlatformDeviceLinux::Create( | 72 BitmapPlatformDevice* BitmapPlatformDevice::Create(int width, int height, |
| 72 int width, int height, bool is_opaque, uint8_t* data) { | 73 bool is_opaque, |
| 74 uint8_t* data) { |
| 73 cairo_surface_t* surface = cairo_image_surface_create_for_data( | 75 cairo_surface_t* surface = cairo_image_surface_create_for_data( |
| 74 data, CAIRO_FORMAT_ARGB32, width, height, | 76 data, CAIRO_FORMAT_ARGB32, width, height, |
| 75 cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width)); | 77 cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width)); |
| 76 | 78 |
| 77 return Create(width, height, is_opaque, surface); | 79 return Create(width, height, is_opaque, surface); |
| 78 } | 80 } |
| 79 | 81 |
| 80 // The device will own the bitmap, which corresponds to also owning the pixel | 82 // The device will own the bitmap, which corresponds to also owning the pixel |
| 81 // data. Therefore, we do not transfer ownership to the SkDevice's bitmap. | 83 // data. Therefore, we do not transfer ownership to the SkDevice's bitmap. |
| 82 BitmapPlatformDeviceLinux::BitmapPlatformDeviceLinux( | 84 BitmapPlatformDevice::BitmapPlatformDevice( |
| 83 const SkBitmap& bitmap, | 85 const SkBitmap& bitmap, |
| 84 BitmapPlatformDeviceLinuxData* data) | 86 BitmapPlatformDeviceData* data) |
| 85 : PlatformDeviceLinux(bitmap), | 87 : PlatformDevice(bitmap), |
| 86 data_(data) { | 88 data_(data) { |
| 87 } | 89 } |
| 88 | 90 |
| 89 BitmapPlatformDeviceLinux::BitmapPlatformDeviceLinux( | 91 BitmapPlatformDevice::BitmapPlatformDevice( |
| 90 const BitmapPlatformDeviceLinux& other) | 92 const BitmapPlatformDevice& other) |
| 91 : PlatformDeviceLinux(const_cast<BitmapPlatformDeviceLinux&>( | 93 : PlatformDevice(const_cast<BitmapPlatformDevice&>( |
| 92 other).accessBitmap(true)), | 94 other).accessBitmap(true)), |
| 93 data_(other.data_) { | 95 data_(other.data_) { |
| 94 } | 96 } |
| 95 | 97 |
| 96 BitmapPlatformDeviceLinux::~BitmapPlatformDeviceLinux() { | 98 BitmapPlatformDevice::~BitmapPlatformDevice() { |
| 97 } | 99 } |
| 98 | 100 |
| 99 cairo_surface_t* BitmapPlatformDeviceLinux::surface() const { | 101 cairo_surface_t* BitmapPlatformDevice::surface() const { |
| 100 return data_->surface(); | 102 return data_->surface(); |
| 101 } | 103 } |
| 102 | 104 |
| 103 BitmapPlatformDeviceLinux& BitmapPlatformDeviceLinux::operator=( | 105 BitmapPlatformDevice& BitmapPlatformDevice::operator=( |
| 104 const BitmapPlatformDeviceLinux& other) { | 106 const BitmapPlatformDevice& other) { |
| 105 data_ = other.data_; | 107 data_ = other.data_; |
| 106 return *this; | 108 return *this; |
| 107 } | 109 } |
| 108 | 110 |
| 109 } // namespace skia | 111 } // namespace skia |
| OLD | NEW |