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