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