• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <atomic>
9 #include <cmath>
10 #include "include/core/SkCanvas.h"
11 #include "include/gpu/GrBackendSurface.h"
12 #include "src/core/SkAutoPixmapStorage.h"
13 #include "src/core/SkImagePriv.h"
14 #include "src/core/SkPaintPriv.h"
15 #include "src/image/SkImage_Base.h"
16 #include "src/image/SkRescaleAndReadPixels.h"
17 #include "src/image/SkSurface_Base.h"
18 
SkSurfaceProps()19 SkSurfaceProps::SkSurfaceProps() : fFlags(0), fPixelGeometry(kUnknown_SkPixelGeometry) {}
20 
SkSurfaceProps(uint32_t flags,SkPixelGeometry pg)21 SkSurfaceProps::SkSurfaceProps(uint32_t flags, SkPixelGeometry pg)
22     : fFlags(flags), fPixelGeometry(pg)
23 {}
24 
25 SkSurfaceProps::SkSurfaceProps(const SkSurfaceProps&) = default;
26 SkSurfaceProps& SkSurfaceProps::operator=(const SkSurfaceProps&) = default;
27 
28 ///////////////////////////////////////////////////////////////////////////////
29 
SkSurface_Base(int width,int height,const SkSurfaceProps * props)30 SkSurface_Base::SkSurface_Base(int width, int height, const SkSurfaceProps* props)
31     : INHERITED(width, height, props) {
32 }
33 
SkSurface_Base(const SkImageInfo & info,const SkSurfaceProps * props)34 SkSurface_Base::SkSurface_Base(const SkImageInfo& info, const SkSurfaceProps* props)
35     : INHERITED(info, props) {
36 }
37 
~SkSurface_Base()38 SkSurface_Base::~SkSurface_Base() {
39     // in case the canvas outsurvives us, we null the callback
40     if (fCachedCanvas) {
41         fCachedCanvas->setSurfaceBase(nullptr);
42     }
43 #if SK_SUPPORT_GPU
44     if (fCachedImage) {
45         as_IB(fCachedImage.get())->generatingSurfaceIsDeleted();
46     }
47 #endif
48 }
49 
onGetRecordingContext()50 GrRecordingContext* SkSurface_Base::onGetRecordingContext() {
51     return nullptr;
52 }
53 
onGetBackendTexture(BackendHandleAccess)54 GrBackendTexture SkSurface_Base::onGetBackendTexture(BackendHandleAccess) {
55     return GrBackendTexture(); // invalid
56 }
57 
onGetBackendRenderTarget(BackendHandleAccess)58 GrBackendRenderTarget SkSurface_Base::onGetBackendRenderTarget(BackendHandleAccess) {
59     return GrBackendRenderTarget(); // invalid
60 }
61 
onReplaceBackendTexture(const GrBackendTexture &,GrSurfaceOrigin,ContentChangeMode,TextureReleaseProc,ReleaseContext)62 bool SkSurface_Base::onReplaceBackendTexture(const GrBackendTexture&,
63                                              GrSurfaceOrigin, ContentChangeMode,
64                                              TextureReleaseProc,
65                                              ReleaseContext) {
66     return false;
67 }
68 
onDraw(SkCanvas * canvas,SkScalar x,SkScalar y,const SkSamplingOptions & sampling,const SkPaint * paint)69 void SkSurface_Base::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
70                             const SkSamplingOptions& sampling, const SkPaint* paint) {
71     auto image = this->makeImageSnapshot();
72     if (image) {
73         canvas->drawImage(image.get(), x, y, sampling, paint);
74     }
75 }
76 
onAsyncRescaleAndReadPixels(const SkImageInfo & info,const SkIRect & origSrcRect,SkSurface::RescaleGamma rescaleGamma,RescaleMode rescaleMode,SkSurface::ReadPixelsCallback callback,SkSurface::ReadPixelsContext context)77 void SkSurface_Base::onAsyncRescaleAndReadPixels(const SkImageInfo& info,
78                                                  const SkIRect& origSrcRect,
79                                                  SkSurface::RescaleGamma rescaleGamma,
80                                                  RescaleMode rescaleMode,
81                                                  SkSurface::ReadPixelsCallback callback,
82                                                  SkSurface::ReadPixelsContext context) {
83     SkBitmap src;
84     SkPixmap peek;
85     SkIRect srcRect;
86     if (this->peekPixels(&peek)) {
87         src.installPixels(peek);
88         srcRect = origSrcRect;
89     } else {
90         src.setInfo(this->imageInfo().makeDimensions(origSrcRect.size()));
91         src.allocPixels();
92         if (!this->readPixels(src, origSrcRect.x(), origSrcRect.y())) {
93             callback(context, nullptr);
94             return;
95         }
96         srcRect = SkIRect::MakeSize(src.dimensions());
97     }
98     return SkRescaleAndReadPixels(src, info, srcRect, rescaleGamma, rescaleMode, callback,
99                                   context);
100 }
101 
onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,sk_sp<SkColorSpace> dstColorSpace,const SkIRect & srcRect,const SkISize & dstSize,RescaleGamma rescaleGamma,RescaleMode,ReadPixelsCallback callback,ReadPixelsContext context)102 void SkSurface_Base::onAsyncRescaleAndReadPixelsYUV420(
103         SkYUVColorSpace yuvColorSpace, sk_sp<SkColorSpace> dstColorSpace, const SkIRect& srcRect,
104         const SkISize& dstSize, RescaleGamma rescaleGamma, RescaleMode,
105         ReadPixelsCallback callback, ReadPixelsContext context) {
106     // TODO: Call non-YUV asyncRescaleAndReadPixels and then make our callback convert to YUV and
107     // call client's callback.
108     callback(context, nullptr);
109 }
110 
outstandingImageSnapshot() const111 bool SkSurface_Base::outstandingImageSnapshot() const {
112     return fCachedImage && !fCachedImage->unique();
113 }
114 
aboutToDraw(ContentChangeMode mode)115 void SkSurface_Base::aboutToDraw(ContentChangeMode mode) {
116     this->dirtyGenerationID();
117 
118     SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);
119 
120     if (fCachedImage) {
121         // the surface may need to fork its backend, if its sharing it with
122         // the cached image. Note: we only call if there is an outstanding owner
123         // on the image (besides us).
124         bool unique = fCachedImage->unique();
125         if (!unique) {
126             this->onCopyOnWrite(mode);
127         }
128 
129         // regardless of copy-on-write, we must drop our cached image now, so
130         // that the next request will get our new contents.
131         fCachedImage.reset();
132 
133         if (unique) {
134             // Our content isn't held by any image now, so we can consider that content mutable.
135             // Raster surfaces need to be told it's safe to consider its pixels mutable again.
136             // We make this call after the ->unref() so the subclass can assert there are no images.
137             this->onRestoreBackingMutability();
138         }
139     } else if (kDiscard_ContentChangeMode == mode) {
140         this->onDiscard();
141     }
142 }
143 
newGenerationID()144 uint32_t SkSurface_Base::newGenerationID() {
145     SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);
146     static std::atomic<uint32_t> nextID{1};
147     return nextID.fetch_add(1, std::memory_order_relaxed);
148 }
149 
asSB(SkSurface * surface)150 static SkSurface_Base* asSB(SkSurface* surface) {
151     return static_cast<SkSurface_Base*>(surface);
152 }
153 
asConstSB(const SkSurface * surface)154 static const SkSurface_Base* asConstSB(const SkSurface* surface) {
155     return static_cast<const SkSurface_Base*>(surface);
156 }
157 
158 ///////////////////////////////////////////////////////////////////////////////
159 
SkSurface(int width,int height,const SkSurfaceProps * props)160 SkSurface::SkSurface(int width, int height, const SkSurfaceProps* props)
161     : fProps(SkSurfacePropsCopyOrDefault(props)), fWidth(width), fHeight(height)
162 {
163     SkASSERT(fWidth > 0);
164     SkASSERT(fHeight > 0);
165     fGenerationID = 0;
166 }
167 
SkSurface(const SkImageInfo & info,const SkSurfaceProps * props)168 SkSurface::SkSurface(const SkImageInfo& info, const SkSurfaceProps* props)
169     : fProps(SkSurfacePropsCopyOrDefault(props)), fWidth(info.width()), fHeight(info.height())
170 {
171     SkASSERT(fWidth > 0);
172     SkASSERT(fHeight > 0);
173     fGenerationID = 0;
174 }
175 
imageInfo()176 SkImageInfo SkSurface::imageInfo() {
177     // TODO: do we need to go through canvas for this?
178     return this->getCanvas()->imageInfo();
179 }
180 
generationID()181 uint32_t SkSurface::generationID() {
182     if (0 == fGenerationID) {
183         fGenerationID = asSB(this)->newGenerationID();
184     }
185     return fGenerationID;
186 }
187 
notifyContentWillChange(ContentChangeMode mode)188 void SkSurface::notifyContentWillChange(ContentChangeMode mode) {
189     asSB(this)->aboutToDraw(mode);
190 }
191 
getCanvas()192 SkCanvas* SkSurface::getCanvas() {
193     return asSB(this)->getCachedCanvas();
194 }
195 
makeImageSnapshot()196 sk_sp<SkImage> SkSurface::makeImageSnapshot() {
197     return asSB(this)->refCachedImage();
198 }
199 
makeImageSnapshot(const SkIRect & srcBounds)200 sk_sp<SkImage> SkSurface::makeImageSnapshot(const SkIRect& srcBounds) {
201     const SkIRect surfBounds = { 0, 0, fWidth, fHeight };
202     SkIRect bounds = srcBounds;
203     if (!bounds.intersect(surfBounds)) {
204         return nullptr;
205     }
206     SkASSERT(!bounds.isEmpty());
207     if (bounds == surfBounds) {
208         return this->makeImageSnapshot();
209     } else {
210         return asSB(this)->onNewImageSnapshot(&bounds);
211     }
212 }
213 
makeSurface(const SkImageInfo & info)214 sk_sp<SkSurface> SkSurface::makeSurface(const SkImageInfo& info) {
215     return asSB(this)->onNewSurface(info);
216 }
217 
makeSurface(int width,int height)218 sk_sp<SkSurface> SkSurface::makeSurface(int width, int height) {
219     return this->makeSurface(this->imageInfo().makeWH(width, height));
220 }
221 
draw(SkCanvas * canvas,SkScalar x,SkScalar y,const SkSamplingOptions & sampling,const SkPaint * paint)222 void SkSurface::draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkSamplingOptions& sampling,
223                      const SkPaint* paint) {
224     asSB(this)->onDraw(canvas, x, y, sampling, paint);
225 }
226 
peekPixels(SkPixmap * pmap)227 bool SkSurface::peekPixels(SkPixmap* pmap) {
228     return this->getCanvas()->peekPixels(pmap);
229 }
230 
readPixels(const SkPixmap & pm,int srcX,int srcY)231 bool SkSurface::readPixels(const SkPixmap& pm, int srcX, int srcY) {
232     return this->getCanvas()->readPixels(pm, srcX, srcY);
233 }
234 
readPixels(const SkImageInfo & dstInfo,void * dstPixels,size_t dstRowBytes,int srcX,int srcY)235 bool SkSurface::readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
236                            int srcX, int srcY) {
237     return this->readPixels({dstInfo, dstPixels, dstRowBytes}, srcX, srcY);
238 }
239 
readPixels(const SkBitmap & bitmap,int srcX,int srcY)240 bool SkSurface::readPixels(const SkBitmap& bitmap, int srcX, int srcY) {
241     SkPixmap pm;
242     return bitmap.peekPixels(&pm) && this->readPixels(pm, srcX, srcY);
243 }
244 
asyncRescaleAndReadPixels(const SkImageInfo & info,const SkIRect & srcRect,RescaleGamma rescaleGamma,RescaleMode rescaleMode,ReadPixelsCallback callback,ReadPixelsContext context)245 void SkSurface::asyncRescaleAndReadPixels(const SkImageInfo& info,
246                                           const SkIRect& srcRect,
247                                           RescaleGamma rescaleGamma,
248                                           RescaleMode rescaleMode,
249                                           ReadPixelsCallback callback,
250                                           ReadPixelsContext context) {
251     if (!SkIRect::MakeWH(this->width(), this->height()).contains(srcRect) ||
252         !SkImageInfoIsValid(info)) {
253         callback(context, nullptr);
254         return;
255     }
256     asSB(this)->onAsyncRescaleAndReadPixels(
257             info, srcRect, rescaleGamma, rescaleMode, callback, context);
258 }
259 
asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,sk_sp<SkColorSpace> dstColorSpace,const SkIRect & srcRect,const SkISize & dstSize,RescaleGamma rescaleGamma,RescaleMode rescaleMode,ReadPixelsCallback callback,ReadPixelsContext context)260 void SkSurface::asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
261                                                 sk_sp<SkColorSpace> dstColorSpace,
262                                                 const SkIRect& srcRect,
263                                                 const SkISize& dstSize,
264                                                 RescaleGamma rescaleGamma,
265                                                 RescaleMode rescaleMode,
266                                                 ReadPixelsCallback callback,
267                                                 ReadPixelsContext context) {
268     if (!SkIRect::MakeWH(this->width(), this->height()).contains(srcRect) || dstSize.isZero() ||
269         (dstSize.width() & 0b1) || (dstSize.height() & 0b1)) {
270         callback(context, nullptr);
271         return;
272     }
273     asSB(this)->onAsyncRescaleAndReadPixelsYUV420(yuvColorSpace,
274                                                   std::move(dstColorSpace),
275                                                   srcRect,
276                                                   dstSize,
277                                                   rescaleGamma,
278                                                   rescaleMode,
279                                                   callback,
280                                                   context);
281 }
282 
writePixels(const SkPixmap & pmap,int x,int y)283 void SkSurface::writePixels(const SkPixmap& pmap, int x, int y) {
284     if (pmap.addr() == nullptr || pmap.width() <= 0 || pmap.height() <= 0) {
285         return;
286     }
287 
288     const SkIRect srcR = SkIRect::MakeXYWH(x, y, pmap.width(), pmap.height());
289     const SkIRect dstR = SkIRect::MakeWH(this->width(), this->height());
290     if (SkIRect::Intersects(srcR, dstR)) {
291         ContentChangeMode mode = kRetain_ContentChangeMode;
292         if (srcR.contains(dstR)) {
293             mode = kDiscard_ContentChangeMode;
294         }
295         asSB(this)->aboutToDraw(mode);
296         asSB(this)->onWritePixels(pmap, x, y);
297     }
298 }
299 
writePixels(const SkBitmap & src,int x,int y)300 void SkSurface::writePixels(const SkBitmap& src, int x, int y) {
301     SkPixmap pm;
302     if (src.peekPixels(&pm)) {
303         this->writePixels(pm, x, y);
304     }
305 }
306 
recordingContext()307 GrRecordingContext* SkSurface::recordingContext() {
308     return asSB(this)->onGetRecordingContext();
309 }
310 
getBackendTexture(BackendHandleAccess access)311 GrBackendTexture SkSurface::getBackendTexture(BackendHandleAccess access) {
312     return asSB(this)->onGetBackendTexture(access);
313 }
314 
getBackendRenderTarget(BackendHandleAccess access)315 GrBackendRenderTarget SkSurface::getBackendRenderTarget(BackendHandleAccess access) {
316     return asSB(this)->onGetBackendRenderTarget(access);
317 }
318 
replaceBackendTexture(const GrBackendTexture & backendTexture,GrSurfaceOrigin origin,ContentChangeMode mode,TextureReleaseProc textureReleaseProc,ReleaseContext releaseContext)319 bool SkSurface::replaceBackendTexture(const GrBackendTexture& backendTexture,
320                                       GrSurfaceOrigin origin, ContentChangeMode mode,
321                                       TextureReleaseProc textureReleaseProc,
322                                       ReleaseContext releaseContext) {
323     return asSB(this)->onReplaceBackendTexture(backendTexture, origin, mode, textureReleaseProc,
324                                                releaseContext);
325 }
326 
flush(BackendSurfaceAccess access,const GrFlushInfo & flushInfo)327 GrSemaphoresSubmitted SkSurface::flush(BackendSurfaceAccess access, const GrFlushInfo& flushInfo) {
328     return asSB(this)->onFlush(access, flushInfo, nullptr);
329 }
330 
flush(const GrFlushInfo & info,const GrBackendSurfaceMutableState * newState)331 GrSemaphoresSubmitted SkSurface::flush(const GrFlushInfo& info,
332                                        const GrBackendSurfaceMutableState* newState) {
333     return asSB(this)->onFlush(BackendSurfaceAccess::kNoAccess, info, newState);
334 }
335 
wait(int numSemaphores,const GrBackendSemaphore * waitSemaphores,bool deleteSemaphoresAfterWait)336 bool SkSurface::wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores,
337                      bool deleteSemaphoresAfterWait) {
338     return asSB(this)->onWait(numSemaphores, waitSemaphores, deleteSemaphoresAfterWait);
339 }
340 
characterize(SkSurfaceCharacterization * characterization) const341 bool SkSurface::characterize(SkSurfaceCharacterization* characterization) const {
342     return asConstSB(this)->onCharacterize(characterization);
343 }
344 
isCompatible(const SkSurfaceCharacterization & characterization) const345 bool SkSurface::isCompatible(const SkSurfaceCharacterization& characterization) const {
346     return asConstSB(this)->onIsCompatible(characterization);
347 }
348 
draw(sk_sp<const SkDeferredDisplayList> ddl,int xOffset,int yOffset)349 bool SkSurface::draw(sk_sp<const SkDeferredDisplayList> ddl, int xOffset, int yOffset) {
350     if (xOffset != 0 || yOffset != 0) {
351         return false; // the offsets currently aren't supported
352     }
353 
354     return asSB(this)->onDraw(std::move(ddl), { xOffset, yOffset });
355 }
356 
357 //////////////////////////////////////////////////////////////////////////////////////
358 #include "include/utils/SkNoDrawCanvas.h"
359 
360 class SkNullSurface : public SkSurface_Base {
361 public:
SkNullSurface(int width,int height)362     SkNullSurface(int width, int height) : SkSurface_Base(width, height, nullptr) {}
363 
364 protected:
onNewCanvas()365     SkCanvas* onNewCanvas() override {
366         return new SkNoDrawCanvas(this->width(), this->height());
367     }
onNewSurface(const SkImageInfo & info)368     sk_sp<SkSurface> onNewSurface(const SkImageInfo& info) override {
369         return MakeNull(info.width(), info.height());
370     }
onNewImageSnapshot(const SkIRect * subsetOrNull)371     sk_sp<SkImage> onNewImageSnapshot(const SkIRect* subsetOrNull) override { return nullptr; }
onWritePixels(const SkPixmap &,int x,int y)372     void onWritePixels(const SkPixmap&, int x, int y) override {}
onDraw(SkCanvas *,SkScalar,SkScalar,const SkSamplingOptions &,const SkPaint *)373     void onDraw(SkCanvas*, SkScalar, SkScalar, const SkSamplingOptions&, const SkPaint*) override {}
onCopyOnWrite(ContentChangeMode)374     void onCopyOnWrite(ContentChangeMode) override {}
375 };
376 
MakeNull(int width,int height)377 sk_sp<SkSurface> SkSurface::MakeNull(int width, int height) {
378     if (width < 1 || height < 1) {
379         return nullptr;
380     }
381     return sk_sp<SkSurface>(new SkNullSurface(width, height));
382 }
383 
384 //////////////////////////////////////////////////////////////////////////////////////
385 
386 #if !SK_SUPPORT_GPU
387 
MakeRenderTarget(GrRecordingContext *,SkBudgeted,const SkImageInfo &,int,GrSurfaceOrigin,const SkSurfaceProps *,bool)388 sk_sp<SkSurface> SkSurface::MakeRenderTarget(GrRecordingContext*, SkBudgeted, const SkImageInfo&,
389                                              int, GrSurfaceOrigin, const SkSurfaceProps*, bool) {
390     return nullptr;
391 }
392 
MakeRenderTarget(GrRecordingContext *,const SkSurfaceCharacterization &,SkBudgeted)393 sk_sp<SkSurface> SkSurface::MakeRenderTarget(GrRecordingContext*, const SkSurfaceCharacterization&,
394                                              SkBudgeted) {
395     return nullptr;
396 }
397 
MakeFromBackendTexture(GrRecordingContext *,const GrBackendTexture &,GrSurfaceOrigin origin,int sampleCnt,SkColorType,sk_sp<SkColorSpace>,const SkSurfaceProps *,TextureReleaseProc,ReleaseContext)398 sk_sp<SkSurface> SkSurface::MakeFromBackendTexture(GrRecordingContext*, const GrBackendTexture&,
399                                                    GrSurfaceOrigin origin, int sampleCnt,
400                                                    SkColorType, sk_sp<SkColorSpace>,
401                                                    const SkSurfaceProps*,
402                                                    TextureReleaseProc, ReleaseContext) {
403     return nullptr;
404 }
405 
MakeFromBackendRenderTarget(GrRecordingContext *,const GrBackendRenderTarget &,GrSurfaceOrigin origin,SkColorType,sk_sp<SkColorSpace>,const SkSurfaceProps *,RenderTargetReleaseProc,ReleaseContext)406 sk_sp<SkSurface> SkSurface::MakeFromBackendRenderTarget(GrRecordingContext*,
407                                                         const GrBackendRenderTarget&,
408                                                         GrSurfaceOrigin origin,
409                                                         SkColorType,
410                                                         sk_sp<SkColorSpace>,
411                                                         const SkSurfaceProps*,
412                                                         RenderTargetReleaseProc, ReleaseContext) {
413     return nullptr;
414 }
415 
flushAndSubmit(bool syncCpu)416 void SkSurface::flushAndSubmit(bool syncCpu) {
417     this->flush(BackendSurfaceAccess::kNoAccess, GrFlushInfo());
418 }
419 
420 #endif
421