• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 "experimental/graphite/include/SkStuff.h"
9 
10 #include "experimental/graphite/include/BackendTexture.h"
11 #include "experimental/graphite/include/Context.h"
12 #include "experimental/graphite/include/Recorder.h"
13 #include "experimental/graphite/src/Caps.h"
14 #include "experimental/graphite/src/ContextPriv.h"
15 #include "experimental/graphite/src/Device.h"
16 #include "experimental/graphite/src/Gpu.h"
17 #include "experimental/graphite/src/RecorderPriv.h"
18 #include "experimental/graphite/src/ResourceProvider.h"
19 #include "experimental/graphite/src/Surface_Graphite.h"
20 #include "experimental/graphite/src/Texture.h"
21 #include "experimental/graphite/src/TextureProxy.h"
22 
MakeGraphite(skgpu::Recorder * recorder,const SkImageInfo & ii)23 sk_sp<SkSurface> MakeGraphite(skgpu::Recorder* recorder, const SkImageInfo& ii) {
24     sk_sp<skgpu::Device> device = skgpu::Device::Make(recorder, ii);
25     if (!device) {
26         return nullptr;
27     }
28 
29     return sk_make_sp<skgpu::Surface>(std::move(device));
30 }
31 
validate_backend_texture(const skgpu::Caps * caps,const skgpu::BackendTexture & texture,SkColorType ct)32 static bool validate_backend_texture(const skgpu::Caps* caps,
33                                      const skgpu::BackendTexture& texture,
34                                      SkColorType ct) {
35     if (!texture.isValid()) {
36         return false;
37     }
38 
39     const skgpu::TextureInfo& info = texture.info();
40     if (!caps->areColorTypeAndTextureInfoCompatible(ct, info)) {
41         return false;
42     }
43 
44     if (!caps->isRenderable(info)) {
45         return false;
46     }
47     return true;
48 }
49 
MakeGraphiteFromBackendTexture(skgpu::Recorder * recorder,const skgpu::BackendTexture & beTexture,SkColorType colorType,sk_sp<SkColorSpace> colorSpace,const SkSurfaceProps * props)50 sk_sp<SkSurface> MakeGraphiteFromBackendTexture(skgpu::Recorder* recorder,
51                                                 const skgpu::BackendTexture& beTexture,
52                                                 SkColorType colorType,
53                                                 sk_sp<SkColorSpace> colorSpace,
54                                                 const SkSurfaceProps* props) {
55 
56     if (!recorder) {
57         return nullptr;
58     }
59 
60     if (!validate_backend_texture(recorder->priv().caps(),
61                                   beTexture,
62                                   colorType)) {
63         return nullptr;
64     }
65 
66     sk_sp<skgpu::Texture> texture =
67             recorder->priv().resourceProvider()->createWrappedTexture(beTexture);
68 
69     if (!texture) {
70         return nullptr;
71     }
72 
73     sk_sp<skgpu::TextureProxy> proxy(new skgpu::TextureProxy(std::move(texture)));
74 
75     sk_sp<skgpu::Device> device = skgpu::Device::Make(recorder,
76                                                       std::move(proxy),
77                                                       std::move(colorSpace),
78                                                       colorType,
79                                                       kPremul_SkAlphaType);
80     if (!device) {
81         return nullptr;
82     }
83 
84     return sk_make_sp<skgpu::Surface>(std::move(device));
85 }
86