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 "SkImageInfo.h" 12 #include "SkRefCnt.h" 13 #include "SkSurfaceProps.h" 14 15 #if SK_SUPPORT_GPU 16 #include "GrTypesPriv.h" 17 #endif 18 19 class GrBackendFormat; 20 class GrContext; 21 class SkBitmap; 22 class SkCanvas; 23 class SkSpecialImage; 24 25 /** 26 * SkSpecialSurface is a restricted form of SkSurface solely for internal use. It differs 27 * from SkSurface in that: 28 * - it can be backed by GrTextures larger than [ fWidth, fHeight ] 29 * - it can't be used for tiling 30 * - it becomes inactive once a snapshot of it is taken (i.e., no copy-on-write) 31 * - it has no generation ID 32 */ 33 class SkSpecialSurface : public SkRefCnt { 34 public: props()35 const SkSurfaceProps& props() const { return fProps; } 36 width()37 int width() const { return fSubset.width(); } height()38 int height() const { return fSubset.height(); } 39 40 /** 41 * Return a canvas that will draw into this surface. This will always 42 * return the same canvas for a given surface, and is managed/owned by the 43 * surface. 44 * 45 * The canvas will be invalid after 'newImageSnapshot' is called. 46 */ 47 SkCanvas* getCanvas(); 48 49 /** 50 * Returns an image of the current state of the surface pixels up to this 51 * point. The canvas returned by 'getCanvas' becomes invalidated by this 52 * call and no more drawing to this surface is allowed. 53 * 54 * Note: the caller inherits a ref from this call that must be balanced 55 */ 56 sk_sp<SkSpecialImage> makeImageSnapshot(); 57 58 #if SK_SUPPORT_GPU 59 /** 60 * Allocate a new GPU-backed SkSpecialSurface. If the requested surface cannot 61 * be created, nullptr will be returned. 62 */ 63 static sk_sp<SkSpecialSurface> MakeRenderTarget(GrContext*, 64 const GrBackendFormat& format, 65 int width, int height, 66 GrPixelConfig config, 67 sk_sp<SkColorSpace> colorSpace, 68 const SkSurfaceProps* = nullptr); 69 #endif 70 71 /** 72 * Use and existing SkBitmap as the backing store. 73 */ 74 static sk_sp<SkSpecialSurface> MakeFromBitmap(const SkIRect& subset, SkBitmap& bm, 75 const SkSurfaceProps* = nullptr); 76 77 /** 78 * Return a new CPU-backed surface, with the memory for the pixels automatically 79 * allocated. 80 * 81 * If the requested surface cannot be created, or the request is not a 82 * supported configuration, nullptr will be returned. 83 */ 84 static sk_sp<SkSpecialSurface> MakeRaster(const SkImageInfo&, 85 const SkSurfaceProps* = nullptr); 86 87 protected: 88 SkSpecialSurface(const SkIRect& subset, const SkSurfaceProps*); 89 90 // For testing only 91 friend class TestingSpecialSurfaceAccess; subset()92 const SkIRect& subset() const { return fSubset; } 93 94 private: 95 const SkSurfaceProps fProps; 96 const SkIRect fSubset; 97 98 typedef SkRefCnt INHERITED; 99 }; 100 101 #endif 102