| 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 |
#include "src/gpu/graphite/Image_Graphite.h" |
| 9 |
|
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColorSpace.h" |
| 12 |
#include "include/core/SkImage.h" |
| 13 |
#include "include/core/SkSurface.h" |
| 14 |
#include "include/gpu/graphite/Image.h" |
| 15 |
#include "include/gpu/graphite/Recorder.h" |
| 16 |
#include "include/gpu/graphite/Surface.h" |
| 17 |
#include "src/gpu/SkBackingFit.h" |
| 18 |
#include "src/gpu/graphite/Caps.h" |
| 19 |
#include "src/gpu/graphite/Device.h" |
| 20 |
#include "src/gpu/graphite/DrawContext.h" |
| 21 |
#include "src/gpu/graphite/Log.h" |
| 22 |
#include "src/gpu/graphite/RecorderPriv.h" |
| 23 |
#include "src/gpu/graphite/ResourceProvider.h" |
| 24 |
#include "src/gpu/graphite/Texture.h" |
| 25 |
#include "src/gpu/graphite/TextureUtils.h" |
| 26 |
#include "src/gpu/graphite/task/CopyTask.h" |
| 27 |
|
| 28 |
#if defined(GPU_TEST_UTILS) |
| 29 |
#include "include/gpu/graphite/Context.h" |
| 30 |
#include "src/gpu/graphite/ContextPriv.h" |
| 31 |
#endif |
| 32 |
|
| 33 |
namespace skgpu::graphite { |
| 34 |
|
| 35 |
// Graphite does not cache based on the image's unique ID so always request a new one. |
| 36 |
Image::Image(TextureProxyView view, |
| 37 |
const SkColorInfo& info) |
| 38 |
: Image_Base(SkImageInfo::Make(view.proxy()->dimensions(), info), kNeedNewImageUniqueID) |
| 39 |
, fTextureProxyView(std::move(view)) {} |
| 40 |
|
| 41 |
Image::~Image() = default; |
| 42 |
|
| 43 |
sk_sp<Image> Image::WrapDevice(sk_sp<Device> device) { |
| 44 |
TextureProxyView proxy = device->readSurfaceView(); |
| 45 |
if (!proxy) { |
| 46 |
return nullptr; |
| 47 |
} |
| 48 |
// NOTE: If the device was created with an approx backing fit, its SkImageInfo reports the |
| 49 |
// logical dimensions, but its proxy has the approximate fit. These larger dimensions are |
| 50 |
// propagated to the SkImageInfo of this image view. |
| 51 |
sk_sp<Image> image = sk_make_sp<Image>(std::move(proxy), |
| 52 |
device->imageInfo().colorInfo()); |
| 53 |
image->linkDevice(std::move(device)); |
| 54 |
return image; |
| 55 |
} |
| 56 |
|
| 57 |
sk_sp<Image> Image::Copy(Recorder* recorder, |
| 58 |
DrawContext* drawContext, |
| 59 |
const TextureProxyView& srcView, |
| 60 |
const SkColorInfo& srcColorInfo, |
| 61 |
const SkIRect& subset, |
| 62 |
Budgeted budgeted, |
| 63 |
Mipmapped mipmapped, |
| 64 |
SkBackingFit backingFit, |
| 65 |
std::string_view label) { |
| 66 |
SkASSERT(!drawContext || budgeted == Budgeted::kYes); |
| 67 |
SkASSERT(!(mipmapped == Mipmapped::kYes && backingFit == SkBackingFit::kApprox)); |
| 68 |
if (!srcView) { |
| 69 |
return nullptr; |
| 70 |
} |
| 71 |
|
| 72 |
SkASSERT(srcView.proxy()->isFullyLazy() || |
| 73 |
SkIRect::MakeSize(srcView.proxy()->dimensions()).contains(subset)); |
| 74 |
|
| 75 |
if (!recorder->priv().caps()->supportsReadPixels(srcView.proxy()->textureInfo())) { |
| 76 |
if (!recorder->priv().caps()->isTexturable(srcView.proxy()->textureInfo())) { |
| 77 |
// The texture is not blittable nor texturable so copying cannot be done. |
| 78 |
return nullptr; |
| 79 |
} |
| 80 |
// Copy-as-draw |
| 81 |
sk_sp<Image> srcImage(new Image(srcView, srcColorInfo)); |
| 82 |
return CopyAsDraw(recorder, drawContext, srcImage.get(), subset, srcColorInfo, |
| 83 |
budgeted, mipmapped, backingFit, label); |
| 84 |
} |
| 85 |
|
| 86 |
skgpu::graphite::TextureInfo textureInfo = |
| 87 |
recorder->priv().caps()->getTextureInfoForSampledCopy(srcView.proxy()->textureInfo(), |
| 88 |
mipmapped); |
| 89 |
|
| 90 |
sk_sp<TextureProxy> dst = TextureProxy::Make( |
| 91 |
recorder->priv().caps(), |
| 92 |
recorder->priv().resourceProvider(), |
| 93 |
backingFit == SkBackingFit::kApprox ? GetApproxSize(subset.size()) : subset.size(), |
| 94 |
textureInfo, |
| 95 |
label, |
| 96 |
budgeted); |
| 97 |
if (!dst) { |
| 98 |
return nullptr; |
| 99 |
} |
| 100 |
|
| 101 |
auto copyTask = CopyTextureToTextureTask::Make(srcView.refProxy(), subset, dst, {0, 0}); |
| 102 |
if (!copyTask) { |
| 103 |
return nullptr; |
| 104 |
} |
| 105 |
|
| 106 |
if (drawContext) { |
| 107 |
drawContext->recordDependency(std::move(copyTask)); |
| 108 |
} else { |
| 109 |
recorder->priv().add(std::move(copyTask)); |
| 110 |
} |
| 111 |
|
| 112 |
if (mipmapped == Mipmapped::kYes) { |
| 113 |
if (!GenerateMipmaps(recorder, drawContext, dst, srcColorInfo)) { |
| 114 |
SKGPU_LOG_W("Image::Copy failed to generate mipmaps"); |
| 115 |
return nullptr; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
return sk_sp<Image>(new Image({std::move(dst), srcView.swizzle()}, srcColorInfo)); |
| 120 |
} |
| 121 |
|
| 122 |
size_t Image::textureSize() const { |
| 123 |
if (!fTextureProxyView.proxy()) { |
| 124 |
return 0; |
| 125 |
} |
| 126 |
|
| 127 |
if (!fTextureProxyView.proxy()->texture()) { |
| 128 |
return fTextureProxyView.proxy()->uninstantiatedGpuMemorySize(); |
| 129 |
} |
| 130 |
|
| 131 |
return fTextureProxyView.proxy()->texture()->gpuMemorySize(); |
| 132 |
} |
| 133 |
|
| 134 |
sk_sp<Image> Image::copyImage(Recorder* recorder, |
| 135 |
const SkIRect& subset, |
| 136 |
Budgeted budgeted, |
| 137 |
Mipmapped mipmapped, |
| 138 |
SkBackingFit backingFit, |
| 139 |
std::string_view label) const { |
| 140 |
this->notifyInUse(recorder, /*drawContext=*/nullptr); |
| 141 |
return Image::Copy(recorder, /*drawContext=*/nullptr, |
| 142 |
fTextureProxyView, this->imageInfo().colorInfo(), |
| 143 |
subset, budgeted, mipmapped, backingFit, label); |
| 144 |
} |
| 145 |
|
| 146 |
sk_sp<SkImage> Image::onReinterpretColorSpace(sk_sp<SkColorSpace> newCS) const { |
| 147 |
sk_sp<Image> view = sk_make_sp<Image>(fTextureProxyView, |
| 148 |
this->imageInfo().colorInfo() |
| 149 |
.makeColorSpace(std::move(newCS))); |
| 150 |
// The new Image object shares the same texture proxy, so it should also share linked Devices |
| 151 |
view->linkDevices(this); |
| 152 |
return view; |
| 153 |
} |
| 154 |
|
| 155 |
#if defined(GPU_TEST_UTILS) |
| 156 |
bool Image::readPixelsGraphite(SkRecorder* recorder, |
| 157 |
const SkPixmap& dst, |
| 158 |
int srcX, |
| 159 |
int srcY) const { |
| 160 |
auto gRecorder = AsGraphiteRecorder(recorder); |
| 161 |
if (!gRecorder) { |
| 162 |
return false; |
| 163 |
} |
| 164 |
if (Context* context = gRecorder->priv().context()) { |
| 165 |
// Add all previous commands generated to the command buffer. |
| 166 |
// If the client snaps later they'll only get post-read commands in their Recording, |
| 167 |
// but since they're doing a readPixels in the middle that shouldn't be unexpected. |
| 168 |
std::unique_ptr<Recording> recording = gRecorder->snap(); |
| 169 |
if (!recording) { |
| 170 |
return false; |
| 171 |
} |
| 172 |
InsertRecordingInfo info; |
| 173 |
info.fRecording = recording.get(); |
| 174 |
if (!context->insertRecording(info)) { |
| 175 |
return false; |
| 176 |
} |
| 177 |
return context->priv().readPixels(dst, |
| 178 |
fTextureProxyView.proxy(), |
| 179 |
this->imageInfo(), |
| 180 |
srcX, |
| 181 |
srcY); |
| 182 |
} |
| 183 |
return false; |
| 184 |
} |
| 185 |
#endif |
| 186 |
|
| 187 |
} // namespace skgpu::graphite |