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
8#include "include/private/GrContext_Base.h"
9
10#include "src/gpu/GrBaseContextPriv.h"
11#include "src/gpu/GrCaps.h"
12#include "src/gpu/GrShaderUtils.h"
13#include "src/gpu/effects/GrSkSLFP.h"
14
15static int32_t next_id() {
16 static std::atomic<int32_t> nextID{1};
17 int32_t id;
18 do {
19 id = nextID++;
20 } while (id == SK_InvalidGenID);
21 return id;
22}
23
24GrContext_Base::GrContext_Base(GrBackendApi backend,
25 const GrContextOptions& options,
26 uint32_t contextID)
27 : fBackend(backend)
28 , fOptions(options)
29 , fContextID(SK_InvalidGenID == contextID ? next_id() : contextID) {
30}
31
32GrContext_Base::~GrContext_Base() { }
33
34bool GrContext_Base::init(sk_sp<const GrCaps> caps) {
35 SkASSERT(caps);
36
37 fCaps = caps;
38 return true;
39}
40
41const GrCaps* GrContext_Base::caps() const { return fCaps.get(); }
42sk_sp<const GrCaps> GrContext_Base::refCaps() const { return fCaps; }
43
44GrBackendFormat GrContext_Base::defaultBackendFormat(SkColorType skColorType,
45 GrRenderable renderable) const {
46 const GrCaps* caps = this->caps();
47
48 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
49
50 GrBackendFormat format = caps->getDefaultBackendFormat(grColorType, renderable);
51 if (!format.isValid()) {
52 return GrBackendFormat();
53 }
54
55 SkASSERT(renderable == GrRenderable::kNo ||
56 caps->isFormatAsColorTypeRenderable(grColorType, format));
57
58 return format;
59}
60
61GrBackendFormat GrContext_Base::compressedBackendFormat(SkImage::CompressionType c) const {
62 const GrCaps* caps = this->caps();
63
64 GrBackendFormat format = caps->getBackendFormatFromCompressionType(c);
65
66 SkASSERT(!format.isValid() || caps->isFormatTexturable(format));
67 return format;
68}
69
70///////////////////////////////////////////////////////////////////////////////////////////////////
71sk_sp<const GrCaps> GrBaseContextPriv::refCaps() const {
72 return fContext->refCaps();
73}
74
75GrContextOptions::ShaderErrorHandler* GrBaseContextPriv::getShaderErrorHandler() const {
76 const GrContextOptions& options(this->options());
77 return options.fShaderErrorHandler ? options.fShaderErrorHandler
78 : GrShaderUtils::DefaultShaderErrorHandler();
79}
80