blob: 970335faf87ba188f7214a4e92500419c6c33618 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/image/SkImage_Lazy.h"
Brian Osmandf7e0752017-04-26 16:20:28 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkBitmap.h"
11#include "include/core/SkData.h"
12#include "include/core/SkImageGenerator.h"
13#include "src/core/SkBitmapCache.h"
14#include "src/core/SkCachedData.h"
15#include "src/core/SkImagePriv.h"
16#include "src/core/SkNextID.h"
reed85d91782015-09-10 14:33:38 -070017
Brian Osmandf7e0752017-04-26 16:20:28 -040018#if SK_SUPPORT_GPU
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/private/GrRecordingContext.h"
20#include "include/private/GrResourceKey.h"
21#include "src/gpu/GrCaps.h"
22#include "src/gpu/GrGpuResourcePriv.h"
23#include "src/gpu/GrImageTextureMaker.h"
24#include "src/gpu/GrProxyProvider.h"
25#include "src/gpu/GrRecordingContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040026#include "src/gpu/GrSamplerState.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrYUVProvider.h"
28#include "src/gpu/SkGr.h"
Brian Osmandf7e0752017-04-26 16:20:28 -040029#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;
Herb Derby9b869552019-05-10 12:16:17 -0400120 SkAutoMutexExclusive fAutoAquire;
Brian Osmandf7e0752017-04-26 16:20:28 -0400121};
122
123///////////////////////////////////////////////////////////////////////////////
124
125SkImage_Lazy::SkImage_Lazy(Validator* validator)
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400126 : INHERITED(validator->fInfo, validator->fUniqueID)
Brian Osmandf7e0752017-04-26 16:20:28 -0400127 , fSharedGenerator(std::move(validator->fSharedGenerator))
Brian Osmandf7e0752017-04-26 16:20:28 -0400128 , fOrigin(validator->fOrigin) {
129 SkASSERT(fSharedGenerator);
Brian Osmaneb7e5292018-08-08 14:32:06 -0400130 fUniqueID = validator->fUniqueID;
Brian Osmandf7e0752017-04-26 16:20:28 -0400131}
132
Brian Osmanbd659552018-09-11 10:03:19 -0400133SkImage_Lazy::~SkImage_Lazy() {
134#if SK_SUPPORT_GPU
135 for (int i = 0; i < fUniqueKeyInvalidatedMessages.count(); ++i) {
136 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(*fUniqueKeyInvalidatedMessages[i]);
137 }
138 fUniqueKeyInvalidatedMessages.deleteAll();
139#endif
140}
141
Brian Osmandf7e0752017-04-26 16:20:28 -0400142//////////////////////////////////////////////////////////////////////////////////////////////////
143
Brian Osmanc87cfb62018-07-11 09:08:46 -0400144static bool generate_pixels(SkImageGenerator* gen, const SkPixmap& pmap, int originX, int originY) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400145 const int genW = gen->getInfo().width();
146 const int genH = gen->getInfo().height();
147 const SkIRect srcR = SkIRect::MakeWH(genW, genH);
148 const SkIRect dstR = SkIRect::MakeXYWH(originX, originY, pmap.width(), pmap.height());
149 if (!srcR.contains(dstR)) {
150 return false;
151 }
152
153 // If they are requesting a subset, we have to have a temp allocation for full image, and
154 // then copy the subset into their allocation
155 SkBitmap full;
156 SkPixmap fullPM;
157 const SkPixmap* dstPM = &pmap;
158 if (srcR != dstR) {
159 if (!full.tryAllocPixels(pmap.info().makeWH(genW, genH))) {
160 return false;
161 }
162 if (!full.peekPixels(&fullPM)) {
163 return false;
164 }
165 dstPM = &fullPM;
166 }
167
Brian Osmanc87cfb62018-07-11 09:08:46 -0400168 if (!gen->getPixels(dstPM->info(), dstPM->writable_addr(), dstPM->rowBytes())) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400169 return false;
170 }
171
172 if (srcR != dstR) {
173 if (!full.readPixels(pmap, originX, originY)) {
174 return false;
175 }
176 }
177 return true;
178}
179
Brian Osman00766bf2018-10-22 15:59:23 -0400180bool SkImage_Lazy::getROPixels(SkBitmap* bitmap, SkImage::CachingHint chint) const {
181 auto check_output_bitmap = [bitmap]() {
182 SkASSERT(bitmap->isImmutable());
183 SkASSERT(bitmap->getPixels());
184 (void)bitmap;
185 };
186
187 auto desc = SkBitmapCacheDesc::Make(this);
188 if (SkBitmapCache::Find(desc, bitmap)) {
189 check_output_bitmap();
Brian Osmandf7e0752017-04-26 16:20:28 -0400190 return true;
191 }
192
Brian Osmandf7e0752017-04-26 16:20:28 -0400193 if (SkImage::kAllow_CachingHint == chint) {
Brian Osman00766bf2018-10-22 15:59:23 -0400194 SkPixmap pmap;
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400195 SkBitmapCache::RecPtr cacheRec = SkBitmapCache::Alloc(desc, this->imageInfo(), &pmap);
Brian Osman00766bf2018-10-22 15:59:23 -0400196 if (!cacheRec ||
197 !generate_pixels(ScopedGenerator(fSharedGenerator), pmap,
198 fOrigin.x(), fOrigin.y())) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400199 return false;
200 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400201 SkBitmapCache::Add(std::move(cacheRec), bitmap);
Mike Reed30301c42018-07-19 09:39:21 -0400202 this->notifyAddedToRasterCache();
Brian Osmandf7e0752017-04-26 16:20:28 -0400203 } else {
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400204 if (!bitmap->tryAllocPixels(this->imageInfo()) ||
205 !generate_pixels(ScopedGenerator(fSharedGenerator), bitmap->pixmap(), fOrigin.x(),
206 fOrigin.y())) {
Brian Osman00766bf2018-10-22 15:59:23 -0400207 return false;
208 }
Brian Osmana0dc3d22018-10-09 15:17:38 -0400209 bitmap->setImmutable();
Brian Osmandf7e0752017-04-26 16:20:28 -0400210 }
211
Brian Osman00766bf2018-10-22 15:59:23 -0400212 check_output_bitmap();
Brian Osmandf7e0752017-04-26 16:20:28 -0400213 return true;
214}
215
216//////////////////////////////////////////////////////////////////////////////////////////////////
217
Brian Osmanf1b43822017-04-20 13:43:23 -0400218bool SkImage_Lazy::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
219 int srcX, int srcY, CachingHint chint) const {
reed85d91782015-09-10 14:33:38 -0700220 SkBitmap bm;
Brian Osmane50cdf02018-10-19 13:02:14 -0400221 if (this->getROPixels(&bm, chint)) {
reed85d91782015-09-10 14:33:38 -0700222 return bm.readPixels(dstInfo, dstPixels, dstRB, srcX, srcY);
223 }
224 return false;
225}
226
Ben Wagnerbdf54332018-05-15 14:12:14 -0400227sk_sp<SkData> SkImage_Lazy::onRefEncoded() const {
228 ScopedGenerator generator(fSharedGenerator);
229 return generator->refEncodedData();
230}
reed85d91782015-09-10 14:33:38 -0700231
Brian Osman5bbd0762017-05-08 11:07:42 -0400232bool SkImage_Lazy::onIsValid(GrContext* context) const {
233 ScopedGenerator generator(fSharedGenerator);
234 return generator->isValid(context);
235}
236
Brian Osmandf7e0752017-04-26 16:20:28 -0400237///////////////////////////////////////////////////////////////////////////////////////////////////
238
Robert Phillipsb726d582017-03-09 16:36:32 -0500239#if SK_SUPPORT_GPU
Robert Phillips9338c602019-02-19 12:52:29 -0500240sk_sp<GrTextureProxy> SkImage_Lazy::asTextureProxyRef(GrRecordingContext* context,
Brian Salomon2bbdcc42017-09-07 12:36:34 -0400241 const GrSamplerState& params,
Brian Osmanf1b43822017-04-20 13:43:23 -0400242 SkScalar scaleAdjust[2]) const {
Brian Osmandf7e0752017-04-26 16:20:28 -0400243 if (!context) {
244 return nullptr;
245 }
246
247 GrImageTextureMaker textureMaker(context, this, kAllow_CachingHint);
Brian Osman6064e1c2018-10-19 14:27:54 -0400248 return textureMaker.refTextureProxyForParams(params, scaleAdjust);
Robert Phillipsb726d582017-03-09 16:36:32 -0500249}
250#endif
251
Robert Phillips6603a172019-03-05 12:35:44 -0500252sk_sp<SkImage> SkImage_Lazy::onMakeSubset(GrRecordingContext* context,
253 const SkIRect& subset) const {
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400254 SkASSERT(this->bounds().contains(subset));
255 SkASSERT(this->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 Salomon5ad6fd32019-03-21 15:30:08 -0400258 const SkColorType colorType = this->colorType();
259 Validator validator(fSharedGenerator, &generatorSubset, &colorType, this->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
Robert Phillips6603a172019-03-05 12:35:44 -0500263sk_sp<SkImage> SkImage_Lazy::onMakeColorTypeAndColorSpace(GrRecordingContext*,
264 SkColorType targetCT,
Brian Osmanf48c9962019-01-14 11:15:50 -0500265 sk_sp<SkColorSpace> targetCS) const {
Herb Derby9b869552019-05-10 12:16:17 -0400266 SkAutoMutexExclusive autoAquire(fOnMakeColorTypeAndSpaceMutex);
Brian Osmanf48c9962019-01-14 11:15:50 -0500267 if (fOnMakeColorTypeAndSpaceResult &&
268 targetCT == fOnMakeColorTypeAndSpaceResult->colorType() &&
269 SkColorSpace::Equals(targetCS.get(), fOnMakeColorTypeAndSpaceResult->colorSpace())) {
270 return fOnMakeColorTypeAndSpaceResult;
Christopher Camerond4b67872017-07-13 15:18:08 -0700271 }
Christopher Cameron77e96662017-07-08 01:47:47 -0700272 const SkIRect generatorSubset =
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400273 SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), this->width(), this->height());
Brian Osmanf48c9962019-01-14 11:15:50 -0500274 Validator validator(fSharedGenerator, &generatorSubset, &targetCT, targetCS);
Christopher Camerond4b67872017-07-13 15:18:08 -0700275 sk_sp<SkImage> result = validator ? sk_sp<SkImage>(new SkImage_Lazy(&validator)) : nullptr;
276 if (result) {
Brian Osmanf48c9962019-01-14 11:15:50 -0500277 fOnMakeColorTypeAndSpaceResult = result;
Christopher Camerond4b67872017-07-13 15:18:08 -0700278 }
279 return result;
Matt Sarett6de13102017-03-14 14:10:48 -0400280}
281
Mike Reed185130c2017-02-15 15:14:16 -0500282sk_sp<SkImage> SkImage::MakeFromGenerator(std::unique_ptr<SkImageGenerator> generator,
283 const SkIRect* subset) {
Brian Osmanf48c9962019-01-14 11:15:50 -0500284 SkImage_Lazy::Validator
285 validator(SharedGenerator::Make(std::move(generator)), subset, nullptr, nullptr);
fmalita7929e3a2016-10-27 08:15:44 -0700286
Brian Osmanf1b43822017-04-20 13:43:23 -0400287 return validator ? sk_make_sp<SkImage_Lazy>(&validator) : nullptr;
reed85d91782015-09-10 14:33:38 -0700288}
Brian Osmandf7e0752017-04-26 16:20:28 -0400289
Mike Reeda5acbf92019-08-14 11:08:41 -0400290sk_sp<SkImage> SkImage::DecodeToRaster(const void* encoded, size_t length, const SkIRect* subset) {
291 // The generator will not outlive this function, so we can wrap the encoded data without copy
292 auto gen = SkImageGenerator::MakeFromEncoded(SkData::MakeWithoutCopy(encoded, length));
293 if (!gen) {
294 return nullptr;
295 }
296 SkImageInfo info = gen->getInfo();
297 if (info.isEmpty()) {
298 return nullptr;
299 }
300
301 SkIPoint origin = {0, 0};
302 if (subset) {
303 if (!SkIRect::MakeWH(info.width(), info.height()).contains(*subset)) {
304 return nullptr;
305 }
306 info = info.makeWH(subset->width(), subset->height());
307 origin = {subset->x(), subset->y()};
308 }
309
310 size_t rb = info.minRowBytes();
311 if (rb == 0) {
312 return nullptr; // rb was too big
313 }
314 size_t size = info.computeByteSize(rb);
315 if (size == SIZE_MAX) {
316 return nullptr;
317 }
318 auto data = SkData::MakeUninitialized(size);
319
320 SkPixmap pmap(info, data->writable_data(), rb);
321 if (!generate_pixels(gen.get(), pmap, origin.x(), origin.y())) {
322 return nullptr;
323 }
324
325 return SkImage::MakeRasterData(info, data, rb);
326}
327
Brian Osmandf7e0752017-04-26 16:20:28 -0400328//////////////////////////////////////////////////////////////////////////////////////////////////
329
Brian Osmandf7e0752017-04-26 16:20:28 -0400330#if SK_SUPPORT_GPU
331
Brian Osmanbd659552018-09-11 10:03:19 -0400332void SkImage_Lazy::makeCacheKeyFromOrigKey(const GrUniqueKey& origKey,
333 GrUniqueKey* cacheKey) const {
Brian Osman10494e32018-09-10 12:45:18 -0400334 SkASSERT(!cacheKey->isValid());
335 if (origKey.isValid()) {
336 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
337 GrUniqueKey::Builder builder(cacheKey, origKey, kDomain, 0, "Image");
338 }
339}
340
Brian Osmandf7e0752017-04-26 16:20:28 -0400341class Generator_GrYUVProvider : public GrYUVProvider {
Brian Osmandf7e0752017-04-26 16:20:28 -0400342public:
343 Generator_GrYUVProvider(SkImageGenerator* gen) : fGen(gen) {}
344
Robert Phillipsb4a8eac2018-09-21 08:26:33 -0400345private:
346 uint32_t onGetID() const override { return fGen->uniqueID(); }
Jim Van Verthe24b5872018-10-29 16:26:02 -0400347 bool onQueryYUVA8(SkYUVASizeInfo* sizeInfo,
Jim Van Verth8f11e432018-10-18 14:36:59 -0400348 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
349 SkYUVColorSpace* colorSpace) const override {
350 return fGen->queryYUVA8(sizeInfo, yuvaIndices, colorSpace);
Brian Osmandf7e0752017-04-26 16:20:28 -0400351 }
Jim Van Verthe24b5872018-10-29 16:26:02 -0400352 bool onGetYUVA8Planes(const SkYUVASizeInfo& sizeInfo,
Jim Van Verth8f11e432018-10-18 14:36:59 -0400353 const SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
354 void* planes[]) override {
355 return fGen->getYUVA8Planes(sizeInfo, yuvaIndices, planes);
Brian Osmandf7e0752017-04-26 16:20:28 -0400356 }
Robert Phillipsb4a8eac2018-09-21 08:26:33 -0400357
358 SkImageGenerator* fGen;
359
360 typedef GrYUVProvider INHERITED;
Brian Osmandf7e0752017-04-26 16:20:28 -0400361};
362
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500363static void set_key_on_proxy(GrProxyProvider* proxyProvider,
Greg Danielfc5060d2017-10-04 18:36:15 +0000364 GrTextureProxy* proxy, GrTextureProxy* originalProxy,
365 const GrUniqueKey& key) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400366 if (key.isValid()) {
Greg Danielf6f7b672018-02-15 13:06:26 -0500367 if (originalProxy && originalProxy->getUniqueKey().isValid()) {
368 SkASSERT(originalProxy->getUniqueKey() == key);
Greg Daniele252f082017-10-23 16:05:23 -0400369 SkASSERT(GrMipMapped::kYes == proxy->mipMapped() &&
370 GrMipMapped::kNo == originalProxy->mipMapped());
Greg Danielf6f7b672018-02-15 13:06:26 -0500371 // If we had an originalProxy with a valid key, that means there already is a proxy in
372 // the cache which matches the key, but it does not have mip levels and we require them.
373 // Thus we must remove the unique key from that proxy.
Chris Dalton2de13dd2019-01-03 15:11:59 -0700374 SkASSERT(originalProxy->getUniqueKey() == key);
375 proxyProvider->removeUniqueKeyFromProxy(originalProxy);
Greg Danielfc5060d2017-10-04 18:36:15 +0000376 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500377 proxyProvider->assignUniqueKeyToProxy(key, proxy);
Brian Osmandf7e0752017-04-26 16:20:28 -0400378 }
379}
380
Jim Van Verthe24b5872018-10-29 16:26:02 -0400381sk_sp<SkCachedData> SkImage_Lazy::getPlanes(SkYUVASizeInfo* yuvaSizeInfo,
Jim Van Verth8f11e432018-10-18 14:36:59 -0400382 SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
Robert Phillipsb4a8eac2018-09-21 08:26:33 -0400383 SkYUVColorSpace* yuvColorSpace,
Jim Van Verthe24b5872018-10-29 16:26:02 -0400384 const void* planes[SkYUVASizeInfo::kMaxCount]) {
Robert Phillipsb4a8eac2018-09-21 08:26:33 -0400385 ScopedGenerator generator(fSharedGenerator);
386 Generator_GrYUVProvider provider(generator);
387
Jim Van Verth8f11e432018-10-18 14:36:59 -0400388 sk_sp<SkCachedData> data = provider.getPlanes(yuvaSizeInfo, yuvaIndices, yuvColorSpace, planes);
Robert Phillipsb4a8eac2018-09-21 08:26:33 -0400389 if (!data) {
390 return nullptr;
391 }
392
393 return data;
394}
395
396
Brian Osmandf7e0752017-04-26 16:20:28 -0400397/*
398 * We have 4 ways to try to return a texture (in sorted order)
399 *
400 * 1. Check the cache for a pre-existing one
401 * 2. Ask the generator to natively create one
402 * 3. Ask the generator to return YUV planes, which the GPU can convert
403 * 4. Ask the generator to return RGB(A) data, which the GPU can convert
404 */
Brian Osmanbd659552018-09-11 10:03:19 -0400405sk_sp<GrTextureProxy> SkImage_Lazy::lockTextureProxy(
Robert Phillips9338c602019-02-19 12:52:29 -0500406 GrRecordingContext* ctx,
Brian Osmanbd659552018-09-11 10:03:19 -0400407 const GrUniqueKey& origKey,
408 SkImage::CachingHint chint,
409 bool willBeMipped,
Brian Osmanbd659552018-09-11 10:03:19 -0400410 GrTextureMaker::AllowedTexGenType genType) const {
Brian Osmandf7e0752017-04-26 16:20:28 -0400411 // Values representing the various texture lock paths we can take. Used for logging the path
412 // taken to a histogram.
413 enum LockTexturePath {
414 kFailure_LockTexturePath,
415 kPreExisting_LockTexturePath,
416 kNative_LockTexturePath,
417 kCompressed_LockTexturePath, // Deprecated
418 kYUV_LockTexturePath,
419 kRGBA_LockTexturePath,
420 };
421
422 enum { kLockTexturePathCount = kRGBA_LockTexturePath + 1 };
423
Brian Osman10494e32018-09-10 12:45:18 -0400424 // Build our texture key.
Greg Daniel25ceb1c2018-09-27 17:01:41 -0400425 // Even though some proxies created here may have a specific origin and use that origin, we do
426 // not include that in the key. Since SkImages are meant to be immutable, a given SkImage will
427 // always have an associated proxy that is always one origin or the other. It never can change
428 // origins. Thus we don't need to include that info in the key iteself.
Brian Osman10494e32018-09-10 12:45:18 -0400429 GrUniqueKey key;
430 this->makeCacheKeyFromOrigKey(origKey, &key);
Brian Osmandf7e0752017-04-26 16:20:28 -0400431
Robert Phillips9da87e02019-02-04 13:26:26 -0500432 GrProxyProvider* proxyProvider = ctx->priv().proxyProvider();
Greg Danielfc5060d2017-10-04 18:36:15 +0000433 sk_sp<GrTextureProxy> proxy;
434
Brian Osmandf7e0752017-04-26 16:20:28 -0400435 // 1. Check the cache for a pre-existing one
436 if (key.isValid()) {
Brian Salomon2af3e702019-08-11 19:10:31 -0400437 auto ct = SkColorTypeToGrColorType(this->colorType());
438 proxy = proxyProvider->findOrCreateProxyByUniqueKey(key, ct, kTopLeft_GrSurfaceOrigin);
Greg Danielfc5060d2017-10-04 18:36:15 +0000439 if (proxy) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400440 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kPreExisting_LockTexturePath,
441 kLockTexturePathCount);
Greg Daniele252f082017-10-23 16:05:23 -0400442 if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
Greg Danielfc5060d2017-10-04 18:36:15 +0000443 return proxy;
444 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400445 }
446 }
447
Brian Osmandf7e0752017-04-26 16:20:28 -0400448 // 2. Ask the generator to natively create one
Greg Danielfc5060d2017-10-04 18:36:15 +0000449 if (!proxy) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400450 ScopedGenerator generator(fSharedGenerator);
Stan Ilievba81af22017-06-08 15:16:53 -0400451 if (GrTextureMaker::AllowedTexGenType::kCheap == genType &&
452 SkImageGenerator::TexGenType::kCheap != generator->onCanGenerateTexture()) {
453 return nullptr;
454 }
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400455 if ((proxy = generator->generateTexture(ctx, this->imageInfo(), fOrigin, willBeMipped))) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400456 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kNative_LockTexturePath,
457 kLockTexturePathCount);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500458 set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key);
Greg Daniele252f082017-10-23 16:05:23 -0400459 if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
Greg Daniel2ad08202018-09-07 09:13:36 -0400460 *fUniqueKeyInvalidatedMessages.append() =
Robert Phillips9da87e02019-02-04 13:26:26 -0500461 new GrUniqueKeyInvalidatedMessage(key, ctx->priv().contextID());
Greg Danielfc5060d2017-10-04 18:36:15 +0000462 return proxy;
463 }
Brian Osmandf7e0752017-04-26 16:20:28 -0400464 }
465 }
466
Greg Daniel3e70fa32017-10-05 16:27:06 -0400467 // 3. Ask the generator to return YUV planes, which the GPU can convert. If we will be mipping
468 // the texture we fall through here and have the CPU generate the mip maps for us.
Robert Phillips9da87e02019-02-04 13:26:26 -0500469 if (!proxy && !willBeMipped && !ctx->priv().options().fDisableGpuYUVConversion) {
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400470 const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(this->imageInfo());
Greg Daniel4065d452018-11-16 15:43:41 -0500471
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400472 SkColorType colorType = this->colorType();
Greg Daniel4065d452018-11-16 15:43:41 -0500473
Brian Osmandf7e0752017-04-26 16:20:28 -0400474 ScopedGenerator generator(fSharedGenerator);
475 Generator_GrYUVProvider provider(generator);
Christopher Cameron77e96662017-07-08 01:47:47 -0700476
Brian Osmanf48c9962019-01-14 11:15:50 -0500477 // The pixels in the texture will be in the generator's color space.
478 // If onMakeColorTypeAndColorSpace has been called then this will not match this image's
479 // color space. To correct this, apply a color space conversion from the generator's color
480 // space to this image's color space.
Brian Osman861ea5b2018-06-14 09:14:03 -0400481 SkColorSpace* generatorColorSpace = fSharedGenerator->fGenerator->getInfo().colorSpace();
Brian Salomon5ad6fd32019-03-21 15:30:08 -0400482 SkColorSpace* thisColorSpace = this->colorSpace();
Christopher Cameron77e96662017-07-08 01:47:47 -0700483
Mike Kleinae5e8642018-10-03 17:00:41 -0400484 // TODO: Update to create the mipped surface in the YUV generator and draw the base
485 // layer directly into the mipped surface.
Brian Salomon27ae52c2019-07-03 11:27:44 -0400486 proxy = provider.refAsTextureProxy(ctx, desc, SkColorTypeToGrColorType(colorType),
Brian Salomond6287472019-06-24 15:50:07 -0400487 generatorColorSpace, thisColorSpace);
Mike Kleinae5e8642018-10-03 17:00:41 -0400488 if (proxy) {
489 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kYUV_LockTexturePath,
490 kLockTexturePathCount);
491 set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key);
492 *fUniqueKeyInvalidatedMessages.append() =
Robert Phillips9da87e02019-02-04 13:26:26 -0500493 new GrUniqueKeyInvalidatedMessage(key, ctx->priv().contextID());
Mike Kleinae5e8642018-10-03 17:00:41 -0400494 return proxy;
Brian Osmandf7e0752017-04-26 16:20:28 -0400495 }
496 }
497
498 // 4. Ask the generator to return RGB(A) data, which the GPU can convert
499 SkBitmap bitmap;
Brian Osman00766bf2018-10-22 15:59:23 -0400500 if (!proxy && this->getROPixels(&bitmap, chint)) {
Brian Osmande496652019-03-22 13:42:33 -0400501 proxy = proxyProvider->createProxyFromBitmap(bitmap, willBeMipped ? GrMipMapped::kYes
502 : GrMipMapped::kNo);
Greg Daniele252f082017-10-23 16:05:23 -0400503 if (proxy && (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped())) {
Brian Osmandf7e0752017-04-26 16:20:28 -0400504 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kRGBA_LockTexturePath,
505 kLockTexturePathCount);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500506 set_key_on_proxy(proxyProvider, proxy.get(), nullptr, key);
Greg Daniel2ad08202018-09-07 09:13:36 -0400507 *fUniqueKeyInvalidatedMessages.append() =
Robert Phillips9da87e02019-02-04 13:26:26 -0500508 new GrUniqueKeyInvalidatedMessage(key, ctx->priv().contextID());
Brian Osmandf7e0752017-04-26 16:20:28 -0400509 return proxy;
510 }
511 }
Greg Danielfc5060d2017-10-04 18:36:15 +0000512
513 if (proxy) {
514 // We need a mipped proxy, but we either found a proxy earlier that wasn't mipped, generated
515 // a native non mipped proxy, or generated a non-mipped yuv proxy. Thus we generate a new
516 // mipped surface and copy the original proxy into the base layer. We will then let the gpu
517 // generate the rest of the mips.
518 SkASSERT(willBeMipped);
Greg Daniele252f082017-10-23 16:05:23 -0400519 SkASSERT(GrMipMapped::kNo == proxy->mipMapped());
Greg Daniel2ad08202018-09-07 09:13:36 -0400520 *fUniqueKeyInvalidatedMessages.append() =
Robert Phillips9da87e02019-02-04 13:26:26 -0500521 new GrUniqueKeyInvalidatedMessage(key, ctx->priv().contextID());
Greg Daniele1da1d92017-10-06 15:59:27 -0400522 if (auto mippedProxy = GrCopyBaseMipMapToTextureProxy(ctx, proxy.get())) {
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500523 set_key_on_proxy(proxyProvider, mippedProxy.get(), proxy.get(), key);
Greg Danielfc5060d2017-10-04 18:36:15 +0000524 return mippedProxy;
525 }
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400526 // We failed to make a mipped proxy with the base copied into it. This could have
527 // been from failure to make the proxy or failure to do the copy. Thus we will fall
528 // back to just using the non mipped proxy; See skbug.com/7094.
529 return proxy;
Greg Danielfc5060d2017-10-04 18:36:15 +0000530 }
531
Brian Osmandf7e0752017-04-26 16:20:28 -0400532 SK_HISTOGRAM_ENUMERATION("LockTexturePath", kFailure_LockTexturePath,
533 kLockTexturePathCount);
534 return nullptr;
535}
536
537///////////////////////////////////////////////////////////////////////////////////////////////////
538
Mike Reeda5acbf92019-08-14 11:08:41 -0400539sk_sp<SkImage> SkImage::DecodeToTexture(GrContext* ctx, const void* encoded, size_t length,
540 const SkIRect* subset) {
541 // img will not survive this function, so we don't need to copy/own the encoded data,
542 auto img = MakeFromEncoded(SkData::MakeWithoutCopy(encoded, length), subset);
543 if (!img) {
544 return nullptr;
545 }
546 return img->makeTextureImage(ctx, nullptr);
547}
548
Brian Osmandf7e0752017-04-26 16:20:28 -0400549#endif