1 /*
2 * Copyright 2023 Google LLC
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_Base.h"
9
10 #include "include/core/SkBitmap.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkCapabilities.h"
13 #include "include/core/SkColorSpace.h" // IWYU pragma: keep
14 #include "include/core/SkImage.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkPixmap.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkScalar.h"
20 #include "include/core/SkSize.h"
21 #include "src/image/SkRescaleAndReadPixels.h"
22
23 #include <atomic>
24 #include <cstdint>
25 #include <memory>
26
27 class GrRecordingContext;
28 class SkPaint;
29 class SkSurfaceProps;
30 namespace skgpu { namespace graphite { class Recorder; } }
31
SkSurface_Base(int width,int height,const SkSurfaceProps * props)32 SkSurface_Base::SkSurface_Base(int width, int height, const SkSurfaceProps* props)
33 : SkSurface(width, height, props) {}
34
SkSurface_Base(const SkImageInfo & info,const SkSurfaceProps * props)35 SkSurface_Base::SkSurface_Base(const SkImageInfo& info, const SkSurfaceProps* props)
36 : SkSurface(info, props) {}
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 }
44
onGetRecordingContext() const45 GrRecordingContext* SkSurface_Base::onGetRecordingContext() const { return nullptr; }
46
onGetRecorder() const47 skgpu::graphite::Recorder* SkSurface_Base::onGetRecorder() const { return nullptr; }
48
onDraw(SkCanvas * canvas,SkScalar x,SkScalar y,const SkSamplingOptions & sampling,const SkPaint * paint)49 void SkSurface_Base::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
50 const SkSamplingOptions& sampling, const SkPaint* paint) {
51 auto image = this->makeImageSnapshot();
52 if (image) {
53 canvas->drawImage(image.get(), x, y, sampling, paint);
54 }
55 }
56
onAsyncRescaleAndReadPixels(const SkImageInfo & info,SkIRect origSrcRect,SkSurface::RescaleGamma rescaleGamma,RescaleMode rescaleMode,SkSurface::ReadPixelsCallback callback,SkSurface::ReadPixelsContext context)57 void SkSurface_Base::onAsyncRescaleAndReadPixels(const SkImageInfo& info,
58 SkIRect origSrcRect,
59 SkSurface::RescaleGamma rescaleGamma,
60 RescaleMode rescaleMode,
61 SkSurface::ReadPixelsCallback callback,
62 SkSurface::ReadPixelsContext context) {
63 SkBitmap src;
64 SkPixmap peek;
65 SkIRect srcRect;
66 if (this->peekPixels(&peek)) {
67 src.installPixels(peek);
68 srcRect = origSrcRect;
69 } else {
70 src.setInfo(this->imageInfo().makeDimensions(origSrcRect.size()));
71 src.allocPixels();
72 if (!this->readPixels(src, origSrcRect.x(), origSrcRect.y())) {
73 callback(context, nullptr);
74 return;
75 }
76 srcRect = SkIRect::MakeSize(src.dimensions());
77 }
78 return SkRescaleAndReadPixels(src, info, srcRect, rescaleGamma, rescaleMode, callback,
79 context);
80 }
81
onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,bool readAlpha,sk_sp<SkColorSpace> dstColorSpace,SkIRect srcRect,SkISize dstSize,RescaleGamma rescaleGamma,RescaleMode,ReadPixelsCallback callback,ReadPixelsContext context)82 void SkSurface_Base::onAsyncRescaleAndReadPixelsYUV420(
83 SkYUVColorSpace yuvColorSpace, bool readAlpha, sk_sp<SkColorSpace> dstColorSpace,
84 SkIRect srcRect, SkISize dstSize, RescaleGamma rescaleGamma, RescaleMode,
85 ReadPixelsCallback callback, ReadPixelsContext context) {
86 // TODO: Call non-YUV asyncRescaleAndReadPixels and then make our callback convert to YUV and
87 // call client's callback.
88 callback(context, nullptr);
89 }
90
outstandingImageSnapshot() const91 bool SkSurface_Base::outstandingImageSnapshot() const {
92 return fCachedImage && !fCachedImage->unique();
93 }
94
aboutToDraw(ContentChangeMode mode)95 bool SkSurface_Base::aboutToDraw(ContentChangeMode mode) {
96 this->dirtyGenerationID();
97
98 SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);
99
100 if (fCachedImage) {
101 // the surface may need to fork its backend, if its sharing it with
102 // the cached image. Note: we only call if there is an outstanding owner
103 // on the image (besides us).
104 bool unique = fCachedImage->unique();
105 if (!unique) {
106 if (!this->onCopyOnWrite(mode)) {
107 return false;
108 }
109 }
110
111 // regardless of copy-on-write, we must drop our cached image now, so
112 // that the next request will get our new contents.
113 fCachedImage.reset();
114
115 if (unique) {
116 // Our content isn't held by any image now, so we can consider that content mutable.
117 // Raster surfaces need to be told it's safe to consider its pixels mutable again.
118 // We make this call after the ->unref() so the subclass can assert there are no images.
119 this->onRestoreBackingMutability();
120 }
121 } else if (kDiscard_ContentChangeMode == mode) {
122 this->onDiscard();
123 }
124 return true;
125 }
126
newGenerationID()127 uint32_t SkSurface_Base::newGenerationID() {
128 SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);
129 static std::atomic<uint32_t> nextID{1};
130 return nextID.fetch_add(1, std::memory_order_relaxed);
131 }
132
onCapabilities()133 sk_sp<const SkCapabilities> SkSurface_Base::onCapabilities() {
134 return SkCapabilities::RasterBackend();
135 }
136