reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | */ |
| 7 | |
| 8 | #include "SkSurface_Base.h" |
| 9 | #include "SkImagePriv.h" |
| 10 | #include "SkCanvas.h" |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 11 | #include "SkDevice.h" |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 12 | #include "SkMallocPixelRef.h" |
| 13 | |
| 14 | static const size_t kIgnoreRowBytesValue = (size_t)~0; |
| 15 | |
| 16 | class SkSurface_Raster : public SkSurface_Base { |
| 17 | public: |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 18 | static bool Valid(const SkImageInfo&, size_t rb = kIgnoreRowBytesValue); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 19 | |
reed | 982542d | 2014-06-27 06:48:14 -0700 | [diff] [blame] | 20 | SkSurface_Raster(const SkImageInfo&, void*, size_t rb, |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 21 | void (*releaseProc)(void* pixels, void* context), void* context, |
| 22 | const SkSurfaceProps*); |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 23 | SkSurface_Raster(sk_sp<SkPixelRef>, const SkSurfaceProps*); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 24 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 25 | SkCanvas* onNewCanvas() override; |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 26 | sk_sp<SkSurface> onNewSurface(const SkImageInfo&) override; |
Mike Reed | 7eb01f8 | 2016-12-30 06:23:12 -0500 | [diff] [blame^] | 27 | sk_sp<SkImage> onNewImageSnapshot(SkBudgeted) override; |
tfarina | 7831a4b | 2015-04-27 07:53:07 -0700 | [diff] [blame] | 28 | void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) override; |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 29 | void onCopyOnWrite(ContentChangeMode) override; |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 30 | void onRestoreBackingMutability() override; |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 31 | |
| 32 | private: |
| 33 | SkBitmap fBitmap; |
reed | 9cd016e | 2016-01-30 10:01:06 -0800 | [diff] [blame] | 34 | size_t fRowBytes; |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 35 | bool fWeOwnThePixels; |
| 36 | |
| 37 | typedef SkSurface_Base INHERITED; |
| 38 | }; |
| 39 | |
| 40 | /////////////////////////////////////////////////////////////////////////////// |
| 41 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 42 | bool SkSurface_Raster::Valid(const SkImageInfo& info, size_t rowBytes) { |
reed | b2497c2 | 2014-12-31 12:31:43 -0800 | [diff] [blame] | 43 | if (info.isEmpty()) { |
| 44 | return false; |
| 45 | } |
| 46 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 47 | static const size_t kMaxTotalSize = SK_MaxS32; |
| 48 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 49 | int shift = 0; |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 50 | switch (info.colorType()) { |
reed@google.com | 1360c52 | 2014-01-08 21:25:26 +0000 | [diff] [blame] | 51 | case kAlpha_8_SkColorType: |
brianosman | 0e22eb8 | 2016-08-30 07:07:59 -0700 | [diff] [blame] | 52 | if (info.colorSpace()) { |
| 53 | return false; |
| 54 | } |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 55 | shift = 0; |
| 56 | break; |
reed@google.com | 1360c52 | 2014-01-08 21:25:26 +0000 | [diff] [blame] | 57 | case kRGB_565_SkColorType: |
brianosman | 0e22eb8 | 2016-08-30 07:07:59 -0700 | [diff] [blame] | 58 | if (info.colorSpace()) { |
| 59 | return false; |
| 60 | } |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 61 | shift = 1; |
| 62 | break; |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 63 | case kN32_SkColorType: |
brianosman | 0e22eb8 | 2016-08-30 07:07:59 -0700 | [diff] [blame] | 64 | if (info.colorSpace() && !info.colorSpace()->gammaCloseToSRGB()) { |
| 65 | return false; |
| 66 | } |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 67 | shift = 2; |
| 68 | break; |
reed | a34be68 | 2016-02-15 07:48:35 -0800 | [diff] [blame] | 69 | case kRGBA_F16_SkColorType: |
Brian Osman | eb21ef6 | 2016-11-01 16:30:21 -0400 | [diff] [blame] | 70 | if (info.colorSpace() && !info.colorSpace()->gammaIsLinear()) { |
brianosman | 0e22eb8 | 2016-08-30 07:07:59 -0700 | [diff] [blame] | 71 | return false; |
| 72 | } |
reed | a34be68 | 2016-02-15 07:48:35 -0800 | [diff] [blame] | 73 | shift = 3; |
| 74 | break; |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 75 | default: |
| 76 | return false; |
| 77 | } |
| 78 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 79 | if (kIgnoreRowBytesValue == rowBytes) { |
| 80 | return true; |
| 81 | } |
| 82 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 83 | uint64_t minRB = (uint64_t)info.width() << shift; |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 84 | if (minRB > rowBytes) { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | size_t alignedRowBytes = rowBytes >> shift << shift; |
| 89 | if (alignedRowBytes != rowBytes) { |
| 90 | return false; |
| 91 | } |
| 92 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 93 | uint64_t size = sk_64_mul(info.height(), rowBytes); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 94 | if (size > kMaxTotalSize) { |
| 95 | return false; |
| 96 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 97 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 98 | return true; |
| 99 | } |
| 100 | |
reed | 982542d | 2014-06-27 06:48:14 -0700 | [diff] [blame] | 101 | SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, void* pixels, size_t rb, |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 102 | void (*releaseProc)(void* pixels, void* context), void* context, |
| 103 | const SkSurfaceProps* props) |
| 104 | : INHERITED(info, props) |
reed@google.com | 1360c52 | 2014-01-08 21:25:26 +0000 | [diff] [blame] | 105 | { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 106 | fBitmap.installPixels(info, pixels, rb, nullptr, releaseProc, context); |
reed | 9cd016e | 2016-01-30 10:01:06 -0800 | [diff] [blame] | 107 | fRowBytes = 0; // don't need to track the rowbytes |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 108 | fWeOwnThePixels = false; // We are "Direct" |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 111 | SkSurface_Raster::SkSurface_Raster(sk_sp<SkPixelRef> pr, const SkSurfaceProps* props) |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 112 | : INHERITED(pr->info().width(), pr->info().height(), props) |
reed@google.com | 1360c52 | 2014-01-08 21:25:26 +0000 | [diff] [blame] | 113 | { |
commit-bot@chromium.org | e13af71 | 2014-01-13 20:39:14 +0000 | [diff] [blame] | 114 | const SkImageInfo& info = pr->info(); |
| 115 | |
reed | 9cd016e | 2016-01-30 10:01:06 -0800 | [diff] [blame] | 116 | fBitmap.setInfo(info, pr->rowBytes()); |
reed | 9cd016e | 2016-01-30 10:01:06 -0800 | [diff] [blame] | 117 | fRowBytes = pr->rowBytes(); // we track this, so that subsequent re-allocs will match |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 118 | fBitmap.setPixelRef(std::move(pr), 0, 0); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 119 | fWeOwnThePixels = true; |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 120 | } |
| 121 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 122 | SkCanvas* SkSurface_Raster::onNewCanvas() { return new SkCanvas(fBitmap, this->props()); } |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 123 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 124 | sk_sp<SkSurface> SkSurface_Raster::onNewSurface(const SkImageInfo& info) { |
| 125 | return SkSurface::MakeRaster(info, &this->props()); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 126 | } |
| 127 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 128 | void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, |
| 129 | const SkPaint* paint) { |
| 130 | canvas->drawBitmap(fBitmap, x, y, paint); |
| 131 | } |
| 132 | |
Mike Reed | 7eb01f8 | 2016-12-30 06:23:12 -0500 | [diff] [blame^] | 133 | sk_sp<SkImage> SkSurface_Raster::onNewImageSnapshot(SkBudgeted) { |
| 134 | SkCopyPixelsMode cpm = kIfMutable_SkCopyPixelsMode; |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 135 | if (fWeOwnThePixels) { |
| 136 | // SkImage_raster requires these pixels are immutable for its full lifetime. |
| 137 | // We'll undo this via onRestoreBackingMutability() if we can avoid the COW. |
| 138 | if (SkPixelRef* pr = fBitmap.pixelRef()) { |
| 139 | pr->setTemporarilyImmutable(); |
| 140 | } |
bsalomon | f47b9a3 | 2016-02-22 11:02:58 -0800 | [diff] [blame] | 141 | } else { |
reed | 1ec04d9 | 2016-08-05 12:07:41 -0700 | [diff] [blame] | 142 | cpm = kAlways_SkCopyPixelsMode; |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 143 | } |
bsalomon | f47b9a3 | 2016-02-22 11:02:58 -0800 | [diff] [blame] | 144 | |
fmalita | 9a5d1ab | 2015-07-27 10:27:28 -0700 | [diff] [blame] | 145 | // Our pixels are in memory, so read access on the snapshot SkImage could be cheap. |
| 146 | // Lock the shared pixel ref to ensure peekPixels() is usable. |
reed | 1ec04d9 | 2016-08-05 12:07:41 -0700 | [diff] [blame] | 147 | return SkMakeImageFromRasterBitmap(fBitmap, cpm); |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void SkSurface_Raster::onRestoreBackingMutability() { |
| 151 | SkASSERT(!this->hasCachedImage()); // Shouldn't be any snapshots out there. |
| 152 | if (SkPixelRef* pr = fBitmap.pixelRef()) { |
| 153 | pr->restoreMutability(); |
| 154 | } |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 155 | } |
| 156 | |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 157 | void SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) { |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 158 | // are we sharing pixelrefs with the image? |
Mike Reed | 85ff848 | 2016-12-29 09:36:20 -0500 | [diff] [blame] | 159 | sk_sp<SkImage> cached(this->refCachedImage(SkBudgeted::kNo)); |
bsalomon | f47b9a3 | 2016-02-22 11:02:58 -0800 | [diff] [blame] | 160 | SkASSERT(cached); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 161 | if (SkBitmapImageGetPixelRef(cached.get()) == fBitmap.pixelRef()) { |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 162 | SkASSERT(fWeOwnThePixels); |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 163 | if (kDiscard_ContentChangeMode == mode) { |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 164 | fBitmap.allocPixels(); |
| 165 | } else { |
| 166 | SkBitmap prev(fBitmap); |
reed | 9cd016e | 2016-01-30 10:01:06 -0800 | [diff] [blame] | 167 | fBitmap.allocPixels(); |
| 168 | prev.lockPixels(); |
| 169 | SkASSERT(prev.info() == fBitmap.info()); |
| 170 | SkASSERT(prev.rowBytes() == fBitmap.rowBytes()); |
| 171 | memcpy(fBitmap.getPixels(), prev.getPixels(), fBitmap.getSafeSize()); |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 172 | } |
reed | 9cd016e | 2016-01-30 10:01:06 -0800 | [diff] [blame] | 173 | SkASSERT(fBitmap.rowBytes() == fRowBytes); // be sure we always use the same value |
| 174 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 175 | // Now fBitmap is a deep copy of itself (and therefore different from |
| 176 | // what is being used by the image. Next we update the canvas to use |
| 177 | // this as its backend, so we can't modify the image's pixels anymore. |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 178 | SkASSERT(this->getCachedCanvas()); |
junov@chromium.org | acea3ef | 2013-04-16 19:41:09 +0000 | [diff] [blame] | 179 | this->getCachedCanvas()->getDevice()->replaceBitmapBackendForRasterSurface(fBitmap); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 183 | /////////////////////////////////////////////////////////////////////////////// |
| 184 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 185 | sk_sp<SkSurface> SkSurface::MakeRasterDirectReleaseProc(const SkImageInfo& info, void* pixels, |
| 186 | size_t rb, void (*releaseProc)(void* pixels, void* context), void* context, |
| 187 | const SkSurfaceProps* props) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 188 | if (nullptr == releaseProc) { |
| 189 | context = nullptr; |
reed | 982542d | 2014-06-27 06:48:14 -0700 | [diff] [blame] | 190 | } |
| 191 | if (!SkSurface_Raster::Valid(info, rb)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 192 | return nullptr; |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 193 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 194 | if (nullptr == pixels) { |
| 195 | return nullptr; |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 196 | } |
reed | 26e0e58 | 2015-07-29 11:44:52 -0700 | [diff] [blame] | 197 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 198 | return sk_make_sp<SkSurface_Raster>(info, pixels, rb, releaseProc, context, props); |
reed | 982542d | 2014-06-27 06:48:14 -0700 | [diff] [blame] | 199 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 200 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 201 | sk_sp<SkSurface> SkSurface::MakeRasterDirect(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 202 | const SkSurfaceProps* props) { |
| 203 | return MakeRasterDirectReleaseProc(info, pixels, rowBytes, nullptr, nullptr, props); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 204 | } |
| 205 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 206 | sk_sp<SkSurface> SkSurface::MakeRaster(const SkImageInfo& info, size_t rowBytes, |
| 207 | const SkSurfaceProps* props) { |
mike@reedtribe.org | b947625 | 2012-11-15 02:37:45 +0000 | [diff] [blame] | 208 | if (!SkSurface_Raster::Valid(info)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 209 | return nullptr; |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 210 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 211 | |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 212 | sk_sp<SkPixelRef> pr(SkMallocPixelRef::NewZeroed(info, rowBytes, nullptr)); |
| 213 | if (!pr) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 214 | return nullptr; |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 215 | } |
reed | 9cd016e | 2016-01-30 10:01:06 -0800 | [diff] [blame] | 216 | if (rowBytes) { |
| 217 | SkASSERT(pr->rowBytes() == rowBytes); |
| 218 | } |
Hal Canary | 1b3387b | 2016-12-12 13:48:12 -0500 | [diff] [blame] | 219 | return sk_make_sp<SkSurface_Raster>(std::move(pr), props); |
reed@google.com | c906204 | 2012-07-30 18:06:00 +0000 | [diff] [blame] | 220 | } |