• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 Google Inc.
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/dawn/GrDawnTexture.h"
9 
10 #include "src/gpu/ganesh/dawn/GrDawnGpu.h"
11 #include "src/gpu/ganesh/dawn/GrDawnTextureRenderTarget.h"
12 #include "src/gpu/ganesh/dawn/GrDawnUtil.h"
13 
GrDawnTexture(GrDawnGpu * gpu,SkISize dimensions,const GrDawnTextureInfo & info,GrMipmapStatus mipmapStatus,std::string_view label)14 GrDawnTexture::GrDawnTexture(GrDawnGpu* gpu,
15                              SkISize dimensions,
16                              const GrDawnTextureInfo& info,
17                              GrMipmapStatus mipmapStatus,
18                              std::string_view label)
19         : GrSurface(gpu, dimensions, GrProtected::kNo, label)
20         , GrTexture(gpu, dimensions, GrProtected::kNo, GrTextureType::k2D, mipmapStatus, label)
21         , fInfo(info) {}
22 
Make(GrDawnGpu * gpu,SkISize dimensions,wgpu::TextureFormat format,GrRenderable renderable,int sampleCnt,skgpu::Budgeted budgeted,int mipLevels,GrMipmapStatus status,std::string_view label)23 sk_sp<GrDawnTexture> GrDawnTexture::Make(GrDawnGpu* gpu,
24                                          SkISize dimensions,
25                                          wgpu::TextureFormat format,
26                                          GrRenderable renderable,
27                                          int sampleCnt,
28                                          skgpu::Budgeted budgeted,
29                                          int mipLevels,
30                                          GrMipmapStatus status,
31                                          std::string_view label) {
32     bool renderTarget = renderable == GrRenderable::kYes;
33     wgpu::TextureDescriptor textureDesc;
34 
35     textureDesc.usage = wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::CopySrc |
36                         wgpu::TextureUsage::CopyDst;
37 
38     if (renderTarget) {
39         textureDesc.usage |= wgpu::TextureUsage::RenderAttachment;
40     }
41 
42     textureDesc.size.width = dimensions.fWidth;
43     textureDesc.size.height = dimensions.fHeight;
44     textureDesc.size.depthOrArrayLayers = 1;
45     textureDesc.format = format;
46     textureDesc.mipLevelCount = std::max(mipLevels, 1);
47     textureDesc.sampleCount = sampleCnt;
48 
49     wgpu::Texture tex = gpu->device().CreateTexture(&textureDesc);
50 
51     if (!tex) {
52         return nullptr;
53     }
54 
55     GrDawnTextureInfo info;
56     info.fTexture = tex;
57     info.fFormat = textureDesc.format;
58     info.fLevelCount = mipLevels;
59     sk_sp<GrDawnTexture> result;
60     if (renderTarget) {
61         result = sk_sp<GrDawnTextureRenderTarget>(new GrDawnTextureRenderTarget(
62                 gpu, dimensions, sampleCnt, info, status, label));
63     } else {
64         result = sk_sp<GrDawnTexture>(
65                 new GrDawnTexture(gpu, dimensions, info, status, label));
66     }
67     result->registerWithCache(budgeted);
68     return result;
69 }
70 
backendFormat() const71 GrBackendFormat GrDawnTexture::backendFormat() const {
72     return GrBackendFormat::MakeDawn(fInfo.fFormat);
73 }
74 
MakeWrapped(GrDawnGpu * gpu,SkISize dimensions,GrRenderable renderable,int sampleCnt,GrWrapCacheable cacheable,GrIOType ioType,const GrDawnTextureInfo & info,std::string_view label)75 sk_sp<GrDawnTexture> GrDawnTexture::MakeWrapped(GrDawnGpu* gpu, SkISize dimensions,
76                                                 GrRenderable renderable, int sampleCnt,
77                                                 GrWrapCacheable cacheable, GrIOType ioType,
78                                                 const GrDawnTextureInfo& info,
79                                                 std::string_view label) {
80     sk_sp<GrDawnTexture> tex;
81     GrMipmapStatus status = info.fLevelCount > 1 ? GrMipmapStatus::kValid
82                                                  : GrMipmapStatus::kNotAllocated;
83     if (GrRenderable::kYes == renderable) {
84         tex = sk_sp<GrDawnTexture>(new GrDawnTextureRenderTarget(
85                 gpu, dimensions, sampleCnt, info, status, label));
86     } else {
87         tex = sk_sp<GrDawnTexture>(new GrDawnTexture(gpu, dimensions, info, status, label));
88     }
89     tex->registerWithCacheWrapped(cacheable);
90     if (ioType == kRead_GrIOType) {
91       tex->setReadOnly();
92     }
93     return tex;
94 }
95 
~GrDawnTexture()96 GrDawnTexture::~GrDawnTexture() {
97 }
98 
getDawnGpu() const99 GrDawnGpu* GrDawnTexture::getDawnGpu() const {
100     SkASSERT(!this->wasDestroyed());
101     return static_cast<GrDawnGpu*>(this->getGpu());
102 }
103 
onRelease()104 void GrDawnTexture::onRelease() {
105     INHERITED::onRelease();
106 }
107 
onAbandon()108 void GrDawnTexture::onAbandon() {
109     INHERITED::onAbandon();
110 }
111 
getBackendTexture() const112 GrBackendTexture GrDawnTexture::getBackendTexture() const {
113     return GrBackendTexture(this->width(), this->height(), fInfo);
114 }
115