Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkSpecialImage_Ganesh.cpp
Go to the documentation of this file.
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
9
10#include "include/core/SkColorSpace.h" // IWYU pragma: keep
13#include "include/core/SkRect.h"
14#include "include/core/SkSize.h"
25
26#include <cstddef>
27#include <utility>
28
29enum SkColorType : int;
30
31class SkSpecialImage_Gpu final : public SkSpecialImage {
32public:
34 const SkIRect& subset,
35 uint32_t uniqueID,
38 const SkSurfaceProps& props)
40 , fContext(context)
41 , fView(std::move(view)) {}
42
43 size_t getSize() const override { return fView.proxy()->gpuMemorySize(); }
44
45 bool isGaneshBacked() const override { return true; }
46
47 GrRecordingContext* getContext() const override { return fContext; }
48
49 GrSurfaceProxyView view(GrRecordingContext*) const { return fView; }
50
52 return fView.proxy()->backingStoreDimensions();
53 }
54
56 return SkSpecialImages::MakeDeferredFromGpu(
57 fContext, subset, this->uniqueID(), fView, this->colorInfo(), this->props());
58 }
59
60 sk_sp<SkImage> asImage() const override {
61 fView.proxy()->priv().exactify();
62 return sk_make_sp<SkImage_Ganesh>(
63 sk_ref_sp(fContext), this->uniqueID(), fView, this->colorInfo());
64 }
65
66private:
67 GrRecordingContext* fContext;
69};
70
71namespace SkSpecialImages {
72
73sk_sp<SkSpecialImage> MakeFromTextureImage(GrRecordingContext* rContext,
74 const SkIRect& subset,
76 const SkSurfaceProps& props) {
77 if (!rContext || !image || subset.isEmpty()) {
78 return nullptr;
79 }
80
81 SkASSERT(image->bounds().contains(subset));
82
83 // This will work even if the image is a raster-backed image.
84 auto [view, ct] = skgpu::ganesh::AsView(rContext, image, skgpu::Mipmapped::kNo);
85 return MakeDeferredFromGpu(rContext,
86 subset,
87 image->uniqueID(),
88 std::move(view),
89 {ct, image->alphaType(), image->refColorSpace()},
90 props);
91}
92
93sk_sp<SkSpecialImage> MakeDeferredFromGpu(GrRecordingContext* context,
94 const SkIRect& subset,
95 uint32_t uniqueID,
97 const GrColorInfo& colorInfo,
98 const SkSurfaceProps& props) {
99 if (!context || context->abandoned() || !view.asTextureProxy()) {
100 return nullptr;
101 }
102
104
106 return sk_make_sp<SkSpecialImage_Gpu>(
107 context,
108 subset,
109 uniqueID,
110 std::move(view),
111 SkColorInfo(ct, colorInfo.alphaType(), colorInfo.refColorSpace()),
112 props);
113}
114
116 if (!context || !img || !img->isGaneshBacked()) {
117 return {};
118 }
119 auto grImg = static_cast<const SkSpecialImage_Gpu*>(img);
120 return grImg->view(context);
121}
122
123} // namespace SkSpecialImages
static constexpr SkColorType GrColorTypeToSkColorType(GrColorType ct)
#define SkASSERT(cond)
Definition SkAssert.h:116
SkColorType
Definition SkColorType.h:19
sk_sp< T > sk_ref_sp(T *obj)
Definition SkRefCnt.h:381
Type::kYUV Type::kRGBA() int(0.7 *637)
sk_sp< SkColorSpace > refColorSpace() const
GrColorType colorType() const
Definition GrColorInfo.h:43
SkAlphaType alphaType() const
Definition GrColorInfo.h:44
bool abandoned() override
GrTextureProxy * asTextureProxy() const
GrSurfaceProxy * proxy() const
SkISize backingStoreDimensions() const
size_t gpuMemorySize() const
GrSurfaceProxyPriv priv()
SkIRect backingStoreBoundsIRect() const
uint32_t uniqueID() const
Definition SkImage.h:311
SkIRect bounds() const
Definition SkImage.h:303
sk_sp< SkSpecialImage > onMakeBackingStoreSubset(const SkIRect &subset) const override
SkSpecialImage_Gpu(GrRecordingContext *context, const SkIRect &subset, uint32_t uniqueID, GrSurfaceProxyView view, const SkColorInfo &colorInfo, const SkSurfaceProps &props)
GrRecordingContext * getContext() const override
size_t getSize() const override
bool isGaneshBacked() const override
sk_sp< SkImage > asImage() const override
GrSurfaceProxyView view(GrRecordingContext *) const
SkISize backingStoreDimensions() const override
virtual bool isGaneshBacked() const
uint32_t uniqueID() const
const SkIRect & subset() const
const SkSurfaceProps & props() const
const SkColorInfo & colorInfo() const
sk_sp< SkImage > image
Definition examples.cpp:29
std::tuple< GrSurfaceProxyView, GrColorType > AsView(GrRecordingContext *rContext, const SkImage *img, skgpu::Mipmapped mipmapped, GrImageTexGenPolicy policy)
Definition ref_ptr.h:256
bool isEmpty() const
Definition SkRect.h:202
bool contains(int32_t x, int32_t y) const
Definition SkRect.h:463