Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/vk/GrVkSecondaryCBDrawContext.h" |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkImageInfo.h" |
| 11 | #include "include/core/SkSurfaceCharacterization.h" |
| 12 | #include "include/gpu/GrContext.h" |
| 13 | #include "include/gpu/vk/GrVkTypes.h" |
| 14 | #include "include/private/SkDeferredDisplayList.h" |
| 15 | #include "src/core/SkSurfacePriv.h" |
| 16 | #include "src/gpu/GrContextPriv.h" |
| 17 | #include "src/gpu/GrContextThreadSafeProxyPriv.h" |
| 18 | #include "src/gpu/GrRenderTargetContext.h" |
| 19 | #include "src/gpu/SkGpuDevice.h" |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 20 | |
| 21 | sk_sp<GrVkSecondaryCBDrawContext> GrVkSecondaryCBDrawContext::Make(GrContext* ctx, |
| 22 | const SkImageInfo& imageInfo, |
| 23 | const GrVkDrawableInfo& vkInfo, |
| 24 | const SkSurfaceProps* props) { |
| 25 | if (!ctx) { |
| 26 | return nullptr; |
| 27 | } |
| 28 | |
Robert Phillips | 4217ea7 | 2019-01-30 13:08:28 -0500 | [diff] [blame] | 29 | if (ctx->backend() != GrBackendApi::kVulkan) { |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 30 | return nullptr; |
| 31 | } |
| 32 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 33 | auto rtc = ctx->priv().makeVulkanSecondaryCBRenderTargetContext(imageInfo, vkInfo, props); |
Robert Phillips | b2adbef | 2019-07-02 16:33:05 -0400 | [diff] [blame] | 34 | SkASSERT(rtc->asSurfaceProxy()->isInstantiated()); |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 35 | |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 36 | sk_sp<SkGpuDevice> device( |
| 37 | SkGpuDevice::Make(ctx, std::move(rtc), SkGpuDevice::kUninit_InitContents)); |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 38 | if (!device) { |
| 39 | return nullptr; |
| 40 | } |
| 41 | |
Greg Daniel | b085fa9 | 2019-03-05 16:55:12 -0500 | [diff] [blame] | 42 | return sk_sp<GrVkSecondaryCBDrawContext>(new GrVkSecondaryCBDrawContext(std::move(device), |
| 43 | props)); |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 44 | } |
| 45 | |
Greg Daniel | b085fa9 | 2019-03-05 16:55:12 -0500 | [diff] [blame] | 46 | GrVkSecondaryCBDrawContext::GrVkSecondaryCBDrawContext(sk_sp<SkGpuDevice> device, |
| 47 | const SkSurfaceProps* props) |
| 48 | : fDevice(device) |
| 49 | , fProps(SkSurfacePropsCopyOrDefault(props)) {} |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 50 | |
| 51 | GrVkSecondaryCBDrawContext::~GrVkSecondaryCBDrawContext() { |
| 52 | SkASSERT(!fDevice); |
| 53 | SkASSERT(!fCachedCanvas.get()); |
| 54 | } |
| 55 | |
| 56 | SkCanvas* GrVkSecondaryCBDrawContext::getCanvas() { |
| 57 | if (!fCachedCanvas) { |
| 58 | fCachedCanvas = std::unique_ptr<SkCanvas>(new SkCanvas(fDevice)); |
| 59 | } |
| 60 | return fCachedCanvas.get(); |
| 61 | } |
| 62 | |
| 63 | void GrVkSecondaryCBDrawContext::flush() { |
| 64 | fDevice->flush(); |
| 65 | } |
| 66 | |
Greg Daniel | 3d92a49 | 2019-01-07 12:59:03 -0500 | [diff] [blame] | 67 | bool GrVkSecondaryCBDrawContext::wait(int numSemaphores, |
| 68 | const GrBackendSemaphore waitSemaphores[]) { |
| 69 | return fDevice->wait(numSemaphores, waitSemaphores); |
| 70 | } |
| 71 | |
Greg Daniel | b46add8 | 2019-01-02 14:51:29 -0500 | [diff] [blame] | 72 | void GrVkSecondaryCBDrawContext::releaseResources() { |
| 73 | fCachedCanvas.reset(); |
| 74 | fDevice.reset(); |
| 75 | } |
| 76 | |
Greg Daniel | b085fa9 | 2019-03-05 16:55:12 -0500 | [diff] [blame] | 77 | bool GrVkSecondaryCBDrawContext::characterize(SkSurfaceCharacterization* characterization) const { |
| 78 | GrRenderTargetContext* rtc = fDevice->accessRenderTargetContext(); |
| 79 | GrContext* ctx = fDevice->context(); |
| 80 | |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 81 | size_t maxResourceBytes = ctx->getResourceCacheLimit(); |
Greg Daniel | b085fa9 | 2019-03-05 16:55:12 -0500 | [diff] [blame] | 82 | |
| 83 | // We current don't support textured GrVkSecondaryCBDrawContexts. |
| 84 | SkASSERT(!rtc->asTextureProxy()); |
| 85 | |
Brian Salomon | bd3d8d3 | 2019-07-02 09:16:28 -0400 | [diff] [blame] | 86 | SkColorType ct = GrColorTypeToSkColorType(rtc->colorSpaceInfo().colorType()); |
| 87 | if (ct == kUnknown_SkColorType) { |
Greg Daniel | b085fa9 | 2019-03-05 16:55:12 -0500 | [diff] [blame] | 88 | return false; |
| 89 | } |
| 90 | |
| 91 | SkImageInfo ii = SkImageInfo::Make(rtc->width(), rtc->height(), ct, kPremul_SkAlphaType, |
| 92 | rtc->colorSpaceInfo().refColorSpace()); |
| 93 | |
Robert Phillips | b2adbef | 2019-07-02 16:33:05 -0400 | [diff] [blame] | 94 | GrBackendFormat format = rtc->asRenderTargetProxy()->backendFormat(); |
| 95 | |
| 96 | characterization->set(ctx->threadSafeProxy(), maxResourceBytes, ii, format, |
| 97 | rtc->origin(), rtc->numSamples(), |
| 98 | SkSurfaceCharacterization::Textureable(false), |
Greg Daniel | b085fa9 | 2019-03-05 16:55:12 -0500 | [diff] [blame] | 99 | SkSurfaceCharacterization::MipMapped(false), |
| 100 | SkSurfaceCharacterization::UsesGLFBO0(false), |
| 101 | SkSurfaceCharacterization::VulkanSecondaryCBCompatible(true), |
Robert Phillips | 3cd5432 | 2019-07-10 09:28:59 -0400 | [diff] [blame] | 102 | GrProtected(rtc->asRenderTargetProxy()->isProtected()), |
Greg Daniel | b085fa9 | 2019-03-05 16:55:12 -0500 | [diff] [blame] | 103 | this->props()); |
| 104 | |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | bool GrVkSecondaryCBDrawContext::isCompatible( |
| 109 | const SkSurfaceCharacterization& characterization) const { |
| 110 | GrRenderTargetContext* rtc = fDevice->accessRenderTargetContext(); |
| 111 | GrContext* ctx = fDevice->context(); |
| 112 | |
| 113 | if (!characterization.isValid()) { |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | if (!characterization.vulkanSecondaryCBCompatible()) { |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | // As long as the current state in the context allows for greater or equal resources, |
| 122 | // we allow the DDL to be replayed. |
| 123 | // DDL TODO: should we just remove the resource check and ignore the cache limits on playback? |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 124 | size_t maxResourceBytes = ctx->getResourceCacheLimit(); |
Greg Daniel | b085fa9 | 2019-03-05 16:55:12 -0500 | [diff] [blame] | 125 | |
| 126 | if (characterization.isTextureable()) { |
| 127 | // We don't support textureable DDL when rendering to a GrVkSecondaryCBDrawContext. |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | if (characterization.usesGLFBO0()) { |
| 132 | return false; |
| 133 | } |
| 134 | |
Brian Salomon | bd3d8d3 | 2019-07-02 09:16:28 -0400 | [diff] [blame] | 135 | SkColorType rtColorType = GrColorTypeToSkColorType(rtc->colorSpaceInfo().colorType()); |
| 136 | if (rtColorType == kUnknown_SkColorType) { |
Greg Daniel | b085fa9 | 2019-03-05 16:55:12 -0500 | [diff] [blame] | 137 | return false; |
| 138 | } |
| 139 | |
Robert Phillips | b2adbef | 2019-07-02 16:33:05 -0400 | [diff] [blame] | 140 | GrBackendFormat rtcFormat = rtc->asRenderTargetProxy()->backendFormat(); |
Robert Phillips | 3cd5432 | 2019-07-10 09:28:59 -0400 | [diff] [blame] | 141 | GrProtected isProtected = GrProtected(rtc->asRenderTargetProxy()->isProtected()); |
Robert Phillips | b2adbef | 2019-07-02 16:33:05 -0400 | [diff] [blame] | 142 | |
Greg Daniel | b085fa9 | 2019-03-05 16:55:12 -0500 | [diff] [blame] | 143 | return characterization.contextInfo() && characterization.contextInfo()->priv().matches(ctx) && |
| 144 | characterization.cacheMaxResourceBytes() <= maxResourceBytes && |
| 145 | characterization.origin() == rtc->origin() && |
Robert Phillips | b2adbef | 2019-07-02 16:33:05 -0400 | [diff] [blame] | 146 | characterization.backendFormat() == rtcFormat && |
Greg Daniel | b085fa9 | 2019-03-05 16:55:12 -0500 | [diff] [blame] | 147 | characterization.width() == rtc->width() && |
| 148 | characterization.height() == rtc->height() && |
Brian Salomon | bd3d8d3 | 2019-07-02 09:16:28 -0400 | [diff] [blame] | 149 | characterization.colorType() == rtColorType && |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 150 | characterization.sampleCount() == rtc->numSamples() && |
Greg Daniel | b085fa9 | 2019-03-05 16:55:12 -0500 | [diff] [blame] | 151 | SkColorSpace::Equals(characterization.colorSpace(), |
| 152 | rtc->colorSpaceInfo().colorSpace()) && |
Robert Phillips | 3cd5432 | 2019-07-10 09:28:59 -0400 | [diff] [blame] | 153 | characterization.isProtected() == isProtected && |
Greg Daniel | b085fa9 | 2019-03-05 16:55:12 -0500 | [diff] [blame] | 154 | characterization.surfaceProps() == rtc->surfaceProps(); |
| 155 | } |
| 156 | |
| 157 | bool GrVkSecondaryCBDrawContext::draw(SkDeferredDisplayList* ddl) { |
| 158 | if (!ddl || !this->isCompatible(ddl->characterization())) { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | GrRenderTargetContext* rtc = fDevice->accessRenderTargetContext(); |
| 163 | GrContext* ctx = fDevice->context(); |
| 164 | |
Chris Dalton | 6b49810 | 2019-08-01 14:14:52 -0600 | [diff] [blame] | 165 | ctx->priv().copyRenderTasksFromDDL(ddl, rtc->asRenderTargetProxy()); |
Greg Daniel | b085fa9 | 2019-03-05 16:55:12 -0500 | [diff] [blame] | 166 | return true; |
| 167 | } |
| 168 | |
| 169 | |