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 | */ |
reed | ad7604b | 2016-07-20 16:13:32 -0700 | [diff] [blame] | 7 | |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 8 | #include "SkSpecialImage.h" |
reed | ad7604b | 2016-07-20 16:13:32 -0700 | [diff] [blame] | 9 | #include "SkBitmap.h" |
| 10 | #include "SkImage.h" |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 11 | #include "SkBitmapCache.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 12 | #include "SkCanvas.h" |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame] | 13 | #include "SkImage_Base.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 14 | #include "SkSpecialSurface.h" |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 15 | #include "SkSurfacePriv.h" |
reed | ad7604b | 2016-07-20 16:13:32 -0700 | [diff] [blame] | 16 | #include "SkPixelRef.h" |
| 17 | |
| 18 | #if SK_SUPPORT_GPU |
| 19 | #include "GrContext.h" |
| 20 | #include "GrTexture.h" |
| 21 | #include "GrTextureParams.h" |
| 22 | #include "SkGr.h" |
reed | ad7604b | 2016-07-20 16:13:32 -0700 | [diff] [blame] | 23 | #include "SkGrPriv.h" |
| 24 | #endif |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 25 | |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 26 | // Currently the raster imagefilters can only handle certain imageinfos. Call this to know if |
| 27 | // a given info is supported. |
| 28 | static bool valid_for_imagefilters(const SkImageInfo& info) { |
| 29 | // no support for other swizzles/depths yet |
| 30 | return info.colorType() == kN32_SkColorType; |
| 31 | } |
| 32 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 33 | /////////////////////////////////////////////////////////////////////////////// |
| 34 | class SkSpecialImage_Base : public SkSpecialImage { |
| 35 | public: |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 36 | SkSpecialImage_Base(const SkIRect& subset, uint32_t uniqueID, const SkSurfaceProps* props) |
| 37 | : INHERITED(subset, uniqueID, props) { |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 38 | } |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 39 | ~SkSpecialImage_Base() override { } |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 40 | |
robertphillips | e8c3497 | 2016-02-16 12:09:36 -0800 | [diff] [blame] | 41 | virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const = 0; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 42 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 43 | virtual bool onGetROPixels(SkBitmap*) const = 0; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 44 | |
| 45 | virtual GrTexture* onPeekTexture() const { return nullptr; } |
| 46 | |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 47 | virtual SkColorSpace* onGetColorSpace() const = 0; |
| 48 | |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 49 | #if SK_SUPPORT_GPU |
| 50 | virtual sk_sp<GrTexture> onAsTextureRef(GrContext* context) const = 0; |
| 51 | #endif |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 52 | |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 53 | virtual sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const = 0; |
| 54 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame^] | 55 | virtual sk_sp<SkSpecialSurface> onMakeSurface(const SkImageFilter::OutputProperties& outProps, |
| 56 | const SkISize& size, SkAlphaType at) const = 0; |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 57 | |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 58 | virtual sk_sp<SkImage> onMakeTightSubset(const SkIRect& subset) const = 0; |
| 59 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame^] | 60 | virtual sk_sp<SkSurface> onMakeTightSurface(const SkImageFilter::OutputProperties& outProps, |
| 61 | const SkISize& size, SkAlphaType at) const = 0; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 62 | |
| 63 | private: |
| 64 | typedef SkSpecialImage INHERITED; |
| 65 | }; |
| 66 | |
| 67 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame] | 68 | static inline const SkSpecialImage_Base* as_SIB(const SkSpecialImage* image) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 69 | return static_cast<const SkSpecialImage_Base*>(image); |
| 70 | } |
| 71 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 72 | SkSpecialImage::SkSpecialImage(const SkIRect& subset, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 73 | uint32_t uniqueID, |
| 74 | const SkSurfaceProps* props) |
| 75 | : fProps(SkSurfacePropsCopyOrDefault(props)) |
| 76 | , fSubset(subset) |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 77 | , fUniqueID(kNeedNewImageUniqueID_SpecialImage == uniqueID ? SkNextID::ImageID() : uniqueID) { |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 78 | } |
| 79 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 80 | sk_sp<SkSpecialImage> SkSpecialImage::makeTextureImage(GrContext* context) { |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 81 | #if SK_SUPPORT_GPU |
| 82 | if (!context) { |
| 83 | return nullptr; |
| 84 | } |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 85 | if (GrTexture* peek = as_SIB(this)->onPeekTexture()) { |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 86 | return peek->getContext() == context ? sk_sp<SkSpecialImage>(SkRef(this)) : nullptr; |
| 87 | } |
| 88 | |
| 89 | SkBitmap bmp; |
reed | cf5c846 | 2016-07-20 12:28:40 -0700 | [diff] [blame] | 90 | // At this point, we are definitely not texture-backed, so we must be raster or generator |
| 91 | // backed. If we remove the special-wrapping-an-image subclass, we may be able to assert that |
| 92 | // we are strictly raster-backed (i.e. generator images become raster when they are specialized) |
| 93 | // in which case getROPixels could turn into peekPixels... |
| 94 | if (!this->getROPixels(&bmp)) { |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 95 | return nullptr; |
| 96 | } |
| 97 | |
robertphillips | 83f2e5a | 2016-03-24 06:31:25 -0700 | [diff] [blame] | 98 | if (bmp.empty()) { |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 99 | return SkSpecialImage::MakeFromRaster(SkIRect::MakeEmpty(), bmp, &this->props()); |
robertphillips | 83f2e5a | 2016-03-24 06:31:25 -0700 | [diff] [blame] | 100 | } |
| 101 | |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 102 | sk_sp<GrTexture> resultTex(GrRefCachedBitmapTexture(context, |
| 103 | bmp, |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 104 | GrTextureParams::ClampNoFilter(), |
| 105 | SkSourceGammaTreatment::kRespect)); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 106 | if (!resultTex) { |
| 107 | return nullptr; |
| 108 | } |
| 109 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 110 | return SkSpecialImage::MakeFromGpu(SkIRect::MakeWH(resultTex->width(), resultTex->height()), |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 111 | this->uniqueID(), |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 112 | resultTex, sk_ref_sp(this->getColorSpace()), &this->props(), |
brianosman | 80e9608 | 2016-08-16 07:09:47 -0700 | [diff] [blame] | 113 | this->alphaType()); |
robertphillips | 83c17fa | 2016-03-18 08:14:27 -0700 | [diff] [blame] | 114 | #else |
| 115 | return nullptr; |
| 116 | #endif |
| 117 | } |
| 118 | |
robertphillips | e8c3497 | 2016-02-16 12:09:36 -0800 | [diff] [blame] | 119 | void SkSpecialImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const { |
bsalomon | 84a4e5a | 2016-02-29 11:41:52 -0800 | [diff] [blame] | 120 | return as_SIB(this)->onDraw(canvas, x, y, paint); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 121 | } |
| 122 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 123 | bool SkSpecialImage::getROPixels(SkBitmap* bm) const { |
| 124 | return as_SIB(this)->onGetROPixels(bm); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 125 | } |
| 126 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 127 | bool SkSpecialImage::isTextureBacked() const { |
| 128 | #if SK_SUPPORT_GPU |
| 129 | return as_SIB(this)->onPeekTexture() && as_SIB(this)->onPeekTexture()->getContext(); |
| 130 | #else |
| 131 | return false; |
| 132 | #endif |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 133 | } |
| 134 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 135 | GrContext* SkSpecialImage::getContext() const { |
| 136 | #if SK_SUPPORT_GPU |
| 137 | GrTexture* texture = as_SIB(this)->onPeekTexture(); |
| 138 | |
| 139 | if (texture) { |
| 140 | return texture->getContext(); |
| 141 | } |
| 142 | #endif |
| 143 | return nullptr; |
| 144 | } |
| 145 | |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 146 | SkColorSpace* SkSpecialImage::getColorSpace() const { |
| 147 | return as_SIB(this)->onGetColorSpace(); |
| 148 | } |
| 149 | |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 150 | #if SK_SUPPORT_GPU |
| 151 | sk_sp<GrTexture> SkSpecialImage::asTextureRef(GrContext* context) const { |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 152 | return as_SIB(this)->onAsTextureRef(context); |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 153 | } |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 154 | #endif |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 155 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame^] | 156 | sk_sp<SkSpecialSurface> SkSpecialImage::makeSurface(const SkImageFilter::OutputProperties& outProps, |
| 157 | const SkISize& size, SkAlphaType at) const { |
| 158 | return as_SIB(this)->onMakeSurface(outProps, size, at); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 159 | } |
| 160 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame^] | 161 | sk_sp<SkSurface> SkSpecialImage::makeTightSurface(const SkImageFilter::OutputProperties& outProps, |
| 162 | const SkISize& size, SkAlphaType at) const { |
| 163 | return as_SIB(this)->onMakeTightSurface(outProps, size, at); |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 164 | } |
| 165 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 166 | sk_sp<SkSpecialImage> SkSpecialImage::makeSubset(const SkIRect& subset) const { |
| 167 | return as_SIB(this)->onMakeSubset(subset); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 168 | } |
| 169 | |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 170 | sk_sp<SkImage> SkSpecialImage::makeTightSubset(const SkIRect& subset) const { |
| 171 | return as_SIB(this)->onMakeTightSubset(subset); |
| 172 | } |
| 173 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 174 | #ifdef SK_DEBUG |
| 175 | static bool rect_fits(const SkIRect& rect, int width, int height) { |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 176 | if (0 == width && 0 == height) { |
| 177 | SkASSERT(0 == rect.fLeft && 0 == rect.fRight && 0 == rect.fTop && 0 == rect.fBottom); |
| 178 | return true; |
| 179 | } |
| 180 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 181 | return rect.fLeft >= 0 && rect.fLeft < width && rect.fLeft < rect.fRight && |
| 182 | rect.fRight >= 0 && rect.fRight <= width && |
| 183 | rect.fTop >= 0 && rect.fTop < height && rect.fTop < rect.fBottom && |
| 184 | rect.fBottom >= 0 && rect.fBottom <= height; |
| 185 | } |
| 186 | #endif |
| 187 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 188 | sk_sp<SkSpecialImage> SkSpecialImage::MakeFromImage(const SkIRect& subset, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 189 | sk_sp<SkImage> image, |
| 190 | const SkSurfaceProps* props) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 191 | SkASSERT(rect_fits(subset, image->width(), image->height())); |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 192 | |
reed | ad7604b | 2016-07-20 16:13:32 -0700 | [diff] [blame] | 193 | #if SK_SUPPORT_GPU |
| 194 | if (GrTexture* texture = as_IB(image)->peekTexture()) { |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 195 | return MakeFromGpu(subset, image->uniqueID(), sk_ref_sp(texture), |
| 196 | sk_ref_sp(as_IB(image)->onImageInfo().colorSpace()), props); |
reed | ad7604b | 2016-07-20 16:13:32 -0700 | [diff] [blame] | 197 | } else |
| 198 | #endif |
| 199 | { |
| 200 | SkBitmap bm; |
| 201 | if (as_IB(image)->getROPixels(&bm)) { |
| 202 | return MakeFromRaster(subset, bm, props); |
| 203 | } |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 204 | } |
reed | ad7604b | 2016-07-20 16:13:32 -0700 | [diff] [blame] | 205 | return nullptr; |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /////////////////////////////////////////////////////////////////////////////// |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 209 | |
| 210 | class SkSpecialImage_Raster : public SkSpecialImage_Base { |
| 211 | public: |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 212 | SkSpecialImage_Raster(const SkIRect& subset, const SkBitmap& bm, const SkSurfaceProps* props) |
| 213 | : INHERITED(subset, bm.getGenerationID(), props) |
reed | bd2bd5c | 2016-07-25 14:26:02 -0700 | [diff] [blame] | 214 | , fBitmap(bm) |
| 215 | { |
| 216 | SkASSERT(bm.pixelRef()); |
| 217 | |
| 218 | // We have to lock now, while bm is still in scope, since it may have come from our |
| 219 | // cache, which means we need to keep it locked until we (the special) are done, since |
| 220 | // we cannot re-generate the cache entry (if bm came from a generator). |
| 221 | fBitmap.lockPixels(); |
| 222 | SkASSERT(fBitmap.getPixels()); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 223 | } |
| 224 | |
brianosman | 80e9608 | 2016-08-16 07:09:47 -0700 | [diff] [blame] | 225 | SkAlphaType alphaType() const override { return fBitmap.alphaType(); } |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 226 | |
| 227 | size_t getSize() const override { return fBitmap.getSize(); } |
| 228 | |
robertphillips | e8c3497 | 2016-02-16 12:09:36 -0800 | [diff] [blame] | 229 | void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 230 | SkRect dst = SkRect::MakeXYWH(x, y, |
| 231 | this->subset().width(), this->subset().height()); |
| 232 | |
| 233 | canvas->drawBitmapRect(fBitmap, this->subset(), |
| 234 | dst, paint, SkCanvas::kStrict_SrcRectConstraint); |
| 235 | } |
| 236 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 237 | bool onGetROPixels(SkBitmap* bm) const override { |
| 238 | *bm = fBitmap; |
robertphillips | 3b087f4 | 2016-02-18 08:48:03 -0800 | [diff] [blame] | 239 | return true; |
| 240 | } |
| 241 | |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 242 | SkColorSpace* onGetColorSpace() const override { |
| 243 | return fBitmap.colorSpace(); |
| 244 | } |
| 245 | |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 246 | #if SK_SUPPORT_GPU |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 247 | sk_sp<GrTexture> onAsTextureRef(GrContext* context) const override { |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 248 | if (context) { |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 249 | return sk_ref_sp(GrRefCachedBitmapTexture(context, |
| 250 | fBitmap, |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 251 | GrTextureParams::ClampNoFilter(), |
| 252 | SkSourceGammaTreatment::kRespect)); |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 253 | } |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 254 | |
| 255 | return nullptr; |
| 256 | } |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 257 | #endif |
robertphillips | 6461251 | 2016-04-08 12:10:42 -0700 | [diff] [blame] | 258 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame^] | 259 | // TODO: The raster implementations of image filters all currently assume that the pixels are |
| 260 | // legacy N32. Until they actually check the format and operate on sRGB or F16 data appropriately, |
| 261 | // we can't enable this. (They will continue to produce incorrect results, but less-so). |
| 262 | #define RASTER_IMAGE_FILTERS_SUPPORT_SRGB_AND_F16 0 |
| 263 | |
| 264 | sk_sp<SkSpecialSurface> onMakeSurface(const SkImageFilter::OutputProperties& outProps, |
| 265 | const SkISize& size, SkAlphaType at) const override { |
| 266 | #if RASTER_IMAGE_FILTERS_SUPPORT_SRGB_AND_F16 |
| 267 | SkColorSpace* colorSpace = outProps.colorSpace(); |
| 268 | #else |
| 269 | SkColorSpace* colorSpace = nullptr; |
| 270 | #endif |
| 271 | SkColorType colorType = colorSpace && colorSpace->gammaIsLinear() |
| 272 | ? kRGBA_F16_SkColorType : kN32_SkColorType; |
| 273 | SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), colorType, at, |
| 274 | sk_ref_sp(colorSpace)); |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 275 | return SkSpecialSurface::MakeRaster(info, nullptr); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 276 | } |
| 277 | |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 278 | sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const override { |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 279 | SkBitmap subsetBM; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 280 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 281 | if (!fBitmap.extractSubset(&subsetBM, subset)) { |
| 282 | return nullptr; |
| 283 | } |
| 284 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 285 | return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(subset.width(), subset.height()), |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 286 | subsetBM, |
| 287 | &this->props()); |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 288 | } |
| 289 | |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 290 | sk_sp<SkImage> onMakeTightSubset(const SkIRect& subset) const override { |
| 291 | SkBitmap subsetBM; |
| 292 | |
| 293 | if (!fBitmap.extractSubset(&subsetBM, subset)) { |
| 294 | return nullptr; |
| 295 | } |
| 296 | |
| 297 | return SkImage::MakeFromBitmap(subsetBM); |
| 298 | } |
| 299 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame^] | 300 | sk_sp<SkSurface> onMakeTightSurface(const SkImageFilter::OutputProperties& outProps, |
| 301 | const SkISize& size, SkAlphaType at) const override { |
| 302 | #if RASTER_IMAGE_FILTERS_SUPPORT_SRGB_AND_F16 |
| 303 | SkColorSpace* colorSpace = outProps.colorSpace(); |
| 304 | #else |
| 305 | SkColorSpace* colorSpace = nullptr; |
| 306 | #endif |
| 307 | SkColorType colorType = colorSpace && colorSpace->gammaIsLinear() |
| 308 | ? kRGBA_F16_SkColorType : kN32_SkColorType; |
| 309 | SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), colorType, at, |
| 310 | sk_ref_sp(colorSpace)); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 311 | return SkSurface::MakeRaster(info); |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 312 | } |
| 313 | |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 314 | private: |
| 315 | SkBitmap fBitmap; |
| 316 | |
| 317 | typedef SkSpecialImage_Base INHERITED; |
| 318 | }; |
| 319 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 320 | sk_sp<SkSpecialImage> SkSpecialImage::MakeFromRaster(const SkIRect& subset, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 321 | const SkBitmap& bm, |
| 322 | const SkSurfaceProps* props) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 323 | SkASSERT(rect_fits(subset, bm.width(), bm.height())); |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 324 | |
reed | bd2bd5c | 2016-07-25 14:26:02 -0700 | [diff] [blame] | 325 | if (!bm.pixelRef()) { |
| 326 | return nullptr; |
| 327 | } |
| 328 | |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 329 | const SkBitmap* srcBM = &bm; |
| 330 | SkBitmap tmpStorage; |
| 331 | // ImageFilters only handle N32 at the moment, so force our src to be that |
| 332 | if (!valid_for_imagefilters(bm.info())) { |
| 333 | if (!bm.copyTo(&tmpStorage, kN32_SkColorType)) { |
| 334 | return nullptr; |
| 335 | } |
| 336 | srcBM = &tmpStorage; |
| 337 | } |
| 338 | return sk_make_sp<SkSpecialImage_Raster>(subset, *srcBM, props); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | #if SK_SUPPORT_GPU |
| 342 | /////////////////////////////////////////////////////////////////////////////// |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 343 | #include "GrTexture.h" |
robertphillips | b4bd11e | 2016-03-21 13:44:18 -0700 | [diff] [blame] | 344 | #include "SkImage_Gpu.h" |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 345 | |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 346 | class SkSpecialImage_Gpu : public SkSpecialImage_Base { |
| 347 | public: |
| 348 | SkSpecialImage_Gpu(const SkIRect& subset, |
| 349 | uint32_t uniqueID, sk_sp<GrTexture> tex, SkAlphaType at, |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 350 | sk_sp<SkColorSpace> colorSpace, const SkSurfaceProps* props) |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 351 | : INHERITED(subset, uniqueID, props) |
| 352 | , fTexture(std::move(tex)) |
| 353 | , fAlphaType(at) |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 354 | , fColorSpace(std::move(colorSpace)) |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 355 | , fAddedRasterVersionToCache(false) { |
| 356 | } |
| 357 | |
| 358 | ~SkSpecialImage_Gpu() override { |
| 359 | if (fAddedRasterVersionToCache.load()) { |
| 360 | SkNotifyBitmapGenIDIsStale(this->uniqueID()); |
| 361 | } |
| 362 | } |
| 363 | |
brianosman | 80e9608 | 2016-08-16 07:09:47 -0700 | [diff] [blame] | 364 | SkAlphaType alphaType() const override { return fAlphaType; } |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 365 | |
| 366 | size_t getSize() const override { return fTexture->gpuMemorySize(); } |
| 367 | |
| 368 | void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override { |
| 369 | SkRect dst = SkRect::MakeXYWH(x, y, |
| 370 | this->subset().width(), this->subset().height()); |
| 371 | |
reed | 77d6f7d | 2016-07-13 12:24:48 -0700 | [diff] [blame] | 372 | auto img = sk_sp<SkImage>(new SkImage_Gpu(fTexture->width(), fTexture->height(), |
| 373 | this->uniqueID(), fAlphaType, fTexture.get(), |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 374 | fColorSpace, SkBudgeted::kNo)); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 375 | |
reed | 77d6f7d | 2016-07-13 12:24:48 -0700 | [diff] [blame] | 376 | canvas->drawImageRect(img, this->subset(), |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 377 | dst, paint, SkCanvas::kStrict_SrcRectConstraint); |
| 378 | } |
| 379 | |
| 380 | GrTexture* onPeekTexture() const override { return fTexture.get(); } |
| 381 | |
| 382 | sk_sp<GrTexture> onAsTextureRef(GrContext*) const override { return fTexture; } |
| 383 | |
| 384 | bool onGetROPixels(SkBitmap* dst) const override { |
| 385 | if (SkBitmapCache::Find(this->uniqueID(), dst)) { |
| 386 | SkASSERT(dst->getGenerationID() == this->uniqueID()); |
| 387 | SkASSERT(dst->isImmutable()); |
| 388 | SkASSERT(dst->getPixels()); |
| 389 | return true; |
| 390 | } |
| 391 | |
| 392 | SkImageInfo info = SkImageInfo::MakeN32(this->width(), this->height(), |
brianosman | 80e9608 | 2016-08-16 07:09:47 -0700 | [diff] [blame] | 393 | this->alphaType(), fColorSpace); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 394 | |
| 395 | if (!dst->tryAllocPixels(info)) { |
| 396 | return false; |
| 397 | } |
| 398 | |
| 399 | if (!fTexture->readPixels(0, 0, dst->width(), dst->height(), kSkia8888_GrPixelConfig, |
| 400 | dst->getPixels(), dst->rowBytes())) { |
| 401 | return false; |
| 402 | } |
| 403 | |
| 404 | dst->pixelRef()->setImmutableWithID(this->uniqueID()); |
| 405 | SkBitmapCache::Add(this->uniqueID(), *dst); |
| 406 | fAddedRasterVersionToCache.store(true); |
| 407 | return true; |
| 408 | } |
| 409 | |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 410 | SkColorSpace* onGetColorSpace() const override { |
| 411 | return fColorSpace.get(); |
| 412 | } |
| 413 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame^] | 414 | sk_sp<SkSpecialSurface> onMakeSurface(const SkImageFilter::OutputProperties& outProps, |
| 415 | const SkISize& size, SkAlphaType at) const override { |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 416 | if (!fTexture->getContext()) { |
| 417 | return nullptr; |
| 418 | } |
| 419 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame^] | 420 | SkColorSpace* colorSpace = outProps.colorSpace(); |
| 421 | return SkSpecialSurface::MakeRenderTarget( |
| 422 | fTexture->getContext(), size.width(), size.height(), |
| 423 | GrRenderableConfigForColorSpace(colorSpace), sk_ref_sp(colorSpace)); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const override { |
| 427 | return SkSpecialImage::MakeFromGpu(subset, |
| 428 | this->uniqueID(), |
| 429 | fTexture, |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 430 | fColorSpace, |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 431 | &this->props(), |
| 432 | fAlphaType); |
| 433 | } |
| 434 | |
| 435 | sk_sp<SkImage> onMakeTightSubset(const SkIRect& subset) const override { |
| 436 | if (0 == subset.fLeft && 0 == subset.fTop && |
| 437 | fTexture->width() == subset.width() && |
| 438 | fTexture->height() == subset.height()) { |
| 439 | // The existing GrTexture is already tight so reuse it in the SkImage |
| 440 | return sk_make_sp<SkImage_Gpu>(fTexture->width(), fTexture->height(), |
| 441 | kNeedNewImageUniqueID, |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 442 | fAlphaType, fTexture.get(), fColorSpace, |
| 443 | SkBudgeted::kYes); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | GrContext* ctx = fTexture->getContext(); |
| 447 | GrSurfaceDesc desc = fTexture->desc(); |
| 448 | desc.fWidth = subset.width(); |
| 449 | desc.fHeight = subset.height(); |
| 450 | |
| 451 | sk_sp<GrTexture> subTx(ctx->textureProvider()->createTexture(desc, SkBudgeted::kYes)); |
| 452 | if (!subTx) { |
| 453 | return nullptr; |
| 454 | } |
| 455 | ctx->copySurface(subTx.get(), fTexture.get(), subset, SkIPoint::Make(0, 0)); |
| 456 | return sk_make_sp<SkImage_Gpu>(desc.fWidth, desc.fHeight, kNeedNewImageUniqueID, |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 457 | fAlphaType, subTx.get(), fColorSpace, SkBudgeted::kYes); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 458 | } |
| 459 | |
brianosman | eed6b0e | 2016-09-23 13:04:05 -0700 | [diff] [blame^] | 460 | sk_sp<SkSurface> onMakeTightSurface(const SkImageFilter::OutputProperties& outProps, |
| 461 | const SkISize& size, SkAlphaType at) const override { |
| 462 | SkColorSpace* colorSpace = outProps.colorSpace(); |
| 463 | SkColorType colorType = colorSpace && colorSpace->gammaIsLinear() |
| 464 | ? kRGBA_F16_SkColorType : kRGBA_8888_SkColorType; |
| 465 | SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), colorType, at, |
| 466 | sk_ref_sp(colorSpace)); |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 467 | return SkSurface::MakeRenderTarget(fTexture->getContext(), SkBudgeted::kYes, info); |
| 468 | } |
| 469 | |
| 470 | private: |
| 471 | sk_sp<GrTexture> fTexture; |
| 472 | const SkAlphaType fAlphaType; |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 473 | sk_sp<SkColorSpace> fColorSpace; |
robertphillips | ed086ca | 2016-04-26 15:02:25 -0700 | [diff] [blame] | 474 | mutable SkAtomic<bool> fAddedRasterVersionToCache; |
| 475 | |
| 476 | typedef SkSpecialImage_Base INHERITED; |
| 477 | }; |
| 478 | |
robertphillips | 3e30227 | 2016-04-20 11:48:36 -0700 | [diff] [blame] | 479 | sk_sp<SkSpecialImage> SkSpecialImage::MakeFromGpu(const SkIRect& subset, |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 480 | uint32_t uniqueID, |
robertphillips | c91fd34 | 2016-04-25 12:32:54 -0700 | [diff] [blame] | 481 | sk_sp<GrTexture> tex, |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 482 | sk_sp<SkColorSpace> colorSpace, |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 483 | const SkSurfaceProps* props, |
robertphillips | 37bd7c3 | 2016-03-17 14:31:39 -0700 | [diff] [blame] | 484 | SkAlphaType at) { |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 485 | SkASSERT(rect_fits(subset, tex->width(), tex->height())); |
brianosman | afbf71d | 2016-07-21 07:15:37 -0700 | [diff] [blame] | 486 | return sk_make_sp<SkSpecialImage_Gpu>(subset, uniqueID, std::move(tex), at, |
| 487 | std::move(colorSpace), props); |
robertphillips | b6c65e9 | 2016-02-04 10:52:42 -0800 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | #endif |