blob: 00dd48d0a9029b801eb2a7a66ce844723f82f8d4 [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 Danield1935c12017-09-28 12:07:28 -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 Danield1935c12017-09-28 12:07:28 -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 Danield1935c12017-09-28 12:07:28 -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 Danield1935c12017-09-28 12:07:28 -040040 sk_sp<GrTextureProxy> originalProxy;
Robert Phillips0c984a02017-03-16 07:51:56 -040041
42 if (fOriginalKey.isValid()) {
Greg Danield1935c12017-09-28 12:07:28 -040043 originalProxy = this->context()->resourceProvider()->findOrCreateProxyByUniqueKey(
44 fOriginalKey, kTopLeft_GrSurfaceOrigin);
45 if (originalProxy && (!willBeMipped || originalProxy->isMipMapped())) {
46 return originalProxy;
Robert Phillips0c984a02017-03-16 07:51:56 -040047 }
48 }
Greg Danield1935c12017-09-28 12:07:28 -040049
50 sk_sp<GrTextureProxy> proxy;
Robert Phillips0c984a02017-03-16 07:51:56 -040051 if (willBeMipped) {
Greg Danield1935c12017-09-28 12:07:28 -040052 if (!originalProxy) {
53 proxy = GrGenerateMipMapsAndUploadToTextureProxy(this->context(), fBitmap,
54 dstColorSpace);
55 } else {
56 proxy = GrCopyBaseMipMapToTextureProxy(this->context(), originalProxy.get(),
57 dstColorSpace);
58 }
Robert Phillips0c984a02017-03-16 07:51:56 -040059 }
60 if (!proxy) {
Matt Sarettdedac852017-05-12 10:56:49 -040061 proxy = GrUploadBitmapToTextureProxy(this->context()->resourceProvider(), fBitmap,
62 dstColorSpace);
Robert Phillips0c984a02017-03-16 07:51:56 -040063 }
64 if (proxy && fOriginalKey.isValid()) {
Robert Phillips019ff272017-07-24 14:47:57 -040065 SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
Greg Danield1935c12017-09-28 12:07:28 -040066 if (originalProxy) {
67 // In this case we are stealing the key from the original proxy which should only happen
68 // when we have just generated mipmaps for an originally unmipped proxy/texture. This
69 // means that all future uses of the key will access the mipmapped version. The texture
70 // backing the unmipped version will remain in the resource cache until the last texture
71 // proxy referencing it is deleted at which time it too will be deleted or recycled.
72 this->context()->resourceProvider()->removeUniqueKeyFromProxy(fOriginalKey,
73 originalProxy.get());
74 }
Robert Phillips0c984a02017-03-16 07:51:56 -040075 this->context()->resourceProvider()->assignUniqueKeyToProxy(fOriginalKey, proxy.get());
Robert Phillips0c984a02017-03-16 07:51:56 -040076 GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, fBitmap.pixelRef());
77 }
78 return proxy;
79}
80
Brian Osman3b66ab62016-11-28 09:26:31 -050081void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey,
Brian Osman61624f02016-12-09 14:51:59 -050082 SkColorSpace* dstColorSpace) {
83 // Destination color space is irrelevant - we always upload the bitmap's contents as-is
Brian Osman3b66ab62016-11-28 09:26:31 -050084 if (fOriginalKey.isValid()) {
85 MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey);
86 }
87}
88
89void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) {
90 GrInstallBitmapUniqueKeyInvalidator(copyKey, fBitmap.pixelRef());
91}
92
93SkAlphaType GrBitmapTextureMaker::alphaType() const {
94 return fBitmap.alphaType();
95}
96
Brian Osman61624f02016-12-09 14:51:59 -050097sk_sp<SkColorSpace> GrBitmapTextureMaker::getColorSpace(SkColorSpace* dstColorSpace) {
98 // Color space doesn't depend on destination color space - it's just whatever is in the bitmap
Robert Phillips256c37b2017-03-01 14:32:46 -050099 return fBitmap.refColorSpace();
Brian Osman3b66ab62016-11-28 09:26:31 -0500100}