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" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) { | 45 SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) { |
46 SkBitmap* bmp = new SkBitmap(); | 46 SkBitmap* bmp = new SkBitmap(); |
47 bmp->setInfo(canvas->imageInfo()); | 47 bmp->setInfo(canvas->imageInfo()); |
48 if (!canvas->readPixels(bmp, 0, 0)) { | 48 if (!canvas->readPixels(bmp, 0, 0)) { |
49 fprintf(stderr, "Can't read pixels\n"); | 49 fprintf(stderr, "Can't read pixels\n"); |
50 return nullptr; | 50 return nullptr; |
51 } | 51 } |
52 return bmp; | 52 return bmp; |
53 } | 53 } |
54 | 54 |
55 SkData* Request::writeCanvasToPng(SkCanvas* canvas) { | 55 sk_sp<SkData> Request::writeCanvasToPng(SkCanvas* canvas) { |
56 // capture pixels | 56 // capture pixels |
57 SkAutoTDelete<SkBitmap> bmp(this->getBitmapFromCanvas(canvas)); | 57 SkAutoTDelete<SkBitmap> bmp(this->getBitmapFromCanvas(canvas)); |
58 SkASSERT(bmp); | 58 SkASSERT(bmp); |
59 | 59 |
60 // Convert to format suitable for PNG output | 60 // Convert to format suitable for PNG output |
61 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(*bmp); | 61 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(*bmp); |
62 SkASSERT(encodedBitmap.get()); | 62 SkASSERT(encodedBitmap.get()); |
63 | 63 |
64 // write to an opaque png (black background) | 64 // write to an opaque png (black background) |
65 SkDynamicMemoryWStream buffer; | 65 SkDynamicMemoryWStream buffer; |
66 SkDrawCommand::WritePNG((const png_bytep) encodedBitmap->bytes(), bmp->width
(), bmp->height(), | 66 SkDrawCommand::WritePNG((const png_bytep) encodedBitmap->bytes(), bmp->width
(), bmp->height(), |
67 buffer, true); | 67 buffer, true); |
68 return buffer.copyToData(); | 68 return sk_sp<SkData>(buffer.copyToData()); |
69 } | 69 } |
70 | 70 |
71 SkCanvas* Request::getCanvas() { | 71 SkCanvas* Request::getCanvas() { |
72 #if SK_SUPPORT_GPU | 72 #if SK_SUPPORT_GPU |
73 GrContextFactory* factory = fContextFactory; | 73 GrContextFactory* factory = fContextFactory; |
74 GLTestContext* gl = factory->getContextInfo(GrContextFactory::kNativeGL_Cont
extType, | 74 GLTestContext* gl = factory->getContextInfo(GrContextFactory::kNativeGL_Cont
extType, |
75 GrContextFactory::kNone_ContextO
ptions).glContext(); | 75 GrContextFactory::kNone_ContextO
ptions).glContext(); |
76 if (!gl) { | 76 if (!gl) { |
77 gl = factory->getContextInfo(GrContextFactory::kMESA_ContextType, | 77 gl = factory->getContextInfo(GrContextFactory::kMESA_ContextType, |
78 GrContextFactory::kNone_ContextOptions).glC
ontext(); | 78 GrContextFactory::kNone_ContextOptions).glC
ontext(); |
(...skipping 10 matching lines...) Expand all Loading... |
89 } | 89 } |
90 SkCanvas* target = fSurface->getCanvas(); | 90 SkCanvas* target = fSurface->getCanvas(); |
91 return target; | 91 return target; |
92 } | 92 } |
93 | 93 |
94 void Request::drawToCanvas(int n, int m) { | 94 void Request::drawToCanvas(int n, int m) { |
95 SkCanvas* target = this->getCanvas(); | 95 SkCanvas* target = this->getCanvas(); |
96 fDebugCanvas->drawTo(target, n, m); | 96 fDebugCanvas->drawTo(target, n, m); |
97 } | 97 } |
98 | 98 |
99 SkData* Request::drawToPng(int n, int m) { | 99 sk_sp<SkData> Request::drawToPng(int n, int m) { |
100 this->drawToCanvas(n, m); | 100 this->drawToCanvas(n, m); |
101 return writeCanvasToPng(this->getCanvas()); | 101 return writeCanvasToPng(this->getCanvas()); |
102 } | 102 } |
103 | 103 |
104 SkData* Request::writeOutSkp() { | 104 sk_sp<SkData> Request::writeOutSkp() { |
105 // Playback into picture recorder | 105 // Playback into picture recorder |
106 SkIRect bounds = this->getBounds(); | 106 SkIRect bounds = this->getBounds(); |
107 SkPictureRecorder recorder; | 107 SkPictureRecorder recorder; |
108 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(bounds.width()), | 108 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(bounds.width()), |
109 SkIntToScalar(bounds.height())); | 109 SkIntToScalar(bounds.height())); |
110 | 110 |
111 fDebugCanvas->draw(canvas); | 111 fDebugCanvas->draw(canvas); |
112 | 112 |
113 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); | 113 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture()); |
114 | 114 |
115 SkDynamicMemoryWStream outStream; | 115 SkDynamicMemoryWStream outStream; |
116 | 116 |
117 SkAutoTUnref<SkPixelSerializer> serializer(SkImageEncoder::CreatePixelSerial
izer()); | 117 SkAutoTUnref<SkPixelSerializer> serializer(SkImageEncoder::CreatePixelSerial
izer()); |
118 picture->serialize(&outStream, serializer); | 118 picture->serialize(&outStream, serializer); |
119 | 119 |
120 return outStream.copyToData(); | 120 return sk_sp<SkData>(outStream.copyToData()); |
121 } | 121 } |
122 | 122 |
123 GrContext* Request::getContext() { | 123 GrContext* Request::getContext() { |
124 #if SK_SUPPORT_GPU | 124 #if SK_SUPPORT_GPU |
125 GrContext* result = fContextFactory->get(GrContextFactory::kNativeGL_Context
Type, | 125 GrContext* result = fContextFactory->get(GrContextFactory::kNativeGL_Context
Type, |
126 GrContextFactory::kNone_ContextOpti
ons); | 126 GrContextFactory::kNone_ContextOpti
ons); |
127 if (!result) { | 127 if (!result) { |
128 result = fContextFactory->get(GrContextFactory::kMESA_ContextType, | 128 result = fContextFactory->get(GrContextFactory::kMESA_ContextType, |
129 GrContextFactory::kNone_ContextOptions); | 129 GrContextFactory::kNone_ContextOptions); |
130 } | 130 } |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 SkIRect bounds = this->getBounds(); | 235 SkIRect bounds = this->getBounds(); |
236 fDebugCanvas.reset(new SkDebugCanvas(bounds.width(), bounds.height())); | 236 fDebugCanvas.reset(new SkDebugCanvas(bounds.width(), bounds.height())); |
237 fDebugCanvas->drawPicture(fPicture); | 237 fDebugCanvas->drawPicture(fPicture); |
238 | 238 |
239 // for some reason we need to 'flush' the debug canvas by drawing all of the
ops | 239 // for some reason we need to 'flush' the debug canvas by drawing all of the
ops |
240 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp()); | 240 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp()); |
241 this->getCanvas()->flush(); | 241 this->getCanvas()->flush(); |
242 return true; | 242 return true; |
243 } | 243 } |
244 | 244 |
245 SkData* Request::getJsonOps(int n) { | 245 sk_sp<SkData> Request::getJsonOps(int n) { |
246 SkCanvas* canvas = this->getCanvas(); | 246 SkCanvas* canvas = this->getCanvas(); |
247 Json::Value root = fDebugCanvas->toJSON(fUrlDataManager, n, canvas); | 247 Json::Value root = fDebugCanvas->toJSON(fUrlDataManager, n, canvas); |
248 root["mode"] = Json::Value(fGPUEnabled ? "gpu" : "cpu"); | 248 root["mode"] = Json::Value(fGPUEnabled ? "gpu" : "cpu"); |
249 root["drawGpuBatchBounds"] = Json::Value(fDebugCanvas->getDrawGpuBatchBounds
()); | 249 root["drawGpuBatchBounds"] = Json::Value(fDebugCanvas->getDrawGpuBatchBounds
()); |
250 root["colorMode"] = Json::Value(fColorMode); | 250 root["colorMode"] = Json::Value(fColorMode); |
251 SkDynamicMemoryWStream stream; | 251 SkDynamicMemoryWStream stream; |
252 stream.writeText(Json::FastWriter().write(root).c_str()); | 252 stream.writeText(Json::FastWriter().write(root).c_str()); |
253 | 253 |
254 return stream.copyToData(); | 254 return sk_sp<SkData>(stream.copyToData()); |
255 } | 255 } |
256 | 256 |
257 SkData* Request::getJsonBatchList(int n) { | 257 sk_sp<SkData> Request::getJsonBatchList(int n) { |
258 SkCanvas* canvas = this->getCanvas(); | 258 SkCanvas* canvas = this->getCanvas(); |
259 SkASSERT(fGPUEnabled); | 259 SkASSERT(fGPUEnabled); |
260 | 260 |
261 Json::Value result = fDebugCanvas->toJSONBatchList(n, canvas); | 261 Json::Value result = fDebugCanvas->toJSONBatchList(n, canvas); |
262 | 262 |
263 SkDynamicMemoryWStream stream; | 263 SkDynamicMemoryWStream stream; |
264 stream.writeText(Json::FastWriter().write(result).c_str()); | 264 stream.writeText(Json::FastWriter().write(result).c_str()); |
265 | 265 |
266 return stream.copyToData(); | 266 return sk_sp<SkData>(stream.copyToData()); |
267 } | 267 } |
268 | 268 |
269 SkData* Request::getJsonInfo(int n) { | 269 sk_sp<SkData> Request::getJsonInfo(int n) { |
270 // drawTo | 270 // drawTo |
271 SkAutoTUnref<SkSurface> surface(this->createCPUSurface()); | 271 SkAutoTUnref<SkSurface> surface(this->createCPUSurface()); |
272 SkCanvas* canvas = surface->getCanvas(); | 272 SkCanvas* canvas = surface->getCanvas(); |
273 | 273 |
274 // TODO this is really slow and we should cache the matrix and clip | 274 // TODO this is really slow and we should cache the matrix and clip |
275 fDebugCanvas->drawTo(canvas, n); | 275 fDebugCanvas->drawTo(canvas, n); |
276 | 276 |
277 // make some json | 277 // make some json |
278 SkMatrix vm = fDebugCanvas->getCurrentMatrix(); | 278 SkMatrix vm = fDebugCanvas->getCurrentMatrix(); |
279 SkIRect clip = fDebugCanvas->getCurrentClip(); | 279 SkIRect clip = fDebugCanvas->getCurrentClip(); |
280 Json::Value info(Json::objectValue); | 280 Json::Value info(Json::objectValue); |
281 info["ViewMatrix"] = SkDrawCommand::MakeJsonMatrix(vm); | 281 info["ViewMatrix"] = SkDrawCommand::MakeJsonMatrix(vm); |
282 info["ClipRect"] = SkDrawCommand::MakeJsonIRect(clip); | 282 info["ClipRect"] = SkDrawCommand::MakeJsonIRect(clip); |
283 | 283 |
284 std::string json = Json::FastWriter().write(info); | 284 std::string json = Json::FastWriter().write(info); |
285 | 285 |
286 // We don't want the null terminator so strlen is correct | 286 // We don't want the null terminator so strlen is correct |
287 return SkData::NewWithCopy(json.c_str(), strlen(json.c_str())); | 287 return SkData::MakeWithCopy(json.c_str(), strlen(json.c_str())); |
288 } | 288 } |
289 | 289 |
290 SkColor Request::getPixel(int x, int y) { | 290 SkColor Request::getPixel(int x, int y) { |
291 SkCanvas* canvas = this->getCanvas(); | 291 SkCanvas* canvas = this->getCanvas(); |
292 canvas->flush(); | 292 canvas->flush(); |
293 SkAutoTDelete<SkBitmap> bitmap(this->getBitmapFromCanvas(canvas)); | 293 SkAutoTDelete<SkBitmap> bitmap(this->getBitmapFromCanvas(canvas)); |
294 SkASSERT(bitmap); | 294 SkASSERT(bitmap); |
295 | 295 |
296 // Convert to format suitable for inspection | 296 // Convert to format suitable for inspection |
297 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(*bitmap); | 297 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(*bitmap); |
298 SkASSERT(encodedBitmap.get()); | 298 SkASSERT(encodedBitmap.get()); |
299 | 299 |
300 const uint8_t* start = encodedBitmap->bytes() + ((y * bitmap->width() + x) *
4); | 300 const uint8_t* start = encodedBitmap->bytes() + ((y * bitmap->width() + x) *
4); |
301 SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]); | 301 SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]); |
302 return result; | 302 return result; |
303 } | 303 } |
OLD | NEW |