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