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 #ifndef GrContext_Base_DEFINED 9 #define GrContext_Base_DEFINED 10 11 #include "include/core/SkRefCnt.h" 12 #include "include/gpu/GrBackendSurface.h" 13 #include "include/gpu/GrContextOptions.h" 14 #include "include/gpu/GrTypes.h" 15 16 class GrBaseContextPriv; 17 class GrCaps; 18 class GrContextThreadSafeProxy; 19 class GrDirectContext; 20 class GrImageContext; 21 class GrRecordingContext; 22 23 class GrContext_Base : public SkRefCnt { 24 public: 25 ~GrContext_Base() override; 26 27 /* 28 * Safely downcast to a GrDirectContext. 29 */ asDirectContext()30 virtual GrDirectContext* asDirectContext() { return nullptr; } 31 32 /* 33 * The 3D API backing this context 34 */ 35 SK_API GrBackendApi backend() const; 36 37 /* 38 * Retrieve the default GrBackendFormat for a given SkColorType and renderability. 39 * It is guaranteed that this backend format will be the one used by the GrContext 40 * SkColorType and SkSurfaceCharacterization-based createBackendTexture methods. 41 * 42 * The caller should check that the returned format is valid. 43 */ 44 SK_API GrBackendFormat defaultBackendFormat(SkColorType, GrRenderable) const; 45 46 SK_API GrBackendFormat compressedBackendFormat(SkImage::CompressionType) const; 47 48 /** 49 * Gets the maximum supported sample count for a color type. 1 is returned if only non-MSAA 50 * rendering is supported for the color type. 0 is returned if rendering to this color type 51 * is not supported at all. 52 */ 53 SK_API int maxSurfaceSampleCountForColorType(SkColorType colorType) const; 54 55 // TODO: When the public version is gone, rename to refThreadSafeProxy and add raw ptr ver. 56 sk_sp<GrContextThreadSafeProxy> threadSafeProxy(); 57 58 // Provides access to functions that aren't part of the public API. 59 GrBaseContextPriv priv(); 60 const GrBaseContextPriv priv() const; // NOLINT(readability-const-return-type) 61 62 protected: 63 friend class GrBaseContextPriv; // for hidden functions 64 65 GrContext_Base(sk_sp<GrContextThreadSafeProxy>); 66 67 virtual bool init(); 68 69 /** 70 * An identifier for this context. The id is used by all compatible contexts. For example, 71 * if SkImages are created on one thread using an image creation context, then fed into a 72 * DDL Recorder on second thread (which has a recording context) and finally replayed on 73 * a third thread with a direct context, then all three contexts will report the same id. 74 * It is an error for an image to be used with contexts that report different ids. 75 */ 76 uint32_t contextID() const; 77 matches(GrContext_Base * candidate)78 bool matches(GrContext_Base* candidate) const { 79 return candidate && candidate->contextID() == this->contextID(); 80 } 81 82 /* 83 * The options in effect for this context 84 */ 85 const GrContextOptions& options() const; 86 87 const GrCaps* caps() const; 88 sk_sp<const GrCaps> refCaps() const; 89 asImageContext()90 virtual GrImageContext* asImageContext() { return nullptr; } asRecordingContext()91 virtual GrRecordingContext* asRecordingContext() { return nullptr; } 92 93 sk_sp<GrContextThreadSafeProxy> fThreadSafeProxy; 94 95 private: 96 using INHERITED = SkRefCnt; 97 }; 98 99 #endif 100