blob: 47e0dbed335a5b486abd39649e2cf2744568e637 [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
8#include "SkSurface_Base.h"
9#include "SkImagePriv.h"
10#include "SkCanvas.h"
reed@google.com97af1a62012-08-28 12:19:02 +000011#include "SkDevice.h"
reed@google.comc9062042012-07-30 18:06:00 +000012#include "SkMallocPixelRef.h"
13
14static const size_t kIgnoreRowBytesValue = (size_t)~0;
15
16class SkSurface_Raster : public SkSurface_Base {
17public:
reed@google.com2bd8b812013-11-01 13:46:54 +000018 static bool Valid(const SkImageInfo&, size_t rb = kIgnoreRowBytesValue);
reed@google.comc9062042012-07-30 18:06:00 +000019
reed982542d2014-06-27 06:48:14 -070020 SkSurface_Raster(const SkImageInfo&, void*, size_t rb,
reed4a8126e2014-09-22 07:29:03 -070021 void (*releaseProc)(void* pixels, void* context), void* context,
22 const SkSurfaceProps*);
Hal Canary1b3387b2016-12-12 13:48:12 -050023 SkSurface_Raster(sk_sp<SkPixelRef>, const SkSurfaceProps*);
reed@google.comc9062042012-07-30 18:06:00 +000024
mtklein36352bf2015-03-25 18:17:31 -070025 SkCanvas* onNewCanvas() override;
reede8f30622016-03-23 18:59:25 -070026 sk_sp<SkSurface> onNewSurface(const SkImageInfo&) override;
Mike Reed7eb01f82016-12-30 06:23:12 -050027 sk_sp<SkImage> onNewImageSnapshot(SkBudgeted) override;
tfarina7831a4b2015-04-27 07:53:07 -070028 void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) override;
mtklein36352bf2015-03-25 18:17:31 -070029 void onCopyOnWrite(ContentChangeMode) override;
reed26e0e582015-07-29 11:44:52 -070030 void onRestoreBackingMutability() override;
reed@google.comc9062042012-07-30 18:06:00 +000031
32private:
33 SkBitmap fBitmap;
reed9cd016e2016-01-30 10:01:06 -080034 size_t fRowBytes;
reed@google.comc9062042012-07-30 18:06:00 +000035 bool fWeOwnThePixels;
36
37 typedef SkSurface_Base INHERITED;
38};
39
40///////////////////////////////////////////////////////////////////////////////
41
reed@google.com2bd8b812013-11-01 13:46:54 +000042bool SkSurface_Raster::Valid(const SkImageInfo& info, size_t rowBytes) {
reedb2497c22014-12-31 12:31:43 -080043 if (info.isEmpty()) {
44 return false;
45 }
46
reed@google.comc9062042012-07-30 18:06:00 +000047 static const size_t kMaxTotalSize = SK_MaxS32;
48
reed@google.comc9062042012-07-30 18:06:00 +000049 int shift = 0;
reede5ea5002014-09-03 11:54:58 -070050 switch (info.colorType()) {
reed@google.com1360c522014-01-08 21:25:26 +000051 case kAlpha_8_SkColorType:
brianosman0e22eb82016-08-30 07:07:59 -070052 if (info.colorSpace()) {
53 return false;
54 }
reed@google.comc9062042012-07-30 18:06:00 +000055 shift = 0;
56 break;
reed@google.com1360c522014-01-08 21:25:26 +000057 case kRGB_565_SkColorType:
brianosman0e22eb82016-08-30 07:07:59 -070058 if (info.colorSpace()) {
59 return false;
60 }
reed@google.comc9062042012-07-30 18:06:00 +000061 shift = 1;
62 break;
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000063 case kN32_SkColorType:
brianosman0e22eb82016-08-30 07:07:59 -070064 if (info.colorSpace() && !info.colorSpace()->gammaCloseToSRGB()) {
65 return false;
66 }
reed@google.comc9062042012-07-30 18:06:00 +000067 shift = 2;
68 break;
reeda34be682016-02-15 07:48:35 -080069 case kRGBA_F16_SkColorType:
Brian Osmaneb21ef62016-11-01 16:30:21 -040070 if (info.colorSpace() && !info.colorSpace()->gammaIsLinear()) {
brianosman0e22eb82016-08-30 07:07:59 -070071 return false;
72 }
reeda34be682016-02-15 07:48:35 -080073 shift = 3;
74 break;
reed@google.comc9062042012-07-30 18:06:00 +000075 default:
76 return false;
77 }
78
reed@google.comc9062042012-07-30 18:06:00 +000079 if (kIgnoreRowBytesValue == rowBytes) {
80 return true;
81 }
82
reede5ea5002014-09-03 11:54:58 -070083 uint64_t minRB = (uint64_t)info.width() << shift;
reed@google.comc9062042012-07-30 18:06:00 +000084 if (minRB > rowBytes) {
85 return false;
86 }
87
88 size_t alignedRowBytes = rowBytes >> shift << shift;
89 if (alignedRowBytes != rowBytes) {
90 return false;
91 }
92
reede5ea5002014-09-03 11:54:58 -070093 uint64_t size = sk_64_mul(info.height(), rowBytes);
reed@google.comc9062042012-07-30 18:06:00 +000094 if (size > kMaxTotalSize) {
95 return false;
96 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000097
reed@google.comc9062042012-07-30 18:06:00 +000098 return true;
99}
100
reed982542d2014-06-27 06:48:14 -0700101SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, void* pixels, size_t rb,
reed4a8126e2014-09-22 07:29:03 -0700102 void (*releaseProc)(void* pixels, void* context), void* context,
103 const SkSurfaceProps* props)
104 : INHERITED(info, props)
reed@google.com1360c522014-01-08 21:25:26 +0000105{
halcanary96fcdcc2015-08-27 07:41:13 -0700106 fBitmap.installPixels(info, pixels, rb, nullptr, releaseProc, context);
reed9cd016e2016-01-30 10:01:06 -0800107 fRowBytes = 0; // don't need to track the rowbytes
reed@google.com97af1a62012-08-28 12:19:02 +0000108 fWeOwnThePixels = false; // We are "Direct"
reed@google.comc9062042012-07-30 18:06:00 +0000109}
110
Hal Canary1b3387b2016-12-12 13:48:12 -0500111SkSurface_Raster::SkSurface_Raster(sk_sp<SkPixelRef> pr, const SkSurfaceProps* props)
reed4a8126e2014-09-22 07:29:03 -0700112 : INHERITED(pr->info().width(), pr->info().height(), props)
reed@google.com1360c522014-01-08 21:25:26 +0000113{
commit-bot@chromium.orge13af712014-01-13 20:39:14 +0000114 const SkImageInfo& info = pr->info();
115
reed9cd016e2016-01-30 10:01:06 -0800116 fBitmap.setInfo(info, pr->rowBytes());
reed9cd016e2016-01-30 10:01:06 -0800117 fRowBytes = pr->rowBytes(); // we track this, so that subsequent re-allocs will match
Hal Canary1b3387b2016-12-12 13:48:12 -0500118 fBitmap.setPixelRef(std::move(pr), 0, 0);
reed@google.comc9062042012-07-30 18:06:00 +0000119 fWeOwnThePixels = true;
reed@google.comc9062042012-07-30 18:06:00 +0000120}
121
halcanary385fe4d2015-08-26 13:07:48 -0700122SkCanvas* SkSurface_Raster::onNewCanvas() { return new SkCanvas(fBitmap, this->props()); }
reed@google.comc9062042012-07-30 18:06:00 +0000123
reede8f30622016-03-23 18:59:25 -0700124sk_sp<SkSurface> SkSurface_Raster::onNewSurface(const SkImageInfo& info) {
125 return SkSurface::MakeRaster(info, &this->props());
reed@google.comc9062042012-07-30 18:06:00 +0000126}
127
reed@google.comc9062042012-07-30 18:06:00 +0000128void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
129 const SkPaint* paint) {
130 canvas->drawBitmap(fBitmap, x, y, paint);
131}
132
Mike Reed7eb01f82016-12-30 06:23:12 -0500133sk_sp<SkImage> SkSurface_Raster::onNewImageSnapshot(SkBudgeted) {
134 SkCopyPixelsMode cpm = kIfMutable_SkCopyPixelsMode;
reed26e0e582015-07-29 11:44:52 -0700135 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 }
bsalomonf47b9a32016-02-22 11:02:58 -0800141 } else {
reed1ec04d92016-08-05 12:07:41 -0700142 cpm = kAlways_SkCopyPixelsMode;
reed26e0e582015-07-29 11:44:52 -0700143 }
bsalomonf47b9a32016-02-22 11:02:58 -0800144
fmalita9a5d1ab2015-07-27 10:27:28 -0700145 // 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.
reed1ec04d92016-08-05 12:07:41 -0700147 return SkMakeImageFromRasterBitmap(fBitmap, cpm);
reed26e0e582015-07-29 11:44:52 -0700148}
149
150void 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.com97af1a62012-08-28 12:19:02 +0000155}
156
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000157void SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) {
reed@google.com97af1a62012-08-28 12:19:02 +0000158 // are we sharing pixelrefs with the image?
Mike Reed85ff8482016-12-29 09:36:20 -0500159 sk_sp<SkImage> cached(this->refCachedImage(SkBudgeted::kNo));
bsalomonf47b9a32016-02-22 11:02:58 -0800160 SkASSERT(cached);
reed9ce9d672016-03-17 10:51:11 -0700161 if (SkBitmapImageGetPixelRef(cached.get()) == fBitmap.pixelRef()) {
reed@google.com97af1a62012-08-28 12:19:02 +0000162 SkASSERT(fWeOwnThePixels);
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000163 if (kDiscard_ContentChangeMode == mode) {
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000164 fBitmap.allocPixels();
165 } else {
166 SkBitmap prev(fBitmap);
reed9cd016e2016-01-30 10:01:06 -0800167 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.orgc4c98702013-04-22 14:28:01 +0000172 }
reed9cd016e2016-01-30 10:01:06 -0800173 SkASSERT(fBitmap.rowBytes() == fRowBytes); // be sure we always use the same value
174
reed@google.com97af1a62012-08-28 12:19:02 +0000175 // 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.
bsalomon49f085d2014-09-05 13:34:00 -0700178 SkASSERT(this->getCachedCanvas());
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000179 this->getCachedCanvas()->getDevice()->replaceBitmapBackendForRasterSurface(fBitmap);
reed@google.com97af1a62012-08-28 12:19:02 +0000180 }
181}
182
reed@google.comc9062042012-07-30 18:06:00 +0000183///////////////////////////////////////////////////////////////////////////////
184
reede8f30622016-03-23 18:59:25 -0700185sk_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) {
halcanary96fcdcc2015-08-27 07:41:13 -0700188 if (nullptr == releaseProc) {
189 context = nullptr;
reed982542d2014-06-27 06:48:14 -0700190 }
191 if (!SkSurface_Raster::Valid(info, rb)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700192 return nullptr;
reed@google.comc9062042012-07-30 18:06:00 +0000193 }
halcanary96fcdcc2015-08-27 07:41:13 -0700194 if (nullptr == pixels) {
195 return nullptr;
reed@google.comc9062042012-07-30 18:06:00 +0000196 }
reed26e0e582015-07-29 11:44:52 -0700197
reede8f30622016-03-23 18:59:25 -0700198 return sk_make_sp<SkSurface_Raster>(info, pixels, rb, releaseProc, context, props);
reed982542d2014-06-27 06:48:14 -0700199}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000200
reede8f30622016-03-23 18:59:25 -0700201sk_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.comc9062042012-07-30 18:06:00 +0000204}
205
reede8f30622016-03-23 18:59:25 -0700206sk_sp<SkSurface> SkSurface::MakeRaster(const SkImageInfo& info, size_t rowBytes,
207 const SkSurfaceProps* props) {
mike@reedtribe.orgb9476252012-11-15 02:37:45 +0000208 if (!SkSurface_Raster::Valid(info)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700209 return nullptr;
reed@google.comc9062042012-07-30 18:06:00 +0000210 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000211
Hal Canary67b39de2016-11-07 11:47:44 -0500212 sk_sp<SkPixelRef> pr(SkMallocPixelRef::NewZeroed(info, rowBytes, nullptr));
213 if (!pr) {
halcanary96fcdcc2015-08-27 07:41:13 -0700214 return nullptr;
reed@google.comc9062042012-07-30 18:06:00 +0000215 }
reed9cd016e2016-01-30 10:01:06 -0800216 if (rowBytes) {
217 SkASSERT(pr->rowBytes() == rowBytes);
218 }
Hal Canary1b3387b2016-12-12 13:48:12 -0500219 return sk_make_sp<SkSurface_Raster>(std::move(pr), props);
reed@google.comc9062042012-07-30 18:06:00 +0000220}