blob: c52684815a8a7d06159a4bf69c3348c4ceee7a83 [file] [log] [blame]
Greg Danielb46add82019-01-02 14:51:29 -05001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/vk/GrVkSecondaryCBDrawContext.h"
Greg Danielb46add82019-01-02 14:51:29 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#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 Danielb46add82019-01-02 14:51:29 -050020
21sk_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 Phillips4217ea72019-01-30 13:08:28 -050029 if (ctx->backend() != GrBackendApi::kVulkan) {
Greg Danielb46add82019-01-02 14:51:29 -050030 return nullptr;
31 }
32
33 sk_sp<GrRenderTargetContext> rtc(
Robert Phillips9da87e02019-02-04 13:26:26 -050034 ctx->priv().makeVulkanSecondaryCBRenderTargetContext(imageInfo, vkInfo, props));
Greg Danielb46add82019-01-02 14:51:29 -050035
36 int width = rtc->width();
37 int height = rtc->height();
38
39 sk_sp<SkGpuDevice> device(SkGpuDevice::Make(ctx, std::move(rtc), width, height,
40 SkGpuDevice::kUninit_InitContents));
41 if (!device) {
42 return nullptr;
43 }
44
Greg Danielb085fa92019-03-05 16:55:12 -050045 return sk_sp<GrVkSecondaryCBDrawContext>(new GrVkSecondaryCBDrawContext(std::move(device),
46 props));
Greg Danielb46add82019-01-02 14:51:29 -050047}
48
Greg Danielb085fa92019-03-05 16:55:12 -050049GrVkSecondaryCBDrawContext::GrVkSecondaryCBDrawContext(sk_sp<SkGpuDevice> device,
50 const SkSurfaceProps* props)
51 : fDevice(device)
52 , fProps(SkSurfacePropsCopyOrDefault(props)) {}
Greg Danielb46add82019-01-02 14:51:29 -050053
54GrVkSecondaryCBDrawContext::~GrVkSecondaryCBDrawContext() {
55 SkASSERT(!fDevice);
56 SkASSERT(!fCachedCanvas.get());
57}
58
59SkCanvas* GrVkSecondaryCBDrawContext::getCanvas() {
60 if (!fCachedCanvas) {
61 fCachedCanvas = std::unique_ptr<SkCanvas>(new SkCanvas(fDevice));
62 }
63 return fCachedCanvas.get();
64}
65
66void GrVkSecondaryCBDrawContext::flush() {
67 fDevice->flush();
68}
69
Greg Daniel3d92a492019-01-07 12:59:03 -050070bool GrVkSecondaryCBDrawContext::wait(int numSemaphores,
71 const GrBackendSemaphore waitSemaphores[]) {
72 return fDevice->wait(numSemaphores, waitSemaphores);
73}
74
Greg Danielb46add82019-01-02 14:51:29 -050075void GrVkSecondaryCBDrawContext::releaseResources() {
76 fCachedCanvas.reset();
77 fDevice.reset();
78}
79
Greg Danielb085fa92019-03-05 16:55:12 -050080bool GrVkSecondaryCBDrawContext::characterize(SkSurfaceCharacterization* characterization) const {
81 GrRenderTargetContext* rtc = fDevice->accessRenderTargetContext();
82 GrContext* ctx = fDevice->context();
83
84 int maxResourceCount;
85 size_t maxResourceBytes;
86 ctx->getResourceCacheLimits(&maxResourceCount, &maxResourceBytes);
87
88 // We current don't support textured GrVkSecondaryCBDrawContexts.
89 SkASSERT(!rtc->asTextureProxy());
90
91 // TODO: the addition of colorType to the surfaceContext should remove this calculation
92 SkColorType ct;
93 if (!GrPixelConfigToColorType(rtc->colorSpaceInfo().config(), &ct)) {
94 return false;
95 }
96
97 SkImageInfo ii = SkImageInfo::Make(rtc->width(), rtc->height(), ct, kPremul_SkAlphaType,
98 rtc->colorSpaceInfo().refColorSpace());
99
100 characterization->set(ctx->threadSafeProxy(), maxResourceBytes, ii, rtc->origin(),
Chris Dalton6ce447a2019-06-23 18:07:38 -0600101 rtc->colorSpaceInfo().config(), rtc->numSamples(),
Greg Danielb085fa92019-03-05 16:55:12 -0500102 SkSurfaceCharacterization::Textureable(false),
103 SkSurfaceCharacterization::MipMapped(false),
104 SkSurfaceCharacterization::UsesGLFBO0(false),
105 SkSurfaceCharacterization::VulkanSecondaryCBCompatible(true),
106 this->props());
107
108 return true;
109}
110
111bool GrVkSecondaryCBDrawContext::isCompatible(
112 const SkSurfaceCharacterization& characterization) const {
113 GrRenderTargetContext* rtc = fDevice->accessRenderTargetContext();
114 GrContext* ctx = fDevice->context();
115
116 if (!characterization.isValid()) {
117 return false;
118 }
119
120 if (!characterization.vulkanSecondaryCBCompatible()) {
121 return false;
122 }
123
124 // As long as the current state in the context allows for greater or equal resources,
125 // we allow the DDL to be replayed.
126 // DDL TODO: should we just remove the resource check and ignore the cache limits on playback?
127 int maxResourceCount;
128 size_t maxResourceBytes;
129 ctx->getResourceCacheLimits(&maxResourceCount, &maxResourceBytes);
130
131 if (characterization.isTextureable()) {
132 // We don't support textureable DDL when rendering to a GrVkSecondaryCBDrawContext.
133 return false;
134 }
135
136 if (characterization.usesGLFBO0()) {
137 return false;
138 }
139
140 // TODO: the addition of colorType to the surfaceContext should remove this calculation
141 SkColorType rtcColorType;
142 if (!GrPixelConfigToColorType(rtc->colorSpaceInfo().config(), &rtcColorType)) {
143 return false;
144 }
145
146 return characterization.contextInfo() && characterization.contextInfo()->priv().matches(ctx) &&
147 characterization.cacheMaxResourceBytes() <= maxResourceBytes &&
148 characterization.origin() == rtc->origin() &&
149 characterization.config() == rtc->colorSpaceInfo().config() &&
150 characterization.width() == rtc->width() &&
151 characterization.height() == rtc->height() &&
152 characterization.colorType() == rtcColorType &&
Chris Dalton6ce447a2019-06-23 18:07:38 -0600153 characterization.sampleCount() == rtc->numSamples() &&
Greg Danielb085fa92019-03-05 16:55:12 -0500154 SkColorSpace::Equals(characterization.colorSpace(),
155 rtc->colorSpaceInfo().colorSpace()) &&
156 characterization.surfaceProps() == rtc->surfaceProps();
157}
158
159bool GrVkSecondaryCBDrawContext::draw(SkDeferredDisplayList* ddl) {
160 if (!ddl || !this->isCompatible(ddl->characterization())) {
161 return false;
162 }
163
164 GrRenderTargetContext* rtc = fDevice->accessRenderTargetContext();
165 GrContext* ctx = fDevice->context();
166
167 ctx->priv().copyOpListsFromDDL(ddl, rtc->asRenderTargetProxy());
168 return true;
169}
170
171