blob: 1c79b32ecac61a8041ec6f14d192229ab957593b [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"
Greg Danielf91aeb22019-06-18 09:58:02 -040012#include "src/gpu/GrSkSLFPFactoryCache.h"
Robert Phillipsbb606772019-02-04 17:50:57 -050013
Robert Phillips4217ea72019-01-30 13:08:28 -050014static int32_t next_id() {
15 static std::atomic<int32_t> nextID{1};
16 int32_t id;
17 do {
18 id = nextID++;
19 } while (id == SK_InvalidGenID);
20 return id;
21}
22
23GrContext_Base::GrContext_Base(GrBackendApi backend,
Robert Phillipsc1541ae2019-02-04 12:05:37 -050024 const GrContextOptions& options,
Robert Phillipsfd0d9702019-02-01 10:19:42 -050025 uint32_t contextID)
Robert Phillips4217ea72019-01-30 13:08:28 -050026 : fBackend(backend)
Robert Phillipsc1541ae2019-02-04 12:05:37 -050027 , fOptions(options)
Robert Phillipsfd0d9702019-02-01 10:19:42 -050028 , fContextID(SK_InvalidGenID == contextID ? next_id() : contextID) {
Robert Phillips4217ea72019-01-30 13:08:28 -050029}
30
Robert Phillipsc1541ae2019-02-04 12:05:37 -050031GrContext_Base::~GrContext_Base() { }
Robert Phillips4217ea72019-01-30 13:08:28 -050032
Robert Phillipsbb606772019-02-04 17:50:57 -050033bool GrContext_Base::init(sk_sp<const GrCaps> caps, sk_sp<GrSkSLFPFactoryCache> FPFactoryCache) {
34 SkASSERT(caps && FPFactoryCache);
35
36 fCaps = caps;
37 fFPFactoryCache = FPFactoryCache;
38 return true;
39}
Robert Phillips4217ea72019-01-30 13:08:28 -050040
Robert Phillips292a6b22019-02-14 14:49:02 -050041const GrCaps* GrContext_Base::caps() const { return fCaps.get(); }
42sk_sp<const GrCaps> GrContext_Base::refCaps() const { return fCaps; }
43
44sk_sp<GrSkSLFPFactoryCache> GrContext_Base::fpFactoryCache() { return fFPFactoryCache; }
45
Robert Phillipsf7599642019-08-13 16:16:42 -040046GrBackendFormat GrContext_Base::defaultBackendFormat(SkColorType skColorType,
47 GrRenderable renderable) const {
48 const GrCaps* caps = this->caps();
49
50 GrColorType grColorType = SkColorTypeToGrColorType(skColorType);
51
52 GrBackendFormat format = caps->getDefaultBackendFormat(grColorType, renderable);
53 if (!format.isValid()) {
54 return GrBackendFormat();
55 }
56
Greg Daniel7bfc9132019-08-14 14:23:53 -040057 SkASSERT(caps->isFormatTexturableAndUploadable(grColorType, format));
Robert Phillipsf7599642019-08-13 16:16:42 -040058 SkASSERT(renderable == GrRenderable::kNo ||
59 caps->isFormatAsColorTypeRenderable(grColorType, format));
60
61 return format;
62}
63
Robert Phillipsa41c6852019-02-07 10:44:10 -050064///////////////////////////////////////////////////////////////////////////////////////////////////
65sk_sp<const GrCaps> GrBaseContextPriv::refCaps() const {
66 return fContext->refCaps();
67}
68
69sk_sp<GrSkSLFPFactoryCache> GrBaseContextPriv::fpFactoryCache() {
70 return fContext->fpFactoryCache();
71}