• 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 #ifndef SkSurface_Base_DEFINED
9 #define SkSurface_Base_DEFINED
10 
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkRefCnt.h"
14 #include "include/core/SkSamplingOptions.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkSurface.h"
17 #include "include/core/SkTypes.h"
18 
19 #include <cstdint>
20 #include <memory>
21 
22 class GrBackendSemaphore;
23 class GrBackendTexture;
24 class GrRecordingContext;
25 class SkCapabilities;
26 class SkColorSpace;
27 class SkPaint;
28 class SkPixmap;
29 class GrSurfaceCharacterization;
30 class SkSurfaceProps;
31 enum GrSurfaceOrigin : int;
32 enum SkYUVColorSpace : int;
33 namespace skgpu { namespace graphite { class Recorder; } }
34 struct SkIRect;
35 struct SkISize;
36 struct SkImageInfo;
37 
38 class SkSurface_Base : public SkSurface {
39 public:
40     SkSurface_Base(int width, int height, const SkSurfaceProps*);
41     SkSurface_Base(const SkImageInfo&, const SkSurfaceProps*);
42     ~SkSurface_Base() override;
43 
44     // From SkSurface.h
replaceBackendTexture(const GrBackendTexture &,GrSurfaceOrigin,ContentChangeMode,TextureReleaseProc,ReleaseContext)45     bool replaceBackendTexture(const GrBackendTexture&,
46                                GrSurfaceOrigin,
47                                ContentChangeMode,
48                                TextureReleaseProc,
49                                ReleaseContext) override {
50         return false;
51     }
52 
53     enum class Type {
54         kNull,     // intentionally associating 0 with a null canvas
55         kGanesh,
56         kGraphite,
57         kRaster,
58     };
59 
60     // TODO(kjlubick) Android directly subclasses SkSurface_Base for tests, so we
61     // cannot make this a pure virtual. They seem to want a surface that is spy-able
62     // or mockable, so maybe we should provide something like that.
type()63     virtual Type type() const { return Type::kNull; }
64 
65     // True for surfaces instantiated by pixels in CPU memory
isRasterBacked()66     bool isRasterBacked() const { return this->type() == Type::kRaster; }
67     // True for surfaces instantiated by Ganesh in GPU memory
isGaneshBacked()68     bool isGaneshBacked() const { return this->type() == Type::kGanesh; }
69     // True for surfaces instantiated by Graphite in GPU memory
isGraphiteBacked()70     bool isGraphiteBacked() const { return this->type() == Type::kGraphite; }
71 
72     virtual GrRecordingContext* onGetRecordingContext() const;
73     virtual skgpu::graphite::Recorder* onGetRecorder() const;
74 
75     /**
76      *  Allocate a canvas that will draw into this surface. We will cache this
77      *  canvas, to return the same object to the caller multiple times. We
78      *  take ownership, and will call unref() on the canvas when we go out of
79      *  scope.
80      */
81     virtual SkCanvas* onNewCanvas() = 0;
82 
83     virtual sk_sp<SkSurface> onNewSurface(const SkImageInfo&) = 0;
84 
85     /**
86      *  Allocate an SkImage that represents the current contents of the surface.
87      *  This needs to be able to outlive the surface itself (if need be), and
88      *  must faithfully represent the current contents, even if the surface
89      *  is changed after this called (e.g. it is drawn to via its canvas).
90      *
91      *  If a subset is specified, the the impl must make a copy, rather than try to wait
92      *  on copy-on-write.
93      */
94     virtual sk_sp<SkImage> onNewImageSnapshot(const SkIRect* subset = nullptr) { return nullptr; }
95 
onMakeTemporaryImage()96     virtual sk_sp<SkImage> onMakeTemporaryImage() { return this->makeImageSnapshot(); }
97 
98     virtual void onWritePixels(const SkPixmap&, int x, int y) = 0;
99 
100     /**
101      * Default implementation does a rescale/read and then calls the callback.
102      */
103     virtual void onAsyncRescaleAndReadPixels(const SkImageInfo&,
104                                              const SkIRect srcRect,
105                                              RescaleGamma,
106                                              RescaleMode,
107                                              ReadPixelsCallback,
108                                              ReadPixelsContext);
109     /**
110      * Default implementation does a rescale/read/yuv conversion and then calls the callback.
111      */
112     virtual void onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace,
113                                                    bool readAlpha,
114                                                    sk_sp<SkColorSpace> dstColorSpace,
115                                                    SkIRect srcRect,
116                                                    SkISize dstSize,
117                                                    RescaleGamma,
118                                                    RescaleMode,
119                                                    ReadPixelsCallback,
120                                                    ReadPixelsContext);
121 
122     /**
123      *  Default implementation:
124      *
125      *  image = this->newImageSnapshot();
126      *  if (image) {
127      *      image->draw(canvas, ...);
128      *      image->unref();
129      *  }
130      */
131     virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkSamplingOptions&,const SkPaint*);
132 
133     /**
134      * Called as a performance hint when the Surface is allowed to make it's contents
135      * undefined.
136      */
onDiscard()137     virtual void onDiscard() {}
138 
139     /**
140      *  If the surface is about to change, we call this so that our subclass
141      *  can optionally fork their backend (copy-on-write) in case it was
142      *  being shared with the cachedImage.
143      *
144      *  Returns false if the backing cannot be un-shared.
145      */
146     [[nodiscard]] virtual bool onCopyOnWrite(ContentChangeMode) = 0;
147 
148     /**
149      *  Signal the surface to remind its backing store that it's mutable again.
150      *  Called only when we _didn't_ copy-on-write; we assume the copies start mutable.
151      */
onRestoreBackingMutability()152     virtual void onRestoreBackingMutability() {}
153 
154     /**
155      * Caused the current backend 3D API to wait on the passed in semaphores before executing new
156      * commands on the gpu. Any previously submitting commands will not be blocked by these
157      * semaphores.
158      */
onWait(int numSemaphores,const GrBackendSemaphore * waitSemaphores,bool deleteSemaphoresAfterWait)159     virtual bool onWait(int numSemaphores, const GrBackendSemaphore* waitSemaphores,
160                         bool deleteSemaphoresAfterWait) {
161         return false;
162     }
163 
onCharacterize(GrSurfaceCharacterization *)164     virtual bool onCharacterize(GrSurfaceCharacterization*) const { return false; }
onIsCompatible(const GrSurfaceCharacterization &)165     virtual bool onIsCompatible(const GrSurfaceCharacterization&) const { return false; }
166 
167     // TODO: Remove this (make it pure virtual) after updating Android (which has a class derived
168     // from SkSurface_Base).
169     virtual sk_sp<const SkCapabilities> onCapabilities();
170 
171     inline SkCanvas* getCachedCanvas();
172     inline sk_sp<SkImage> refCachedImage();
173 
hasCachedImage()174     bool hasCachedImage() const { return fCachedImage != nullptr; }
175 
176     // called by SkSurface to compute a new genID
177     uint32_t newGenerationID();
178 
179 private:
180     std::unique_ptr<SkCanvas> fCachedCanvas = nullptr;
181     sk_sp<SkImage>            fCachedImage  = nullptr;
182 
183     // Returns false if drawing should not take place (allocation failure).
184     [[nodiscard]] bool aboutToDraw(ContentChangeMode mode);
185 
186     // Returns true if there is an outstanding image-snapshot, indicating that a call to aboutToDraw
187     // would trigger a copy-on-write.
188     bool outstandingImageSnapshot() const;
189 
190     friend class SkCanvas;
191     friend class SkSurface;
192 };
193 
getCachedCanvas()194 SkCanvas* SkSurface_Base::getCachedCanvas() {
195     if (nullptr == fCachedCanvas) {
196         fCachedCanvas = std::unique_ptr<SkCanvas>(this->onNewCanvas());
197         if (fCachedCanvas) {
198             fCachedCanvas->setSurfaceBase(this);
199         }
200     }
201     return fCachedCanvas.get();
202 }
203 
refCachedImage()204 sk_sp<SkImage> SkSurface_Base::refCachedImage() {
205     if (fCachedImage) {
206         return fCachedImage;
207     }
208 
209     fCachedImage = this->onNewImageSnapshot();
210 
211     SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);
212     return fCachedImage;
213 }
214 
asSB(SkSurface * surface)215 static inline SkSurface_Base* asSB(SkSurface* surface) {
216     return static_cast<SkSurface_Base*>(surface);
217 }
218 
asConstSB(const SkSurface * surface)219 static inline const SkSurface_Base* asConstSB(const SkSurface* surface) {
220     return static_cast<const SkSurface_Base*>(surface);
221 }
222 
223 #endif
224