blob: 6fb2c6a3a8fda446f275cc431136ab451b152179 [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"
11#include "GrGpuResourcePriv.h"
Brian Osman32342f02017-03-04 08:12:46 -050012#include "GrResourceProvider.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050013#include "SkBitmap.h"
Brian Osman3b655982017-03-07 16:58:08 -050014#include "SkGr.h"
Brian Osman3b66ab62016-11-28 09:26:31 -050015#include "SkPixelRef.h"
16
17static bool bmp_is_alpha_only(const SkBitmap& bm) { return kAlpha_8_SkColorType == bm.colorType(); }
18
19GrBitmapTextureMaker::GrBitmapTextureMaker(GrContext* context, const SkBitmap& bitmap)
20 : INHERITED(context, bitmap.width(), bitmap.height(), bmp_is_alpha_only(bitmap))
21 , fBitmap(bitmap) {
22 if (!bitmap.isVolatile()) {
23 SkIPoint origin = bitmap.pixelRefOrigin();
24 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(),
25 bitmap.height());
26 GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGenerationID(), subset);
27 }
28}
29
Robert Phillips0c984a02017-03-16 07:51:56 -040030sk_sp<GrTextureProxy> GrBitmapTextureMaker::refOriginalTextureProxy(bool willBeMipped,
Stan Ilievba81af22017-06-08 15:16:53 -040031 SkColorSpace* dstColorSpace,
32 AllowedTexGenType onlyIfFast) {
33 if (AllowedTexGenType::kCheap == onlyIfFast) {
34 return nullptr;
35 }
36
Robert Phillips0c984a02017-03-16 07:51:56 -040037 sk_sp<GrTextureProxy> proxy;
38
39 if (fOriginalKey.isValid()) {
Robert Phillips96be9df2017-07-21 14:17:45 +000040 proxy = this->context()->resourceProvider()->findProxyByUniqueKey(fOriginalKey);
Robert Phillips0c984a02017-03-16 07:51:56 -040041 if (proxy) {
42 return proxy;
43 }
44 }
45 if (willBeMipped) {
46 proxy = GrGenerateMipMapsAndUploadToTextureProxy(this->context(), fBitmap, dstColorSpace);
47 }
48 if (!proxy) {
Matt Sarettdedac852017-05-12 10:56:49 -040049 proxy = GrUploadBitmapToTextureProxy(this->context()->resourceProvider(), fBitmap,
50 dstColorSpace);
Robert Phillips0c984a02017-03-16 07:51:56 -040051 }
52 if (proxy && fOriginalKey.isValid()) {
Robert Phillips019ff272017-07-24 14:47:57 -040053 SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin);
Robert Phillips0c984a02017-03-16 07:51:56 -040054 this->context()->resourceProvider()->assignUniqueKeyToProxy(fOriginalKey, proxy.get());
55 // MDB TODO (caching): this has to play nice with the GrSurfaceProxy's caching
56 GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, fBitmap.pixelRef());
57 }
58 return proxy;
59}
60
Brian Osman3b66ab62016-11-28 09:26:31 -050061void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey,
Brian Osman61624f02016-12-09 14:51:59 -050062 SkColorSpace* dstColorSpace) {
63 // Destination color space is irrelevant - we always upload the bitmap's contents as-is
Brian Osman3b66ab62016-11-28 09:26:31 -050064 if (fOriginalKey.isValid()) {
65 MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey);
66 }
67}
68
69void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) {
70 GrInstallBitmapUniqueKeyInvalidator(copyKey, fBitmap.pixelRef());
71}
72
73SkAlphaType GrBitmapTextureMaker::alphaType() const {
74 return fBitmap.alphaType();
75}
76
Brian Osman61624f02016-12-09 14:51:59 -050077sk_sp<SkColorSpace> GrBitmapTextureMaker::getColorSpace(SkColorSpace* dstColorSpace) {
78 // Color space doesn't depend on destination color space - it's just whatever is in the bitmap
Robert Phillips256c37b2017-03-01 14:32:46 -050079 return fBitmap.refColorSpace();
Brian Osman3b66ab62016-11-28 09:26:31 -050080}