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