1 /* 2 * Copyright 2016 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 SkSpecialSurface_DEFINED 9 #define SkSpecialSurface_DEFINED 10 11 #include "include/core/SkCanvas.h" 12 #include "include/core/SkImageInfo.h" 13 #include "include/core/SkRefCnt.h" 14 #include "include/core/SkSurfaceProps.h" 15 16 #if defined(SK_GANESH) 17 #include "include/private/gpu/ganesh/GrTypesPriv.h" 18 #endif 19 20 #if SK_GRAPHITE 21 namespace skgpu::graphite { 22 class Recorder; 23 } 24 #endif 25 26 class GrBackendFormat; 27 class GrRecordingContext; 28 class SkBaseDevice; 29 class SkBitmap; 30 class SkCanvas; 31 class SkSpecialImage; 32 33 /** 34 * SkSpecialSurface is a restricted form of SkSurface solely for internal use. It differs 35 * from SkSurface in that: 36 * - it can be backed by GrTextures larger than [ fWidth, fHeight ] 37 * - it can't be used for tiling 38 * - it becomes inactive once a snapshot of it is taken (i.e., no copy-on-write) 39 * - it has no generation ID 40 */ 41 class SkSpecialSurface : public SkRefCnt { 42 public: 43 SkSpecialSurface(sk_sp<SkBaseDevice>, const SkIRect& subset); 44 45 #ifdef SK_DEBUG props()46 SkSurfaceProps props() const { return fCanvas->getBaseProps(); } 47 #endif 48 subset()49 const SkIRect& subset() const { return fSubset; } width()50 int width() const { return fSubset.width(); } height()51 int height() const { return fSubset.height(); } 52 53 /** 54 * Return a canvas that will draw into this special surface. This will always 55 * return the same canvas for a given special surface, and is managed/owned by the 56 * special surface. 57 * 58 * The canvas will be invalid after 'makeImageSnapshot' is called. 59 */ getCanvas()60 SkCanvas* getCanvas() { return fCanvas.get(); } 61 62 /** 63 * Returns an image of the current state of the surface pixels up to this 64 * point. The canvas returned by 'getCanvas' becomes invalidated by this 65 * call and no more drawing to this surface is allowed. 66 */ 67 sk_sp<SkSpecialImage> makeImageSnapshot(); 68 69 #if defined(SK_GANESH) 70 /** 71 * Allocate a new GPU-backed SkSpecialSurface. If the requested surface cannot 72 * be created, nullptr will be returned. 73 */ 74 static sk_sp<SkSpecialSurface> MakeRenderTarget(GrRecordingContext*, 75 const SkImageInfo&, 76 const SkSurfaceProps&, 77 GrSurfaceOrigin); 78 #endif 79 80 #if SK_GRAPHITE 81 static sk_sp<SkSpecialSurface> MakeGraphite(skgpu::graphite::Recorder*, 82 const SkImageInfo&, 83 const SkSurfaceProps&); 84 #endif 85 86 /** 87 * Return a new CPU-backed surface, with the memory for the pixels automatically 88 * allocated. 89 * 90 * If the requested surface cannot be created, or the request is not a 91 * supported configuration, nullptr will be returned. 92 */ 93 static sk_sp<SkSpecialSurface> MakeRaster(const SkImageInfo&, 94 const SkSurfaceProps&); 95 96 private: 97 std::unique_ptr<SkCanvas> fCanvas; 98 const SkIRect fSubset; 99 }; 100 101 #endif 102