blob: 055594743172e564f1ba8e331a57294ef5982e0d [file] [log] [blame]
reed64045422015-06-04 06:31:31 -07001/*
2 * Copyright 2015 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 "SkBitmap.h"
9#include "SkBitmapController.h"
reed013e9e32015-09-15 14:46:27 -070010#include "SkBitmapProvider.h"
reed64045422015-06-04 06:31:31 -070011#include "SkMatrix.h"
reed98ed7b62015-09-15 12:38:12 -070012#include "SkPixelRef.h"
bungemanf3c15b72015-08-19 11:56:48 -070013#include "SkTemplates.h"
reed64045422015-06-04 06:31:31 -070014
reed99138872015-08-31 15:16:17 -070015// RESIZE_LANCZOS3 is another good option, but chrome prefers mitchell at the moment
16#define kHQ_RESIZE_METHOD SkBitmapScaler::RESIZE_MITCHELL
17
reed64045422015-06-04 06:31:31 -070018///////////////////////////////////////////////////////////////////////////////////////////////////
19
reed98ed7b62015-09-15 12:38:12 -070020SkBitmapController::State* SkBitmapController::requestBitmap(const SkBitmapProvider& provider,
reed64045422015-06-04 06:31:31 -070021 const SkMatrix& inv,
22 SkFilterQuality quality,
23 void* storage, size_t storageSize) {
reed98ed7b62015-09-15 12:38:12 -070024 if (!provider.validForDrawing()) {
halcanary96fcdcc2015-08-27 07:41:13 -070025 return nullptr;
reed64045422015-06-04 06:31:31 -070026 }
27
reed98ed7b62015-09-15 12:38:12 -070028 State* state = this->onRequestBitmap(provider, inv, quality, storage, storageSize);
reed64045422015-06-04 06:31:31 -070029 if (state) {
halcanary96fcdcc2015-08-27 07:41:13 -070030 if (nullptr == state->fPixmap.addr()) {
reedad7ae6c2015-06-04 14:12:25 -070031 SkInPlaceDeleteCheck(state, storage);
halcanary96fcdcc2015-08-27 07:41:13 -070032 state = nullptr;
reed64045422015-06-04 06:31:31 -070033 }
34 }
35 return state;
36}
37
38///////////////////////////////////////////////////////////////////////////////////////////////////
39
40#include "SkBitmapCache.h"
41#include "SkBitmapScaler.h"
42#include "SkMipMap.h"
43#include "SkResourceCache.h"
44
45class SkDefaultBitmapControllerState : public SkBitmapController::State {
46public:
reed98ed7b62015-09-15 12:38:12 -070047 SkDefaultBitmapControllerState(const SkBitmapProvider&, const SkMatrix& inv, SkFilterQuality);
reed64045422015-06-04 06:31:31 -070048
49private:
reedad7ae6c2015-06-04 14:12:25 -070050 SkBitmap fResultBitmap;
reed64045422015-06-04 06:31:31 -070051 SkAutoTUnref<const SkMipMap> fCurrMip;
52
reed98ed7b62015-09-15 12:38:12 -070053 bool processHQRequest(const SkBitmapProvider&);
54 bool processMediumRequest(const SkBitmapProvider&);
reed64045422015-06-04 06:31:31 -070055};
56
57// Check to see that the size of the bitmap that would be produced by
58// scaling by the given inverted matrix is less than the maximum allowed.
reed98ed7b62015-09-15 12:38:12 -070059static inline bool cache_size_okay(const SkBitmapProvider& provider, const SkMatrix& invMat) {
reed64045422015-06-04 06:31:31 -070060 size_t maximumAllocation = SkResourceCache::GetEffectiveSingleAllocationByteLimit();
61 if (0 == maximumAllocation) {
62 return true;
63 }
64 // float matrixScaleFactor = 1.0 / (invMat.scaleX * invMat.scaleY);
65 // return ((origBitmapSize * matrixScaleFactor) < maximumAllocationSize);
66 // Skip the division step:
reed98ed7b62015-09-15 12:38:12 -070067 const size_t size = provider.info().getSafeSize(provider.info().minRowBytes());
reeda3d99a52016-02-03 19:07:54 -080068 SkScalar invScaleSqr = invMat.getScaleX() * invMat.getScaleY();
reed04882022016-02-08 11:57:52 -080069 return size < (maximumAllocation * SkScalarAbs(invScaleSqr));
reed64045422015-06-04 06:31:31 -070070}
71
72/*
73 * High quality is implemented by performing up-right scale-only filtering and then
74 * using bilerp for any remaining transformations.
75 */
reed98ed7b62015-09-15 12:38:12 -070076bool SkDefaultBitmapControllerState::processHQRequest(const SkBitmapProvider& provider) {
reed64045422015-06-04 06:31:31 -070077 if (fQuality != kHigh_SkFilterQuality) {
78 return false;
79 }
80
81 // Our default return state is to downgrade the request to Medium, w/ or w/o setting fBitmap
82 // to a valid bitmap. If we succeed, we will set this to Low instead.
83 fQuality = kMedium_SkFilterQuality;
84
reed98ed7b62015-09-15 12:38:12 -070085 if (kN32_SkColorType != provider.info().colorType() || !cache_size_okay(provider, fInvMatrix) ||
reed64045422015-06-04 06:31:31 -070086 fInvMatrix.hasPerspective())
87 {
88 return false; // can't handle the reqeust
89 }
90
91 SkScalar invScaleX = fInvMatrix.getScaleX();
92 SkScalar invScaleY = fInvMatrix.getScaleY();
93 if (fInvMatrix.getType() & SkMatrix::kAffine_Mask) {
94 SkSize scale;
95 if (!fInvMatrix.decomposeScale(&scale)) {
96 return false;
97 }
98 invScaleX = scale.width();
99 invScaleY = scale.height();
100 }
reeda3d99a52016-02-03 19:07:54 -0800101 invScaleX = SkScalarAbs(invScaleX);
102 invScaleY = SkScalarAbs(invScaleY);
reeda3d99a52016-02-03 19:07:54 -0800103
reed64045422015-06-04 06:31:31 -0700104 if (SkScalarNearlyEqual(invScaleX, 1) && SkScalarNearlyEqual(invScaleY, 1)) {
105 return false; // no need for HQ
106 }
fmalita3e2f5622015-12-09 07:18:16 -0800107
108#ifndef SK_SUPPORT_LEGACY_HQ_DOWNSAMPLING
109 if (invScaleX > 1 || invScaleY > 1) {
110 return false; // only use HQ when upsampling
111 }
112#endif
reed64045422015-06-04 06:31:31 -0700113
reed98ed7b62015-09-15 12:38:12 -0700114 const int dstW = SkScalarRoundToScalar(provider.width() / invScaleX);
115 const int dstH = SkScalarRoundToScalar(provider.height() / invScaleY);
116 const SkBitmapCacheDesc desc = provider.makeCacheDesc(dstW, dstH);
117
118 if (!SkBitmapCache::FindWH(desc, &fResultBitmap)) {
119 SkBitmap orig;
120 if (!provider.asBitmap(&orig)) {
121 return false;
122 }
reed3c834322015-06-12 07:09:59 -0700123 SkAutoPixmapUnlock src;
reed98ed7b62015-09-15 12:38:12 -0700124 if (!orig.requestLock(&src)) {
reed3c834322015-06-12 07:09:59 -0700125 return false;
126 }
reed99138872015-08-31 15:16:17 -0700127 if (!SkBitmapScaler::Resize(&fResultBitmap, src.pixmap(), kHQ_RESIZE_METHOD,
128 dstW, dstH, SkResourceCache::GetAllocator())) {
reed64045422015-06-04 06:31:31 -0700129 return false; // we failed to create fScaledBitmap
130 }
131
reedad7ae6c2015-06-04 14:12:25 -0700132 SkASSERT(fResultBitmap.getPixels());
133 fResultBitmap.setImmutable();
reed09553032015-11-23 12:32:16 -0800134 if (!provider.isVolatile()) {
135 if (SkBitmapCache::AddWH(desc, fResultBitmap)) {
136 provider.notifyAddedToCache();
137 }
reed98ed7b62015-09-15 12:38:12 -0700138 }
reed64045422015-06-04 06:31:31 -0700139 }
140
reedad7ae6c2015-06-04 14:12:25 -0700141 SkASSERT(fResultBitmap.getPixels());
reed64045422015-06-04 06:31:31 -0700142
reed98ed7b62015-09-15 12:38:12 -0700143 fInvMatrix.postScale(SkIntToScalar(dstW) / provider.width(),
144 SkIntToScalar(dstH) / provider.height());
reed64045422015-06-04 06:31:31 -0700145 fQuality = kLow_SkFilterQuality;
146 return true;
147}
148
149/*
150 * Modulo internal errors, this should always succeed *if* the matrix is downscaling
151 * (in this case, we have the inverse, so it succeeds if fInvMatrix is upscaling)
152 */
reed98ed7b62015-09-15 12:38:12 -0700153bool SkDefaultBitmapControllerState::processMediumRequest(const SkBitmapProvider& provider) {
reed64045422015-06-04 06:31:31 -0700154 SkASSERT(fQuality <= kMedium_SkFilterQuality);
155 if (fQuality != kMedium_SkFilterQuality) {
156 return false;
157 }
158
159 // Our default return state is to downgrade the request to Low, w/ or w/o setting fBitmap
160 // to a valid bitmap.
161 fQuality = kLow_SkFilterQuality;
162
163 SkSize invScaleSize;
halcanary96fcdcc2015-08-27 07:41:13 -0700164 if (!fInvMatrix.decomposeScale(&invScaleSize, nullptr)) {
reed64045422015-06-04 06:31:31 -0700165 return false;
166 }
fmalita921d7ac2016-01-22 11:45:39 -0800167
fmalita33ed3ad2016-02-09 08:20:18 -0800168 if (invScaleSize.width() > SK_Scalar1 || invScaleSize.height() > SK_Scalar1) {
reed98ed7b62015-09-15 12:38:12 -0700169 fCurrMip.reset(SkMipMapCache::FindAndRef(provider.makeCacheDesc()));
halcanary96fcdcc2015-08-27 07:41:13 -0700170 if (nullptr == fCurrMip.get()) {
reed98ed7b62015-09-15 12:38:12 -0700171 SkBitmap orig;
172 if (!provider.asBitmap(&orig)) {
173 return false;
174 }
175 fCurrMip.reset(SkMipMapCache::AddAndRef(orig));
halcanary96fcdcc2015-08-27 07:41:13 -0700176 if (nullptr == fCurrMip.get()) {
reed64045422015-06-04 06:31:31 -0700177 return false;
178 }
179 }
180 // diagnostic for a crasher...
halcanary96fcdcc2015-08-27 07:41:13 -0700181 if (nullptr == fCurrMip->data()) {
reed64045422015-06-04 06:31:31 -0700182 sk_throw();
183 }
184
fmalita33ed3ad2016-02-09 08:20:18 -0800185 const SkSize scale = SkSize::Make(SkScalarInvert(invScaleSize.width()),
186 SkScalarInvert(invScaleSize.height()));
reed64045422015-06-04 06:31:31 -0700187 SkMipMap::Level level;
fmalita33ed3ad2016-02-09 08:20:18 -0800188 if (fCurrMip->extractLevel(scale, &level)) {
fmalita921d7ac2016-01-22 11:45:39 -0800189 const SkSize& invScaleFixup = level.fScale;
190 fInvMatrix.postScale(invScaleFixup.width(), invScaleFixup.height());
191
reed64045422015-06-04 06:31:31 -0700192 // todo: if we could wrap the fCurrMip in a pixelref, then we could just install
193 // that here, and not need to explicitly track it ourselves.
reed67b09bf2016-01-16 18:50:35 -0800194 return fResultBitmap.installPixels(level.fPixmap);
reed64045422015-06-04 06:31:31 -0700195 } else {
196 // failed to extract, so release the mipmap
halcanary96fcdcc2015-08-27 07:41:13 -0700197 fCurrMip.reset(nullptr);
reed64045422015-06-04 06:31:31 -0700198 }
199 }
200 return false;
201}
202
reed98ed7b62015-09-15 12:38:12 -0700203SkDefaultBitmapControllerState::SkDefaultBitmapControllerState(const SkBitmapProvider& provider,
reed64045422015-06-04 06:31:31 -0700204 const SkMatrix& inv,
205 SkFilterQuality qual) {
206 fInvMatrix = inv;
207 fQuality = qual;
208
reed98ed7b62015-09-15 12:38:12 -0700209 if (this->processHQRequest(provider) || this->processMediumRequest(provider)) {
reedad7ae6c2015-06-04 14:12:25 -0700210 SkASSERT(fResultBitmap.getPixels());
211 } else {
reed98ed7b62015-09-15 12:38:12 -0700212 (void)provider.asBitmap(&fResultBitmap);
reedad7ae6c2015-06-04 14:12:25 -0700213 fResultBitmap.lockPixels();
214 // lock may fail to give us pixels
reed64045422015-06-04 06:31:31 -0700215 }
216 SkASSERT(fQuality <= kLow_SkFilterQuality);
reedad7ae6c2015-06-04 14:12:25 -0700217
218 // fResultBitmap.getPixels() may be null, but our caller knows to check fPixmap.addr()
halcanary96fcdcc2015-08-27 07:41:13 -0700219 // and will destroy us if it is nullptr.
reedad7ae6c2015-06-04 14:12:25 -0700220 fPixmap.reset(fResultBitmap.info(), fResultBitmap.getPixels(), fResultBitmap.rowBytes(),
221 fResultBitmap.getColorTable());
reed64045422015-06-04 06:31:31 -0700222}
223
reed98ed7b62015-09-15 12:38:12 -0700224SkBitmapController::State* SkDefaultBitmapController::onRequestBitmap(const SkBitmapProvider& bm,
reed64045422015-06-04 06:31:31 -0700225 const SkMatrix& inverse,
226 SkFilterQuality quality,
227 void* storage, size_t size) {
228 return SkInPlaceNewCheck<SkDefaultBitmapControllerState>(storage, size, bm, inverse, quality);
229}
230