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