| 1 |
/* |
| 2 |
* Copyright 2021 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 |
#ifndef skgpu_graphite_Surface_Graphite_DEFINED |
| 9 |
#define skgpu_graphite_Surface_Graphite_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkRecorder.h" |
| 12 |
#include "src/image/SkSurface_Base.h" |
| 13 |
|
| 14 |
#include "include/gpu/GpuTypes.h" |
| 15 |
#include "src/gpu/SkBackingFit.h" |
| 16 |
#include "src/gpu/graphite/ResourceTypes.h" |
| 17 |
#include "src/gpu/graphite/TextureProxyView.h" |
| 18 |
|
| 19 |
namespace skgpu::graphite { |
| 20 |
|
| 21 |
class Context; |
| 22 |
class Device; |
| 23 |
class DrawContext; |
| 24 |
class Image; |
| 25 |
class Recorder; |
| 26 |
class TextureProxy; |
| 27 |
|
| 28 |
class Surface final : public SkSurface_Base { |
| 29 |
public: |
| 30 |
// Convenience factory to create a Device, instantiate its target proxy and return as a Surface. |
| 31 |
static sk_sp<Surface> Make(Recorder* recorder, |
| 32 |
const SkImageInfo& info, |
| 33 |
std::string_view label, |
| 34 |
Budgeted budgeted, |
| 35 |
Mipmapped mipmapped = Mipmapped::kNo, |
| 36 |
SkBackingFit backingFit = SkBackingFit::kExact, |
| 37 |
const SkSurfaceProps* props = nullptr) { |
| 38 |
return Make(recorder, info, label, budgeted, mipmapped, backingFit, props, |
| 39 |
LoadOp::kClear, /*registerWithRecorder=*/true); |
| 40 |
} |
| 41 |
// Make a surface that is not registered with the provided recorder. This surface should be |
| 42 |
// short-lived and it must be flushed manually for its draw commands to be recorded. Most |
| 43 |
// scratch surfaces will be budgeted, but if the underlying texture is being returned as a |
| 44 |
// client-owned image, that may not be the case. |
| 45 |
static sk_sp<Surface> MakeScratch(Recorder* recorder, |
| 46 |
const SkImageInfo& info, |
| 47 |
std::string_view label, |
| 48 |
Budgeted budgeted = Budgeted::kYes, |
| 49 |
Mipmapped mipmapped = Mipmapped::kNo, |
| 50 |
SkBackingFit backingFit = SkBackingFit::kApprox) { |
| 51 |
return Make(recorder, info, label, budgeted, mipmapped, backingFit, |
| 52 |
/*props=*/nullptr, LoadOp::kDiscard, /*registerWithRecorder=*/false); |
| 53 |
} |
| 54 |
|
| 55 |
Surface(sk_sp<Device>); |
| 56 |
~Surface() override; |
| 57 |
|
| 58 |
// From SkSurface.h |
| 59 |
SkImageInfo imageInfo() const override; |
| 60 |
|
| 61 |
// From SkSurface_Base.h |
| 62 |
SkSurface_Base::Type type() const override { return SkSurface_Base::Type::kGraphite; } |
| 63 |
|
| 64 |
Recorder* onGetRecorder() const override; |
| 65 |
SkRecorder* onGetBaseRecorder() const override; |
| 66 |
SkCanvas* onNewCanvas() override; |
| 67 |
sk_sp<SkSurface> onNewSurface(const SkImageInfo&) override; |
| 68 |
sk_sp<SkImage> onNewImageSnapshot(const SkIRect* subset) override; |
| 69 |
sk_sp<SkImage> onMakeTemporaryImage() override; |
| 70 |
void onWritePixels(const SkPixmap&, int x, int y) override; |
| 71 |
void onAsyncRescaleAndReadPixels(const SkImageInfo& info, |
| 72 |
SkIRect srcRect, |
| 73 |
RescaleGamma rescaleGamma, |
| 74 |
RescaleMode rescaleMode, |
| 75 |
ReadPixelsCallback callback, |
| 76 |
ReadPixelsContext context) override; |
| 77 |
void onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace, |
| 78 |
bool readAlpha, |
| 79 |
sk_sp<SkColorSpace> dstColorSpace, |
| 80 |
SkIRect srcRect, |
| 81 |
SkISize dstSize, |
| 82 |
RescaleGamma rescaleGamma, |
| 83 |
RescaleMode, |
| 84 |
ReadPixelsCallback callback, |
| 85 |
ReadPixelsContext context) override; |
| 86 |
bool onCopyOnWrite(ContentChangeMode) override; |
| 87 |
sk_sp<const SkCapabilities> onCapabilities() override; |
| 88 |
|
| 89 |
TextureProxyView readSurfaceView() const; |
| 90 |
sk_sp<Image> asImage() const; |
| 91 |
sk_sp<Image> makeImageCopy(const SkIRect* subset, Mipmapped) const; |
| 92 |
TextureProxy* backingTextureProxy() const; |
| 93 |
|
| 94 |
void flushToDrawContext(DrawContext*); |
| 95 |
|
| 96 |
private: |
| 97 |
// Regular and scratch surfaces differ by initial clear and if they are registered or not, |
| 98 |
// otherwise are constructed the same. |
| 99 |
static sk_sp<Surface> Make(Recorder* recorder, |
| 100 |
const SkImageInfo&, |
| 101 |
std::string_view label, |
| 102 |
Budgeted, |
| 103 |
Mipmapped, |
| 104 |
SkBackingFit, |
| 105 |
const SkSurfaceProps* props, |
| 106 |
LoadOp initialLoadOp, |
| 107 |
bool registerWithRecorder); |
| 108 |
|
| 109 |
sk_sp<Device> fDevice; |
| 110 |
sk_sp<Image> fImageView; // the image object returned by asImage() |
| 111 |
|
| 112 |
friend void Flush(SkSurface*); |
| 113 |
}; |
| 114 |
|
| 115 |
// TODO: The long-term for the public API around surfaces and flushing/submitting will likely |
| 116 |
// be replaced with explicit control over Recorders and submitting Recordings to the Context |
| 117 |
// directly. For now, internal tools often rely on surface/canvas flushing to control what's |
| 118 |
// being timed (nanobench or viewer's stats layer), so we flush any pending draws to a DrawPass. |
| 119 |
// While this won't measure actual conversion of the task list to backend command buffers, that |
| 120 |
// should be fairly negligible since most of the work is handled in DrawPass::Make(). |
| 121 |
// Additionally flushing pending work here ensures we don't batch across or clear prior recorded |
| 122 |
// work when looping in a benchmark, as the controlling code expects. |
| 123 |
void Flush(sk_sp<SkSurface> surface); |
| 124 |
void Flush(SkSurface* surface); |
| 125 |
} // namespace skgpu::graphite |
| 126 |
|
| 127 |
#endif // skgpu_graphite_Surface_Graphite_DEFINED |