OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/tiles/gpu_image_decode_cache.h" | 5 #include "cc/tiles/gpu_image_decode_cache.h" |
6 | 6 |
7 #include <inttypes.h> | 7 #include <inttypes.h> |
8 | 8 |
9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
10 #include "base/debug/alias.h" | 10 #include "base/debug/alias.h" |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 return MipMapUtil::GetScaleAdjustmentForLevel(base_size, mip_level); | 103 return MipMapUtil::GetScaleAdjustmentForLevel(base_size, mip_level); |
104 } | 104 } |
105 | 105 |
106 // Calculates the size of a given mip level. | 106 // Calculates the size of a given mip level. |
107 gfx::Size CalculateSizeForMipLevel(const DrawImage& draw_image, int mip_level) { | 107 gfx::Size CalculateSizeForMipLevel(const DrawImage& draw_image, int mip_level) { |
108 gfx::Size base_size(draw_image.image()->width(), | 108 gfx::Size base_size(draw_image.image()->width(), |
109 draw_image.image()->height()); | 109 draw_image.image()->height()); |
110 return MipMapUtil::GetSizeForLevel(base_size, mip_level); | 110 return MipMapUtil::GetSizeForLevel(base_size, mip_level); |
111 } | 111 } |
112 | 112 |
| 113 // Draws and scales the provided |draw_image| into the |target_pixmap|. If the |
| 114 // draw/scale can be done directly, calls directly into SkImage::scalePixels, |
| 115 // if not, decodes to a compatible temporary pixmap and then converts that into |
| 116 // the |target_pixmap|. |
| 117 bool DrawAndScaleImage(const DrawImage& draw_image, SkPixmap* target_pixmap) { |
| 118 const SkImage* image = draw_image.image().get(); |
| 119 if (image->dimensions() == target_pixmap->bounds().size() || |
| 120 target_pixmap->info().colorType() == kN32_SkColorType) { |
| 121 // If no scaling is occurring, or if the target colortype is already N32, |
| 122 // just scale directly. |
| 123 return image->scalePixels(*target_pixmap, |
| 124 CalculateUploadScaleFilterQuality(draw_image), |
| 125 SkImage::kDisallow_CachingHint); |
| 126 } |
| 127 |
| 128 // If the target colortype is not N32, it may be impossible to scale |
| 129 // directly. Instead scale into an N32 pixmap, and convert that into the |
| 130 // |target_pixmap|. |
| 131 SkImageInfo decode_info = |
| 132 target_pixmap->info().makeColorType(kN32_SkColorType); |
| 133 SkBitmap decode_bitmap; |
| 134 if (!decode_bitmap.tryAllocPixels(decode_info)) |
| 135 return false; |
| 136 SkPixmap decode_pixmap(decode_bitmap.info(), decode_bitmap.getPixels(), |
| 137 decode_bitmap.rowBytes()); |
| 138 if (!image->scalePixels(decode_pixmap, |
| 139 CalculateUploadScaleFilterQuality(draw_image), |
| 140 SkImage::kDisallow_CachingHint)) |
| 141 return false; |
| 142 return decode_pixmap.readPixels(*target_pixmap); |
| 143 } |
| 144 |
113 } // namespace | 145 } // namespace |
114 | 146 |
115 // static | 147 // static |
116 GpuImageDecodeCache::InUseCacheKey | 148 GpuImageDecodeCache::InUseCacheKey |
117 GpuImageDecodeCache::InUseCacheKey::FromDrawImage(const DrawImage& draw_image) { | 149 GpuImageDecodeCache::InUseCacheKey::FromDrawImage(const DrawImage& draw_image) { |
118 return InUseCacheKey(draw_image); | 150 return InUseCacheKey(draw_image); |
119 } | 151 } |
120 | 152 |
121 // Extract the information to uniquely identify a DrawImage for the purposes of | 153 // Extract the information to uniquely identify a DrawImage for the purposes of |
122 // the |in_use_cache_|. | 154 // the |in_use_cache_|. |
(...skipping 978 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1101 case DecodedDataMode::CPU: { | 1133 case DecodedDataMode::CPU: { |
1102 SkImageInfo image_info = CreateImageInfoForDrawImage( | 1134 SkImageInfo image_info = CreateImageInfoForDrawImage( |
1103 draw_image, image_data->upload_params.fPreScaleMipLevel); | 1135 draw_image, image_data->upload_params.fPreScaleMipLevel); |
1104 // In order to match GPU scaling quality (which uses mip-maps at high | 1136 // In order to match GPU scaling quality (which uses mip-maps at high |
1105 // quality), we want to use at most medium filter quality for the | 1137 // quality), we want to use at most medium filter quality for the |
1106 // scale. | 1138 // scale. |
1107 SkPixmap image_pixmap(image_info.makeColorSpace(nullptr), | 1139 SkPixmap image_pixmap(image_info.makeColorSpace(nullptr), |
1108 backing_memory->data(), image_info.minRowBytes()); | 1140 backing_memory->data(), image_info.minRowBytes()); |
1109 // Note that scalePixels falls back to readPixels if the scale is 1x, so | 1141 // Note that scalePixels falls back to readPixels if the scale is 1x, so |
1110 // no need to special case that as an optimization. | 1142 // no need to special case that as an optimization. |
1111 if (!draw_image.image()->scalePixels( | 1143 if (!DrawAndScaleImage(draw_image, &image_pixmap)) { |
1112 image_pixmap, CalculateUploadScaleFilterQuality(draw_image), | |
1113 SkImage::kDisallow_CachingHint)) { | |
1114 DLOG(ERROR) << "scalePixels failed."; | 1144 DLOG(ERROR) << "scalePixels failed."; |
1115 backing_memory->Unlock(); | 1145 backing_memory->Unlock(); |
1116 backing_memory.reset(); | 1146 backing_memory.reset(); |
1117 } | 1147 } |
1118 break; | 1148 break; |
1119 } | 1149 } |
1120 case DecodedDataMode::GPU: { | 1150 case DecodedDataMode::GPU: { |
1121 // TODO(crbug.com/649167): Params should not have changed since initial | 1151 // TODO(crbug.com/649167): Params should not have changed since initial |
1122 // sizing. Somehow this still happens. We should investigate and re-add | 1152 // sizing. Somehow this still happens. We should investigate and re-add |
1123 // DCHECKs here to enforce this. | 1153 // DCHECKs here to enforce this. |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1345 | 1375 |
1346 void GpuImageDecodeCache::OnPurgeMemory() { | 1376 void GpuImageDecodeCache::OnPurgeMemory() { |
1347 base::AutoLock lock(lock_); | 1377 base::AutoLock lock(lock_); |
1348 // Temporary changes |memory_state_| to free up cache as much as possible. | 1378 // Temporary changes |memory_state_| to free up cache as much as possible. |
1349 base::AutoReset<base::MemoryState> reset(&memory_state_, | 1379 base::AutoReset<base::MemoryState> reset(&memory_state_, |
1350 base::MemoryState::SUSPENDED); | 1380 base::MemoryState::SUSPENDED); |
1351 EnsureCapacity(0); | 1381 EnsureCapacity(0); |
1352 } | 1382 } |
1353 | 1383 |
1354 } // namespace cc | 1384 } // namespace cc |
OLD | NEW |