Plumb dst color space in many places, rather than "mode"
[platform/upstream/libSkiaSharp.git] / src / gpu / GrBitmapTextureMaker.cpp
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 "GrGpuResourcePriv.h"
12 #include "SkBitmap.h"
13 #include "SkGrPriv.h"
14 #include "SkPixelRef.h"
15
16 static bool bmp_is_alpha_only(const SkBitmap& bm) { return kAlpha_8_SkColorType == bm.colorType(); }
17
18 GrBitmapTextureMaker::GrBitmapTextureMaker(GrContext* context, const SkBitmap& bitmap)
19     : INHERITED(context, bitmap.width(), bitmap.height(), bmp_is_alpha_only(bitmap))
20     , fBitmap(bitmap) {
21     if (!bitmap.isVolatile()) {
22         SkIPoint origin = bitmap.pixelRefOrigin();
23         SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(),
24                                            bitmap.height());
25         GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGenerationID(), subset);
26     }
27 }
28
29 GrTexture* GrBitmapTextureMaker::refOriginalTexture(bool willBeMipped,
30                                                     SkColorSpace* dstColorSpace) {
31     GrTexture* tex = nullptr;
32
33     if (fOriginalKey.isValid()) {
34         tex = this->context()->textureProvider()->findAndRefTextureByUniqueKey(fOriginalKey);
35         if (tex) {
36             return tex;
37         }
38     }
39     if (willBeMipped) {
40         tex = GrGenerateMipMapsAndUploadToTexture(this->context(), fBitmap, dstColorSpace);
41     }
42     if (!tex) {
43         tex = GrUploadBitmapToTexture(this->context(), fBitmap);
44     }
45     if (tex && fOriginalKey.isValid()) {
46         tex->resourcePriv().setUniqueKey(fOriginalKey);
47         GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, fBitmap.pixelRef());
48     }
49     return tex;
50 }
51
52 void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey,
53                                        SkColorSpace* dstColorSpace) {
54     // Destination color space is irrelevant - we always upload the bitmap's contents as-is
55     if (fOriginalKey.isValid()) {
56         MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey);
57     }
58 }
59
60 void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) {
61     GrInstallBitmapUniqueKeyInvalidator(copyKey, fBitmap.pixelRef());
62 }
63
64 SkAlphaType GrBitmapTextureMaker::alphaType() const {
65     return fBitmap.alphaType();
66 }
67
68 sk_sp<SkColorSpace> GrBitmapTextureMaker::getColorSpace(SkColorSpace* dstColorSpace) {
69     // Color space doesn't depend on destination color space - it's just whatever is in the bitmap
70     return sk_ref_sp(fBitmap.colorSpace());
71 }