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 "GrBitmapTextureMaker.h"
9
10#include "GrContext.h"
11#include "GrContextPriv.h"
12#include "GrGpuResourcePriv.h"
13#include "GrProxyProvider.h"
14#include "GrSurfaceContext.h"
15#include "SkBitmap.h"
16#include "SkGr.h"
17#include "SkMipMap.h"
18#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
33sk_sp<GrTextureProxy> GrBitmapTextureMaker::refOriginalTextureProxy(bool willBeMipped,
34                                                                    SkColorSpace* dstColorSpace,
35                                                                    AllowedTexGenType onlyIfFast) {
36    if (AllowedTexGenType::kCheap == onlyIfFast) {
37        return nullptr;
38    }
39
40    GrProxyProvider* proxyProvider = this->context()->contextPriv().proxyProvider();
41    sk_sp<GrTextureProxy> proxy;
42
43    if (fOriginalKey.isValid()) {
44        proxy = proxyProvider->findOrCreateProxyByUniqueKey(fOriginalKey, kTopLeft_GrSurfaceOrigin);
45        if (proxy && (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped())) {
46            return proxy;
47        }
48    }
49
50    if (!proxy) {
51        if (willBeMipped) {
52            proxy = GrGenerateMipMapsAndUploadToTextureProxy(proxyProvider, fBitmap, dstColorSpace);
53        }
54        if (!proxy) {
55            proxy = GrUploadBitmapToTextureProxy(proxyProvider, fBitmap, dstColorSpace);
56        }
57        if (proxy) {
58            if (fOriginalKey.isValid()) {
59                proxyProvider->assignUniqueKeyToProxy(fOriginalKey, proxy.get());
60            }
61            if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) {
62                SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
63                if (fOriginalKey.isValid()) {
64                    GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, fBitmap.pixelRef());
65                }
66                return proxy;
67            }
68        }
69    }
70
71    if (proxy) {
72        SkASSERT(willBeMipped);
73        SkASSERT(GrMipMapped::kNo == proxy->mipMapped());
74        // We need a mipped proxy, but we either found a proxy earlier that wasn't mipped or
75        // generated a non mipped proxy. Thus we generate a new mipped surface and copy the original
76        // proxy into the base layer. We will then let the gpu generate the rest of the mips.
77        if (auto mippedProxy = GrCopyBaseMipMapToTextureProxy(this->context(), proxy.get())) {
78            SkASSERT(mippedProxy->origin() == kTopLeft_GrSurfaceOrigin);
79            if (fOriginalKey.isValid()) {
80                // In this case we are stealing the key from the original proxy which should only
81                // happen when we have just generated mipmaps for an originally unmipped
82                // proxy/texture. This means that all future uses of the key will access the
83                // mipmapped version. The texture backing the unmipped version will remain in the
84                // resource cache until the last texture proxy referencing it is deleted at which
85                // time it too will be deleted or recycled.
86                proxyProvider->removeUniqueKeyFromProxy(fOriginalKey, proxy.get());
87                proxyProvider->assignUniqueKeyToProxy(fOriginalKey, mippedProxy.get());
88                GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, fBitmap.pixelRef());
89            }
90            return mippedProxy;
91        }
92        // We failed to make a mipped proxy with the base copied into it. This could have
93        // been from failure to make the proxy or failure to do the copy. Thus we will fall
94        // back to just using the non mipped proxy; See skbug.com/7094.
95        return proxy;
96    }
97    return nullptr;
98}
99
100void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey,
101                                       SkColorSpace* dstColorSpace) {
102    // Destination color space is irrelevant - we always upload the bitmap's contents as-is
103    if (fOriginalKey.isValid()) {
104        MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey);
105    }
106}
107
108void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) {
109    GrInstallBitmapUniqueKeyInvalidator(copyKey, fBitmap.pixelRef());
110}
111
112SkAlphaType GrBitmapTextureMaker::alphaType() const {
113    return fBitmap.alphaType();
114}
115
116sk_sp<SkColorSpace> GrBitmapTextureMaker::getColorSpace(SkColorSpace* dstColorSpace) {
117    // Color space doesn't depend on destination color space - it's just whatever is in the bitmap
118    return fBitmap.refColorSpace();
119}
120