blob: ad69750541daf226ef6a4825d5540481ed24b67a [file] [log] [blame]
reed85d91782015-09-10 14:33:38 -07001/*
2 * Copyright 2015 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
Brian Osmanbd659552018-09-11 10:03:19 -04008#include "SkImage_Lazy.h"
Brian Osmandf7e0752017-04-26 16:20:28 -04009
10#include "SkBitmap.h"
11#include "SkBitmapCache.h"
Brian Osmanb70fd912018-10-22 16:10:44 -040012#include "SkCachedData.h"
Brian Osmandf7e0752017-04-26 16:20:28 -040013#include "SkData.h"
14#include "SkImageGenerator.h"
reed85d91782015-09-10 14:33:38 -070015#include "SkImagePriv.h"
Brian Osmandf7e0752017-04-26 16:20:28 -040016#include "SkNextID.h"
reed85d91782015-09-10 14:33:38 -070017
Brian Osmandf7e0752017-04-26 16:20:28 -040018#if SK_SUPPORT_GPU
Robert Phillips9338c602019-02-19 12:52:29 -050019#include "GrCaps.h"
Brian Osmandf7e0752017-04-26 16:20:28 -040020#include "GrGpuResourcePriv.h"
21#include "GrImageTextureMaker.h"
22#include "GrResourceKey.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050023#include "GrProxyProvider.h"
Robert Phillips9338c602019-02-19 12:52:29 -050024#include "GrRecordingContext.h"
25#include "GrRecordingContextPriv.h"
Brian Salomon2bbdcc42017-09-07 12:36:34 -040026#include "GrSamplerState.h"
Brian Osmandf7e0752017-04-26 16:20:28 -040027#include "GrYUVProvider.h"
28#include "SkGr.h"
29#endif
reed85d91782015-09-10 14:33:38 -070030
Brian Osmandf7e0752017-04-26 16:20:28 -040031// Ref-counted tuple(SkImageGenerator, SkMutex) which allows sharing one generator among N images
Mike Klein408ef212018-10-30 15:23:00 +000032class SharedGenerator final : public SkNVRefCnt<SharedGenerator> {
Brian Osmandf7e0752017-04-26 16:20:28 -040033public:
34 static sk_sp<SharedGenerator> Make(std::unique_ptr<SkImageGenerator> gen) {
35 return gen ? sk_sp<SharedGenerator>(new SharedGenerator(std::move(gen))) : nullptr;
36 }
37
Matt Sarettb2004f72017-05-18 09:26:50 -040038 // This is thread safe. It is a const field set in the constructor.
39 const SkImageInfo& getInfo() { return fGenerator->getInfo(); }
40
Brian Osmandf7e0752017-04-26 16:20:28 -040041private:
42 explicit SharedGenerator(std::unique_ptr<SkImageGenerator> gen)
43 : fGenerator(std::move(gen)) {
44 SkASSERT(fGenerator);
45 }
46
47 friend class ScopedGenerator;
48 friend class SkImage_Lazy;
49
50 std::unique_ptr<SkImageGenerator> fGenerator;
51 SkMutex fMutex;
52};
53
reed85d91782015-09-10 14:33:38 -070054///////////////////////////////////////////////////////////////////////////////
55
Christopher Cameron77e96662017-07-08 01:47:47 -070056SkImage_Lazy::Validator::Validator(sk_sp<SharedGenerator> gen, const SkIRect* subset,
Brian Osmanf48c9962019-01-14 11:15:50 -050057 const SkColorType* colorType, sk_sp<SkColorSpace> colorSpace)
Brian Osmandf7e0752017-04-26 16:20:28 -040058 : fSharedGenerator(std::move(gen)) {
Brian Osmandf7e0752017-04-26 16:20:28 -040059 if (!fSharedGenerator) {
60 return;
61 }
62
63 // The following generator accessors are safe without acquiring the mutex (const getters).
64 // TODO: refactor to use a ScopedGenerator instead, for clarity.
65 const SkImageInfo& info = fSharedGenerator->fGenerator->getInfo();
66 if (info.isEmpty()) {
67 fSharedGenerator.reset();
68 return;
69 }
70
71 fUniqueID = fSharedGenerator->fGenerator->uniqueID();
72 const SkIRect bounds = SkIRect::MakeWH(info.width(), info.height());
73 if (subset) {
74 if (!bounds.contains(*subset)) {
75 fSharedGenerator.reset();
76 return;
77 }
78 if (*subset != bounds) {
79 // we need a different uniqueID since we really are a subset of the raw generator
80 fUniqueID = SkNextID::ImageID();
81 }
82 } else {
83 subset = &bounds;
84 }
85
86 fInfo = info.makeWH(subset->width(), subset->height());
87 fOrigin = SkIPoint::Make(subset->x(), subset->y());
Brian Osmanf48c9962019-01-14 11:15:50 -050088 if (colorType || colorSpace) {
89 if (colorType) {
90 fInfo = fInfo.makeColorType(*colorType);
91 }
92 if (colorSpace) {
93 fInfo = fInfo.makeColorSpace(colorSpace);
94 }
Christopher Cameron77e96662017-07-08 01:47:47 -070095 fUniqueID = SkNextID::ImageID();
96 }
Brian Osmandf7e0752017-04-26 16:20:28 -040097}
98
99///////////////////////////////////////////////////////////////////////////////
100
101// Helper for exclusive access to a shared generator.
102class SkImage_Lazy::ScopedGenerator {
103public:
104 ScopedGenerator(const sk_sp<SharedGenerator>& gen)
105 : fSharedGenerator(gen)
106 , fAutoAquire(gen->fMutex) {}
107
108 SkImageGenerator* operator->() const {
109 fSharedGenerator->fMutex.assertHeld();
110 return fSharedGenerator->fGenerator.get();
111 }
112
113 operator SkImageGenerator*() const {
114 fSharedGenerator->fMutex.assertHeld();
115 return fSharedGenerator->fGenerator.get();
116 }
117
118private:
119 const sk_sp<SharedGenerator>& fSharedGenerator;
120 SkAutoExclusive fAutoAquire;
121};
122
123///////////////////////////////////////////////////////////////////////////////
124
125SkImage_Lazy::SkImage_Lazy(Validator* validator)
126 : INHERITED(validator->fInfo.width(), validator->fInfo.height(), validator->fUniqueID)
127 , fSharedGenerator(std::move(validator->fSharedGenerator))
128 , fInfo(validator->fInfo)
129 , fOrigin(validator->fOrigin) {
130 SkASSERT(fSharedGenerator);
Brian Osmaneb7e5292018-08-08 14:32:06 -0400131 fUniqueID = validator->fUniqueID;
Brian Osmandf7e0752017-04-26 16:20:28 -0400132}
133
Brian Osmanbd659552018-09-11 10:03:19 -0400134SkImage_Lazy::~SkImage_Lazy() {
135#if SK_SUPPORT_GPU
136 for (int i = 0; i < fUniqueKeyInvalidatedMessages.count(); ++i) {
137 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(*fUniqueKeyInvalidatedMessages[i]);
138 }
139 fUniqueKeyInvalidatedMessages.deleteAll();
140#endif
141}
142
Brian Osmandf7e0752017-04-26 16:20:28 -0400143//////////////////////////////////////////////////////////////////////////////////////////////////
144
Brian Osmanc87cfb62018-07-11 09:08:46 -0400145static bool generate_pixels(SkImageGenerator* gen, const SkPixmap& pmap, int originX, int originY) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400146 const int genW = gen->getInfo().width();
147 const int genH = gen->getInfo().height();
148 const SkIRect srcR = SkIRect::MakeWH(genW, genH);
149 const SkIRect dstR = SkIRect::MakeXYWH(originX, originY, pmap.width(), pmap.height());
150 if (!srcR.contains(dstR)) {
151 return false;
152 }
153
154 // If they are requesting a subset, we have to have a temp allocation for full image, and
155 // then copy the subset into their allocation
156 SkBitmap full;
157 SkPixmap fullPM;
158 const SkPixmap* dstPM = &pmap;
159 if (srcR != dstR) {
160 if (!full.tryAllocPixels(pmap.info().makeWH(genW, genH))) {
161 return false;
162 }
163 if (!full.peekPixels(&fullPM)) {
164 return false;
165 }
166 dstPM = &fullPM;
167 }
168
Brian Osmanc87cfb62018-07-11 09:08:46 -0400169 if (!gen->getPixels(dstPM->info(), dstPM->writable_addr(), dstPM->rowBytes())) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400170 return false;
171 }
172
173 if (srcR != dstR) {
174 if (!full.readPixels(pmap, originX, originY)) {
175 return false;
176 }
177 }
178 return true;
179}
180
Brian Osman00766bf2018-10-22 15:59:23 -0400181bool SkImage_Lazy::getROPixels(SkBitmap* bitmap, SkImage::CachingHint chint) const {
182 auto check_output_bitmap = [bitmap]() {
183 SkASSERT(bitmap->isImmutable());
184 SkASSERT(bitmap->getPixels());
185 (void)bitmap;
186 };
187
188 auto desc = SkBitmapCacheDesc::Make(this);
189 if (SkBitmapCache::Find(desc, bitmap)) {
190 check_output_bitmap();
Brian Osmandf7e0752017-04-26 16:20:28 -0400191 return true;
192 }
193
Brian Osmandf7e0752017-04-26 16:20:28 -0400194 if (SkImage::kAllow_CachingHint == chint) {
Brian Osman00766bf2018-10-22 15:59:23 -0400195 SkPixmap pmap;
196 SkBitmapCache::RecPtr cacheRec = SkBitmapCache::Alloc(desc, fInfo, &pmap);
197 if (!cacheRec ||
198 !generate_pixels(ScopedGenerator(fSharedGenerator), pmap,
199 fOrigin.x(), fOrigin.y())) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400200 return false;
201 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400202 SkBitmapCache::Add(std::move(cacheRec), bitmap);
Mike Reed30301c42018-07-19 09:39:21 -0400203 this->notifyAddedToRasterCache();
Brian Osmandf7e0752017-04-26 16:20:28 -0400204 } else {
Brian Osman00766bf2018-10-22 15:59:23 -0400205 if (!bitmap->tryAllocPixels(fInfo) ||
206 !generate_pixels(ScopedGenerator(fSharedGenerator), bitmap->pixmap(),
207 fOrigin.x(), fOrigin.y())) {
208 return false;
209 }
Brian Osmana0dc3d22018-10-09 15:17:38 -0400210 bitmap->setImmutable();
Brian Osmandf7e0752017-04-26 16:20:28 -0400211 }
212
Brian Osman00766bf2018-10-22 15:59:23 -0400213 check_output_bitmap();
Brian Osmandf7e0752017-04-26 16:20:28 -0400214 return true;
215}
216
217//////////////////////////////////////////////////////////////////////////////////////////////////
218
Brian Osmanf1b43822017-04-20 13:43:23 -0400219bool SkImage_Lazy::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
220 int srcX, int srcY, CachingHint chint) const {
reed85d91782015-09-10 14:33:38 -0700221 SkBitmap bm;
Brian Osmane50cdf02018-10-19 13:02:14 -0400222 if (this->getROPixels(&bm, chint)) {
reed85d91782015-09-10 14:33:38 -0700223 return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY);
224 }
225 return false;
226}
227
Ben Wagnerbdf54332018-05-15 14:12:14 -0400228sk_sp<SkData> SkImage_Lazy::onRefEncoded() const {
229 ScopedGenerator generator(fSharedGenerator);
230 return generator->refEncodedData();
231}
reed85d91782015-09-10 14:33:38 -0700232
Brian Osman5bbd0762017-05-08 11:07:42 -0400233bool SkImage_Lazy::onIsValid(GrContext* context) const {
234 ScopedGenerator generator(fSharedGenerator);
235 return generator->isValid(context);
236}
237
Brian Osmandf7e0752017-04-26 16:20:28 -0400238///////////////////////////////////////////////////////////////////////////////////////////////////
239
Robert Phillipsb726d582017-03-09 16:36:32 -0500240#if SK_SUPPORT_GPU
Robert Phillips9338c602019-02-19 12:52:29 -0500241sk_sp<GrTextureProxy> SkImage_Lazy::asTextureProxyRef(GrRecordingContext* context,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400242 const GrSamplerState& params,
Brian Osmanf1b43822017-04-20 13:43:23 -0400243 SkScalar scaleAdjust[2]) const {
Brian Osmandf7e0752017-04-26 16:20:28 -0400244 if (!context) {
245 return nullptr;
246 }
247
248 GrImageTextureMaker textureMaker(context, this, kAllow_CachingHint);
Brian Osman6064e1c2018-10-19 14:27:54 -0400249 return textureMaker.refTextureProxyForParams(params, scaleAdjust);
Robert Phillipsb726d582017-03-09 16:36:32 -0500250}
251#endif
252
Robert Phillips6603a172019-03-05 12:35:44 -0500253sk_sp<SkImage> SkImage_Lazy::onMakeSubset(GrRecordingContext* context,
254 const SkIRect& subset) const {
Brian Osmandf7e0752017-04-26 16:20:28 -0400255 SkASSERT(fInfo.bounds().contains(subset));
256 SkASSERT(fInfo.bounds() != subset);
reed7b6945b2015-09-24 00:50:58 -0700257
Brian Osmandf7e0752017-04-26 16:20:28 -0400258 const SkIRect generatorSubset = subset.makeOffset(fOrigin.x(), fOrigin.y());
Brian Osmanf48c9962019-01-14 11:15:50 -0500259 const SkColorType colorType = fInfo.colorType();
260 Validator validator(fSharedGenerator, &generatorSubset, &colorType, fInfo.refColorSpace());
Brian Osmanf1b43822017-04-20 13:43:23 -0400261 return validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr;
reed7b6945b2015-09-24 00:50:58 -0700262}
263
Robert Phillips6603a172019-03-05 12:35:44 -0500264sk_sp<SkImage> SkImage_Lazy::onMakeColorTypeAndColorSpace(GrRecordingContext*,
265 SkColorType targetCT,
Brian Osmanf48c9962019-01-14 11:15:50 -0500266 sk_sp<SkColorSpace> targetCS) const {
267 SkAutoExclusive autoAquire(fOnMakeColorTypeAndSpaceMutex);
268 if (fOnMakeColorTypeAndSpaceResult &&
269 targetCT == fOnMakeColorTypeAndSpaceResult->colorType() &&
270 SkColorSpace::Equals(targetCS.get(), fOnMakeColorTypeAndSpaceResult->colorSpace())) {
271 return fOnMakeColorTypeAndSpaceResult;
Christopher Camerond4b67872017-07-13 15:18:08 -0700272 }
Christopher Cameron77e96662017-07-08 01:47:47 -0700273 const SkIRect generatorSubset =
274 SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width(), fInfo.height());
Brian Osmanf48c9962019-01-14 11:15:50 -0500275 Validator validator(fSharedGenerator, &generatorSubset, &targetCT, targetCS);
Christopher Camerond4b67872017-07-13 15:18:08 -0700276 sk_sp<SkImage> result = validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr;
277 if (result) {
Brian Osmanf48c9962019-01-14 11:15:50 -0500278 fOnMakeColorTypeAndSpaceResult = result;
Christopher Camerond4b67872017-07-13 15:18:08 -0700279 }
280 return result;
Matt Sarett6de13102017-03-14 14:10:48 -0400281}
282
Mike Reed185130c2017-02-15 15:14:16 -0500283sk_sp<SkImage> SkImage::MakeFromGenerator(std::unique_ptr<SkImageGenerator> generator,
284 const SkIRect* subset) {
Brian Osmanf48c9962019-01-14 11:15:50 -0500285 SkImage_Lazy::Validator
286 validator(SharedGenerator::Make(std::move(generator)), subset, nullptr, nullptr);
fmalita7929e3a2016-10-27 08:15:44 -0700287
Brian Osmanf1b43822017-04-20 13:43:23 -0400288 return validator ? sk_make_sp<SkImage_Lazy>(&validator) : nullptr;
reed85d91782015-09-10 14:33:38 -0700289}
Brian Osmandf7e0752017-04-26 16:20:28 -0400290
291//////////////////////////////////////////////////////////////////////////////////////////////////
292
Brian Osmandf7e0752017-04-26 16:20:28 -0400293#if SK_SUPPORT_GPU
294
Brian Osmanbd659552018-09-11 10:03:19 -0400295void SkImage_Lazy::makeCacheKeyFromOrigKey(const GrUniqueKey& origKey,
296 GrUniqueKey* cacheKey) const {
Brian Osman10494e32018-09-10 12:45:18 -0400297 SkASSERT(!cacheKey->isValid());
298 if (origKey.isValid()) {
299 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
300 GrUniqueKey::Builder builder(cacheKey, origKey, kDomain, 0, "Image");
301 }
302}
303
Brian Osmandf7e0752017-04-26 16:20:28 -0400304class Generator_GrYUVProvider : public GrYUVProvider {
Brian Osmandf7e0752017-04-26 16:20:28 -0400305public:
306 Generator_GrYUVProvider(SkImageGenerator* gen) : fGen(gen) {}
307
Robert Phillipsb4a8eac2018-09-21 08:26:33 -0400308private:
309 uint32_t onGetID() const override { return fGen->uniqueID(); }
Jim Van Verthe24b5872018-10-29 16:26:02 -0400310 bool onQueryYUVA8(SkYUVASizeInfo* sizeInfo,
Jim Van Verth8f11e432018-10-18 14:36:59 -0400311 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
312 SkYUVColorSpace* colorSpace) const override {
313 return fGen->queryYUVA8(sizeInfo, yuvaIndices, colorSpace);
Brian Osmandf7e0752017-04-26 16:20:28 -0400314 }
Jim Van Verthe24b5872018-10-29 16:26:02 -0400315 bool onGetYUVA8Planes(const SkYUVASizeInfo& sizeInfo,
Jim Van Verth8f11e432018-10-18 14:36:59 -0400316 const SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
317 void* planes[]) override {
318 return fGen->getYUVA8Planes(sizeInfo, yuvaIndices, planes);
Brian Osmandf7e0752017-04-26 16:20:28 -0400319 }
Robert Phillipsb4a8eac2018-09-21 08:26:33 -0400320
321 SkImageGenerator* fGen;
322
323 typedef GrYUVProvider INHERITED;
Brian Osmandf7e0752017-04-26 16:20:28 -0400324};
325
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500326static void set_key_on_proxy(GrProxyProvider* proxyProvider,
Greg Danielfc5060d2017-10-04 18:36:15 +0000327 GrTextureProxy* proxy, GrTextureProxy* originalProxy,
328 const GrUniqueKey& key) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400329 if (key.isValid()) {
Greg Danielf6f7b672018-02-15 13:06:26 -0500330 if (originalProxy && originalProxy->getUniqueKey().isValid()) {
331 SkASSERT(originalProxy->getUniqueKey() == key);
Greg Daniele252f082017-10-23 16:05:23 -0400332 SkASSERT(GrMipMapped::kYes == proxy->mipMapped() &&
333 GrMipMapped::kNo == originalProxy->mipMapped());
Greg Danielf6f7b672018-02-15 13:06:26 -0500334 // If we had an originalProxy with a valid key, that means there already is a proxy in
335 // the cache which matches the key, but it does not have mip levels and we require them.
336 // Thus we must remove the unique key from that proxy.
Chris Dalton2de13dd2019-01-03 15:11:59 -0700337 SkASSERT(originalProxy->getUniqueKey() == key);
338 proxyProvider->removeUniqueKeyFromProxy(originalProxy);
Greg Danielfc5060d2017-10-04 18:36:15 +0000339 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500340 proxyProvider->assignUniqueKeyToProxy(key, proxy);
Brian Osmandf7e0752017-04-26 16:20:28 -0400341 }
342}
343
Jim Van Verthe24b5872018-10-29 16:26:02 -0400344sk_sp<SkCachedData> SkImage_Lazy::getPlanes(SkYUVASizeInfo* yuvaSizeInfo,
Jim Van Verth8f11e432018-10-18 14:36:59 -0400345 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
Robert Phillipsb4a8eac2018-09-21 08:26:33 -0400346 SkYUVColorSpace* yuvColorSpace,
Jim Van Verthe24b5872018-10-29 16:26:02 -0400347 const void* planes[SkYUVASizeInfo::kMaxCount]) {
Robert Phillipsb4a8eac2018-09-21 08:26:33 -0400348 ScopedGenerator generator(fSharedGenerator);
349 Generator_GrYUVProvider provider(generator);
350
Jim Van Verth8f11e432018-10-18 14:36:59 -0400351 sk_sp<SkCachedData> data = provider.getPlanes(yuvaSizeInfo, yuvaIndices, yuvColorSpace, planes);
Robert Phillipsb4a8eac2018-09-21 08:26:33 -0400352 if (!data) {
353 return nullptr;
354 }
355
356 return data;
357}
358
359
Brian Osmandf7e0752017-04-26 16:20:28 -0400360/*
361 * We have 4 ways to try to return a texture (in sorted order)
362 *
363 * 1. Check the cache for a pre-existing one
364 * 2. Ask the generator to natively create one
365 * 3. Ask the generator to return YUV planes, which the GPU can convert
366 * 4. Ask the generator to return RGB(A) data, which the GPU can convert
367 */
Brian Osmanbd659552018-09-11 10:03:19 -0400368sk_sp<GrTextureProxy> SkImage_Lazy::lockTextureProxy(
Robert Phillips9338c602019-02-19 12:52:29 -0500369 GrRecordingContext* ctx,
Brian Osmanbd659552018-09-11 10:03:19 -0400370 const GrUniqueKey& origKey,
371 SkImage::CachingHint chint,
372 bool willBeMipped,
Brian Osmanbd659552018-09-11 10:03:19 -0400373 GrTextureMaker::AllowedTexGenType genType) const {
Brian Osmandf7e0752017-04-26 16:20:28 -0400374 // Values representing the various texture lock paths we can take. Used for logging the path
375 // taken to a histogram.
376 enum LockTexturePath {
377 kFailure_LockTexturePath,
378 kPreExisting_LockTexturePath,
379 kNative_LockTexturePath,
380 kCompressed_LockTexturePath, // Deprecated
381 kYUV_LockTexturePath,
382 kRGBA_LockTexturePath,
383 };
384
385 enum { kLockTexturePathCount = kRGBA_LockTexturePath + 1 };
386
Brian Osman10494e32018-09-10 12:45:18 -0400387 // Build our texture key.
Greg Daniel25ceb1c2018-09-27 17:01:41 -0400388 // Even though some proxies created here may have a specific origin and use that origin, we do
389 // not include that in the key. Since SkImages are meant to be immutable, a given SkImage will
390 // always have an associated proxy that is always one origin or the other. It never can change
391 // origins. Thus we don't need to include that info in the key iteself.
Brian Osman10494e32018-09-10 12:45:18 -0400392 GrUniqueKey key;
393 this->makeCacheKeyFromOrigKey(origKey, &key);
Brian Osmandf7e0752017-04-26 16:20:28 -0400394
Robert Phillips9da87e02019-02-04 13:26:26 -0500395 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
Greg Danielfc5060d2017-10-04 18:36:15 +0000396 sk_sp<GrTextureProxy> proxy;
397
Brian Osmandf7e0752017-04-26 16:20:28 -0400398 // 1. Check the cache for a pre-existing one
399 if (key.isValid()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500400 proxy = proxyProvider->findOrCreateProxyByUniqueKey(key, kTopLeft_GrSurfaceOrigin);
Greg Danielfc5060d2017-10-04 18:36:15 +0000401 if (proxy) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400402 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kPreExisting_LockTexturePath,
403 kLockTexturePathCount);
Greg Daniele252f082017-10-23 16:05:23 -0400404 if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
Greg Danielfc5060d2017-10-04 18:36:15 +0000405 return proxy;
406 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400407 }
408 }
409
Brian Osmandf7e0752017-04-26 16:20:28 -0400410 // 2. Ask the generator to natively create one
Greg Danielfc5060d2017-10-04 18:36:15 +0000411 if (!proxy) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400412 ScopedGenerator generator(fSharedGenerator);
Stan Ilievba81af22017-06-08 15:16:53 -0400413 if (GrTextureMaker::AllowedTexGenType::kCheap == genType &&
414 SkImageGenerator::TexGenType::kCheap != generator->onCanGenerateTexture()) {
415 return nullptr;
416 }
Brian Osmanb3f38302018-09-07 15:24:44 -0400417 if ((proxy = generator->generateTexture(ctx, fInfo, fOrigin, willBeMipped))) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400418 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kNative_LockTexturePath,
419 kLockTexturePathCount);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500420 set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key);
Greg Daniele252f082017-10-23 16:05:23 -0400421 if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
Greg Daniel2ad08202018-09-07 09:13:36 -0400422 *fUniqueKeyInvalidatedMessages.append() =
Robert Phillips9da87e02019-02-04 13:26:26 -0500423 new GrUniqueKeyInvalidatedMessage(key, ctx->priv().contextID());
Greg Danielfc5060d2017-10-04 18:36:15 +0000424 return proxy;
425 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400426 }
427 }
428
Greg Daniel3e70fa32017-10-05 16:27:06 -0400429 // 3. Ask the generator to return YUV planes, which the GPU can convert. If we will be mipping
430 // the texture we fall through here and have the CPU generate the mip maps for us.
Robert Phillips9da87e02019-02-04 13:26:26 -0500431 if (!proxy && !willBeMipped && !ctx->priv().options().fDisableGpuYUVConversion) {
Brian Osmanb3f38302018-09-07 15:24:44 -0400432 const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(fInfo);
Greg Daniel4065d452018-11-16 15:43:41 -0500433
434 SkColorType colorType = fInfo.colorType();
435 GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500436 ctx->priv().caps()->getBackendFormatFromColorType(colorType);
Greg Daniel4065d452018-11-16 15:43:41 -0500437
Brian Osmandf7e0752017-04-26 16:20:28 -0400438 ScopedGenerator generator(fSharedGenerator);
439 Generator_GrYUVProvider provider(generator);
Christopher Cameron77e96662017-07-08 01:47:47 -0700440
Brian Osmanf48c9962019-01-14 11:15:50 -0500441 // The pixels in the texture will be in the generator's color space.
442 // If onMakeColorTypeAndColorSpace has been called then this will not match this image's
443 // color space. To correct this, apply a color space conversion from the generator's color
444 // space to this image's color space.
Brian Osman861ea5b2018-06-14 09:14:03 -0400445 SkColorSpace* generatorColorSpace = fSharedGenerator->fGenerator->getInfo().colorSpace();
446 SkColorSpace* thisColorSpace = fInfo.colorSpace();
Christopher Cameron77e96662017-07-08 01:47:47 -0700447
Mike Kleinae5e8642018-10-03 17:00:41 -0400448 // TODO: Update to create the mipped surface in the YUV generator and draw the base
449 // layer directly into the mipped surface.
Greg Daniel4065d452018-11-16 15:43:41 -0500450 proxy = provider.refAsTextureProxy(ctx, format, desc, generatorColorSpace, thisColorSpace);
Mike Kleinae5e8642018-10-03 17:00:41 -0400451 if (proxy) {
452 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kYUV_LockTexturePath,
453 kLockTexturePathCount);
454 set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key);
455 *fUniqueKeyInvalidatedMessages.append() =
Robert Phillips9da87e02019-02-04 13:26:26 -0500456 new GrUniqueKeyInvalidatedMessage(key, ctx->priv().contextID());
Mike Kleinae5e8642018-10-03 17:00:41 -0400457 return proxy;
Brian Osmandf7e0752017-04-26 16:20:28 -0400458 }
459 }
460
461 // 4. Ask the generator to return RGB(A) data, which the GPU can convert
462 SkBitmap bitmap;
Brian Osman00766bf2018-10-22 15:59:23 -0400463 if (!proxy && this->getROPixels(&bitmap, chint)) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400464 if (willBeMipped) {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400465 proxy = proxyProvider->createMipMapProxyFromBitmap(bitmap);
Brian Osmandf7e0752017-04-26 16:20:28 -0400466 }
467 if (!proxy) {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400468 proxy = GrUploadBitmapToTextureProxy(proxyProvider, bitmap);
Brian Osmandf7e0752017-04-26 16:20:28 -0400469 }
Greg Daniele252f082017-10-23 16:05:23 -0400470 if (proxy && (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped())) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400471 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kRGBA_LockTexturePath,
472 kLockTexturePathCount);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500473 set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key);
Greg Daniel2ad08202018-09-07 09:13:36 -0400474 *fUniqueKeyInvalidatedMessages.append() =
Robert Phillips9da87e02019-02-04 13:26:26 -0500475 new GrUniqueKeyInvalidatedMessage(key, ctx->priv().contextID());
Brian Osmandf7e0752017-04-26 16:20:28 -0400476 return proxy;
477 }
478 }
Greg Danielfc5060d2017-10-04 18:36:15 +0000479
480 if (proxy) {
481 // We need a mipped proxy, but we either found a proxy earlier that wasn't mipped, generated
482 // a native non mipped proxy, or generated a non-mipped yuv proxy. Thus we generate a new
483 // mipped surface and copy the original proxy into the base layer. We will then let the gpu
484 // generate the rest of the mips.
485 SkASSERT(willBeMipped);
Greg Daniele252f082017-10-23 16:05:23 -0400486 SkASSERT(GrMipMapped::kNo == proxy->mipMapped());
Greg Daniel2ad08202018-09-07 09:13:36 -0400487 *fUniqueKeyInvalidatedMessages.append() =
Robert Phillips9da87e02019-02-04 13:26:26 -0500488 new GrUniqueKeyInvalidatedMessage(key, ctx->priv().contextID());
Greg Daniele1da1d92017-10-06 15:59:27 -0400489 if (auto mippedProxy = GrCopyBaseMipMapToTextureProxy(ctx, proxy.get())) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500490 set_key_on_proxy(proxyProvider, mippedProxy.get(), proxy.get(), key);
Greg Danielfc5060d2017-10-04 18:36:15 +0000491 return mippedProxy;
492 }
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400493 // We failed to make a mipped proxy with the base copied into it. This could have
494 // been from failure to make the proxy or failure to do the copy. Thus we will fall
495 // back to just using the non mipped proxy; See skbug.com/7094.
496 return proxy;
Greg Danielfc5060d2017-10-04 18:36:15 +0000497 }
498
Brian Osmandf7e0752017-04-26 16:20:28 -0400499 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kFailure_LockTexturePath,
500 kLockTexturePathCount);
501 return nullptr;
502}
503
504///////////////////////////////////////////////////////////////////////////////////////////////////
505
506#endif