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"
11 #include "SkDevice.h"
12 #include "SkMallocPixelRef.h"
13
14 class SkSurface_Raster : public SkSurface_Base {
15 public:
16 SkSurface_Raster(const SkImageInfo&, void*, size_t rb,
17 void (*releaseProc)(void* pixels, void* context), void* context,
18 const SkSurfaceProps*);
19 SkSurface_Raster(const SkImageInfo& info, sk_sp<SkPixelRef>, const SkSurfaceProps*);
20
21 SkCanvas* onNewCanvas() override;
22 sk_sp<SkSurface> onNewSurface(const SkImageInfo&) override;
23 sk_sp<SkImage> onNewImageSnapshot() override;
24 void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) override;
25 void onCopyOnWrite(ContentChangeMode) override;
26 void onRestoreBackingMutability() override;
27
28 private:
29 SkBitmap fBitmap;
30 size_t fRowBytes;
31 bool fWeOwnThePixels;
32
33 typedef SkSurface_Base INHERITED;
34 };
35
36 ///////////////////////////////////////////////////////////////////////////////
37
SkSurfaceValidateRasterInfo(const SkImageInfo & info,size_t rowBytes)38 bool SkSurfaceValidateRasterInfo(const SkImageInfo& info, size_t rowBytes) {
39 if (info.isEmpty()) {
40 return false;
41 }
42
43 static const size_t kMaxTotalSize = SK_MaxS32;
44
45 // TODO(mtklein,brianosman): revisit all these color space decisions
46 switch (info.colorType()) {
47 case kAlpha_8_SkColorType:
48 case kGray_8_SkColorType:
49 case kRGB_565_SkColorType:
50 case kARGB_4444_SkColorType:
51 case kRGB_888x_SkColorType:
52 case kRGBA_1010102_SkColorType:
53 case kRGB_101010x_SkColorType:
54 if (info.colorSpace()) {
55 return false;
56 }
57 break;
58
59 case kRGBA_8888_SkColorType:
60 case kBGRA_8888_SkColorType:
61 if (info.colorSpace() && !info.colorSpace()->gammaCloseToSRGB()) {
62 return false;
63 }
64 break;
65 case kRGBA_F16_SkColorType:
66 if (info.colorSpace() && (!info.colorSpace()->gammaIsLinear())) {
67 return false;
68 }
69 break;
70 default:
71 return false;
72 }
73
74 if (kIgnoreRowBytesValue == rowBytes) {
75 return true;
76 }
77
78 int shift = SkColorTypeShiftPerPixel(info.colorType());
79
80 uint64_t minRB = (uint64_t)info.width() << shift;
81 if (minRB > rowBytes) {
82 return false;
83 }
84
85 size_t alignedRowBytes = rowBytes >> shift << shift;
86 if (alignedRowBytes != rowBytes) {
87 return false;
88 }
89
90 uint64_t size = sk_64_mul(info.height(), rowBytes);
91 if (size > kMaxTotalSize) {
92 return false;
93 }
94
95 return true;
96 }
97
SkSurface_Raster(const SkImageInfo & info,void * pixels,size_t rb,void (* releaseProc)(void * pixels,void * context),void * context,const SkSurfaceProps * props)98 SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, void* pixels, size_t rb,
99 void (*releaseProc)(void* pixels, void* context), void* context,
100 const SkSurfaceProps* props)
101 : INHERITED(info, props)
102 {
103 fBitmap.installPixels(info, pixels, rb, releaseProc, context);
104 fRowBytes = 0; // don't need to track the rowbytes
105 fWeOwnThePixels = false; // We are "Direct"
106 }
107
SkSurface_Raster(const SkImageInfo & info,sk_sp<SkPixelRef> pr,const SkSurfaceProps * props)108 SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, sk_sp<SkPixelRef> pr,
109 const SkSurfaceProps* props)
110 : INHERITED(pr->width(), pr->height(), props)
111 {
112 fBitmap.setInfo(info, pr->rowBytes());
113 fRowBytes = pr->rowBytes(); // we track this, so that subsequent re-allocs will match
114 fBitmap.setPixelRef(std::move(pr), 0, 0);
115 fWeOwnThePixels = true;
116 }
117
onNewCanvas()118 SkCanvas* SkSurface_Raster::onNewCanvas() { return new SkCanvas(fBitmap, this->props()); }
119
onNewSurface(const SkImageInfo & info)120 sk_sp<SkSurface> SkSurface_Raster::onNewSurface(const SkImageInfo& info) {
121 return SkSurface::MakeRaster(info, &this->props());
122 }
123
onDraw(SkCanvas * canvas,SkScalar x,SkScalar y,const SkPaint * paint)124 void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
125 const SkPaint* paint) {
126 canvas->drawBitmap(fBitmap, x, y, paint);
127 }
128
onNewImageSnapshot()129 sk_sp<SkImage> SkSurface_Raster::onNewImageSnapshot() {
130 SkCopyPixelsMode cpm = kIfMutable_SkCopyPixelsMode;
131 if (fWeOwnThePixels) {
132 // SkImage_raster requires these pixels are immutable for its full lifetime.
133 // We'll undo this via onRestoreBackingMutability() if we can avoid the COW.
134 if (SkPixelRef* pr = fBitmap.pixelRef()) {
135 pr->setTemporarilyImmutable();
136 }
137 } else {
138 cpm = kAlways_SkCopyPixelsMode;
139 }
140
141 // Our pixels are in memory, so read access on the snapshot SkImage could be cheap.
142 // Lock the shared pixel ref to ensure peekPixels() is usable.
143 return SkMakeImageFromRasterBitmap(fBitmap, cpm);
144 }
145
onRestoreBackingMutability()146 void SkSurface_Raster::onRestoreBackingMutability() {
147 SkASSERT(!this->hasCachedImage()); // Shouldn't be any snapshots out there.
148 if (SkPixelRef* pr = fBitmap.pixelRef()) {
149 pr->restoreMutability();
150 }
151 }
152
onCopyOnWrite(ContentChangeMode mode)153 void SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) {
154 // are we sharing pixelrefs with the image?
155 sk_sp<SkImage> cached(this->refCachedImage());
156 SkASSERT(cached);
157 if (SkBitmapImageGetPixelRef(cached.get()) == fBitmap.pixelRef()) {
158 SkASSERT(fWeOwnThePixels);
159 if (kDiscard_ContentChangeMode == mode) {
160 fBitmap.allocPixels();
161 } else {
162 SkBitmap prev(fBitmap);
163 fBitmap.allocPixels();
164 SkASSERT(prev.info() == fBitmap.info());
165 SkASSERT(prev.rowBytes() == fBitmap.rowBytes());
166 memcpy(fBitmap.getPixels(), prev.getPixels(), fBitmap.computeByteSize());
167 }
168 SkASSERT(fBitmap.rowBytes() == fRowBytes); // be sure we always use the same value
169
170 // Now fBitmap is a deep copy of itself (and therefore different from
171 // what is being used by the image. Next we update the canvas to use
172 // this as its backend, so we can't modify the image's pixels anymore.
173 SkASSERT(this->getCachedCanvas());
174 this->getCachedCanvas()->getDevice()->replaceBitmapBackendForRasterSurface(fBitmap);
175 }
176 }
177
178 ///////////////////////////////////////////////////////////////////////////////
179
MakeRasterDirectReleaseProc(const SkImageInfo & info,void * pixels,size_t rb,void (* releaseProc)(void * pixels,void * context),void * context,const SkSurfaceProps * props)180 sk_sp<SkSurface> SkSurface::MakeRasterDirectReleaseProc(const SkImageInfo& info, void* pixels,
181 size_t rb, void (*releaseProc)(void* pixels, void* context), void* context,
182 const SkSurfaceProps* props) {
183 if (nullptr == releaseProc) {
184 context = nullptr;
185 }
186 if (!SkSurfaceValidateRasterInfo(info, rb)) {
187 return nullptr;
188 }
189 if (nullptr == pixels) {
190 return nullptr;
191 }
192
193 return sk_make_sp<SkSurface_Raster>(info, pixels, rb, releaseProc, context, props);
194 }
195
MakeRasterDirect(const SkImageInfo & info,void * pixels,size_t rowBytes,const SkSurfaceProps * props)196 sk_sp<SkSurface> SkSurface::MakeRasterDirect(const SkImageInfo& info, void* pixels, size_t rowBytes,
197 const SkSurfaceProps* props) {
198 return MakeRasterDirectReleaseProc(info, pixels, rowBytes, nullptr, nullptr, props);
199 }
200
MakeRaster(const SkImageInfo & info,size_t rowBytes,const SkSurfaceProps * props)201 sk_sp<SkSurface> SkSurface::MakeRaster(const SkImageInfo& info, size_t rowBytes,
202 const SkSurfaceProps* props) {
203 if (!SkSurfaceValidateRasterInfo(info)) {
204 return nullptr;
205 }
206
207 sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeZeroed(info, rowBytes);
208 if (!pr) {
209 return nullptr;
210 }
211 if (rowBytes) {
212 SkASSERT(pr->rowBytes() == rowBytes);
213 }
214 return sk_make_sp<SkSurface_Raster>(info, std::move(pr), props);
215 }
216