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