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