blob: db4fadcce27d8abe304c9749aba1a83c7be5422c [file] [log] [blame]
robertphillipsb6c65e92016-02-04 10:52:42 -08001/*
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 */
reedad7604b2016-07-20 16:13:32 -07007
robertphillips83c17fa2016-03-18 08:14:27 -07008#include "SkSpecialImage.h"
reedad7604b2016-07-20 16:13:32 -07009#include "SkBitmap.h"
10#include "SkImage.h"
robertphillips64612512016-04-08 12:10:42 -070011#include "SkBitmapCache.h"
robertphillipsb6c65e92016-02-04 10:52:42 -080012#include "SkCanvas.h"
bsalomon84a4e5a2016-02-29 11:41:52 -080013#include "SkImage_Base.h"
robertphillipsb6c65e92016-02-04 10:52:42 -080014#include "SkSpecialSurface.h"
brianosman898235c2016-04-06 07:38:23 -070015#include "SkSurfacePriv.h"
reedad7604b2016-07-20 16:13:32 -070016#include "SkPixelRef.h"
17
18#if SK_SUPPORT_GPU
19#include "GrContext.h"
Robert Phillips301431d2017-03-29 12:08:49 -040020#include "GrContextPriv.h"
Robert Phillipsb66b42f2017-03-14 08:53:02 -040021#include "GrResourceProvider.h"
Robert Phillipse2f7d182016-12-15 09:23:05 -050022#include "GrSurfaceContext.h"
Robert Phillips8bc06d02016-11-01 17:28:40 -040023#include "GrTextureProxy.h"
Robert Phillips6de99042017-01-31 11:31:39 -050024#include "SkImage_Gpu.h"
reedad7604b2016-07-20 16:13:32 -070025#endif
robertphillipsb6c65e92016-02-04 10:52:42 -080026
reeda2217ef2016-07-20 06:04:34 -070027// Currently the raster imagefilters can only handle certain imageinfos. Call this to know if
28// a given info is supported.
29static bool valid_for_imagefilters(const SkImageInfo& info) {
30 // no support for other swizzles/depths yet
31 return info.colorType() == kN32_SkColorType;
32}
33
robertphillipsb6c65e92016-02-04 10:52:42 -080034///////////////////////////////////////////////////////////////////////////////
35class SkSpecialImage_Base : public SkSpecialImage {
36public:
robertphillips3e302272016-04-20 11:48:36 -070037 SkSpecialImage_Base(const SkIRect& subset, uint32_t uniqueID, const SkSurfaceProps* props)
38 : INHERITED(subset, uniqueID, props) {
robertphillips3b087f42016-02-18 08:48:03 -080039 }
robertphillips3e302272016-04-20 11:48:36 -070040 ~SkSpecialImage_Base() override { }
robertphillipsb6c65e92016-02-04 10:52:42 -080041
robertphillipse8c34972016-02-16 12:09:36 -080042 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) const = 0;
robertphillipsb6c65e92016-02-04 10:52:42 -080043
robertphillips64612512016-04-08 12:10:42 -070044 virtual bool onGetROPixels(SkBitmap*) const = 0;
robertphillipsb6c65e92016-02-04 10:52:42 -080045
Robert Phillips8bc06d02016-11-01 17:28:40 -040046 virtual GrContext* onGetContext() const { return nullptr; }
robertphillipsb6c65e92016-02-04 10:52:42 -080047
brianosmanafbf71d2016-07-21 07:15:37 -070048 virtual SkColorSpace* onGetColorSpace() const = 0;
49
robertphillipsc91fd342016-04-25 12:32:54 -070050#if SK_SUPPORT_GPU
Robert Phillips8e1c4e62017-02-19 12:27:01 -050051 virtual sk_sp<GrTextureProxy> onAsTextureProxyRef(GrContext* context) const = 0;
robertphillipsc91fd342016-04-25 12:32:54 -070052#endif
robertphillips4418dba2016-03-07 12:45:14 -080053
robertphillipsb4bd11e2016-03-21 13:44:18 -070054 virtual sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const = 0;
55
brianosmaneed6b0e2016-09-23 13:04:05 -070056 virtual sk_sp<SkSpecialSurface> onMakeSurface(const SkImageFilter::OutputProperties& outProps,
57 const SkISize& size, SkAlphaType at) const = 0;
robertphillipsc5035e72016-03-17 06:58:39 -070058
Robert Phillipsa5fdc972017-02-18 16:58:09 -050059 virtual sk_sp<SkImage> onAsImage(const SkIRect* subset) const = 0;
robertphillipsb4bd11e2016-03-21 13:44:18 -070060
brianosmaneed6b0e2016-09-23 13:04:05 -070061 virtual sk_sp<SkSurface> onMakeTightSurface(const SkImageFilter::OutputProperties& outProps,
62 const SkISize& size, SkAlphaType at) const = 0;
robertphillipsb6c65e92016-02-04 10:52:42 -080063
64private:
65 typedef SkSpecialImage INHERITED;
66};
67
68///////////////////////////////////////////////////////////////////////////////
bsalomon84a4e5a2016-02-29 11:41:52 -080069static inline const SkSpecialImage_Base* as_SIB(const SkSpecialImage* image) {
robertphillipsb6c65e92016-02-04 10:52:42 -080070 return static_cast<const SkSpecialImage_Base*>(image);
71}
72
robertphillips3e302272016-04-20 11:48:36 -070073SkSpecialImage::SkSpecialImage(const SkIRect& subset,
brianosman898235c2016-04-06 07:38:23 -070074 uint32_t uniqueID,
75 const SkSurfaceProps* props)
76 : fProps(SkSurfacePropsCopyOrDefault(props))
77 , fSubset(subset)
robertphillips3e302272016-04-20 11:48:36 -070078 , fUniqueID(kNeedNewImageUniqueID_SpecialImage == uniqueID ? SkNextID::ImageID() : uniqueID) {
brianosman898235c2016-04-06 07:38:23 -070079}
80
robertphillips3e302272016-04-20 11:48:36 -070081sk_sp<SkSpecialImage> SkSpecialImage::makeTextureImage(GrContext* context) {
robertphillips83c17fa2016-03-18 08:14:27 -070082#if SK_SUPPORT_GPU
83 if (!context) {
84 return nullptr;
85 }
Robert Phillips8bc06d02016-11-01 17:28:40 -040086 if (GrContext* curContext = as_SIB(this)->onGetContext()) {
87 return curContext == context ? sk_sp<SkSpecialImage>(SkRef(this)) : nullptr;
robertphillips83c17fa2016-03-18 08:14:27 -070088 }
89
90 SkBitmap bmp;
reedcf5c8462016-07-20 12:28:40 -070091 // At this point, we are definitely not texture-backed, so we must be raster or generator
92 // backed. If we remove the special-wrapping-an-image subclass, we may be able to assert that
93 // we are strictly raster-backed (i.e. generator images become raster when they are specialized)
94 // in which case getROPixels could turn into peekPixels...
95 if (!this->getROPixels(&bmp)) {
robertphillips83c17fa2016-03-18 08:14:27 -070096 return nullptr;
97 }
98
robertphillips83f2e5a2016-03-24 06:31:25 -070099 if (bmp.empty()) {
robertphillips3e302272016-04-20 11:48:36 -0700100 return SkSpecialImage::MakeFromRaster(SkIRect::MakeEmpty(), bmp, &this->props());
robertphillips83f2e5a2016-03-24 06:31:25 -0700101 }
102
Robert Phillipse14d3052017-02-15 13:18:21 -0500103 // TODO: this is a tight copy of 'bmp' but it doesn't have to be (given SkSpecialImage's
104 // semantics). Since this is cached though we would have to bake the fit into the cache key.
Robert Phillips26c90e02017-03-14 14:39:29 -0400105 sk_sp<GrTextureProxy> proxy = GrMakeCachedBitmapProxy(context->resourceProvider(), bmp);
Robert Phillipse14d3052017-02-15 13:18:21 -0500106 if (!proxy) {
robertphillips83c17fa2016-03-18 08:14:27 -0700107 return nullptr;
108 }
109
Robert Phillipse14d3052017-02-15 13:18:21 -0500110 const SkIRect rect = SkIRect::MakeWH(proxy->width(), proxy->height());
111
112 // GrMakeCachedBitmapProxy has uploaded only the specified subset of 'bmp' so we need not
113 // bother with SkBitmap::getSubset
114 return SkSpecialImage::MakeDeferredFromGpu(context,
115 rect,
116 this->uniqueID(),
117 std::move(proxy),
118 sk_ref_sp(this->getColorSpace()),
119 &this->props(),
120 this->alphaType());
robertphillips83c17fa2016-03-18 08:14:27 -0700121#else
122 return nullptr;
123#endif
124}
125
robertphillipse8c34972016-02-16 12:09:36 -0800126void SkSpecialImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const {
bsalomon84a4e5a2016-02-29 11:41:52 -0800127 return as_SIB(this)->onDraw(canvas, x, y, paint);
robertphillipsb6c65e92016-02-04 10:52:42 -0800128}
129
robertphillips64612512016-04-08 12:10:42 -0700130bool SkSpecialImage::getROPixels(SkBitmap* bm) const {
131 return as_SIB(this)->onGetROPixels(bm);
robertphillipsb6c65e92016-02-04 10:52:42 -0800132}
133
robertphillips64612512016-04-08 12:10:42 -0700134bool SkSpecialImage::isTextureBacked() const {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400135 return SkToBool(as_SIB(this)->onGetContext());
robertphillipsb6c65e92016-02-04 10:52:42 -0800136}
137
robertphillips64612512016-04-08 12:10:42 -0700138GrContext* SkSpecialImage::getContext() const {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400139 return as_SIB(this)->onGetContext();
robertphillips64612512016-04-08 12:10:42 -0700140}
141
brianosmanafbf71d2016-07-21 07:15:37 -0700142SkColorSpace* SkSpecialImage::getColorSpace() const {
143 return as_SIB(this)->onGetColorSpace();
144}
145
robertphillipsc91fd342016-04-25 12:32:54 -0700146#if SK_SUPPORT_GPU
Robert Phillips8e1c4e62017-02-19 12:27:01 -0500147sk_sp<GrTextureProxy> SkSpecialImage::asTextureProxyRef(GrContext* context) const {
148 return as_SIB(this)->onAsTextureProxyRef(context);
Robert Phillips8bc06d02016-11-01 17:28:40 -0400149}
robertphillipsc91fd342016-04-25 12:32:54 -0700150#endif
robertphillips4418dba2016-03-07 12:45:14 -0800151
brianosmaneed6b0e2016-09-23 13:04:05 -0700152sk_sp<SkSpecialSurface> SkSpecialImage::makeSurface(const SkImageFilter::OutputProperties& outProps,
153 const SkISize& size, SkAlphaType at) const {
154 return as_SIB(this)->onMakeSurface(outProps, size, at);
robertphillipsb6c65e92016-02-04 10:52:42 -0800155}
156
brianosmaneed6b0e2016-09-23 13:04:05 -0700157sk_sp<SkSurface> SkSpecialImage::makeTightSurface(const SkImageFilter::OutputProperties& outProps,
158 const SkISize& size, SkAlphaType at) const {
159 return as_SIB(this)->onMakeTightSurface(outProps, size, at);
robertphillipsb4bd11e2016-03-21 13:44:18 -0700160}
161
robertphillips37bd7c32016-03-17 14:31:39 -0700162sk_sp<SkSpecialImage> SkSpecialImage::makeSubset(const SkIRect& subset) const {
163 return as_SIB(this)->onMakeSubset(subset);
robertphillipsc5035e72016-03-17 06:58:39 -0700164}
165
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500166sk_sp<SkImage> SkSpecialImage::asImage(const SkIRect* subset) const {
167 return as_SIB(this)->onAsImage(subset);
robertphillipsb4bd11e2016-03-21 13:44:18 -0700168}
169
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500170
robertphillipsb6c65e92016-02-04 10:52:42 -0800171#ifdef SK_DEBUG
172static bool rect_fits(const SkIRect& rect, int width, int height) {
robertphillips4418dba2016-03-07 12:45:14 -0800173 if (0 == width && 0 == height) {
174 SkASSERT(0 == rect.fLeft && 0 == rect.fRight && 0 == rect.fTop && 0 == rect.fBottom);
175 return true;
176 }
177
robertphillipsb6c65e92016-02-04 10:52:42 -0800178 return rect.fLeft >= 0 && rect.fLeft < width && rect.fLeft < rect.fRight &&
179 rect.fRight >= 0 && rect.fRight <= width &&
180 rect.fTop >= 0 && rect.fTop < height && rect.fTop < rect.fBottom &&
181 rect.fBottom >= 0 && rect.fBottom <= height;
182}
183#endif
184
robertphillips3e302272016-04-20 11:48:36 -0700185sk_sp<SkSpecialImage> SkSpecialImage::MakeFromImage(const SkIRect& subset,
brianosman898235c2016-04-06 07:38:23 -0700186 sk_sp<SkImage> image,
Brian Osman61624f02016-12-09 14:51:59 -0500187 SkColorSpace* dstColorSpace,
brianosman898235c2016-04-06 07:38:23 -0700188 const SkSurfaceProps* props) {
robertphillipsb6c65e92016-02-04 10:52:42 -0800189 SkASSERT(rect_fits(subset, image->width(), image->height()));
robertphillips37bd7c32016-03-17 14:31:39 -0700190
reedad7604b2016-07-20 16:13:32 -0700191#if SK_SUPPORT_GPU
Robert Phillips6de99042017-01-31 11:31:39 -0500192 if (sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef()) {
193 GrContext* context = ((SkImage_Gpu*) as_IB(image))->context();
194
195 return MakeDeferredFromGpu(context, subset, image->uniqueID(), std::move(proxy),
Robert Phillips256c37b2017-03-01 14:32:46 -0500196 as_IB(image)->onImageInfo().refColorSpace(), props);
reedad7604b2016-07-20 16:13:32 -0700197 } else
198#endif
199 {
200 SkBitmap bm;
Brian Osman61624f02016-12-09 14:51:59 -0500201 if (as_IB(image)->getROPixels(&bm, dstColorSpace)) {
reedad7604b2016-07-20 16:13:32 -0700202 return MakeFromRaster(subset, bm, props);
203 }
reeda2217ef2016-07-20 06:04:34 -0700204 }
reedad7604b2016-07-20 16:13:32 -0700205 return nullptr;
robertphillipsb6c65e92016-02-04 10:52:42 -0800206}
207
208///////////////////////////////////////////////////////////////////////////////
robertphillipsb6c65e92016-02-04 10:52:42 -0800209
210class SkSpecialImage_Raster : public SkSpecialImage_Base {
211public:
robertphillips3e302272016-04-20 11:48:36 -0700212 SkSpecialImage_Raster(const SkIRect& subset, const SkBitmap& bm, const SkSurfaceProps* props)
213 : INHERITED(subset, bm.getGenerationID(), props)
reedbd2bd5c2016-07-25 14:26:02 -0700214 , fBitmap(bm)
215 {
216 SkASSERT(bm.pixelRef());
reedbd2bd5c2016-07-25 14:26:02 -0700217 SkASSERT(fBitmap.getPixels());
robertphillipsb6c65e92016-02-04 10:52:42 -0800218 }
219
brianosman80e96082016-08-16 07:09:47 -0700220 SkAlphaType alphaType() const override { return fBitmap.alphaType(); }
robertphillips3b087f42016-02-18 08:48:03 -0800221
Mike Reed98a62162017-09-26 12:47:08 -0400222 size_t getSize() const override { return fBitmap.computeByteSize(); }
robertphillips3b087f42016-02-18 08:48:03 -0800223
robertphillipse8c34972016-02-16 12:09:36 -0800224 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override {
robertphillipsb6c65e92016-02-04 10:52:42 -0800225 SkRect dst = SkRect::MakeXYWH(x, y,
226 this->subset().width(), this->subset().height());
227
228 canvas->drawBitmapRect(fBitmap, this->subset(),
229 dst, paint, SkCanvas::kStrict_SrcRectConstraint);
230 }
231
robertphillips64612512016-04-08 12:10:42 -0700232 bool onGetROPixels(SkBitmap* bm) const override {
233 *bm = fBitmap;
robertphillips3b087f42016-02-18 08:48:03 -0800234 return true;
235 }
236
brianosmanafbf71d2016-07-21 07:15:37 -0700237 SkColorSpace* onGetColorSpace() const override {
238 return fBitmap.colorSpace();
239 }
240
robertphillips64612512016-04-08 12:10:42 -0700241#if SK_SUPPORT_GPU
Robert Phillips8e1c4e62017-02-19 12:27:01 -0500242 sk_sp<GrTextureProxy> onAsTextureProxyRef(GrContext* context) const override {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400243 if (context) {
Robert Phillips26c90e02017-03-14 14:39:29 -0400244 return GrMakeCachedBitmapProxy(context->resourceProvider(), fBitmap);
Robert Phillips8bc06d02016-11-01 17:28:40 -0400245 }
246
247 return nullptr;
248 }
robertphillipsc91fd342016-04-25 12:32:54 -0700249#endif
robertphillips64612512016-04-08 12:10:42 -0700250
brianosmaneed6b0e2016-09-23 13:04:05 -0700251// TODO: The raster implementations of image filters all currently assume that the pixels are
252// legacy N32. Until they actually check the format and operate on sRGB or F16 data appropriately,
253// we can't enable this. (They will continue to produce incorrect results, but less-so).
254#define RASTER_IMAGE_FILTERS_SUPPORT_SRGB_AND_F16 0
255
256 sk_sp<SkSpecialSurface> onMakeSurface(const SkImageFilter::OutputProperties& outProps,
257 const SkISize& size, SkAlphaType at) const override {
258#if RASTER_IMAGE_FILTERS_SUPPORT_SRGB_AND_F16
259 SkColorSpace* colorSpace = outProps.colorSpace();
260#else
261 SkColorSpace* colorSpace = nullptr;
262#endif
263 SkColorType colorType = colorSpace && colorSpace->gammaIsLinear()
264 ? kRGBA_F16_SkColorType : kN32_SkColorType;
265 SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), colorType, at,
266 sk_ref_sp(colorSpace));
robertphillips3e302272016-04-20 11:48:36 -0700267 return SkSpecialSurface::MakeRaster(info, nullptr);
robertphillipsb6c65e92016-02-04 10:52:42 -0800268 }
269
robertphillips37bd7c32016-03-17 14:31:39 -0700270 sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const override {
robertphillipsc5035e72016-03-17 06:58:39 -0700271 SkBitmap subsetBM;
halcanary9d524f22016-03-29 09:03:52 -0700272
robertphillipsc5035e72016-03-17 06:58:39 -0700273 if (!fBitmap.extractSubset(&subsetBM, subset)) {
274 return nullptr;
275 }
276
robertphillips3e302272016-04-20 11:48:36 -0700277 return SkSpecialImage::MakeFromRaster(SkIRect::MakeWH(subset.width(), subset.height()),
brianosman898235c2016-04-06 07:38:23 -0700278 subsetBM,
279 &this->props());
robertphillipsc5035e72016-03-17 06:58:39 -0700280 }
281
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500282 sk_sp<SkImage> onAsImage(const SkIRect* subset) const override {
283 if (subset) {
284 SkBitmap subsetBM;
robertphillipsb4bd11e2016-03-21 13:44:18 -0700285
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500286 if (!fBitmap.extractSubset(&subsetBM, *subset)) {
287 return nullptr;
288 }
289
290 return SkImage::MakeFromBitmap(subsetBM);
robertphillipsb4bd11e2016-03-21 13:44:18 -0700291 }
292
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500293 return SkImage::MakeFromBitmap(fBitmap);
robertphillipsb4bd11e2016-03-21 13:44:18 -0700294 }
295
brianosmaneed6b0e2016-09-23 13:04:05 -0700296 sk_sp<SkSurface> onMakeTightSurface(const SkImageFilter::OutputProperties& outProps,
297 const SkISize& size, SkAlphaType at) const override {
298#if RASTER_IMAGE_FILTERS_SUPPORT_SRGB_AND_F16
299 SkColorSpace* colorSpace = outProps.colorSpace();
300#else
301 SkColorSpace* colorSpace = nullptr;
302#endif
303 SkColorType colorType = colorSpace && colorSpace->gammaIsLinear()
304 ? kRGBA_F16_SkColorType : kN32_SkColorType;
305 SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), colorType, at,
306 sk_ref_sp(colorSpace));
reede8f30622016-03-23 18:59:25 -0700307 return SkSurface::MakeRaster(info);
robertphillipsb4bd11e2016-03-21 13:44:18 -0700308 }
309
robertphillipsb6c65e92016-02-04 10:52:42 -0800310private:
311 SkBitmap fBitmap;
312
313 typedef SkSpecialImage_Base INHERITED;
314};
315
robertphillips3e302272016-04-20 11:48:36 -0700316sk_sp<SkSpecialImage> SkSpecialImage::MakeFromRaster(const SkIRect& subset,
brianosman898235c2016-04-06 07:38:23 -0700317 const SkBitmap& bm,
318 const SkSurfaceProps* props) {
robertphillipsb6c65e92016-02-04 10:52:42 -0800319 SkASSERT(rect_fits(subset, bm.width(), bm.height()));
robertphillips37bd7c32016-03-17 14:31:39 -0700320
reedbd2bd5c2016-07-25 14:26:02 -0700321 if (!bm.pixelRef()) {
322 return nullptr;
323 }
324
reeda2217ef2016-07-20 06:04:34 -0700325 const SkBitmap* srcBM = &bm;
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400326 SkBitmap tmp;
reeda2217ef2016-07-20 06:04:34 -0700327 // ImageFilters only handle N32 at the moment, so force our src to be that
328 if (!valid_for_imagefilters(bm.info())) {
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400329 if (!tmp.tryAllocPixels(bm.info().makeColorType(kN32_SkColorType)) ||
330 !bm.readPixels(tmp.info(), tmp.getPixels(), tmp.rowBytes(), 0, 0))
331 {
reeda2217ef2016-07-20 06:04:34 -0700332 return nullptr;
333 }
Matt Sarett68b8e3d2017-04-28 11:15:22 -0400334 srcBM = &tmp;
reeda2217ef2016-07-20 06:04:34 -0700335 }
336 return sk_make_sp<SkSpecialImage_Raster>(subset, *srcBM, props);
robertphillipsb6c65e92016-02-04 10:52:42 -0800337}
338
339#if SK_SUPPORT_GPU
340///////////////////////////////////////////////////////////////////////////////
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400341static sk_sp<SkImage> wrap_proxy_in_image(GrContext* context, sk_sp<GrTextureProxy> proxy,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500342 SkAlphaType alphaType, sk_sp<SkColorSpace> colorSpace) {
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400343 return sk_make_sp<SkImage_Gpu>(context, kNeedNewImageUniqueID, alphaType,
344 std::move(proxy), std::move(colorSpace), SkBudgeted::kYes);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500345}
346
robertphillipsed086ca2016-04-26 15:02:25 -0700347class SkSpecialImage_Gpu : public SkSpecialImage_Base {
348public:
Robert Phillips8bc06d02016-11-01 17:28:40 -0400349 SkSpecialImage_Gpu(GrContext* context, const SkIRect& subset,
Robert Phillips63c67462017-02-15 14:19:01 -0500350 uint32_t uniqueID, sk_sp<GrTextureProxy> proxy, SkAlphaType at,
Robert Phillips8bc06d02016-11-01 17:28:40 -0400351 sk_sp<SkColorSpace> colorSpace, const SkSurfaceProps* props)
352 : INHERITED(subset, uniqueID, props)
353 , fContext(context)
Robert Phillips63c67462017-02-15 14:19:01 -0500354 , fTextureProxy(std::move(proxy))
robertphillipsed086ca2016-04-26 15:02:25 -0700355 , fAlphaType(at)
brianosmanafbf71d2016-07-21 07:15:37 -0700356 , fColorSpace(std::move(colorSpace))
robertphillipsed086ca2016-04-26 15:02:25 -0700357 , fAddedRasterVersionToCache(false) {
358 }
359
360 ~SkSpecialImage_Gpu() override {
361 if (fAddedRasterVersionToCache.load()) {
362 SkNotifyBitmapGenIDIsStale(this->uniqueID());
363 }
364 }
365
brianosman80e96082016-08-16 07:09:47 -0700366 SkAlphaType alphaType() const override { return fAlphaType; }
robertphillipsed086ca2016-04-26 15:02:25 -0700367
Robert Phillips63c67462017-02-15 14:19:01 -0500368 size_t getSize() const override { return fTextureProxy->gpuMemorySize(); }
robertphillipsed086ca2016-04-26 15:02:25 -0700369
370 void onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const override {
371 SkRect dst = SkRect::MakeXYWH(x, y,
372 this->subset().width(), this->subset().height());
373
Robert Phillips7f6cd902016-11-10 17:03:43 -0500374 // TODO: In this instance we know we're going to draw a sub-portion of the backing
375 // texture into the canvas so it is okay to wrap it in an SkImage. This poses
376 // some problems for full deferral however in that when the deferred SkImage_Gpu
377 // instantiates itself it is going to have to either be okay with having a larger
Robert Phillips63c67462017-02-15 14:19:01 -0500378 // than expected backing texture (unlikely) or the 'fit' of the SurfaceProxy needs
Robert Phillips7f6cd902016-11-10 17:03:43 -0500379 // to be tightened (if it is deferred).
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400380 sk_sp<SkImage> img = sk_sp<SkImage>(new SkImage_Gpu(canvas->getGrContext(),
381 this->uniqueID(), fAlphaType,
382 fTextureProxy,
383 fColorSpace, SkBudgeted::kNo));
robertphillipsed086ca2016-04-26 15:02:25 -0700384
reed77d6f7d2016-07-13 12:24:48 -0700385 canvas->drawImageRect(img, this->subset(),
Robert Phillips7f6cd902016-11-10 17:03:43 -0500386 dst, paint, SkCanvas::kStrict_SrcRectConstraint);
robertphillipsed086ca2016-04-26 15:02:25 -0700387 }
388
Robert Phillips8bc06d02016-11-01 17:28:40 -0400389 GrContext* onGetContext() const override { return fContext; }
robertphillipsed086ca2016-04-26 15:02:25 -0700390
Robert Phillips8e1c4e62017-02-19 12:27:01 -0500391 sk_sp<GrTextureProxy> onAsTextureProxyRef(GrContext*) const override {
Robert Phillips63c67462017-02-15 14:19:01 -0500392 return fTextureProxy;
Robert Phillips8bc06d02016-11-01 17:28:40 -0400393 }
robertphillipsed086ca2016-04-26 15:02:25 -0700394
395 bool onGetROPixels(SkBitmap* dst) const override {
Mike Reed5fa3d6d2017-03-25 09:51:00 -0400396 const auto desc = SkBitmapCacheDesc::Make(this->uniqueID(), this->width(), this->height());
397 if (SkBitmapCache::Find(desc, dst)) {
robertphillipsed086ca2016-04-26 15:02:25 -0700398 SkASSERT(dst->getGenerationID() == this->uniqueID());
399 SkASSERT(dst->isImmutable());
400 SkASSERT(dst->getPixels());
401 return true;
402 }
403
Mike Reed7a542c52017-04-11 12:03:44 -0400404 SkPixmap pmap;
robertphillipsed086ca2016-04-26 15:02:25 -0700405 SkImageInfo info = SkImageInfo::MakeN32(this->width(), this->height(),
brianosman80e96082016-08-16 07:09:47 -0700406 this->alphaType(), fColorSpace);
Mike Reed7a542c52017-04-11 12:03:44 -0400407 auto rec = SkBitmapCache::Alloc(desc, info, &pmap);
408 if (!rec) {
robertphillipsed086ca2016-04-26 15:02:25 -0700409 return false;
410 }
411
Robert Phillips301431d2017-03-29 12:08:49 -0400412 sk_sp<GrSurfaceContext> sContext = fContext->contextPriv().makeWrappedSurfaceContext(
413 fTextureProxy, nullptr);
414 if (!sContext) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500415 return false;
416 }
Robert Phillips8bc06d02016-11-01 17:28:40 -0400417
Mike Reed7a542c52017-04-11 12:03:44 -0400418 if (!sContext->readPixels(info, pmap.writable_addr(), pmap.rowBytes(), 0, 0)) {
robertphillipsed086ca2016-04-26 15:02:25 -0700419 return false;
420 }
421
Mike Reed7a542c52017-04-11 12:03:44 -0400422 SkBitmapCache::Add(std::move(rec), dst);
robertphillipsed086ca2016-04-26 15:02:25 -0700423 fAddedRasterVersionToCache.store(true);
424 return true;
425 }
426
brianosmanafbf71d2016-07-21 07:15:37 -0700427 SkColorSpace* onGetColorSpace() const override {
428 return fColorSpace.get();
429 }
430
brianosmaneed6b0e2016-09-23 13:04:05 -0700431 sk_sp<SkSpecialSurface> onMakeSurface(const SkImageFilter::OutputProperties& outProps,
432 const SkISize& size, SkAlphaType at) const override {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400433 if (!fContext) {
robertphillipsed086ca2016-04-26 15:02:25 -0700434 return nullptr;
435 }
436
brianosmaneed6b0e2016-09-23 13:04:05 -0700437 SkColorSpace* colorSpace = outProps.colorSpace();
438 return SkSpecialSurface::MakeRenderTarget(
Robert Phillips8bc06d02016-11-01 17:28:40 -0400439 fContext, size.width(), size.height(),
brianosmaneed6b0e2016-09-23 13:04:05 -0700440 GrRenderableConfigForColorSpace(colorSpace), sk_ref_sp(colorSpace));
robertphillipsed086ca2016-04-26 15:02:25 -0700441 }
442
443 sk_sp<SkSpecialImage> onMakeSubset(const SkIRect& subset) const override {
Robert Phillips8bc06d02016-11-01 17:28:40 -0400444 return SkSpecialImage::MakeDeferredFromGpu(fContext,
445 subset,
446 this->uniqueID(),
Robert Phillips63c67462017-02-15 14:19:01 -0500447 fTextureProxy,
Robert Phillips8bc06d02016-11-01 17:28:40 -0400448 fColorSpace,
449 &this->props(),
450 fAlphaType);
robertphillipsed086ca2016-04-26 15:02:25 -0700451 }
452
Robert Phillipse2f7d182016-12-15 09:23:05 -0500453 // TODO: move all the logic here into the subset-flavor GrSurfaceProxy::copy?
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500454 sk_sp<SkImage> onAsImage(const SkIRect* subset) const override {
455 if (subset) {
456 // TODO: if this becomes a bottle neck we could base this logic on what the size
457 // will be when it is finally instantiated - but that is more fraught.
Robert Phillipsb66b42f2017-03-14 08:53:02 -0400458 if (GrResourceProvider::IsFunctionallyExact(fTextureProxy.get()) &&
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500459 0 == subset->fLeft && 0 == subset->fTop &&
460 fTextureProxy->width() == subset->width() &&
461 fTextureProxy->height() == subset->height()) {
462 // The existing GrTexture is already tight so reuse it in the SkImage
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400463 return wrap_proxy_in_image(fContext, fTextureProxy, fAlphaType, fColorSpace);
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500464 }
465
466 sk_sp<GrTextureProxy> subsetProxy(GrSurfaceProxy::Copy(fContext, fTextureProxy.get(),
467 *subset, SkBudgeted::kYes));
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400468 if (!subsetProxy) {
469 return nullptr;
470 }
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500471
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400472 SkASSERT(subsetProxy->priv().isExact());
473 // MDB: this is acceptable (wrapping subsetProxy in an SkImage) bc Copy will
474 // return a kExact-backed proxy
475 return wrap_proxy_in_image(fContext, std::move(subsetProxy), fAlphaType, fColorSpace);
robertphillipsed086ca2016-04-26 15:02:25 -0700476 }
477
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400478 fTextureProxy->priv().exactify();
479
480 return wrap_proxy_in_image(fContext, fTextureProxy, fAlphaType, fColorSpace);
robertphillipsed086ca2016-04-26 15:02:25 -0700481 }
482
brianosmaneed6b0e2016-09-23 13:04:05 -0700483 sk_sp<SkSurface> onMakeTightSurface(const SkImageFilter::OutputProperties& outProps,
484 const SkISize& size, SkAlphaType at) const override {
485 SkColorSpace* colorSpace = outProps.colorSpace();
486 SkColorType colorType = colorSpace && colorSpace->gammaIsLinear()
487 ? kRGBA_F16_SkColorType : kRGBA_8888_SkColorType;
488 SkImageInfo info = SkImageInfo::Make(size.width(), size.height(), colorType, at,
489 sk_ref_sp(colorSpace));
Robert Phillips8bc06d02016-11-01 17:28:40 -0400490 return SkSurface::MakeRenderTarget(fContext, SkBudgeted::kYes, info);
robertphillipsed086ca2016-04-26 15:02:25 -0700491 }
492
493private:
Robert Phillips8bc06d02016-11-01 17:28:40 -0400494 GrContext* fContext;
Robert Phillips63c67462017-02-15 14:19:01 -0500495 sk_sp<GrTextureProxy> fTextureProxy;
robertphillipsed086ca2016-04-26 15:02:25 -0700496 const SkAlphaType fAlphaType;
brianosmanafbf71d2016-07-21 07:15:37 -0700497 sk_sp<SkColorSpace> fColorSpace;
robertphillipsed086ca2016-04-26 15:02:25 -0700498 mutable SkAtomic<bool> fAddedRasterVersionToCache;
499
500 typedef SkSpecialImage_Base INHERITED;
501};
502
Robert Phillips8bc06d02016-11-01 17:28:40 -0400503sk_sp<SkSpecialImage> SkSpecialImage::MakeDeferredFromGpu(GrContext* context,
504 const SkIRect& subset,
505 uint32_t uniqueID,
Robert Phillips63c67462017-02-15 14:19:01 -0500506 sk_sp<GrTextureProxy> proxy,
Robert Phillips8bc06d02016-11-01 17:28:40 -0400507 sk_sp<SkColorSpace> colorSpace,
508 const SkSurfaceProps* props,
509 SkAlphaType at) {
510 SkASSERT(rect_fits(subset, proxy->width(), proxy->height()));
511 return sk_make_sp<SkSpecialImage_Gpu>(context, subset, uniqueID, std::move(proxy), at,
512 std::move(colorSpace), props);
513}
robertphillipsb6c65e92016-02-04 10:52:42 -0800514#endif