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