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