blob: 086e40e06b7270deabb6cbdf78d01c3ddb456137 [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 "GrImageTextureMaker.h"
9
10#include "GrContext.h"
11#include "GrGpuResourcePriv.h"
12#include "SkGrPriv.h"
13#include "SkImage_Base.h"
14#include "SkImageCacherator.h"
15#include "SkPixelRef.h"
16
17static bool cacher_is_alpha_only(const SkImageCacherator& cacher) {
18 return kAlpha_8_SkColorType == cacher.info().colorType();
19}
20
21GrImageTextureMaker::GrImageTextureMaker(GrContext* context, SkImageCacherator* cacher,
22 const SkImage* client, SkImage::CachingHint chint)
23 : INHERITED(context, cacher->info().width(), cacher->info().height(),
24 cacher_is_alpha_only(*cacher))
25 , fCacher(cacher)
26 , fClient(client)
27 , fCachingHint(chint) {
28 if (client) {
29 GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(),
30 SkIRect::MakeWH(this->width(), this->height()));
31 }
32}
33
Brian Osman61624f02016-12-09 14:51:59 -050034GrTexture* GrImageTextureMaker::refOriginalTexture(bool willBeMipped, SkColorSpace* dstColorSpace) {
Brian Osman3b66ab62016-11-28 09:26:31 -050035 return fCacher->lockTexture(this->context(), fOriginalKey, fClient, fCachingHint, willBeMipped,
Brian Osman61624f02016-12-09 14:51:59 -050036 dstColorSpace);
Brian Osman3b66ab62016-11-28 09:26:31 -050037}
38
39void GrImageTextureMaker::makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey,
Brian Osman61624f02016-12-09 14:51:59 -050040 SkColorSpace* dstColorSpace) {
Brian Osman3b66ab62016-11-28 09:26:31 -050041 if (fOriginalKey.isValid() && SkImage::kAllow_CachingHint == fCachingHint) {
42 SkImageCacherator::CachedFormat cacheFormat =
Brian Osman61624f02016-12-09 14:51:59 -050043 fCacher->chooseCacheFormat(dstColorSpace, this->context()->caps());
Brian Osman3b66ab62016-11-28 09:26:31 -050044 GrUniqueKey cacheKey;
45 fCacher->makeCacheKeyFromOrigKey(fOriginalKey, cacheFormat, &cacheKey);
46 MakeCopyKeyFromOrigKey(cacheKey, stretch, paramsCopyKey);
47 }
48}
49
50void GrImageTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) {
51 if (fClient) {
52 as_IB(fClient)->notifyAddedToCache();
53 }
54}
55
56SkAlphaType GrImageTextureMaker::alphaType() const {
57 return fCacher->info().alphaType();
58}
Brian Osman61624f02016-12-09 14:51:59 -050059sk_sp<SkColorSpace> GrImageTextureMaker::getColorSpace(SkColorSpace* dstColorSpace) {
60 return fCacher->getColorSpace(this->context(), dstColorSpace);
Brian Osman3b66ab62016-11-28 09:26:31 -050061}