1 /* 2 * Copyright 2021 Google LLC 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 skgpu_Context_DEFINED 9 #define skgpu_Context_DEFINED 10 11 #include <vector> 12 #include "include/core/SkBlendMode.h" 13 #include "include/core/SkRefCnt.h" 14 #include "include/core/SkShader.h" 15 #include "include/core/SkTileMode.h" 16 #include "include/private/SkNoncopyable.h" 17 18 #include "experimental/graphite/include/GraphiteTypes.h" 19 20 namespace skgpu { 21 22 class BackendTexture; 23 class ContextPriv; 24 class GlobalCache; 25 class Gpu; 26 class Recorder; 27 class Recording; 28 class TextureInfo; 29 namespace mtl { struct BackendContext; } 30 31 struct ShaderCombo { 32 enum class ShaderType { 33 kNone, // does not modify color buffer, e.g. depth and/or stencil only 34 kSolidColor, 35 kLinearGradient, 36 kRadialGradient, 37 kSweepGradient, 38 kConicalGradient 39 }; 40 ShaderComboShaderCombo41 ShaderCombo() {} ShaderComboShaderCombo42 ShaderCombo(std::vector<ShaderType> types, 43 std::vector<SkTileMode> tileModes) 44 : fTypes(std::move(types)) 45 , fTileModes(std::move(tileModes)) { 46 } 47 std::vector<ShaderType> fTypes; 48 std::vector<SkTileMode> fTileModes; 49 }; 50 51 struct PaintCombo { 52 std::vector<ShaderCombo> fShaders; 53 std::vector<SkBlendMode> fBlendModes; 54 }; 55 56 class Context final { 57 public: 58 Context(const Context&) = delete; 59 Context(Context&&) = delete; 60 Context& operator=(const Context&) = delete; 61 Context& operator=(Context&&) = delete; 62 63 ~Context(); 64 65 #ifdef SK_METAL 66 static std::unique_ptr<Context> MakeMetal(const skgpu::mtl::BackendContext&); 67 #endif 68 backend()69 BackendApi backend() const { return fBackend; } 70 71 std::unique_ptr<Recorder> makeRecorder(); 72 73 void insertRecording(std::unique_ptr<Recording>); 74 void submit(SyncToCpu = SyncToCpu::kNo); 75 76 void preCompile(const PaintCombo&); 77 78 /** 79 * Creates a new backend gpu texture matching the dimensinos and TextureInfo. If an invalid 80 * TextureInfo or a TextureInfo Skia can't support is passed in, this will return an invalid 81 * BackendTexture. Thus the client should check isValid on the returned BackendTexture to know 82 * if it succeeded or not. 83 * 84 * If this does return a valid BackendTexture, the caller is required to use 85 * Context::deleteBackendTexture to delete that texture. 86 */ 87 BackendTexture createBackendTexture(SkISize dimensions, const TextureInfo&); 88 89 /** 90 * Called to delete the passed in BackendTexture. This should only be called if the 91 * BackendTexture was created by calling Context::createBackendTexture. If the BackendTexture is 92 * not valid or does not match the BackendApi of the Context then nothing happens. 93 * 94 * Otherwise this will delete/release the backend object that is wrapped in the BackendTexture. 95 * The BackendTexture will be reset to an invalid state and should not be used again. 96 */ 97 void deleteBackendTexture(BackendTexture&); 98 99 // Provides access to functions that aren't part of the public API. 100 ContextPriv priv(); 101 const ContextPriv priv() const; // NOLINT(readability-const-return-type) 102 103 protected: 104 Context(sk_sp<Gpu>, BackendApi); 105 106 private: 107 friend class ContextPriv; 108 109 std::vector<std::unique_ptr<Recording>> fRecordings; 110 sk_sp<Gpu> fGpu; 111 sk_sp<GlobalCache> fGlobalCache; 112 BackendApi fBackend; 113 }; 114 115 } // namespace skgpu 116 117 #endif // skgpu_Context_DEFINED 118