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 "src/image/SkSurface_Raster.h"
9
10 #include "include/core/SkBitmap.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkCapabilities.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkMallocPixelRef.h"
15 #include "include/core/SkPixelRef.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkSurface.h"
20 #include "include/private/base/SkAssert.h"
21 #include "include/private/base/SkMath.h"
22 #include "src/core/SkBitmapDevice.h"
23 #include "src/core/SkImageInfoPriv.h"
24 #include "src/core/SkImagePriv.h"
25 #include "src/core/SkSurfacePriv.h"
26
27 #include <cstdint>
28 #include <cstring>
29 #include <utility>
30
31 class SkImage;
32 class SkPaint;
33 class SkPixmap;
34 class SkSurfaceProps;
35
SkSurfaceValidateRasterInfo(const SkImageInfo & info,size_t rowBytes)36 bool SkSurfaceValidateRasterInfo(const SkImageInfo& info, size_t rowBytes) {
37 if (!SkImageInfoIsValid(info)) {
38 return false;
39 }
40
41 if (kIgnoreRowBytesValue == rowBytes) {
42 return true;
43 }
44
45 if (!info.validRowBytes(rowBytes)) {
46 return false;
47 }
48
49 uint64_t size = sk_64_mul(info.height(), rowBytes);
50 static const size_t kMaxTotalSize = SK_MaxS32;
51 if (size > kMaxTotalSize) {
52 return false;
53 }
54
55 return true;
56 }
57
SkSurface_Raster(const SkImageInfo & info,void * pixels,size_t rb,void (* releaseProc)(void * pixels,void * context),void * context,const SkSurfaceProps * props)58 SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, void* pixels, size_t rb,
59 void (*releaseProc)(void* pixels, void* context), void* context,
60 const SkSurfaceProps* props)
61 : INHERITED(info, props)
62 {
63 fBitmap.installPixels(info, pixels, rb, releaseProc, context);
64 fWeOwnThePixels = false; // We are "Direct"
65 }
66
SkSurface_Raster(const SkImageInfo & info,sk_sp<SkPixelRef> pr,const SkSurfaceProps * props)67 SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, sk_sp<SkPixelRef> pr,
68 const SkSurfaceProps* props)
69 : INHERITED(pr->width(), pr->height(), props)
70 {
71 fBitmap.setInfo(info, pr->rowBytes());
72 fBitmap.setPixelRef(std::move(pr), 0, 0);
73 fWeOwnThePixels = true;
74 }
75
onNewCanvas()76 SkCanvas* SkSurface_Raster::onNewCanvas() { return new SkCanvas(fBitmap, this->props()); }
77
onNewSurface(const SkImageInfo & info)78 sk_sp<SkSurface> SkSurface_Raster::onNewSurface(const SkImageInfo& info) {
79 return SkSurfaces::Raster(info, &this->props());
80 }
81
onDraw(SkCanvas * canvas,SkScalar x,SkScalar y,const SkSamplingOptions & sampling,const SkPaint * paint)82 void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
83 const SkSamplingOptions& sampling, const SkPaint* paint) {
84 canvas->drawImage(fBitmap.asImage().get(), x, y, sampling, paint);
85 }
86
onNewImageSnapshot(const SkIRect * subset)87 sk_sp<SkImage> SkSurface_Raster::onNewImageSnapshot(const SkIRect* subset) {
88 if (subset) {
89 SkASSERT(SkIRect::MakeWH(fBitmap.width(), fBitmap.height()).contains(*subset));
90 SkBitmap dst;
91 dst.allocPixels(fBitmap.info().makeDimensions(subset->size()));
92 SkAssertResult(fBitmap.readPixels(dst.pixmap(), subset->left(), subset->top()));
93 dst.setImmutable(); // key, so MakeFromBitmap doesn't make a copy of the buffer
94 return dst.asImage();
95 }
96
97 SkCopyPixelsMode cpm = kIfMutable_SkCopyPixelsMode;
98 if (fWeOwnThePixels) {
99 // SkImage_raster requires these pixels are immutable for its full lifetime.
100 // We'll undo this via onRestoreBackingMutability() if we can avoid the COW.
101 if (SkPixelRef* pr = fBitmap.pixelRef()) {
102 pr->setTemporarilyImmutable();
103 }
104 } else {
105 cpm = kAlways_SkCopyPixelsMode;
106 }
107
108 // Our pixels are in memory, so read access on the snapshot SkImage could be cheap.
109 // Lock the shared pixel ref to ensure peekPixels() is usable.
110 return SkMakeImageFromRasterBitmap(fBitmap, cpm);
111 }
112
onWritePixels(const SkPixmap & src,int x,int y)113 void SkSurface_Raster::onWritePixels(const SkPixmap& src, int x, int y) {
114 fBitmap.writePixels(src, x, y);
115 }
116
onRestoreBackingMutability()117 void SkSurface_Raster::onRestoreBackingMutability() {
118 SkASSERT(!this->hasCachedImage()); // Shouldn't be any snapshots out there.
119 if (SkPixelRef* pr = fBitmap.pixelRef()) {
120 pr->restoreMutability();
121 }
122 }
123
onCopyOnWrite(ContentChangeMode mode)124 bool SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) {
125 // are we sharing pixelrefs with the image?
126 sk_sp<SkImage> cached(this->refCachedImage());
127 SkASSERT(cached);
128 if (SkBitmapImageGetPixelRef(cached.get()) == fBitmap.pixelRef()) {
129 SkASSERT(fWeOwnThePixels);
130 if (kDiscard_ContentChangeMode == mode) {
131 if (!fBitmap.tryAllocPixels()) {
132 return false;
133 }
134 } else {
135 SkBitmap prev(fBitmap);
136 if (!fBitmap.tryAllocPixels()) {
137 return false;
138 }
139 SkASSERT(prev.info() == fBitmap.info());
140 SkASSERT(prev.rowBytes() == fBitmap.rowBytes());
141 memcpy(fBitmap.getPixels(), prev.getPixels(), fBitmap.computeByteSize());
142 }
143
144 // Now fBitmap is a deep copy of itself (and therefore different from
145 // what is being used by the image. Next we update the canvas to use
146 // this as its backend, so we can't modify the image's pixels anymore.
147 SkASSERT(this->getCachedCanvas());
148 SkBitmapDevice* bmDev = static_cast<SkBitmapDevice*>(this->getCachedCanvas()->rootDevice());
149 bmDev->replaceBitmapBackendForRasterSurface(fBitmap);
150 }
151 return true;
152 }
153
onCapabilities()154 sk_sp<const SkCapabilities> SkSurface_Raster::onCapabilities() {
155 return SkCapabilities::RasterBackend();
156 }
157
158 ///////////////////////////////////////////////////////////////////////////////
159 namespace SkSurfaces {
WrapPixels(const SkImageInfo & info,void * pixels,size_t rowBytes,PixelsReleaseProc releaseProc,void * context,const SkSurfaceProps * props)160 sk_sp<SkSurface> WrapPixels(const SkImageInfo& info,
161 void* pixels,
162 size_t rowBytes,
163 PixelsReleaseProc releaseProc,
164 void* context,
165 const SkSurfaceProps* props) {
166 if (nullptr == releaseProc) {
167 context = nullptr;
168 }
169 if (!SkSurfaceValidateRasterInfo(info, rowBytes)) {
170 return nullptr;
171 }
172 if (nullptr == pixels) {
173 return nullptr;
174 }
175
176 return sk_make_sp<SkSurface_Raster>(info, pixels, rowBytes, releaseProc, context, props);
177 }
178
WrapPixels(const SkImageInfo & info,void * pixels,size_t rowBytes,const SkSurfaceProps * props)179 sk_sp<SkSurface> WrapPixels(const SkImageInfo& info,
180 void* pixels,
181 size_t rowBytes,
182 const SkSurfaceProps* props) {
183 return WrapPixels(info, pixels, rowBytes, nullptr, nullptr, props);
184 }
185
Raster(const SkImageInfo & info,size_t rowBytes,const SkSurfaceProps * props)186 sk_sp<SkSurface> Raster(const SkImageInfo& info, size_t rowBytes, const SkSurfaceProps* props) {
187 if (!SkSurfaceValidateRasterInfo(info)) {
188 return nullptr;
189 }
190
191 sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, rowBytes);
192 if (!pr) {
193 return nullptr;
194 }
195 if (rowBytes) {
196 SkASSERT(pr->rowBytes() == rowBytes);
197 }
198 return sk_make_sp<SkSurface_Raster>(info, std::move(pr), props);
199 }
200
201 } // namespace SkSurfaces
202