1/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "src/gpu/GrBitmapTextureMaker.h"
9
10#include "include/core/SkBitmap.h"
11#include "include/core/SkPixelRef.h"
12#include "include/private/GrRecordingContext.h"
13#include "include/private/SkIDChangeListener.h"
14#include "src/gpu/GrGpuResourcePriv.h"
15#include "src/gpu/GrProxyProvider.h"
16#include "src/gpu/GrRecordingContextPriv.h"
17#include "src/gpu/GrSurfaceContext.h"
18#include "src/gpu/SkGr.h"
19
20static GrImageInfo get_image_info(GrRecordingContext* context, const SkBitmap& bitmap) {
21 GrColorType ct = SkColorTypeToGrColorType(bitmap.info().colorType());
22 GrBackendFormat format = context->priv().caps()->getDefaultBackendFormat(ct, GrRenderable::kNo);
23 if (!format.isValid()) {
24 ct = GrColorType::kRGBA_8888;
25 }
26 return {ct, bitmap.alphaType(), bitmap.refColorSpace(), bitmap.dimensions()};
27}
28
29GrBitmapTextureMaker::GrBitmapTextureMaker(GrRecordingContext* context,
30 const SkBitmap& bitmap,
31 SkBackingFit fit)
32 : GrBitmapTextureMaker(context, bitmap, GrImageTexGenPolicy::kNew_Uncached_Budgeted, fit) {}
33
34GrBitmapTextureMaker::GrBitmapTextureMaker(GrRecordingContext* context,
35 const SkBitmap& bitmap,
36 GrImageTexGenPolicy cachePolicy)
37 : GrBitmapTextureMaker(context, bitmap, cachePolicy, SkBackingFit::kExact) {}
38
39GrBitmapTextureMaker::GrBitmapTextureMaker(GrRecordingContext* context,
40 const SkBitmap& bitmap,
41 GrImageTexGenPolicy cachePolicy,
42 SkBackingFit fit)
43 : INHERITED(context, get_image_info(context, bitmap))
44 , fBitmap(bitmap)
45 , fFit(fit)
46 , fBudgeted(cachePolicy == GrImageTexGenPolicy::kNew_Uncached_Unbudgeted
47 ? SkBudgeted::kNo
48 : SkBudgeted::kYes) {
49 if (!bitmap.isVolatile() && cachePolicy == GrImageTexGenPolicy::kDraw) {
50 SkIPoint origin = bitmap.pixelRefOrigin();
51 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(),
52 bitmap.height());
53 GrMakeKeyFromImageID(&fKey, bitmap.pixelRef()->getGenerationID(), subset);
54 }
55}
56
57GrSurfaceProxyView GrBitmapTextureMaker::refOriginalTextureProxyView(GrMipMapped mipMapped) {
58 GrProxyProvider* proxyProvider = this->context()->priv().proxyProvider();
59 sk_sp<GrTextureProxy> proxy;
60 GrSwizzle swizzle;
61
62 auto installKey = [&](GrTextureProxy* proxy) {
63 auto listener = GrMakeUniqueKeyInvalidationListener(&fKey, proxyProvider->contextID());
64 fBitmap.pixelRef()->addGenIDChangeListener(std::move(listener));
65 proxyProvider->assignUniqueKeyToProxy(fKey, proxy);
66 };
67
68 if (fKey.isValid()) {
69 proxy = proxyProvider->findOrCreateProxyByUniqueKey(fKey);
70 if (proxy) {
71 swizzle = this->context()->priv().caps()->getReadSwizzle(proxy->backendFormat(),
72 this->colorType());
73 if (mipMapped == GrMipMapped::kNo || proxy->mipMapped() == GrMipMapped::kYes) {
74 return {std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
75 }
76 }
77 }
78
79 if (!proxy) {
80 if (this->colorType() != SkColorTypeToGrColorType(fBitmap.info().colorType())) {
81 SkASSERT(this->colorType() == GrColorType::kRGBA_8888);
82 SkBitmap copy8888;
83 if (!copy8888.tryAllocPixels(fBitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
84 !fBitmap.readPixels(copy8888.pixmap())) {
85 return {};
86 }
87 copy8888.setImmutable();
88 proxy = proxyProvider->createProxyFromBitmap(copy8888, mipMapped, fFit, fBudgeted);
89 } else {
90 proxy = proxyProvider->createProxyFromBitmap(fBitmap, mipMapped, fFit, fBudgeted);
91 }
92 if (proxy) {
93 swizzle = this->context()->priv().caps()->getReadSwizzle(proxy->backendFormat(),
94 this->colorType());
95 SkASSERT(mipMapped == GrMipMapped::kNo || proxy->mipMapped() == GrMipMapped::kYes);
96 if (fKey.isValid()) {
97 installKey(proxy.get());
98 }
99 return {std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
100 }
101 }
102
103 if (proxy) {
104 SkASSERT(mipMapped == GrMipMapped::kYes);
105 SkASSERT(proxy->mipMapped() == GrMipMapped::kNo);
106 SkASSERT(fKey.isValid());
107 // We need a mipped proxy, but we found a proxy earlier that wasn't mipped. Thus we generate
108 // a new mipped surface and copy the original proxy into the base layer. We will then let
109 // the gpu generate the rest of the mips.
110 auto mippedProxy = GrCopyBaseMipMapToTextureProxy(this->context(), proxy.get(),
111 kTopLeft_GrSurfaceOrigin);
112 if (!mippedProxy) {
113 // We failed to make a mipped proxy with the base copied into it. This could have
114 // been from failure to make the proxy or failure to do the copy. Thus we will fall
115 // back to just using the non mipped proxy; See skbug.com/7094.
116 return {std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
117 }
118 // In this case we are stealing the key from the original proxy which should only happen
119 // when we have just generated mipmaps for an originally unmipped proxy/texture. This
120 // means that all future uses of the key will access the mipmapped version. The texture
121 // backing the unmipped version will remain in the resource cache until the last texture
122 // proxy referencing it is deleted at which time it too will be deleted or recycled.
123 SkASSERT(proxy->getUniqueKey() == fKey);
124 proxyProvider->removeUniqueKeyFromProxy(proxy.get());
125 installKey(mippedProxy->asTextureProxy());
126 return {std::move(mippedProxy), kTopLeft_GrSurfaceOrigin, swizzle};
127 }
128 return {};
129}
130