blob: db85e951a113bbdd2f99f72cf584239c8ac1ab36 [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
Brian Osmanf1b43822017-04-20 13:43:23 -0400253sk_sp<SkImage> SkImage_Lazy::onMakeSubset(const SkIRect& subset) const {
Brian Osmandf7e0752017-04-26 16:20:28 -0400254 SkASSERT(fInfo.bounds().contains(subset));
255 SkASSERT(fInfo.bounds() != subset);
reed7b6945b2015-09-24 00:50:58 -0700256
Brian Osmandf7e0752017-04-26 16:20:28 -0400257 const SkIRect generatorSubset = subset.makeOffset(fOrigin.x(), fOrigin.y());
Brian Osmanf48c9962019-01-14 11:15:50 -0500258 const SkColorType colorType = fInfo.colorType();
259 Validator validator(fSharedGenerator, &generatorSubset, &colorType, fInfo.refColorSpace());
Brian Osmanf1b43822017-04-20 13:43:23 -0400260 return validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr;
reed7b6945b2015-09-24 00:50:58 -0700261}
262
Brian Osmanf48c9962019-01-14 11:15:50 -0500263sk_sp<SkImage> SkImage_Lazy::onMakeColorTypeAndColorSpace(SkColorType targetCT,
264 sk_sp<SkColorSpace> targetCS) const {
265 SkAutoExclusive autoAquire(fOnMakeColorTypeAndSpaceMutex);
266 if (fOnMakeColorTypeAndSpaceResult &&
267 targetCT == fOnMakeColorTypeAndSpaceResult->colorType() &&
268 SkColorSpace::Equals(targetCS.get(), fOnMakeColorTypeAndSpaceResult->colorSpace())) {
269 return fOnMakeColorTypeAndSpaceResult;
Christopher Camerond4b67872017-07-13 15:18:08 -0700270 }
Christopher Cameron77e96662017-07-08 01:47:47 -0700271 const SkIRect generatorSubset =
272 SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width(), fInfo.height());
Brian Osmanf48c9962019-01-14 11:15:50 -0500273 Validator validator(fSharedGenerator, &generatorSubset, &targetCT, targetCS);
Christopher Camerond4b67872017-07-13 15:18:08 -0700274 sk_sp<SkImage> result = validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr;
275 if (result) {
Brian Osmanf48c9962019-01-14 11:15:50 -0500276 fOnMakeColorTypeAndSpaceResult = result;
Christopher Camerond4b67872017-07-13 15:18:08 -0700277 }
278 return result;
Matt Sarett6de13102017-03-14 14:10:48 -0400279}
280
Mike Reed185130c2017-02-15 15:14:16 -0500281sk_sp<SkImage> SkImage::MakeFromGenerator(std::unique_ptr<SkImageGenerator> generator,
282 const SkIRect* subset) {
Brian Osmanf48c9962019-01-14 11:15:50 -0500283 SkImage_Lazy::Validator
284 validator(SharedGenerator::Make(std::move(generator)), subset, nullptr, nullptr);
fmalita7929e3a2016-10-27 08:15:44 -0700285
Brian Osmanf1b43822017-04-20 13:43:23 -0400286 return validator ? sk_make_sp<SkImage_Lazy>(&validator) : nullptr;
reed85d91782015-09-10 14:33:38 -0700287}
Brian Osmandf7e0752017-04-26 16:20:28 -0400288
289//////////////////////////////////////////////////////////////////////////////////////////////////
290
Brian Osmandf7e0752017-04-26 16:20:28 -0400291#if SK_SUPPORT_GPU
292
Brian Osmanbd659552018-09-11 10:03:19 -0400293void SkImage_Lazy::makeCacheKeyFromOrigKey(const GrUniqueKey& origKey,
294 GrUniqueKey* cacheKey) const {
Brian Osman10494e32018-09-10 12:45:18 -0400295 SkASSERT(!cacheKey->isValid());
296 if (origKey.isValid()) {
297 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
298 GrUniqueKey::Builder builder(cacheKey, origKey, kDomain, 0, "Image");
299 }
300}
301
Brian Osmandf7e0752017-04-26 16:20:28 -0400302class Generator_GrYUVProvider : public GrYUVProvider {
Brian Osmandf7e0752017-04-26 16:20:28 -0400303public:
304 Generator_GrYUVProvider(SkImageGenerator* gen) : fGen(gen) {}
305
Robert Phillipsb4a8eac2018-09-21 08:26:33 -0400306private:
307 uint32_t onGetID() const override { return fGen->uniqueID(); }
Jim Van Verthe24b5872018-10-29 16:26:02 -0400308 bool onQueryYUVA8(SkYUVASizeInfo* sizeInfo,
Jim Van Verth8f11e432018-10-18 14:36:59 -0400309 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
310 SkYUVColorSpace* colorSpace) const override {
311 return fGen->queryYUVA8(sizeInfo, yuvaIndices, colorSpace);
Brian Osmandf7e0752017-04-26 16:20:28 -0400312 }
Jim Van Verthe24b5872018-10-29 16:26:02 -0400313 bool onGetYUVA8Planes(const SkYUVASizeInfo& sizeInfo,
Jim Van Verth8f11e432018-10-18 14:36:59 -0400314 const SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
315 void* planes[]) override {
316 return fGen->getYUVA8Planes(sizeInfo, yuvaIndices, planes);
Brian Osmandf7e0752017-04-26 16:20:28 -0400317 }
Robert Phillipsb4a8eac2018-09-21 08:26:33 -0400318
319 SkImageGenerator* fGen;
320
321 typedef GrYUVProvider INHERITED;
Brian Osmandf7e0752017-04-26 16:20:28 -0400322};
323
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500324static void set_key_on_proxy(GrProxyProvider* proxyProvider,
Greg Danielfc5060d2017-10-04 18:36:15 +0000325 GrTextureProxy* proxy, GrTextureProxy* originalProxy,
326 const GrUniqueKey& key) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400327 if (key.isValid()) {
Greg Danielf6f7b672018-02-15 13:06:26 -0500328 if (originalProxy && originalProxy->getUniqueKey().isValid()) {
329 SkASSERT(originalProxy->getUniqueKey() == key);
Greg Daniele252f082017-10-23 16:05:23 -0400330 SkASSERT(GrMipMapped::kYes == proxy->mipMapped() &&
331 GrMipMapped::kNo == originalProxy->mipMapped());
Greg Danielf6f7b672018-02-15 13:06:26 -0500332 // If we had an originalProxy with a valid key, that means there already is a proxy in
333 // the cache which matches the key, but it does not have mip levels and we require them.
334 // Thus we must remove the unique key from that proxy.
Chris Dalton2de13dd2019-01-03 15:11:59 -0700335 SkASSERT(originalProxy->getUniqueKey() == key);
336 proxyProvider->removeUniqueKeyFromProxy(originalProxy);
Greg Danielfc5060d2017-10-04 18:36:15 +0000337 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500338 proxyProvider->assignUniqueKeyToProxy(key, proxy);
Brian Osmandf7e0752017-04-26 16:20:28 -0400339 }
340}
341
Jim Van Verthe24b5872018-10-29 16:26:02 -0400342sk_sp<SkCachedData> SkImage_Lazy::getPlanes(SkYUVASizeInfo* yuvaSizeInfo,
Jim Van Verth8f11e432018-10-18 14:36:59 -0400343 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
Robert Phillipsb4a8eac2018-09-21 08:26:33 -0400344 SkYUVColorSpace* yuvColorSpace,
Jim Van Verthe24b5872018-10-29 16:26:02 -0400345 const void* planes[SkYUVASizeInfo::kMaxCount]) {
Robert Phillipsb4a8eac2018-09-21 08:26:33 -0400346 ScopedGenerator generator(fSharedGenerator);
347 Generator_GrYUVProvider provider(generator);
348
Jim Van Verth8f11e432018-10-18 14:36:59 -0400349 sk_sp<SkCachedData> data = provider.getPlanes(yuvaSizeInfo, yuvaIndices, yuvColorSpace, planes);
Robert Phillipsb4a8eac2018-09-21 08:26:33 -0400350 if (!data) {
351 return nullptr;
352 }
353
354 return data;
355}
356
357
Brian Osmandf7e0752017-04-26 16:20:28 -0400358/*
359 * We have 4 ways to try to return a texture (in sorted order)
360 *
361 * 1. Check the cache for a pre-existing one
362 * 2. Ask the generator to natively create one
363 * 3. Ask the generator to return YUV planes, which the GPU can convert
364 * 4. Ask the generator to return RGB(A) data, which the GPU can convert
365 */
Brian Osmanbd659552018-09-11 10:03:19 -0400366sk_sp<GrTextureProxy> SkImage_Lazy::lockTextureProxy(
Robert Phillips9338c602019-02-19 12:52:29 -0500367 GrRecordingContext* ctx,
Brian Osmanbd659552018-09-11 10:03:19 -0400368 const GrUniqueKey& origKey,
369 SkImage::CachingHint chint,
370 bool willBeMipped,
Brian Osmanbd659552018-09-11 10:03:19 -0400371 GrTextureMaker::AllowedTexGenType genType) const {
Brian Osmandf7e0752017-04-26 16:20:28 -0400372 // Values representing the various texture lock paths we can take. Used for logging the path
373 // taken to a histogram.
374 enum LockTexturePath {
375 kFailure_LockTexturePath,
376 kPreExisting_LockTexturePath,
377 kNative_LockTexturePath,
378 kCompressed_LockTexturePath, // Deprecated
379 kYUV_LockTexturePath,
380 kRGBA_LockTexturePath,
381 };
382
383 enum { kLockTexturePathCount = kRGBA_LockTexturePath + 1 };
384
Brian Osman10494e32018-09-10 12:45:18 -0400385 // Build our texture key.
Greg Daniel25ceb1c2018-09-27 17:01:41 -0400386 // Even though some proxies created here may have a specific origin and use that origin, we do
387 // not include that in the key. Since SkImages are meant to be immutable, a given SkImage will
388 // always have an associated proxy that is always one origin or the other. It never can change
389 // origins. Thus we don't need to include that info in the key iteself.
Brian Osman10494e32018-09-10 12:45:18 -0400390 GrUniqueKey key;
391 this->makeCacheKeyFromOrigKey(origKey, &key);
Brian Osmandf7e0752017-04-26 16:20:28 -0400392
Robert Phillips9da87e02019-02-04 13:26:26 -0500393 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
Greg Danielfc5060d2017-10-04 18:36:15 +0000394 sk_sp<GrTextureProxy> proxy;
395
Brian Osmandf7e0752017-04-26 16:20:28 -0400396 // 1. Check the cache for a pre-existing one
397 if (key.isValid()) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500398 proxy = proxyProvider->findOrCreateProxyByUniqueKey(key, kTopLeft_GrSurfaceOrigin);
Greg Danielfc5060d2017-10-04 18:36:15 +0000399 if (proxy) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400400 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kPreExisting_LockTexturePath,
401 kLockTexturePathCount);
Greg Daniele252f082017-10-23 16:05:23 -0400402 if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
Greg Danielfc5060d2017-10-04 18:36:15 +0000403 return proxy;
404 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400405 }
406 }
407
Brian Osmandf7e0752017-04-26 16:20:28 -0400408 // 2. Ask the generator to natively create one
Greg Danielfc5060d2017-10-04 18:36:15 +0000409 if (!proxy) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400410 ScopedGenerator generator(fSharedGenerator);
Stan Ilievba81af22017-06-08 15:16:53 -0400411 if (GrTextureMaker::AllowedTexGenType::kCheap == genType &&
412 SkImageGenerator::TexGenType::kCheap != generator->onCanGenerateTexture()) {
413 return nullptr;
414 }
Brian Osmanb3f38302018-09-07 15:24:44 -0400415 if ((proxy = generator->generateTexture(ctx, fInfo, fOrigin, willBeMipped))) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400416 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kNative_LockTexturePath,
417 kLockTexturePathCount);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500418 set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key);
Greg Daniele252f082017-10-23 16:05:23 -0400419 if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
Greg Daniel2ad08202018-09-07 09:13:36 -0400420 *fUniqueKeyInvalidatedMessages.append() =
Robert Phillips9da87e02019-02-04 13:26:26 -0500421 new GrUniqueKeyInvalidatedMessage(key, ctx->priv().contextID());
Greg Danielfc5060d2017-10-04 18:36:15 +0000422 return proxy;
423 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400424 }
425 }
426
Greg Daniel3e70fa32017-10-05 16:27:06 -0400427 // 3. Ask the generator to return YUV planes, which the GPU can convert. If we will be mipping
428 // the texture we fall through here and have the CPU generate the mip maps for us.
Robert Phillips9da87e02019-02-04 13:26:26 -0500429 if (!proxy && !willBeMipped && !ctx->priv().options().fDisableGpuYUVConversion) {
Brian Osmanb3f38302018-09-07 15:24:44 -0400430 const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(fInfo);
Greg Daniel4065d452018-11-16 15:43:41 -0500431
432 SkColorType colorType = fInfo.colorType();
433 GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500434 ctx->priv().caps()->getBackendFormatFromColorType(colorType);
Greg Daniel4065d452018-11-16 15:43:41 -0500435
Brian Osmandf7e0752017-04-26 16:20:28 -0400436 ScopedGenerator generator(fSharedGenerator);
437 Generator_GrYUVProvider provider(generator);
Christopher Cameron77e96662017-07-08 01:47:47 -0700438
Brian Osmanf48c9962019-01-14 11:15:50 -0500439 // The pixels in the texture will be in the generator's color space.
440 // If onMakeColorTypeAndColorSpace has been called then this will not match this image's
441 // color space. To correct this, apply a color space conversion from the generator's color
442 // space to this image's color space.
Brian Osman861ea5b2018-06-14 09:14:03 -0400443 SkColorSpace* generatorColorSpace = fSharedGenerator->fGenerator->getInfo().colorSpace();
444 SkColorSpace* thisColorSpace = fInfo.colorSpace();
Christopher Cameron77e96662017-07-08 01:47:47 -0700445
Mike Kleinae5e8642018-10-03 17:00:41 -0400446 // TODO: Update to create the mipped surface in the YUV generator and draw the base
447 // layer directly into the mipped surface.
Greg Daniel4065d452018-11-16 15:43:41 -0500448 proxy = provider.refAsTextureProxy(ctx, format, desc, generatorColorSpace, thisColorSpace);
Mike Kleinae5e8642018-10-03 17:00:41 -0400449 if (proxy) {
450 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kYUV_LockTexturePath,
451 kLockTexturePathCount);
452 set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key);
453 *fUniqueKeyInvalidatedMessages.append() =
Robert Phillips9da87e02019-02-04 13:26:26 -0500454 new GrUniqueKeyInvalidatedMessage(key, ctx->priv().contextID());
Mike Kleinae5e8642018-10-03 17:00:41 -0400455 return proxy;
Brian Osmandf7e0752017-04-26 16:20:28 -0400456 }
457 }
458
459 // 4. Ask the generator to return RGB(A) data, which the GPU can convert
460 SkBitmap bitmap;
Brian Osman00766bf2018-10-22 15:59:23 -0400461 if (!proxy && this->getROPixels(&bitmap, chint)) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400462 if (willBeMipped) {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400463 proxy = proxyProvider->createMipMapProxyFromBitmap(bitmap);
Brian Osmandf7e0752017-04-26 16:20:28 -0400464 }
465 if (!proxy) {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400466 proxy = GrUploadBitmapToTextureProxy(proxyProvider, bitmap);
Brian Osmandf7e0752017-04-26 16:20:28 -0400467 }
Greg Daniele252f082017-10-23 16:05:23 -0400468 if (proxy && (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped())) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400469 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kRGBA_LockTexturePath,
470 kLockTexturePathCount);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500471 set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key);
Greg Daniel2ad08202018-09-07 09:13:36 -0400472 *fUniqueKeyInvalidatedMessages.append() =
Robert Phillips9da87e02019-02-04 13:26:26 -0500473 new GrUniqueKeyInvalidatedMessage(key, ctx->priv().contextID());
Brian Osmandf7e0752017-04-26 16:20:28 -0400474 return proxy;
475 }
476 }
Greg Danielfc5060d2017-10-04 18:36:15 +0000477
478 if (proxy) {
479 // We need a mipped proxy, but we either found a proxy earlier that wasn't mipped, generated
480 // a native non mipped proxy, or generated a non-mipped yuv proxy. Thus we generate a new
481 // mipped surface and copy the original proxy into the base layer. We will then let the gpu
482 // generate the rest of the mips.
483 SkASSERT(willBeMipped);
Greg Daniele252f082017-10-23 16:05:23 -0400484 SkASSERT(GrMipMapped::kNo == proxy->mipMapped());
Greg Daniel2ad08202018-09-07 09:13:36 -0400485 *fUniqueKeyInvalidatedMessages.append() =
Robert Phillips9da87e02019-02-04 13:26:26 -0500486 new GrUniqueKeyInvalidatedMessage(key, ctx->priv().contextID());
Greg Daniele1da1d92017-10-06 15:59:27 -0400487 if (auto mippedProxy = GrCopyBaseMipMapToTextureProxy(ctx, proxy.get())) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500488 set_key_on_proxy(proxyProvider, mippedProxy.get(), proxy.get(), key);
Greg Danielfc5060d2017-10-04 18:36:15 +0000489 return mippedProxy;
490 }
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400491 // We failed to make a mipped proxy with the base copied into it. This could have
492 // been from failure to make the proxy or failure to do the copy. Thus we will fall
493 // back to just using the non mipped proxy; See skbug.com/7094.
494 return proxy;
Greg Danielfc5060d2017-10-04 18:36:15 +0000495 }
496
Brian Osmandf7e0752017-04-26 16:20:28 -0400497 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kFailure_LockTexturePath,
498 kLockTexturePathCount);
499 return nullptr;
500}
501
502///////////////////////////////////////////////////////////////////////////////////////////////////
503
504#endif