Debian Codegraph @

src/gpu/ganesh/image/SkImage_RasterPinnable.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_RasterPinnable.h"
9
10 #include "include/android/SkImageAndroid.h"
11 #include "include/core/SkBitmap.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkTypes.h"
16 #include "include/gpu/GpuTypes.h"
17 #include "include/gpu/ganesh/GrRecordingContext.h"
18 #include "src/core/SkImageInfoPriv.h"
19 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
20 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
21 #include "src/gpu/ganesh/SkGr.h"
22 #include "src/gpu/ganesh/image/GrImageUtils.h"
23 #include "src/gpu/ganesh/image/GrMippedBitmap.h"
24 #include "src/image/SkImage_Base.h"
25
26 #include <memory>
27 #include <tuple>
28
29 std::tuple<GrSurfaceProxyView, GrColorType> SkImage_RasterPinnable::asView(
30 GrRecordingContext* rContext,
31 skgpu::Mipmapped mipmapped,
32 GrImageTexGenPolicy policy) const {
33 if (fPinnedData) {
34 // We ignore the mipmap request here. If the pinned view isn't mipmapped then we will
35 // fallback to bilinear. The pin API is used by Android Framework which does not expose
36 // mipmapping. Moreover, we're moving towards requiring that images be made with mip levels
37 // if mipmapping is desired (skbug.com/40041743)
38 mipmapped = skgpu::Mipmapped::kNo;
39 if (policy != GrImageTexGenPolicy::kDraw) {
40 return {skgpu::ganesh::CopyView(
41 rContext,
42 fPinnedData->fPinnedView,
43 mipmapped,
44 policy,
45 /*label=*/"TextureForPinnableRasterImageWithPolicyNotEqualKDraw"),
46 fPinnedData->fPinnedColorType};
47 }
48 return {fPinnedData->fPinnedView, fPinnedData->fPinnedColorType};
49 }
50 return skgpu::ganesh::RasterAsView(rContext, this, mipmapped, policy);
51 }
52
53 namespace SkImages {
54
55 sk_sp<SkImage> PinnableRasterFromBitmap(const SkBitmap& bm) {
56 if (!SkImageInfoIsValid(bm.info()) || bm.rowBytes() < bm.info().minRowBytes()) {
57 return nullptr;
58 }
59
60 return sk_make_sp<SkImage_RasterPinnable>(bm);
61 }
62
63 } // namespace SkImages
64
65 namespace skgpu::ganesh {
66
67 bool PinAsTexture(GrRecordingContext* rContext, SkImage* img) {
68 auto ib = as_IB(img);
69 if (ib->type() == SkImage_Base::Type::kLazyTexture) {
70 return true;
71 }
72 if (ib->type() != SkImage_Base::Type::kRasterPinnable) {
73 // Cannot pin images which are not of subclass SkImage_RasterPinnable
74 return false;
75 }
76 auto raster = static_cast<SkImage_RasterPinnable*>(ib);
77 if (!raster->fPinnedData) {
78 auto data = std::make_unique<PinnedData>();
79 std::tie(data->fPinnedView, data->fPinnedColorType) =
80 GrMakeCachedBitmapProxyView(rContext,
81 GrMippedBitmap(raster->bitmap(), raster->refMips()),
82 /*label=*/"ganesh_PinAsTexture",
83 skgpu::Mipmapped::kNo);
84 if (!data->fPinnedView) {
85 return false;
86 }
87 data->fPinnedUniqueID = raster->bitmap().getGenerationID();
88 data->fPinnedContextID = rContext->priv().contextID();
89 raster->fPinnedData.swap(data);
90 } else {
91 SkASSERT(raster->fPinnedData->fPinnedCount > 0);
92 SkASSERT(raster->fPinnedData->fPinnedUniqueID != 0);
93 if (rContext->priv().contextID() != raster->fPinnedData->fPinnedContextID) {
94 return false;
95 }
96 }
97 // Note: we only increment if the texture was successfully pinned
98 raster->fPinnedData->fPinnedCount++;
99 return true;
100 }
101
102 void UnpinTexture(GrRecordingContext*, SkImage* img) {
103 auto ib = as_IB(img);
104 if (ib->type() != SkImage_Base::Type::kRasterPinnable) {
105 // Cannot pin images which are not of subclass SkImage_RasterPinnable
106 return;
107 }
108 auto raster = static_cast<SkImage_RasterPinnable*>(ib);
109 if (!raster->fPinnedData) {
110 SkASSERT(false);
111 return;
112 }
113
114 SkASSERT(raster->fPinnedData->fPinnedCount > 0);
115 SkASSERT(raster->fPinnedData->fPinnedUniqueID != 0);
116 // It would be good to check rContext->priv().contextID() != fPinnedContextID
117 // but Android used to (maybe still does) call Unpin with a freed context ptr
118
119 raster->fPinnedData->fPinnedCount--;
120 if (raster->fPinnedData->fPinnedCount <= 0) {
121 raster->fPinnedData.reset(nullptr);
122 }
123 }
124
125 } // namespace skgpu::ganesh