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