1/*
2 * Copyright 2018 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 "GrMtlTextureRenderTarget.h"
9#include "GrMtlGpu.h"
10#include "GrMtlUtil.h"
11
12GrMtlTextureRenderTarget::GrMtlTextureRenderTarget(GrMtlGpu* gpu,
13                                                   SkBudgeted budgeted,
14                                                   const GrSurfaceDesc& desc,
15                                                   id<MTLTexture> renderTexture,
16                                                   GrMipMapsStatus mipMapsStatus)
17        : GrSurface(gpu, desc)
18        , GrMtlTexture(gpu, desc, renderTexture, mipMapsStatus)
19        , GrMtlRenderTarget(gpu, desc, renderTexture) {
20    this->registerWithCache(budgeted);
21}
22
23GrMtlTextureRenderTarget::GrMtlTextureRenderTarget(GrMtlGpu* gpu,
24                                                   const GrSurfaceDesc& desc,
25                                                   id<MTLTexture> renderTexture,
26                                                   GrMipMapsStatus mipMapsStatus,
27                                                   GrWrapCacheable cacheable)
28        : GrSurface(gpu, desc)
29        , GrMtlTexture(gpu, desc, renderTexture, mipMapsStatus)
30        , GrMtlRenderTarget(gpu, desc, renderTexture) {
31    this->registerWithCacheWrapped(cacheable);
32}
33
34sk_sp<GrMtlTextureRenderTarget>
35GrMtlTextureRenderTarget::CreateNewTextureRenderTarget(GrMtlGpu* gpu,
36                                                       SkBudgeted budgeted,
37                                                       const GrSurfaceDesc& desc,
38                                                       MTLTextureDescriptor* texDesc,
39                                                       GrMipMapsStatus mipMapsStatus) {
40    id<MTLTexture> renderTexture = [gpu->device() newTextureWithDescriptor:texDesc];
41    SkASSERT(nil != renderTexture);
42    if (desc.fSampleCnt > 1) {
43        return nullptr;
44    }
45    SkASSERT((MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget) & renderTexture.usage);
46    return sk_sp<GrMtlTextureRenderTarget>(
47            new GrMtlTextureRenderTarget(gpu, budgeted, desc, renderTexture, mipMapsStatus));
48}
49
50sk_sp<GrMtlTextureRenderTarget> GrMtlTextureRenderTarget::MakeWrappedTextureRenderTarget(
51        GrMtlGpu* gpu,
52        const GrSurfaceDesc& desc,
53        id<MTLTexture> renderTexture,
54        GrWrapCacheable cacheable) {
55    SkASSERT(nil != renderTexture);
56    GrMipMapsStatus mipMapsStatus = renderTexture.mipmapLevelCount > 1
57                                            ? GrMipMapsStatus::kDirty
58                                            : GrMipMapsStatus::kNotAllocated;
59    if (desc.fSampleCnt > 1) {
60        return nullptr;
61    }
62    SkASSERT((MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget) & renderTexture.usage);
63    return sk_sp<GrMtlTextureRenderTarget>(
64            new GrMtlTextureRenderTarget(gpu, desc, renderTexture, mipMapsStatus, cacheable));
65}
66