blob: 9b3ba36ae793ad615f59e58fa43c8986d72136f8 [file] [log] [blame]
Brian Osman3b66ab62016-11-28 09:26:31 -05001/*
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 "GrBitmapTextureMaker.h"
9
10#include "GrContext.h"
Greg Daniel55afd6d2017-09-29 09:32:44 -040011#include "GrContextPriv.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050012#include "GrGpuResourcePriv.h"
Brian Osman32342f02017-03-04 08:12:46 -050013#include "GrResourceProvider.h"
Greg Daniel55afd6d2017-09-29 09:32:44 -040014#include "GrSurfaceContext.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050015#include "SkBitmap.h"
Brian Osman3b655982017-03-07 16:58:08 -050016#include "SkGr.h"
Greg Daniel55afd6d2017-09-29 09:32:44 -040017#include "SkMipMap.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050018#include "SkPixelRef.h"
19
20static bool bmp_is_alpha_only(const SkBitmap& bm) { return kAlpha_8_SkColorType == bm.colorType(); }
21
22GrBitmapTextureMaker::GrBitmapTextureMaker(GrContext* context, const SkBitmap& bitmap)
23 : INHERITED(context, bitmap.width(), bitmap.height(), bmp_is_alpha_only(bitmap))
24 , fBitmap(bitmap) {
25 if (!bitmap.isVolatile()) {
26 SkIPoint origin = bitmap.pixelRefOrigin();
27 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(),
28 bitmap.height());
29 GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGenerationID(), subset);
30 }
31}
32
Robert Phillips0c984a02017-03-16 07:51:56 -040033sk_sp<GrTextureProxy> GrBitmapTextureMaker::refOriginalTextureProxy(bool willBeMipped,
Stan Ilievba81af22017-06-08 15:16:53 -040034 SkColorSpace* dstColorSpace,
35 AllowedTexGenType onlyIfFast) {
36 if (AllowedTexGenType::kCheap == onlyIfFast) {
37 return nullptr;
38 }
39
Greg Danielfc5060d2017-10-04 18:36:15 +000040 sk_sp<GrTextureProxy> proxy;
Robert Phillips0c984a02017-03-16 07:51:56 -040041
42 if (fOriginalKey.isValid()) {
Greg Danielfc5060d2017-10-04 18:36:15 +000043 proxy = this->context()->resourceProvider()->findOrCreateProxyByUniqueKey(
Greg Daniel55afd6d2017-09-29 09:32:44 -040044 fOriginalKey, kTopLeft_GrSurfaceOrigin);
Greg Daniele252f082017-10-23 16:05:23 -040045 if (proxy && (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped())) {
Greg Danielfc5060d2017-10-04 18:36:15 +000046 return proxy;
Robert Phillips0c984a02017-03-16 07:51:56 -040047 }
48 }
Greg Daniel55afd6d2017-09-29 09:32:44 -040049
Greg Danielfc5060d2017-10-04 18:36:15 +000050 if (!proxy) {
51 if (willBeMipped) {
Greg Daniel55afd6d2017-09-29 09:32:44 -040052 proxy = GrGenerateMipMapsAndUploadToTextureProxy(this->context(), fBitmap,
53 dstColorSpace);
Greg Danielfc5060d2017-10-04 18:36:15 +000054 }
55 if (!proxy) {
56 proxy = GrUploadBitmapToTextureProxy(this->context()->resourceProvider(), fBitmap,
57 dstColorSpace);
58 }
59 if (proxy) {
60 if (fOriginalKey.isValid()) {
61 this->context()->resourceProvider()->assignUniqueKeyToProxy(fOriginalKey,
62 proxy.get());
63 }
Greg Daniele252f082017-10-23 16:05:23 -040064 if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
Greg Danielfc5060d2017-10-04 18:36:15 +000065 SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
66 if (fOriginalKey.isValid()) {
67 GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, fBitmap.pixelRef());
68 }
69 return proxy;
Greg Danielbb76ace2017-09-29 15:58:22 -040070 }
Greg Daniel55afd6d2017-09-29 09:32:44 -040071 }
Robert Phillips0c984a02017-03-16 07:51:56 -040072 }
Greg Danielfc5060d2017-10-04 18:36:15 +000073
74 if (proxy) {
75 SkASSERT(willBeMipped);
Greg Daniele252f082017-10-23 16:05:23 -040076 SkASSERT(GrMipMapped::kNo == proxy->mipMapped());
Greg Danielfc5060d2017-10-04 18:36:15 +000077 // We need a mipped proxy, but we either found a proxy earlier that wasn't mipped or
78 // generated a non mipped proxy. Thus we generate a new mipped surface and copy the original
79 // proxy into the base layer. We will then let the gpu generate the rest of the mips.
Greg Daniele1da1d92017-10-06 15:59:27 -040080 if (auto mippedProxy = GrCopyBaseMipMapToTextureProxy(this->context(), proxy.get())) {
Greg Danielfc5060d2017-10-04 18:36:15 +000081 SkASSERT(mippedProxy->origin() == kTopLeft_GrSurfaceOrigin);
82 if (fOriginalKey.isValid()) {
83 // In this case we are stealing the key from the original proxy which should only
84 // happen when we have just generated mipmaps for an originally unmipped
85 // proxy/texture. This means that all future uses of the key will access the
86 // mipmapped version. The texture backing the unmipped version will remain in the
87 // resource cache until the last texture proxy referencing it is deleted at which
88 // time it too will be deleted or recycled.
89 this->context()->resourceProvider()->removeUniqueKeyFromProxy(fOriginalKey,
90 proxy.get());
91 this->context()->resourceProvider()->assignUniqueKeyToProxy(fOriginalKey,
92 mippedProxy.get());
93 GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, fBitmap.pixelRef());
94 }
95 return mippedProxy;
Greg Daniel87c76ed2017-10-03 13:42:45 +000096 }
Greg Danielfc5060d2017-10-04 18:36:15 +000097 // We failed to make a mipped proxy with the base copied into it. This could have
98 // been from failure to make the proxy or failure to do the copy. Thus we will fall
99 // back to just using the non mipped proxy; See skbug.com/7094.
100 return proxy;
Greg Daniel87c76ed2017-10-03 13:42:45 +0000101 }
Greg Danielfc5060d2017-10-04 18:36:15 +0000102 return nullptr;
Robert Phillips0c984a02017-03-16 07:51:56 -0400103}
104
Brian Osman3b66ab62016-11-28 09:26:31 -0500105void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey,
Brian Osman61624f02016-12-09 14:51:59 -0500106 SkColorSpace* dstColorSpace) {
107 // Destination color space is irrelevant - we always upload the bitmap's contents as-is
Brian Osman3b66ab62016-11-28 09:26:31 -0500108 if (fOriginalKey.isValid()) {
109 MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey);
110 }
111}
112
113void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) {
114 GrInstallBitmapUniqueKeyInvalidator(copyKey, fBitmap.pixelRef());
115}
116
117SkAlphaType GrBitmapTextureMaker::alphaType() const {
118 return fBitmap.alphaType();
119}
120
Brian Osman61624f02016-12-09 14:51:59 -0500121sk_sp<SkColorSpace> GrBitmapTextureMaker::getColorSpace(SkColorSpace* dstColorSpace) {
122 // Color space doesn't depend on destination color space - it's just whatever is in the bitmap
Robert Phillips256c37b2017-03-01 14:32:46 -0500123 return fBitmap.refColorSpace();
Brian Osman3b66ab62016-11-28 09:26:31 -0500124}