blob: 24a7f2069348eb5352be77e7504e4f68bd13b765 [file] [log] [blame]
Robert Phillips4217ea72019-01-30 13:08:28 -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 "include/private/GrContext_Base.h"
Robert Phillips4217ea72019-01-30 13:08:28 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrBaseContextPriv.h"
11#include "src/gpu/GrCaps.h"
Adlai Holler43b15792020-06-01 10:11:49 -040012#include "src/gpu/GrContextThreadSafeProxyPriv.h"
Brian Osmanbd4f8722020-03-06 15:23:54 -050013#include "src/gpu/GrShaderUtils.h"
Robert Phillips7f11fb52019-12-03 13:35:19 -050014#include "src/gpu/effects/GrSkSLFP.h"
Robert Phillipsbb606772019-02-04 17:50:57 -050015
Robert Phillips4217ea72019-01-30 13:08:28 -050016static int32_t next_id() {
17 static std::atomic<int32_t> nextID{1};
18 int32_t id;
19 do {
20 id = nextID++;
21 } while (id == SK_InvalidGenID);
22 return id;
23}
24
25GrContext_Base::GrContext_Base(GrBackendApi backend,
Robert Phillipsc1541ae2019-02-04 12:05:37 -050026 const GrContextOptions& options,
Robert Phillipsfd0d9702019-02-01 10:19:42 -050027 uint32_t contextID)
Robert Phillips4217ea72019-01-30 13:08:28 -050028 : fBackend(backend)
Robert Phillipsc1541ae2019-02-04 12:05:37 -050029 , fOptions(options)
Robert Phillipsfd0d9702019-02-01 10:19:42 -050030 , fContextID(SK_InvalidGenID == contextID ? next_id() : contextID) {
Robert Phillips4217ea72019-01-30 13:08:28 -050031}
32
Robert Phillipsc1541ae2019-02-04 12:05:37 -050033GrContext_Base::~GrContext_Base() { }
Robert Phillips4217ea72019-01-30 13:08:28 -050034
Brian Osman7b1678a2019-12-16 09:17:25 -050035bool GrContext_Base::init(sk_sp<const GrCaps> caps) {
36 SkASSERT(caps);
Adlai Holler43b15792020-06-01 10:11:49 -040037 // We either are a thread safe proxy and we don't have one, or we aren't and we do.
38 SkASSERT((nullptr == this->asThreadSafeProxy()) != (nullptr == fThreadSafeProxy));
Robert Phillipsbb606772019-02-04 17:50:57 -050039
Adlai Holler43b15792020-06-01 10:11:49 -040040 fCaps = std::move(caps);
Robert Phillipsbb606772019-02-04 17:50:57 -050041 return true;
42}
Robert Phillips4217ea72019-01-30 13:08:28 -050043
Robert Phillips292a6b22019-02-14 14:49:02 -050044const GrCaps* GrContext_Base::caps() const { return fCaps.get(); }
45sk_sp<const GrCaps> GrContext_Base::refCaps() const { return fCaps; }
46
Robert Phillipsf7599642019-08-13 16:16:42 -040047GrBackendFormat GrContext_Base::defaultBackendFormat(SkColorType skColorType,
48 GrRenderable renderable) const {
49 const GrCaps* caps = this->caps();
50
51 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
52
53 GrBackendFormat format = caps->getDefaultBackendFormat(grColorType, renderable);
54 if (!format.isValid()) {
55 return GrBackendFormat();
56 }
57
Robert Phillipsf7599642019-08-13 16:16:42 -040058 SkASSERT(renderable == GrRenderable::kNo ||
59 caps->isFormatAsColorTypeRenderable(grColorType, format));
60
61 return format;
62}
63
Robert Phillipsb915c942019-12-17 14:44:37 -050064GrBackendFormat GrContext_Base::compressedBackendFormat(SkImage::CompressionType c) const {
65 const GrCaps* caps = this->caps();
66
67 GrBackendFormat format = caps->getBackendFormatFromCompressionType(c);
68
69 SkASSERT(!format.isValid() || caps->isFormatTexturable(format));
70 return format;
71}
72
Robert Phillipsa41c6852019-02-07 10:44:10 -050073///////////////////////////////////////////////////////////////////////////////////////////////////
74sk_sp<const GrCaps> GrBaseContextPriv::refCaps() const {
75 return fContext->refCaps();
76}
Brian Osmanbd4f8722020-03-06 15:23:54 -050077
78GrContextOptions::ShaderErrorHandler* GrBaseContextPriv::getShaderErrorHandler() const {
79 const GrContextOptions& options(this->options());
80 return options.fShaderErrorHandler ? options.fShaderErrorHandler
81 : GrShaderUtils::DefaultShaderErrorHandler();
82}