Debian Codegraph @

src/gpu/ganesh/image/SkImage_LazyTexture.cpp

raw
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/gpu/ganesh/image/SkImage_LazyTexture.h"
9
10 #include "include/core/SkColorSpace.h"
11 #include "include/core/SkImage.h"
12 #include "include/core/SkImageGenerator.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkPixmap.h"
15 #include "include/gpu/GpuTypes.h"
16 #include "include/gpu/ganesh/GrDirectContext.h"
17 #include "include/gpu/ganesh/SkImageGanesh.h"
18 #include "include/private/gpu/ganesh/GrTextureGenerator.h" // IWYU pragma: keep
19 #include "src/gpu/ganesh/GrColorInfo.h"
20 #include "src/gpu/ganesh/GrDirectContextPriv.h"
21 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
22 #include "src/gpu/ganesh/SkGaneshRecorder.h"
23 #include "src/gpu/ganesh/SkGr.h"
24 #include "src/gpu/ganesh/SurfaceContext.h"
25 #include "src/gpu/ganesh/image/GrImageUtils.h"
26
27 #include <cstddef>
28 #include <memory>
29 #include <utility>
30
31 enum class GrColorType;
32
33 sk_sp<SkImage> SkImage_LazyTexture::onMakeSubset(SkRecorder* recorder,
34 const SkIRect& subset,
35 RequiredProperties props) const {
36 sk_sp<SkImage> pixels;
37 if (auto gRecorder = AsGaneshRecorder(recorder)) {
38 if (auto direct = gRecorder->directContext()) {
39 pixels = SkImages::TextureFromImage(direct, this);
40 }
41 }
42 if (!pixels) {
43 pixels = this->makeRasterImage(nullptr);
44 }
45 if (!pixels) {
46 return nullptr;
47 }
48 return pixels->makeSubset(recorder, subset, props);
49 }
50
51 bool SkImage_LazyTexture::readPixelsProxy(GrDirectContext* ctx, const SkPixmap& pixmap) const {
52 if (!ctx) {
53 return false;
54 }
55 GrSurfaceProxyView view = skgpu::ganesh::LockTextureProxyView(
56 ctx, this, GrImageTexGenPolicy::kDraw, skgpu::Mipmapped::kNo);
57
58 if (!view) {
59 return false;
60 }
61
62 GrColorType ct = skgpu::ganesh::ColorTypeOfLockTextureProxy(ctx->priv().caps(),
63 this->colorType());
64 GrColorInfo colorInfo(ct, this->alphaType(), this->refColorSpace());
65 auto sContext = ctx->priv().makeSC(std::move(view), colorInfo);
66 if (!sContext) {
67 return false;
68 }
69 size_t rowBytes = this->imageInfo().minRowBytes();
70 return sContext->readPixels(ctx, {this->imageInfo(), pixmap.writable_addr(), rowBytes}, {0, 0});
71 }
72
73 namespace SkImages {
74 sk_sp<SkImage> DeferredFromTextureGenerator(std::unique_ptr<GrTextureGenerator> generator) {
75 SkImage_Lazy::Validator validator(
76 SharedGenerator::Make(std::move(generator)), nullptr, nullptr);
77
78 return validator ? sk_make_sp<SkImage_LazyTexture>(&validator) : nullptr;
79 }
80 }