1 /*
2 * Copyright 2021 Google LLC
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/BaseDevice.h"
9
10 #include "include/gpu/GrRecordingContext.h"
11 #include "src/gpu/GrProxyProvider.h"
12 #include "src/gpu/GrRecordingContextPriv.h"
13 #include "src/gpu/GrSurfaceProxyView.h"
14 #include "src/gpu/SurfaceContext.h"
15 #include "src/gpu/SurfaceFillContext.h"
16
17 #define ASSERT_SINGLE_OWNER GR_ASSERT_SINGLE_OWNER(fContext->priv().singleOwner())
18
19 namespace skgpu {
20
BaseDevice(sk_sp<GrRecordingContext> rContext,const SkImageInfo & ii,const SkSurfaceProps & props)21 BaseDevice::BaseDevice(sk_sp<GrRecordingContext> rContext,
22 const SkImageInfo& ii,
23 const SkSurfaceProps& props)
24 : INHERITED(ii, props)
25 , fContext(std::move(rContext)) {
26 }
27
readSurfaceView()28 GrSurfaceProxyView BaseDevice::readSurfaceView() {
29 return this->surfaceFillContext()->readSurfaceView();
30 }
31
32 /** Checks that the alpha type is legal and gets constructor flags. Returns false if device creation
33 should fail. */
CheckAlphaTypeAndGetFlags(SkAlphaType alphaType,InitContents init,DeviceFlags * flags)34 bool BaseDevice::CheckAlphaTypeAndGetFlags(SkAlphaType alphaType,
35 InitContents init,
36 DeviceFlags* flags) {
37 *flags = DeviceFlags::kNone;
38 switch (alphaType) {
39 case kPremul_SkAlphaType:
40 break;
41 case kOpaque_SkAlphaType:
42 *flags |= DeviceFlags::kIsOpaque;
43 break;
44 default: // If it is unpremul or unknown don't try to render
45 return false;
46 }
47 if (InitContents::kClear == init) {
48 *flags |= DeviceFlags::kNeedClear;
49 }
50 return true;
51 }
52
MakeInfo(SurfaceContext * sc,DeviceFlags flags)53 SkImageInfo BaseDevice::MakeInfo(SurfaceContext* sc, DeviceFlags flags) {
54 SkColorType colorType = GrColorTypeToSkColorType(sc->colorInfo().colorType());
55 return SkImageInfo::Make(sc->width(), sc->height(), colorType,
56 flags & DeviceFlags::kIsOpaque ? kOpaque_SkAlphaType
57 : kPremul_SkAlphaType,
58 sc->colorInfo().refColorSpace());
59 }
60
targetProxy()61 GrRenderTargetProxy* BaseDevice::targetProxy() {
62 return this->readSurfaceView().asRenderTargetProxy();
63 }
64
replaceBackingProxy(SkSurface::ContentChangeMode mode)65 bool BaseDevice::replaceBackingProxy(SkSurface::ContentChangeMode mode) {
66 ASSERT_SINGLE_OWNER
67
68 const SkImageInfo& ii = this->imageInfo();
69 GrRenderTargetProxy* oldRTP = this->targetProxy();
70 GrSurfaceProxyView oldView = this->readSurfaceView();
71
72 auto grColorType = SkColorTypeToGrColorType(ii.colorType());
73 auto format = fContext->priv().caps()->getDefaultBackendFormat(grColorType, GrRenderable::kYes);
74 if (!format.isValid()) {
75 return false;
76 }
77
78 GrProxyProvider* proxyProvider = fContext->priv().proxyProvider();
79 // This entry point is used by SkSurface_Gpu::onCopyOnWrite so it must create a
80 // kExact-backed render target proxy
81 sk_sp<GrTextureProxy> proxy = proxyProvider->createProxy(format,
82 ii.dimensions(),
83 GrRenderable::kYes,
84 oldRTP->numSamples(),
85 oldView.mipmapped(),
86 SkBackingFit::kExact,
87 oldRTP->isBudgeted(),
88 GrProtected::kNo);
89 if (!proxy) {
90 return false;
91 }
92
93 return this->replaceBackingProxy(mode, sk_ref_sp(proxy->asRenderTargetProxy()),
94 grColorType, ii.refColorSpace(), oldView.origin(),
95 this->surfaceProps());
96 }
97
98 } // namespace skgpu
99