epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 7 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkDevice.h" |
reed@google.com | a7d9485 | 2011-03-30 21:23:07 +0000 | [diff] [blame] | 9 | #include "SkMetaData.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | |
robertphillips@google.com | 53238bc | 2013-08-30 13:12:10 +0000 | [diff] [blame] | 11 | #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A) |
| 12 | const SkCanvas::Config8888 SkBaseDevice::kPMColorAlias = SkCanvas::kBGRA_Premul_Config8888; |
| 13 | #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A) |
| 14 | const SkCanvas::Config8888 SkBaseDevice::kPMColorAlias = SkCanvas::kRGBA_Premul_Config8888; |
| 15 | #else |
| 16 | const SkCanvas::Config8888 SkBaseDevice::kPMColorAlias = (SkCanvas::Config8888) -1; |
| 17 | #endif |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 18 | |
mike@reedtribe.org | ea4ac97 | 2011-04-26 11:48:33 +0000 | [diff] [blame] | 19 | /////////////////////////////////////////////////////////////////////////////// |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 20 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 21 | SkBaseDevice::SkBaseDevice() |
skia.committer@gmail.com | 772c4e6 | 2013-08-30 07:01:34 +0000 | [diff] [blame] | 22 | : fLeakyProperties(SkDeviceProperties::MakeDefault()) |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 23 | #ifdef SK_DEBUG |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 24 | , fAttachedToCanvas(false) |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 25 | #endif |
| 26 | { |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 27 | fOrigin.setZero(); |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 28 | fMetaData = NULL; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 29 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 31 | SkBaseDevice::SkBaseDevice(const SkDeviceProperties& deviceProperties) |
bungeman@google.com | 532470f | 2013-01-22 19:25:14 +0000 | [diff] [blame] | 32 | : fLeakyProperties(deviceProperties) |
| 33 | #ifdef SK_DEBUG |
| 34 | , fAttachedToCanvas(false) |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 35 | #endif |
| 36 | { |
reed@google.com | af951c9 | 2011-06-16 19:10:39 +0000 | [diff] [blame] | 37 | fOrigin.setZero(); |
| 38 | fMetaData = NULL; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 39 | } |
| 40 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 41 | SkBaseDevice::~SkBaseDevice() { |
reed@google.com | a7d9485 | 2011-03-30 21:23:07 +0000 | [diff] [blame] | 42 | delete fMetaData; |
| 43 | } |
| 44 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 45 | SkBaseDevice* SkBaseDevice::createCompatibleDevice(const SkImageInfo& info) { |
| 46 | #ifdef SK_SUPPORT_LEGACY_COMPATIBLEDEVICE_CONFIG |
| 47 | // We call the old method to support older subclasses. |
| 48 | // If they have, we return their device, else we use the new impl. |
| 49 | SkBitmap::Config config = SkColorTypeToBitmapConfig(info.colorType()); |
| 50 | SkBaseDevice* dev = this->onCreateCompatibleDevice(config, |
| 51 | info.width(), |
| 52 | info.height(), |
| 53 | info.isOpaque(), |
| 54 | kGeneral_Usage); |
| 55 | if (dev) { |
| 56 | return dev; |
| 57 | } |
| 58 | // fall through to new impl |
| 59 | #endif |
| 60 | return this->onCreateDevice(info, kGeneral_Usage); |
| 61 | } |
| 62 | |
| 63 | SkBaseDevice* SkBaseDevice::createCompatibleDeviceForSaveLayer(const SkImageInfo& info) { |
| 64 | #ifdef SK_SUPPORT_LEGACY_COMPATIBLEDEVICE_CONFIG |
| 65 | // We call the old method to support older subclasses. |
| 66 | // If they have, we return their device, else we use the new impl. |
| 67 | SkBitmap::Config config = SkColorTypeToBitmapConfig(info.colorType()); |
| 68 | SkBaseDevice* dev = this->onCreateCompatibleDevice(config, |
| 69 | info.width(), |
| 70 | info.height(), |
| 71 | info.isOpaque(), |
| 72 | kSaveLayer_Usage); |
| 73 | if (dev) { |
| 74 | return dev; |
| 75 | } |
| 76 | // fall through to new impl |
| 77 | #endif |
| 78 | return this->onCreateDevice(info, kSaveLayer_Usage); |
| 79 | } |
| 80 | |
| 81 | #ifdef SK_SUPPORT_LEGACY_COMPATIBLEDEVICE_CONFIG |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 82 | SkBaseDevice* SkBaseDevice::createCompatibleDevice(SkBitmap::Config config, |
| 83 | int width, int height, |
| 84 | bool isOpaque) { |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 85 | SkImageInfo info = SkImageInfo::Make(width, height, |
| 86 | SkBitmapConfigToColorType(config), |
| 87 | isOpaque ? kOpaque_SkAlphaType |
| 88 | : kPremul_SkAlphaType); |
| 89 | return this->createCompatibleDevice(info); |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 90 | } |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 91 | #endif |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 92 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 93 | SkMetaData& SkBaseDevice::getMetaData() { |
reed@google.com | a7d9485 | 2011-03-30 21:23:07 +0000 | [diff] [blame] | 94 | // metadata users are rare, so we lazily allocate it. If that changes we |
| 95 | // can decide to just make it a field in the device (rather than a ptr) |
| 96 | if (NULL == fMetaData) { |
| 97 | fMetaData = new SkMetaData; |
| 98 | } |
| 99 | return *fMetaData; |
| 100 | } |
| 101 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 102 | // TODO: should make this guy pure-virtual. |
| 103 | SkImageInfo SkBaseDevice::imageInfo() const { |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 104 | return SkImageInfo::MakeUnknown(this->width(), this->height()); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 105 | } |
| 106 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 107 | const SkBitmap& SkBaseDevice::accessBitmap(bool changePixels) { |
| 108 | const SkBitmap& bitmap = this->onAccessBitmap(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | if (changePixels) { |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 110 | bitmap.notifyPixelsChanged(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 111 | } |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 112 | return bitmap; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | } |
| 114 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame^] | 115 | #ifdef SK_SUPPORT_LEGACY_READPIXELSCONFIG |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 116 | bool SkBaseDevice::readPixels(SkBitmap* bitmap, int x, int y, |
| 117 | SkCanvas::Config8888 config8888) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 118 | if (SkBitmap::kARGB_8888_Config != bitmap->config() || |
| 119 | NULL != bitmap->getTexture()) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 120 | return false; |
| 121 | } |
| 122 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 123 | const SkBitmap& src = this->accessBitmap(false); |
| 124 | |
| 125 | SkIRect srcRect = SkIRect::MakeXYWH(x, y, bitmap->width(), |
| 126 | bitmap->height()); |
| 127 | SkIRect devbounds = SkIRect::MakeWH(src.width(), src.height()); |
| 128 | if (!srcRect.intersect(devbounds)) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 129 | return false; |
| 130 | } |
| 131 | |
| 132 | SkBitmap tmp; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 133 | SkBitmap* bmp; |
| 134 | if (bitmap->isNull()) { |
reed@google.com | 9ebcac5 | 2014-01-24 18:53:42 +0000 | [diff] [blame] | 135 | if (!tmp.allocPixels(SkImageInfo::MakeN32Premul(bitmap->width(), |
| 136 | bitmap->height()))) { |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 137 | return false; |
| 138 | } |
| 139 | bmp = &tmp; |
| 140 | } else { |
| 141 | bmp = bitmap; |
bsalomon@google.com | 1a8ddf0 | 2011-11-02 19:34:16 +0000 | [diff] [blame] | 142 | } |
bsalomon@google.com | ace7bd5 | 2011-11-02 19:39:51 +0000 | [diff] [blame] | 143 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 144 | SkIRect subrect = srcRect; |
| 145 | subrect.offset(-x, -y); |
| 146 | SkBitmap bmpSubset; |
| 147 | bmp->extractSubset(&bmpSubset, subrect); |
| 148 | |
bsalomon@google.com | 6850eab | 2011-11-03 20:29:47 +0000 | [diff] [blame] | 149 | bool result = this->onReadPixels(bmpSubset, |
| 150 | srcRect.fLeft, |
| 151 | srcRect.fTop, |
| 152 | config8888); |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 153 | if (result && bmp == &tmp) { |
| 154 | tmp.swap(*bitmap); |
| 155 | } |
| 156 | return result; |
| 157 | } |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame^] | 158 | bool SkBaseDevice::onReadPixels(const SkBitmap&, int x, int y, SkCanvas::Config8888) { |
| 159 | return false; |
| 160 | } |
| 161 | #endif |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 162 | |
| 163 | SkSurface* SkBaseDevice::newSurface(const SkImageInfo&) { return NULL; } |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 164 | |
| 165 | const void* SkBaseDevice::peekPixels(SkImageInfo*, size_t*) { return NULL; } |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 166 | |
| 167 | void SkBaseDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer, |
| 168 | const SkRRect& inner, const SkPaint& paint) { |
| 169 | SkPath path; |
| 170 | path.addRRect(outer); |
| 171 | path.addRRect(inner); |
| 172 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 173 | |
| 174 | const SkMatrix* preMatrix = NULL; |
| 175 | const bool pathIsMutable = true; |
| 176 | this->drawPath(draw, path, paint, preMatrix, pathIsMutable); |
| 177 | } |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 178 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame^] | 179 | bool SkBaseDevice::readPixels(const SkImageInfo& info, void* dstP, size_t rowBytes, int x, int y) { |
| 180 | #ifdef SK_DEBUG |
| 181 | SkASSERT(info.width() > 0 && info.height() > 0); |
| 182 | SkASSERT(dstP); |
| 183 | SkASSERT(rowBytes >= info.minRowBytes()); |
| 184 | SkASSERT(x >= 0 && y >= 0); |
| 185 | |
| 186 | const SkImageInfo& srcInfo = this->imageInfo(); |
| 187 | SkASSERT(x + info.width() <= srcInfo.width()); |
| 188 | SkASSERT(y + info.height() <= srcInfo.height()); |
| 189 | #endif |
| 190 | return this->onReadPixels(info, dstP, rowBytes, x, y); |
| 191 | } |
| 192 | |
commit-bot@chromium.org | 4ef54f8 | 2014-03-17 17:03:18 +0000 | [diff] [blame] | 193 | bool SkBaseDevice::writePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes, |
| 194 | int x, int y) { |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 195 | #ifdef SK_DEBUG |
| 196 | SkASSERT(info.width() > 0 && info.height() > 0); |
| 197 | SkASSERT(pixels); |
| 198 | SkASSERT(rowBytes >= info.minRowBytes()); |
| 199 | SkASSERT(x >= 0 && y >= 0); |
| 200 | |
| 201 | const SkImageInfo& dstInfo = this->imageInfo(); |
| 202 | SkASSERT(x + info.width() <= dstInfo.width()); |
| 203 | SkASSERT(y + info.height() <= dstInfo.height()); |
| 204 | #endif |
| 205 | return this->onWritePixels(info, pixels, rowBytes, x, y); |
| 206 | } |
| 207 | |
| 208 | bool SkBaseDevice::onWritePixels(const SkImageInfo&, const void*, size_t, int, int) { |
| 209 | return false; |
| 210 | } |
| 211 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame^] | 212 | bool SkBaseDevice::onReadPixels(const SkImageInfo&, void*, size_t, int x, int y) { |
commit-bot@chromium.org | 2cccf83 | 2014-03-12 03:04:08 +0000 | [diff] [blame] | 213 | return false; |
| 214 | } |
| 215 | |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 216 | void* SkBaseDevice::accessPixels(SkImageInfo* info, size_t* rowBytes) { |
| 217 | SkImageInfo tmpInfo; |
| 218 | size_t tmpRowBytes; |
| 219 | if (NULL == info) { |
| 220 | info = &tmpInfo; |
| 221 | } |
| 222 | if (NULL == rowBytes) { |
| 223 | rowBytes = &tmpRowBytes; |
| 224 | } |
| 225 | return this->onAccessPixels(info, rowBytes); |
| 226 | } |
| 227 | |
| 228 | void* SkBaseDevice::onAccessPixels(SkImageInfo* info, size_t* rowBytes) { |
| 229 | return NULL; |
| 230 | } |
| 231 | |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 232 | void SkBaseDevice::EXPERIMENTAL_optimize(SkPicture* picture) { |
| 233 | // The base class doesn't perform any analysis but derived classes may |
| 234 | } |
| 235 | |
| 236 | bool SkBaseDevice::EXPERIMENTAL_drawPicture(const SkPicture& picture) { |
| 237 | // The base class doesn't perform any accelerated picture rendering |
| 238 | return false; |
| 239 | } |