blob: f6e4c4f161833bc18fc60977f4ab0cd77148f120 [file] [log] [blame]
reed@google.comc9062042012-07-30 18:06:00 +00001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkMallocPixelRef.h"
10#include "include/private/SkImageInfoPriv.h"
11#include "src/core/SkDevice.h"
12#include "src/core/SkImagePriv.h"
13#include "src/image/SkSurface_Base.h"
reed@google.comc9062042012-07-30 18:06:00 +000014
reed@google.comc9062042012-07-30 18:06:00 +000015class SkSurface_Raster : public SkSurface_Base {
16public:
reed982542d2014-06-27 06:48:14 -070017 SkSurface_Raster(const SkImageInfo&, void*, size_t rb,
reed4a8126e2014-09-22 07:29:03 -070018 void (*releaseProc)(void* pixels, void* context), void* context,
19 const SkSurfaceProps*);
Matt Saretta6e976a2017-04-28 15:43:35 -040020 SkSurface_Raster(const SkImageInfo& info, sk_sp<SkPixelRef>, const SkSurfaceProps*);
reed@google.comc9062042012-07-30 18:06:00 +000021
mtklein36352bf2015-03-25 18:17:31 -070022 SkCanvas* onNewCanvas() override;
reede8f30622016-03-23 18:59:25 -070023 sk_sp<SkSurface> onNewSurface(const SkImageInfo&) override;
Mike Reed114bde82018-11-21 09:12:09 -050024 sk_sp<SkImage> onNewImageSnapshot(const SkIRect* subset) override;
Mike Reed4c790bd2018-02-08 14:10:40 -050025 void onWritePixels(const SkPixmap&, int x, int y) override;
tfarina7831a4b2015-04-27 07:53:07 -070026 void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) override;
mtklein36352bf2015-03-25 18:17:31 -070027 void onCopyOnWrite(ContentChangeMode) override;
reed26e0e582015-07-29 11:44:52 -070028 void onRestoreBackingMutability() override;
reed@google.comc9062042012-07-30 18:06:00 +000029
30private:
31 SkBitmap fBitmap;
reed9cd016e2016-01-30 10:01:06 -080032 size_t fRowBytes;
reed@google.comc9062042012-07-30 18:06:00 +000033 bool fWeOwnThePixels;
34
35 typedef SkSurface_Base INHERITED;
36};
37
38///////////////////////////////////////////////////////////////////////////////
39
Florin Malitaf40b2492017-05-03 15:23:21 -040040bool SkSurfaceValidateRasterInfo(const SkImageInfo& info, size_t rowBytes) {
Brian Osmane1adc3a2018-06-04 09:21:17 -040041 if (!SkImageInfoIsValid(info)) {
reedb2497c22014-12-31 12:31:43 -080042 return false;
43 }
44
Robert Phillipsea1b30b2019-09-19 16:05:48 -040045 if (info.colorType() == kR8G8_unorm_SkColorType ||
46 info.colorType() == kR16G16_unorm_SkColorType ||
47 info.colorType() == kR16G16_float_SkColorType ||
48 info.colorType() == kA16_unorm_SkColorType ||
49 info.colorType() == kA16_float_SkColorType ||
50 info.colorType() == kR16G16B16A16_unorm_SkColorType) {
Robert Phillipsd470e1b2019-09-04 15:05:35 -040051 return false;
52 }
53
reed@google.comc9062042012-07-30 18:06:00 +000054 if (kIgnoreRowBytesValue == rowBytes) {
55 return true;
56 }
57
Mike Reed7fcfb622018-02-09 13:26:46 -050058 int shift = info.shiftPerPixel();
Mike Kleinac568a92018-01-25 09:09:32 -050059
reede5ea5002014-09-03 11:54:58 -070060 uint64_t minRB = (uint64_t)info.width() << shift;
reed@google.comc9062042012-07-30 18:06:00 +000061 if (minRB > rowBytes) {
62 return false;
63 }
64
65 size_t alignedRowBytes = rowBytes >> shift << shift;
66 if (alignedRowBytes != rowBytes) {
67 return false;
68 }
69
reede5ea5002014-09-03 11:54:58 -070070 uint64_t size = sk_64_mul(info.height(), rowBytes);
Brian Osman38a62bd2018-10-11 14:45:07 -040071 static const size_t kMaxTotalSize = SK_MaxS32;
reed@google.comc9062042012-07-30 18:06:00 +000072 if (size > kMaxTotalSize) {
73 return false;
74 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000075
reed@google.comc9062042012-07-30 18:06:00 +000076 return true;
77}
78
reed982542d2014-06-27 06:48:14 -070079SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, void* pixels, size_t rb,
reed4a8126e2014-09-22 07:29:03 -070080 void (*releaseProc)(void* pixels, void* context), void* context,
81 const SkSurfaceProps* props)
82 : INHERITED(info, props)
reed@google.com1360c522014-01-08 21:25:26 +000083{
Mike Reed086a4272017-07-18 10:53:11 -040084 fBitmap.installPixels(info, pixels, rb, releaseProc, context);
reed9cd016e2016-01-30 10:01:06 -080085 fRowBytes = 0; // don't need to track the rowbytes
reed@google.com97af1a62012-08-28 12:19:02 +000086 fWeOwnThePixels = false; // We are "Direct"
reed@google.comc9062042012-07-30 18:06:00 +000087}
88
Matt Saretta6e976a2017-04-28 15:43:35 -040089SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, sk_sp<SkPixelRef> pr,
90 const SkSurfaceProps* props)
91 : INHERITED(pr->width(), pr->height(), props)
reed@google.com1360c522014-01-08 21:25:26 +000092{
reed9cd016e2016-01-30 10:01:06 -080093 fBitmap.setInfo(info, pr->rowBytes());
reed9cd016e2016-01-30 10:01:06 -080094 fRowBytes = pr->rowBytes(); // we track this, so that subsequent re-allocs will match
Hal Canary1b3387b2016-12-12 13:48:12 -050095 fBitmap.setPixelRef(std::move(pr), 0, 0);
reed@google.comc9062042012-07-30 18:06:00 +000096 fWeOwnThePixels = true;
reed@google.comc9062042012-07-30 18:06:00 +000097}
98
halcanary385fe4d2015-08-26 13:07:48 -070099SkCanvas* SkSurface_Raster::onNewCanvas() { return new SkCanvas(fBitmap, this->props()); }
reed@google.comc9062042012-07-30 18:06:00 +0000100
reede8f30622016-03-23 18:59:25 -0700101sk_sp<SkSurface> SkSurface_Raster::onNewSurface(const SkImageInfo& info) {
102 return SkSurface::MakeRaster(info, &this->props());
reed@google.comc9062042012-07-30 18:06:00 +0000103}
104
reed@google.comc9062042012-07-30 18:06:00 +0000105void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
106 const SkPaint* paint) {
107 canvas->drawBitmap(fBitmap, x, y, paint);
108}
109
Mike Reed114bde82018-11-21 09:12:09 -0500110sk_sp<SkImage> SkSurface_Raster::onNewImageSnapshot(const SkIRect* subset) {
111 if (subset) {
112 SkASSERT(SkIRect::MakeWH(fBitmap.width(), fBitmap.height()).contains(*subset));
113 SkBitmap dst;
114 dst.allocPixels(fBitmap.info().makeWH(subset->width(), subset->height()));
115 SkAssertResult(fBitmap.readPixels(dst.pixmap(), subset->left(), subset->top()));
116 dst.setImmutable(); // key, so MakeFromBitmap doesn't make a copy of the buffer
117 return SkImage::MakeFromBitmap(dst);
118 }
119
Mike Reed7eb01f82016-12-30 06:23:12 -0500120 SkCopyPixelsMode cpm = kIfMutable_SkCopyPixelsMode;
reed26e0e582015-07-29 11:44:52 -0700121 if (fWeOwnThePixels) {
122 // SkImage_raster requires these pixels are immutable for its full lifetime.
123 // We'll undo this via onRestoreBackingMutability() if we can avoid the COW.
124 if (SkPixelRef* pr = fBitmap.pixelRef()) {
125 pr->setTemporarilyImmutable();
126 }
bsalomonf47b9a32016-02-22 11:02:58 -0800127 } else {
reed1ec04d92016-08-05 12:07:41 -0700128 cpm = kAlways_SkCopyPixelsMode;
reed26e0e582015-07-29 11:44:52 -0700129 }
bsalomonf47b9a32016-02-22 11:02:58 -0800130
fmalita9a5d1ab2015-07-27 10:27:28 -0700131 // Our pixels are in memory, so read access on the snapshot SkImage could be cheap.
132 // Lock the shared pixel ref to ensure peekPixels() is usable.
reed1ec04d92016-08-05 12:07:41 -0700133 return SkMakeImageFromRasterBitmap(fBitmap, cpm);
reed26e0e582015-07-29 11:44:52 -0700134}
135
Mike Reed4c790bd2018-02-08 14:10:40 -0500136void SkSurface_Raster::onWritePixels(const SkPixmap& src, int x, int y) {
137 fBitmap.writePixels(src, x, y);
138}
139
reed26e0e582015-07-29 11:44:52 -0700140void SkSurface_Raster::onRestoreBackingMutability() {
141 SkASSERT(!this->hasCachedImage()); // Shouldn't be any snapshots out there.
142 if (SkPixelRef* pr = fBitmap.pixelRef()) {
143 pr->restoreMutability();
144 }
reed@google.com97af1a62012-08-28 12:19:02 +0000145}
146
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000147void SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) {
reed@google.com97af1a62012-08-28 12:19:02 +0000148 // are we sharing pixelrefs with the image?
Robert Phillipsac6b1fa2017-03-20 08:38:50 -0400149 sk_sp<SkImage> cached(this->refCachedImage());
bsalomonf47b9a32016-02-22 11:02:58 -0800150 SkASSERT(cached);
reed9ce9d672016-03-17 10:51:11 -0700151 if (SkBitmapImageGetPixelRef(cached.get()) == fBitmap.pixelRef()) {
reed@google.com97af1a62012-08-28 12:19:02 +0000152 SkASSERT(fWeOwnThePixels);
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000153 if (kDiscard_ContentChangeMode == mode) {
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000154 fBitmap.allocPixels();
155 } else {
156 SkBitmap prev(fBitmap);
reed9cd016e2016-01-30 10:01:06 -0800157 fBitmap.allocPixels();
reed9cd016e2016-01-30 10:01:06 -0800158 SkASSERT(prev.info() == fBitmap.info());
159 SkASSERT(prev.rowBytes() == fBitmap.rowBytes());
Mike Reedf0ffb892017-10-03 14:47:21 -0400160 memcpy(fBitmap.getPixels(), prev.getPixels(), fBitmap.computeByteSize());
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000161 }
reed9cd016e2016-01-30 10:01:06 -0800162 SkASSERT(fBitmap.rowBytes() == fRowBytes); // be sure we always use the same value
163
reed@google.com97af1a62012-08-28 12:19:02 +0000164 // Now fBitmap is a deep copy of itself (and therefore different from
165 // what is being used by the image. Next we update the canvas to use
166 // this as its backend, so we can't modify the image's pixels anymore.
bsalomon49f085d2014-09-05 13:34:00 -0700167 SkASSERT(this->getCachedCanvas());
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000168 this->getCachedCanvas()->getDevice()->replaceBitmapBackendForRasterSurface(fBitmap);
reed@google.com97af1a62012-08-28 12:19:02 +0000169 }
170}
171
reed@google.comc9062042012-07-30 18:06:00 +0000172///////////////////////////////////////////////////////////////////////////////
173
reede8f30622016-03-23 18:59:25 -0700174sk_sp<SkSurface> SkSurface::MakeRasterDirectReleaseProc(const SkImageInfo& info, void* pixels,
175 size_t rb, void (*releaseProc)(void* pixels, void* context), void* context,
176 const SkSurfaceProps* props) {
halcanary96fcdcc2015-08-27 07:41:13 -0700177 if (nullptr == releaseProc) {
178 context = nullptr;
reed982542d2014-06-27 06:48:14 -0700179 }
Florin Malitaf40b2492017-05-03 15:23:21 -0400180 if (!SkSurfaceValidateRasterInfo(info, rb)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700181 return nullptr;
reed@google.comc9062042012-07-30 18:06:00 +0000182 }
halcanary96fcdcc2015-08-27 07:41:13 -0700183 if (nullptr == pixels) {
184 return nullptr;
reed@google.comc9062042012-07-30 18:06:00 +0000185 }
reed26e0e582015-07-29 11:44:52 -0700186
reede8f30622016-03-23 18:59:25 -0700187 return sk_make_sp<SkSurface_Raster>(info, pixels, rb, releaseProc, context, props);
reed982542d2014-06-27 06:48:14 -0700188}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000189
reede8f30622016-03-23 18:59:25 -0700190sk_sp<SkSurface> SkSurface::MakeRasterDirect(const SkImageInfo& info, void* pixels, size_t rowBytes,
191 const SkSurfaceProps* props) {
192 return MakeRasterDirectReleaseProc(info, pixels, rowBytes, nullptr, nullptr, props);
reed@google.comc9062042012-07-30 18:06:00 +0000193}
194
reede8f30622016-03-23 18:59:25 -0700195sk_sp<SkSurface> SkSurface::MakeRaster(const SkImageInfo& info, size_t rowBytes,
196 const SkSurfaceProps* props) {
Florin Malitaf40b2492017-05-03 15:23:21 -0400197 if (!SkSurfaceValidateRasterInfo(info)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700198 return nullptr;
reed@google.comc9062042012-07-30 18:06:00 +0000199 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000200
Mike Klein930c2952019-04-12 11:50:39 -0500201 sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, rowBytes);
Hal Canary67b39de2016-11-07 11:47:44 -0500202 if (!pr) {
halcanary96fcdcc2015-08-27 07:41:13 -0700203 return nullptr;
reed@google.comc9062042012-07-30 18:06:00 +0000204 }
reed9cd016e2016-01-30 10:01:06 -0800205 if (rowBytes) {
206 SkASSERT(pr->rowBytes() == rowBytes);
207 }
Matt Saretta6e976a2017-04-28 15:43:35 -0400208 return sk_make_sp<SkSurface_Raster>(info, std::move(pr), props);
reed@google.comc9062042012-07-30 18:06:00 +0000209}
Mike Klein9707e902019-02-07 16:18:22 -0500210
211sk_sp<SkSurface> SkSurface::MakeRasterN32Premul(int width, int height,
212 const SkSurfaceProps* surfaceProps) {
213 return MakeRaster(SkImageInfo::MakeN32Premul(width, height), surfaceProps);
214}