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/SkGr.h"
23 #include "src/gpu/ganesh/SurfaceContext.h"
24 #include "src/gpu/ganesh/image/GrImageUtils.h"
25
26 #include <cstddef>
27 #include <memory>
28 #include <utility>
29
30 enum class GrColorType;
31
onMakeSubset(GrDirectContext * direct,const SkIRect & subset) const32 sk_sp<SkImage> SkImage_LazyTexture::onMakeSubset(GrDirectContext* direct,
33 const SkIRect& subset) const {
34 auto pixels = direct ? SkImages::TextureFromImage(direct, this) :
35 this->makeRasterImage(nullptr);
36 return pixels ? pixels->makeSubset(direct, subset) : nullptr;
37 }
38
readPixelsProxy(GrDirectContext * ctx,const SkPixmap & pixmap) const39 bool SkImage_LazyTexture::readPixelsProxy(GrDirectContext* ctx, const SkPixmap& pixmap) const {
40 if (!ctx) {
41 return false;
42 }
43 GrSurfaceProxyView view = skgpu::ganesh::LockTextureProxyView(
44 ctx, this, GrImageTexGenPolicy::kDraw, skgpu::Mipmapped::kNo);
45
46 if (!view) {
47 return false;
48 }
49
50 GrColorType ct = skgpu::ganesh::ColorTypeOfLockTextureProxy(ctx->priv().caps(),
51 this->colorType());
52 GrColorInfo colorInfo(ct, this->alphaType(), this->refColorSpace());
53 auto sContext = ctx->priv().makeSC(std::move(view), colorInfo);
54 if (!sContext) {
55 return false;
56 }
57 size_t rowBytes = this->imageInfo().minRowBytes();
58 return sContext->readPixels(ctx, {this->imageInfo(), pixmap.writable_addr(), rowBytes}, {0, 0});
59 }
60
61 namespace SkImages {
DeferredFromTextureGenerator(std::unique_ptr<GrTextureGenerator> generator)62 sk_sp<SkImage> DeferredFromTextureGenerator(std::unique_ptr<GrTextureGenerator> generator) {
63 SkImage_Lazy::Validator validator(
64 SharedGenerator::Make(std::move(generator)), nullptr, nullptr);
65
66 return validator ? sk_make_sp<SkImage_LazyTexture>(&validator) : nullptr;
67 }
68 }
69