Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "Request.h" | 8 #include "Request.h" |
| 9 | 9 |
| 10 #include "SkPictureRecorder.h" | 10 #include "SkPictureRecorder.h" |
| 11 #include "SkPixelSerializer.h" | 11 #include "SkPixelSerializer.h" |
| 12 | 12 |
| 13 using namespace sk_gpu_test; | 13 using namespace sk_gpu_test; |
| 14 | 14 |
| 15 static int kDefaultWidth = 1920; | 15 static int kDefaultWidth = 1920; |
| 16 static int kDefaultHeight = 1080; | 16 static int kDefaultHeight = 1080; |
| 17 | 17 |
| 18 | 18 |
| 19 Request::Request(SkString rootUrl) | 19 Request::Request(SkString rootUrl) |
| 20 : fUploadContext(nullptr) | 20 : fUploadContext(nullptr) |
| 21 , fUrlDataManager(rootUrl) | 21 , fUrlDataManager(rootUrl) |
| 22 , fGPUEnabled(false) { | 22 , fGPUEnabled(false) |
| 23 , fColorMode(0) { | |
| 23 // create surface | 24 // create surface |
| 24 #if SK_SUPPORT_GPU | 25 #if SK_SUPPORT_GPU |
| 25 GrContextOptions grContextOpts; | 26 GrContextOptions grContextOpts; |
| 26 fContextFactory = new GrContextFactory(grContextOpts); | 27 fContextFactory = new GrContextFactory(grContextOpts); |
| 27 #else | 28 #else |
| 28 fContextFactory = nullptr; | 29 fContextFactory = nullptr; |
| 29 #endif | 30 #endif |
| 30 } | 31 } |
| 31 | 32 |
| 32 Request::~Request() { | 33 Request::~Request() { |
| 33 #if SK_SUPPORT_GPU | 34 #if SK_SUPPORT_GPU |
| 34 if (fContextFactory) { | 35 if (fContextFactory) { |
| 35 delete fContextFactory; | 36 delete fContextFactory; |
| 36 } | 37 } |
| 37 #endif | 38 #endif |
| 38 } | 39 } |
| 39 | 40 |
| 40 SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) { | 41 SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) { |
| 41 SkBitmap* bmp = new SkBitmap(); | 42 SkBitmap* bmp = new SkBitmap(); |
| 42 SkIRect bounds = this->getBounds(); | 43 SkIRect bounds = this->getBounds(); |
| 43 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), | 44 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), |
| 44 kRGBA_8888_SkColorType, kOpaque_SkAlpha Type); | 45 kRGBA_8888_SkColorType, kOpaque_SkAlpha Type); |
| 45 bmp->setInfo(info); | 46 if (kRGBA_F16_SkColorType == canvas->imageInfo().colorType()) { |
|
Brian Osman
2016/04/18 16:17:03
We have other special-case conversion code in DM,
mtklein
2016/04/18 17:40:48
Think this is a sign we should fix readPixels() fi
| |
| 46 if (!canvas->readPixels(bmp, 0, 0)) { | 47 SkBitmap n32TempBmp; |
| 47 fprintf(stderr, "Can't read pixels\n"); | 48 SkImageInfo n32Info = SkImageInfo::Make(bounds.width(), bounds.height(), |
| 48 return nullptr; | 49 kN32_SkColorType, kOpaque_SkAlph aType); |
| 50 n32TempBmp.setInfo(n32Info); | |
| 51 if (!canvas->readPixels(&n32TempBmp, 0, 0)) { | |
| 52 fprintf(stderr, "Can't read pixels\n"); | |
| 53 return nullptr; | |
| 54 } | |
| 55 | |
| 56 if (!n32TempBmp.copyTo(bmp, kRGBA_8888_SkColorType)) { | |
| 57 fprintf(stderr, "Can't copy pixels\n"); | |
| 58 return nullptr; | |
| 59 } | |
| 60 } else { | |
| 61 bmp->setInfo(info); | |
| 62 if (!canvas->readPixels(bmp, 0, 0)) { | |
| 63 fprintf(stderr, "Can't read pixels\n"); | |
| 64 return nullptr; | |
| 65 } | |
| 49 } | 66 } |
| 50 return bmp; | 67 return bmp; |
| 51 } | 68 } |
| 52 | 69 |
| 53 SkData* Request::writeCanvasToPng(SkCanvas* canvas) { | 70 SkData* Request::writeCanvasToPng(SkCanvas* canvas) { |
| 54 // capture pixels | 71 // capture pixels |
| 55 SkAutoTDelete<SkBitmap> bmp(this->getBitmapFromCanvas(canvas)); | 72 SkAutoTDelete<SkBitmap> bmp(this->getBitmapFromCanvas(canvas)); |
| 56 SkASSERT(bmp); | 73 SkASSERT(bmp); |
| 57 | 74 |
| 58 // write to png | 75 // write to png |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 131 bounds = SkIRect::MakeWH(kDefaultWidth, kDefaultHeight); | 148 bounds = SkIRect::MakeWH(kDefaultWidth, kDefaultHeight); |
| 132 } | 149 } |
| 133 | 150 |
| 134 // We clip to kDefaultWidth / kDefaultHeight for performance reasons | 151 // We clip to kDefaultWidth / kDefaultHeight for performance reasons |
| 135 // TODO make this configurable | 152 // TODO make this configurable |
| 136 bounds = SkIRect::MakeWH(SkTMin(bounds.width(), kDefaultWidth), | 153 bounds = SkIRect::MakeWH(SkTMin(bounds.width(), kDefaultWidth), |
| 137 SkTMin(bounds.height(), kDefaultHeight)); | 154 SkTMin(bounds.height(), kDefaultHeight)); |
| 138 return bounds; | 155 return bounds; |
| 139 } | 156 } |
| 140 | 157 |
| 158 namespace { | |
| 159 | |
| 160 struct ColorAndProfile { | |
| 161 SkColorType fColorType; | |
| 162 SkColorProfileType fProfileType; | |
| 163 bool fGammaCorrect; | |
| 164 }; | |
| 165 | |
| 166 ColorAndProfile ColorModes[] = { | |
| 167 { kN32_SkColorType, kLinear_SkColorProfileType, false }, | |
| 168 { kN32_SkColorType, kSRGB_SkColorProfileType, true }, | |
| 169 { kRGBA_F16_SkColorType, kLinear_SkColorProfileType, true }, | |
| 170 }; | |
| 171 | |
| 172 } | |
| 173 | |
| 141 SkSurface* Request::createCPUSurface() { | 174 SkSurface* Request::createCPUSurface() { |
| 142 SkIRect bounds = this->getBounds(); | 175 SkIRect bounds = this->getBounds(); |
| 143 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), kN32_S kColorType, | 176 ColorAndProfile cap = ColorModes[fColorMode]; |
| 144 kPremul_SkAlphaType); | 177 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), cap.fC olorType, |
| 178 kPremul_SkAlphaType, cap.fProfileType); | |
| 145 return SkSurface::MakeRaster(info).release(); | 179 return SkSurface::MakeRaster(info).release(); |
| 146 } | 180 } |
| 147 | 181 |
| 148 SkSurface* Request::createGPUSurface() { | 182 SkSurface* Request::createGPUSurface() { |
| 149 GrContext* context = this->getContext(); | 183 GrContext* context = this->getContext(); |
| 150 SkIRect bounds = this->getBounds(); | 184 SkIRect bounds = this->getBounds(); |
| 151 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), | 185 ColorAndProfile cap = ColorModes[fColorMode]; |
| 152 kN32_SkColorType, kPremul_SkAlphaType); | 186 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), cap.fC olorType, |
| 153 uint32_t flags = 0; | 187 kPremul_SkAlphaType, cap.fProfileType); |
| 188 uint32_t flags = cap.fGammaCorrect ? SkSurfaceProps::kGammaCorrect_Flag : 0; | |
|
Brian Osman
2016/04/18 16:17:03
Need to do this in createCPUSurface, too.
| |
| 154 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); | 189 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); |
| 155 SkSurface* surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, i nfo, 0, | 190 SkSurface* surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, i nfo, 0, |
| 156 &props).release(); | 191 &props).release(); |
| 157 return surface; | 192 return surface; |
| 158 } | 193 } |
| 159 | 194 |
| 195 bool Request::setColorMode(int mode) { | |
| 196 fColorMode = mode; | |
| 197 return enableGPU(fGPUEnabled); | |
| 198 } | |
| 199 | |
| 160 bool Request::enableGPU(bool enable) { | 200 bool Request::enableGPU(bool enable) { |
| 161 if (enable) { | 201 if (enable) { |
| 162 SkSurface* surface = this->createGPUSurface(); | 202 SkSurface* surface = this->createGPUSurface(); |
| 163 if (surface) { | 203 if (surface) { |
| 164 fSurface.reset(surface); | 204 fSurface.reset(surface); |
| 165 fGPUEnabled = true; | 205 fGPUEnabled = true; |
| 166 | 206 |
| 167 // When we switch to GPU, there seems to be some mystery draws in th e canvas. So we | 207 // When we switch to GPU, there seems to be some mystery draws in th e canvas. So we |
| 168 // draw once to flush the pipe | 208 // draw once to flush the pipe |
| 169 // TODO understand what is actually happening here | 209 // TODO understand what is actually happening here |
| 170 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp()); | 210 if (fDebugCanvas) { |
| 171 this->getCanvas()->flush(); | 211 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp()); |
| 212 this->getCanvas()->flush(); | |
| 213 } | |
| 172 | 214 |
| 173 return true; | 215 return true; |
| 174 } | 216 } |
| 175 return false; | 217 return false; |
| 176 } | 218 } |
| 177 fSurface.reset(this->createCPUSurface()); | 219 fSurface.reset(this->createCPUSurface()); |
| 178 fGPUEnabled = false; | 220 fGPUEnabled = false; |
| 179 return true; | 221 return true; |
| 180 } | 222 } |
| 181 | 223 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 199 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp()); | 241 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp()); |
| 200 this->getCanvas()->flush(); | 242 this->getCanvas()->flush(); |
| 201 return true; | 243 return true; |
| 202 } | 244 } |
| 203 | 245 |
| 204 SkData* Request::getJsonOps(int n) { | 246 SkData* Request::getJsonOps(int n) { |
| 205 SkCanvas* canvas = this->getCanvas(); | 247 SkCanvas* canvas = this->getCanvas(); |
| 206 Json::Value root = fDebugCanvas->toJSON(fUrlDataManager, n, canvas); | 248 Json::Value root = fDebugCanvas->toJSON(fUrlDataManager, n, canvas); |
| 207 root["mode"] = Json::Value(fGPUEnabled ? "gpu" : "cpu"); | 249 root["mode"] = Json::Value(fGPUEnabled ? "gpu" : "cpu"); |
| 208 root["drawGpuBatchBounds"] = Json::Value(fDebugCanvas->getDrawGpuBatchBounds ()); | 250 root["drawGpuBatchBounds"] = Json::Value(fDebugCanvas->getDrawGpuBatchBounds ()); |
| 251 root["colorMode"] = Json::Value(fColorMode); | |
| 209 SkDynamicMemoryWStream stream; | 252 SkDynamicMemoryWStream stream; |
| 210 stream.writeText(Json::FastWriter().write(root).c_str()); | 253 stream.writeText(Json::FastWriter().write(root).c_str()); |
| 211 | 254 |
| 212 return stream.copyToData(); | 255 return stream.copyToData(); |
| 213 } | 256 } |
| 214 | 257 |
| 215 SkData* Request::getJsonBatchList(int n) { | 258 SkData* Request::getJsonBatchList(int n) { |
| 216 SkCanvas* canvas = this->getCanvas(); | 259 SkCanvas* canvas = this->getCanvas(); |
| 217 SkASSERT(fGPUEnabled); | 260 SkASSERT(fGPUEnabled); |
| 218 | 261 |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 249 SkCanvas* canvas = this->getCanvas(); | 292 SkCanvas* canvas = this->getCanvas(); |
| 250 canvas->flush(); | 293 canvas->flush(); |
| 251 SkAutoTDelete<SkBitmap> bitmap(this->getBitmapFromCanvas(canvas)); | 294 SkAutoTDelete<SkBitmap> bitmap(this->getBitmapFromCanvas(canvas)); |
| 252 SkASSERT(bitmap); | 295 SkASSERT(bitmap); |
| 253 bitmap->lockPixels(); | 296 bitmap->lockPixels(); |
| 254 uint8_t* start = ((uint8_t*) bitmap->getPixels()) + (y * bitmap->width() + x ) * 4; | 297 uint8_t* start = ((uint8_t*) bitmap->getPixels()) + (y * bitmap->width() + x ) * 4; |
| 255 SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]); | 298 SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]); |
| 256 bitmap->unlockPixels(); | 299 bitmap->unlockPixels(); |
| 257 return result; | 300 return result; |
| 258 } | 301 } |
| OLD | NEW |