2 * Copyright 2016 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "GrBitmapTextureMaker.h"
10 #include "GrContext.h"
11 #include "GrGpuResourcePriv.h"
14 #include "SkPixelRef.h"
16 static bool bmp_is_alpha_only(const SkBitmap& bm) { return kAlpha_8_SkColorType == bm.colorType(); }
18 GrBitmapTextureMaker::GrBitmapTextureMaker(GrContext* context, const SkBitmap& bitmap)
19 : INHERITED(context, bitmap.width(), bitmap.height(), bmp_is_alpha_only(bitmap))
21 if (!bitmap.isVolatile()) {
22 SkIPoint origin = bitmap.pixelRefOrigin();
23 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(),
25 GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGenerationID(), subset);
29 GrTexture* GrBitmapTextureMaker::refOriginalTexture(bool willBeMipped,
30 SkColorSpace* dstColorSpace) {
31 GrTexture* tex = nullptr;
33 if (fOriginalKey.isValid()) {
34 tex = this->context()->textureProvider()->findAndRefTextureByUniqueKey(fOriginalKey);
40 tex = GrGenerateMipMapsAndUploadToTexture(this->context(), fBitmap, dstColorSpace);
43 tex = GrUploadBitmapToTexture(this->context(), fBitmap);
45 if (tex && fOriginalKey.isValid()) {
46 tex->resourcePriv().setUniqueKey(fOriginalKey);
47 GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, fBitmap.pixelRef());
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);
60 void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) {
61 GrInstallBitmapUniqueKeyInvalidator(copyKey, fBitmap.pixelRef());
64 SkAlphaType GrBitmapTextureMaker::alphaType() const {
65 return fBitmap.alphaType();
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());