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 SkSpecialImage_DEFINED |
| 9 | #define SkSpecialImage_DEFINED |
| 10 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 11 | #include "SkNextID.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 12 | #include "SkRefCnt.h" |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 13 | #include "SkSurfaceProps.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 14 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 15 | #include "SkImageFilter.h" // for OutputProperties |
| 16 | #include "SkImageInfo.h" // for SkAlphaType |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 17 | |
| 18 | class GrContext; |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 19 | class GrTextureProxy; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 20 | class SkBitmap; |
| 21 | class SkCanvas; |
| 22 | class SkImage; |
| 23 | struct SkImageInfo; |
| 24 | class SkPaint; |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 25 | class SkPixmap; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 26 | class SkSpecialSurface; |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 27 | class SkSurface; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 28 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 29 | enum { |
| 30 | kNeedNewImageUniqueID_SpecialImage = 0 |
| 31 | }; |
| 32 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 33 | /** |
| 34 | * This is a restricted form of SkImage solely intended for internal use. It |
| 35 | * differs from SkImage in that: |
| 36 | * - it can only be backed by raster or gpu (no generators) |
Robert Phillips | 2c6d2bf | 2017-02-21 10:19:29 -0500 | [diff] [blame] | 37 | * - it can be backed by a GrTextureProxy larger than its nominal bounds |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 38 | * - it can't be drawn tiled |
| 39 | * - it can't be drawn with MIPMAPs |
| 40 | * It is similar to SkImage in that it abstracts how the pixels are stored/represented. |
| 41 | * |
| 42 | * Note: the contents of the backing storage outside of the subset rect are undefined. |
| 43 | */ |
| 44 | class SkSpecialImage : public SkRefCnt { |
| 45 | public: |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 46 | typedef void* ReleaseContext; |
| 47 | typedef void(*RasterReleaseProc)(void* pixels, ReleaseContext); |
| 48 | |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 49 | const SkSurfaceProps& props() const { return fProps; } |
| 50 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 51 | int width() const { return fSubset.width(); } |
| 52 | int height() const { return fSubset.height(); } |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 53 | const SkIRect& subset() const { return fSubset; } |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 54 | SkColorSpace* getColorSpace() const; |
robertphillips | df7bb47 | 2016-02-19 08:19:40 -0800 | [diff] [blame] | 55 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 56 | uint32_t uniqueID() const { return fUniqueID; } |
brianosman | 80e9608 | 2016-08-16 07:09:47 -0700 | [diff] [blame] | 57 | virtual SkAlphaType alphaType() const = 0; |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 58 | virtual size_t getSize() const = 0; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 59 | |
| 60 | /** |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 61 | * Ensures that a special image is backed by a texture (when GrContext is non-null). If no |
| 62 | * transformation is required, the returned image may be the same as this special image. |
| 63 | * If this special image is from a different GrContext, this will fail. |
| 64 | */ |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 65 | sk_sp<SkSpecialImage> makeTextureImage(GrContext*); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 66 | |
| 67 | /** |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 68 | * Draw this SpecialImage into the canvas. |
| 69 | */ |
robertphillips | e8c3497 | 2016-02-16 12:09:36 -0800 | [diff] [blame] | 70 | void draw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 71 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 72 | static sk_sp<SkSpecialImage> MakeFromImage(const SkIRect& subset, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 73 | sk_sp<SkImage>, |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 74 | SkColorSpace* dstColorSpace, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 75 | const SkSurfaceProps* = nullptr); |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 76 | static sk_sp<SkSpecialImage> MakeFromRaster(const SkIRect& subset, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 77 | const SkBitmap&, |
| 78 | const SkSurfaceProps* = nullptr); |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 79 | #if SK_SUPPORT_GPU |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 80 | static sk_sp<SkSpecialImage> MakeDeferredFromGpu(GrContext*, |
| 81 | const SkIRect& subset, |
| 82 | uint32_t uniqueID, |
Robert Phillips | 63c6746 | 2017-02-15 14:19:01 -0500 | [diff] [blame] | 83 | sk_sp<GrTextureProxy>, |
Robert Phillips | 8bc06d0 | 2016-11-01 17:28:40 -0400 | [diff] [blame] | 84 | sk_sp<SkColorSpace>, |
| 85 | const SkSurfaceProps* = nullptr, |
| 86 | SkAlphaType at = kPremul_SkAlphaType); |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 87 | #endif |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 88 | |
| 89 | /** |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 90 | * Create a new special surface with a backend that is compatible with this special image. |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 91 | */ |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 92 | sk_sp<SkSpecialSurface> makeSurface(const SkImageFilter::OutputProperties& outProps, |
| 93 | const SkISize& size, |
| 94 | SkAlphaType at = kPremul_SkAlphaType) const; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 95 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 96 | /** |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 97 | * Create a new surface with a backend that is compatible with this special image. |
| 98 | * TODO: switch this to makeSurface once we resolved the naming issue |
| 99 | */ |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame] | 100 | sk_sp<SkSurface> makeTightSurface(const SkImageFilter::OutputProperties& outProps, |
| 101 | const SkISize& size, |
| 102 | SkAlphaType at = kPremul_SkAlphaType) const; |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 103 | |
| 104 | /** |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 105 | * Extract a subset of this special image and return it as a special image. |
| 106 | * It may or may not point to the same backing memory. |
| 107 | */ |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 108 | sk_sp<SkSpecialImage> makeSubset(const SkIRect& subset) const; |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 109 | |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 110 | /** |
Robert Phillips | a5fdc97 | 2017-02-18 16:58:09 -0500 | [diff] [blame] | 111 | * Create an SkImage from the contents of this special image optionally extracting a subset. |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 112 | * It may or may not point to the same backing memory. |
Robert Phillips | a5fdc97 | 2017-02-18 16:58:09 -0500 | [diff] [blame] | 113 | * Note: when no 'subset' parameter is specified the the entire SkSpecialImage will be |
| 114 | * returned - including whatever extra padding may have resulted from a loose fit! |
| 115 | * When the 'subset' parameter is specified the returned image will be tight even if that |
| 116 | * entails a copy! |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 117 | */ |
Robert Phillips | a5fdc97 | 2017-02-18 16:58:09 -0500 | [diff] [blame] | 118 | sk_sp<SkImage> asImage(const SkIRect* subset = nullptr) const; |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 119 | |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 120 | /** |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 121 | * If the SpecialImage is backed by a gpu texture, return true. |
| 122 | */ |
| 123 | bool isTextureBacked() const; |
| 124 | |
| 125 | /** |
| 126 | * Return the GrContext if the SkSpecialImage is GrTexture-backed |
| 127 | */ |
| 128 | GrContext* getContext() const; |
| 129 | |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 130 | #if SK_SUPPORT_GPU |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 131 | /** |
Robert Phillips | 8e1c4e6 | 2017-02-19 12:27:01 -0500 | [diff] [blame] | 132 | * Regardless of the underlying backing store, return the contents as a GrTextureProxy. |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 133 | * The active portion of the texture can be retrieved via 'subset'. |
| 134 | */ |
Robert Phillips | 8e1c4e6 | 2017-02-19 12:27:01 -0500 | [diff] [blame] | 135 | sk_sp<GrTextureProxy> asTextureProxyRef(GrContext*) const; |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 136 | #endif |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 137 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 138 | /** |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 139 | * Regardless of the underlying backing store, return the contents as an SkBitmap |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 140 | * |
| 141 | * The returned ImageInfo represents the backing memory. Use 'subset' |
| 142 | * to get the active portion's dimensions. |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 143 | */ |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 144 | bool getROPixels(SkBitmap*) const; |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 145 | |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 146 | protected: |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 147 | SkSpecialImage(const SkIRect& subset, uint32_t uniqueID, const SkSurfaceProps*); |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 148 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 149 | private: |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 150 | const SkSurfaceProps fProps; |
| 151 | const SkIRect fSubset; |
| 152 | const uint32_t fUniqueID; |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 153 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 154 | typedef SkRefCnt INHERITED; |
| 155 | }; |
| 156 | |
| 157 | #endif |