• 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/dawn/GrDawnTexture.h"
9 
10 #include "src/gpu/dawn/GrDawnGpu.h"
11 #include "src/gpu/dawn/GrDawnTextureRenderTarget.h"
12 #include "src/gpu/dawn/GrDawnUtil.h"
13 
GrDawnTexture(GrDawnGpu * gpu,SkISize dimensions,const GrDawnTextureInfo & info,GrMipmapStatus mipmapStatus)14 GrDawnTexture::GrDawnTexture(GrDawnGpu* gpu,
15                              SkISize dimensions,
16                              const GrDawnTextureInfo& info,
17                              GrMipmapStatus mipmapStatus)
18         : GrSurface(gpu, dimensions, GrProtected::kNo)
19         , GrTexture(gpu, dimensions, GrProtected::kNo, GrTextureType::k2D, mipmapStatus)
20         , fInfo(info) {}
21 
Make(GrDawnGpu * gpu,SkISize dimensions,wgpu::TextureFormat format,GrRenderable renderable,int sampleCnt,SkBudgeted budgeted,int mipLevels,GrMipmapStatus status)22 sk_sp<GrDawnTexture> GrDawnTexture::Make(GrDawnGpu* gpu, SkISize dimensions,
23                                          wgpu::TextureFormat format,
24                                          GrRenderable renderable, int sampleCnt,
25                                          SkBudgeted budgeted, int mipLevels,
26                                          GrMipmapStatus status) {
27     bool renderTarget = renderable == GrRenderable::kYes;
28     wgpu::TextureDescriptor textureDesc;
29 
30     textureDesc.usage =
31         wgpu::TextureUsage::Sampled |
32         wgpu::TextureUsage::CopySrc |
33         wgpu::TextureUsage::CopyDst;
34 
35     if (renderTarget) {
36         textureDesc.usage |= wgpu::TextureUsage::RenderAttachment;
37     }
38 
39     textureDesc.size.width = dimensions.fWidth;
40     textureDesc.size.height = dimensions.fHeight;
41     textureDesc.size.depthOrArrayLayers = 1;
42     textureDesc.format = format;
43     textureDesc.mipLevelCount = std::max(mipLevels, 1);
44     textureDesc.sampleCount = sampleCnt;
45 
46     wgpu::Texture tex = gpu->device().CreateTexture(&textureDesc);
47 
48     if (!tex) {
49         return nullptr;
50     }
51 
52     GrDawnTextureInfo info;
53     info.fTexture = tex;
54     info.fFormat = textureDesc.format;
55     info.fLevelCount = mipLevels;
56     sk_sp<GrDawnTexture> result;
57     if (renderTarget) {
58         result = sk_sp<GrDawnTextureRenderTarget>(new GrDawnTextureRenderTarget(gpu,
59                                                                                 dimensions,
60                                                                                 sampleCnt,
61                                                                                 info,
62                                                                                 status));
63     } else {
64         result = sk_sp<GrDawnTexture>(
65                 new GrDawnTexture(gpu, dimensions, info, status));
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)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     sk_sp<GrDawnTexture> tex;
80     GrMipmapStatus status = info.fLevelCount > 1 ? GrMipmapStatus::kValid
81                                                  : GrMipmapStatus::kNotAllocated;
82     if (GrRenderable::kYes == renderable) {
83         tex = sk_sp<GrDawnTexture>(new GrDawnTextureRenderTarget(
84                 gpu, dimensions, sampleCnt, info, status));
85     } else {
86         tex = sk_sp<GrDawnTexture>(
87                 new GrDawnTexture(gpu, dimensions, info, status));
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