Robert Phillips | 4217ea7 | 2019-01-30 13:08:28 -0500 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/private/GrContext_Base.h" |
Robert Phillips | 4217ea7 | 2019-01-30 13:08:28 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/gpu/GrBaseContextPriv.h" |
| 11 | #include "src/gpu/GrCaps.h" |
Brian Osman | bd4f872 | 2020-03-06 15:23:54 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrShaderUtils.h" |
Robert Phillips | 7f11fb5 | 2019-12-03 13:35:19 -0500 | [diff] [blame] | 13 | #include "src/gpu/effects/GrSkSLFP.h" |
Robert Phillips | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 14 | |
Robert Phillips | 4217ea7 | 2019-01-30 13:08:28 -0500 | [diff] [blame] | 15 | static 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 | |
| 24 | GrContext_Base::GrContext_Base(GrBackendApi backend, |
Robert Phillips | c1541ae | 2019-02-04 12:05:37 -0500 | [diff] [blame] | 25 | const GrContextOptions& options, |
Robert Phillips | fd0d970 | 2019-02-01 10:19:42 -0500 | [diff] [blame] | 26 | uint32_t contextID) |
Robert Phillips | 4217ea7 | 2019-01-30 13:08:28 -0500 | [diff] [blame] | 27 | : fBackend(backend) |
Robert Phillips | c1541ae | 2019-02-04 12:05:37 -0500 | [diff] [blame] | 28 | , fOptions(options) |
Robert Phillips | fd0d970 | 2019-02-01 10:19:42 -0500 | [diff] [blame] | 29 | , fContextID(SK_InvalidGenID == contextID ? next_id() : contextID) { |
Robert Phillips | 4217ea7 | 2019-01-30 13:08:28 -0500 | [diff] [blame] | 30 | } |
| 31 | |
Robert Phillips | c1541ae | 2019-02-04 12:05:37 -0500 | [diff] [blame] | 32 | GrContext_Base::~GrContext_Base() { } |
Robert Phillips | 4217ea7 | 2019-01-30 13:08:28 -0500 | [diff] [blame] | 33 | |
Brian Osman | 7b1678a | 2019-12-16 09:17:25 -0500 | [diff] [blame] | 34 | bool GrContext_Base::init(sk_sp<const GrCaps> caps) { |
| 35 | SkASSERT(caps); |
Robert Phillips | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 36 | |
| 37 | fCaps = caps; |
Robert Phillips | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 38 | return true; |
| 39 | } |
Robert Phillips | 4217ea7 | 2019-01-30 13:08:28 -0500 | [diff] [blame] | 40 | |
Robert Phillips | 292a6b2 | 2019-02-14 14:49:02 -0500 | [diff] [blame] | 41 | const GrCaps* GrContext_Base::caps() const { return fCaps.get(); } |
| 42 | sk_sp<const GrCaps> GrContext_Base::refCaps() const { return fCaps; } |
| 43 | |
Robert Phillips | f759964 | 2019-08-13 16:16:42 -0400 | [diff] [blame] | 44 | GrBackendFormat 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 | |
Robert Phillips | f759964 | 2019-08-13 16:16:42 -0400 | [diff] [blame] | 55 | SkASSERT(renderable == GrRenderable::kNo || |
| 56 | caps->isFormatAsColorTypeRenderable(grColorType, format)); |
| 57 | |
| 58 | return format; |
| 59 | } |
| 60 | |
Robert Phillips | b915c94 | 2019-12-17 14:44:37 -0500 | [diff] [blame] | 61 | GrBackendFormat 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 | |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 70 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 71 | sk_sp<const GrCaps> GrBaseContextPriv::refCaps() const { |
| 72 | return fContext->refCaps(); |
| 73 | } |
Brian Osman | bd4f872 | 2020-03-06 15:23:54 -0500 | [diff] [blame] | 74 | |
| 75 | GrContextOptions::ShaderErrorHandler* GrBaseContextPriv::getShaderErrorHandler() const { |
| 76 | const GrContextOptions& options(this->options()); |
| 77 | return options.fShaderErrorHandler ? options.fShaderErrorHandler |
| 78 | : GrShaderUtils::DefaultShaderErrorHandler(); |
| 79 | } |