Coverage Report

Created: 2024-05-20 07:14

/src/skia/tools/gpu/BackendTextureImageFactory.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2020 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 "tools/gpu/BackendTextureImageFactory.h"
9
10
#include "include/core/SkColorSpace.h"
11
#include "include/core/SkImage.h"
12
#include "include/core/SkPixmap.h"
13
#include "src/core/SkAutoPixmapStorage.h"
14
#include "tools/gpu/ManagedBackendTexture.h"
15
16
#ifdef SK_GANESH
17
#include "include/gpu/GrBackendSurface.h"
18
#include "include/gpu/GrDirectContext.h"
19
#include "include/gpu/ganesh/SkImageGanesh.h"
20
#endif
21
22
#ifdef SK_GRAPHITE
23
#include "include/core/SkBitmap.h"
24
#include "include/gpu/graphite/Image.h"
25
#include "include/gpu/graphite/Recorder.h"
26
#include "src/gpu/graphite/RecorderPriv.h"
27
#endif
28
29
namespace sk_gpu_test {
30
#ifdef SK_GANESH
31
sk_sp<SkImage> MakeBackendTextureImage(GrDirectContext* dContext,
32
                                       const SkPixmap& pixmap,
33
                                       Renderable renderable,
34
                                       GrSurfaceOrigin origin,
35
0
                                       Protected isProtected) {
36
0
    auto mbet = ManagedBackendTexture::MakeWithData(dContext,
37
0
                                                    pixmap,
38
0
                                                    origin,
39
0
                                                    renderable,
40
0
                                                    isProtected);
41
0
    if (!mbet) {
42
0
        return nullptr;
43
0
    }
44
0
    return SkImages::BorrowTextureFrom(dContext,
45
0
                                       mbet->texture(),
46
0
                                       origin,
47
0
                                       pixmap.colorType(),
48
0
                                       pixmap.alphaType(),
49
0
                                       pixmap.refColorSpace(),
50
0
                                       ManagedBackendTexture::ReleaseProc,
51
0
                                       mbet->releaseContext());
52
0
}
53
54
sk_sp<SkImage> MakeBackendTextureImage(GrDirectContext* dContext,
55
                                       const SkImageInfo& info,
56
                                       SkColor4f color,
57
                                       Mipmapped mipmapped,
58
                                       Renderable renderable,
59
                                       GrSurfaceOrigin origin,
60
0
                                       Protected isProtected) {
61
0
    if (info.alphaType() == kOpaque_SkAlphaType) {
62
0
        color = color.makeOpaque();
63
0
    } else if (info.alphaType() == kPremul_SkAlphaType) {
64
0
        auto pmColor = color.premul();
65
0
        color = {pmColor.fR, pmColor.fG, pmColor.fB, pmColor.fA};
66
0
    }
67
0
    auto mbet = ManagedBackendTexture::MakeWithData(dContext,
68
0
                                                    info.width(),
69
0
                                                    info.height(),
70
0
                                                    info.colorType(),
71
0
                                                    color,
72
0
                                                    mipmapped,
73
0
                                                    renderable,
74
0
                                                    isProtected);
75
0
    if (!mbet) {
76
0
        return nullptr;
77
0
    }
78
0
    return SkImages::BorrowTextureFrom(dContext,
79
0
                                       mbet->texture(),
80
0
                                       origin,
81
0
                                       info.colorType(),
82
0
                                       info.alphaType(),
83
0
                                       info.refColorSpace(),
84
0
                                       ManagedBackendTexture::ReleaseProc,
85
0
                                       mbet->releaseContext());
86
0
}
87
#endif  // SK_GANESH
88
89
#ifdef SK_GRAPHITE
90
using Recorder = skgpu::graphite::Recorder;
91
sk_sp<SkImage> MakeBackendTextureImage(Recorder* recorder,
92
                                       const SkPixmap& pixmap,
93
                                       skgpu::Mipmapped isMipmapped,
94
                                       Renderable isRenderable,
95
                                       Origin origin,
96
0
                                       Protected isProtected) {
97
0
    auto mbet = ManagedGraphiteTexture::MakeFromPixmap(recorder,
98
0
                                                       pixmap,
99
0
                                                       isMipmapped,
100
0
                                                       isRenderable,
101
0
                                                       isProtected);
102
0
    if (!mbet) {
103
0
        return nullptr;
104
0
    }
105
106
0
    return SkImages::WrapTexture(recorder,
107
0
                                 mbet->texture(),
108
0
                                 pixmap.colorType(),
109
0
                                 pixmap.alphaType(),
110
0
                                 pixmap.refColorSpace(),
111
0
                                 origin,
112
0
                                 sk_gpu_test::ManagedGraphiteTexture::ImageReleaseProc,
113
0
                                 mbet->releaseContext());
114
0
}
115
116
sk_sp<SkImage> MakeBackendTextureImage(Recorder* recorder,
117
                                       const SkImageInfo& ii,
118
                                       SkColor4f color,
119
                                       skgpu::Mipmapped isMipmapped,
120
                                       Renderable isRenderable,
121
                                       Origin origin,
122
0
                                       Protected isProtected) {
123
0
    if (ii.alphaType() == kOpaque_SkAlphaType) {
124
0
        color = color.makeOpaque();
125
0
    }
126
127
0
    SkBitmap bitmap;
128
0
    bitmap.allocPixels(ii);
129
130
0
    bitmap.eraseColor(color);
131
132
0
    return MakeBackendTextureImage(recorder, bitmap.pixmap(), isMipmapped, isRenderable,
133
0
                                   origin, isProtected);
134
0
}
135
#endif  // SK_GRAPHITE
136
137
}  // namespace sk_gpu_test