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 "png.h" | 10 #include "png.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 png_write_png(png, info_ptr, PNG_TRANSFORM_IDENTITY, NULL); | 49 png_write_png(png, info_ptr, PNG_TRANSFORM_IDENTITY, NULL); |
50 png_destroy_write_struct(&png, NULL); | 50 png_destroy_write_struct(&png, NULL); |
51 sk_free(rows); | 51 sk_free(rows); |
52 } | 52 } |
53 | 53 |
54 Request::Request(SkString rootUrl) | 54 Request::Request(SkString rootUrl) |
55 : fUploadContext(nullptr) | 55 : fUploadContext(nullptr) |
56 , fUrlDataManager(rootUrl) | 56 , fUrlDataManager(rootUrl) |
57 , fGPUEnabled(false) { | 57 , fGPUEnabled(false) { |
58 // create surface | 58 // create surface |
| 59 #if SK_SUPPORT_GPU |
59 GrContextOptions grContextOpts; | 60 GrContextOptions grContextOpts; |
60 fContextFactory.reset(new GrContextFactory(grContextOpts)); | 61 fContextFactory = new GrContextFactory(grContextOpts); |
| 62 #else |
| 63 fContextFactory = nullptr; |
| 64 #endif |
| 65 } |
| 66 |
| 67 Request::~Request() { |
| 68 #if SK_SUPPORT_GPU |
| 69 if (fContextFactory) { |
| 70 delete fContextFactory; |
| 71 } |
| 72 #endif |
61 } | 73 } |
62 | 74 |
63 SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) { | 75 SkBitmap* Request::getBitmapFromCanvas(SkCanvas* canvas) { |
64 SkBitmap* bmp = new SkBitmap(); | 76 SkBitmap* bmp = new SkBitmap(); |
65 SkIRect bounds = this->getBounds(); | 77 SkIRect bounds = this->getBounds(); |
66 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), | 78 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), |
67 kRGBA_8888_SkColorType, kOpaque_SkAlpha
Type); | 79 kRGBA_8888_SkColorType, kOpaque_SkAlpha
Type); |
68 bmp->setInfo(info); | 80 bmp->setInfo(info); |
69 if (!canvas->readPixels(bmp, 0, 0)) { | 81 if (!canvas->readPixels(bmp, 0, 0)) { |
70 fprintf(stderr, "Can't read pixels\n"); | 82 fprintf(stderr, "Can't read pixels\n"); |
71 return nullptr; | 83 return nullptr; |
72 } | 84 } |
73 return bmp; | 85 return bmp; |
74 } | 86 } |
75 | 87 |
76 SkData* Request::writeCanvasToPng(SkCanvas* canvas) { | 88 SkData* Request::writeCanvasToPng(SkCanvas* canvas) { |
77 // capture pixels | 89 // capture pixels |
78 SkAutoTDelete<SkBitmap> bmp(this->getBitmapFromCanvas(canvas)); | 90 SkAutoTDelete<SkBitmap> bmp(this->getBitmapFromCanvas(canvas)); |
79 SkASSERT(bmp); | 91 SkASSERT(bmp); |
80 | 92 |
81 // write to png | 93 // write to png |
82 SkDynamicMemoryWStream buffer; | 94 SkDynamicMemoryWStream buffer; |
83 write_png((const png_bytep) bmp->getPixels(), bmp->width(), bmp->height(), b
uffer); | 95 write_png((const png_bytep) bmp->getPixels(), bmp->width(), bmp->height(), b
uffer); |
84 return buffer.copyToData(); | 96 return buffer.copyToData(); |
85 } | 97 } |
86 | 98 |
87 SkCanvas* Request::getCanvas() { | 99 SkCanvas* Request::getCanvas() { |
| 100 #if SK_SUPPORT_GPU |
88 GrContextFactory* factory = fContextFactory; | 101 GrContextFactory* factory = fContextFactory; |
89 SkGLContext* gl = factory->getContextInfo(GrContextFactory::kNative_GLContex
tType, | 102 SkGLContext* gl = factory->getContextInfo(GrContextFactory::kNative_GLContex
tType, |
90 GrContextFactory::kNone_GLContextO
ptions).fGLContext; | 103 GrContextFactory::kNone_GLContextO
ptions).fGLContext; |
91 gl->makeCurrent(); | 104 gl->makeCurrent(); |
| 105 #endif |
92 SkASSERT(fDebugCanvas); | 106 SkASSERT(fDebugCanvas); |
93 | 107 |
94 // create the appropriate surface if necessary | 108 // create the appropriate surface if necessary |
95 if (!fSurface) { | 109 if (!fSurface) { |
96 this->enableGPU(fGPUEnabled); | 110 this->enableGPU(fGPUEnabled); |
97 } | 111 } |
98 SkCanvas* target = fSurface->getCanvas(); | 112 SkCanvas* target = fSurface->getCanvas(); |
99 return target; | 113 return target; |
100 } | 114 } |
101 | 115 |
(...skipping 19 matching lines...) Expand all Loading... |
121 | 135 |
122 SkDynamicMemoryWStream outStream; | 136 SkDynamicMemoryWStream outStream; |
123 | 137 |
124 SkAutoTUnref<SkPixelSerializer> serializer(SkImageEncoder::CreatePixelSerial
izer()); | 138 SkAutoTUnref<SkPixelSerializer> serializer(SkImageEncoder::CreatePixelSerial
izer()); |
125 picture->serialize(&outStream, serializer); | 139 picture->serialize(&outStream, serializer); |
126 | 140 |
127 return outStream.copyToData(); | 141 return outStream.copyToData(); |
128 } | 142 } |
129 | 143 |
130 GrContext* Request::getContext() { | 144 GrContext* Request::getContext() { |
| 145 #if SK_SUPPORT_GPU |
131 return fContextFactory->get(GrContextFactory::kNative_GLContextType, | 146 return fContextFactory->get(GrContextFactory::kNative_GLContextType, |
132 GrContextFactory::kNone_GLContextOptions); | 147 GrContextFactory::kNone_GLContextOptions); |
| 148 #else |
| 149 return nullptr; |
| 150 #endif |
133 } | 151 } |
134 | 152 |
135 SkIRect Request::getBounds() { | 153 SkIRect Request::getBounds() { |
136 SkIRect bounds; | 154 SkIRect bounds; |
137 if (fPicture) { | 155 if (fPicture) { |
138 bounds = fPicture->cullRect().roundOut(); | 156 bounds = fPicture->cullRect().roundOut(); |
139 if (fGPUEnabled) { | 157 if (fGPUEnabled) { |
| 158 #if SK_SUPPORT_GPU |
140 int maxRTSize = this->getContext()->caps()->maxRenderTargetSize(); | 159 int maxRTSize = this->getContext()->caps()->maxRenderTargetSize(); |
141 bounds = SkIRect::MakeWH(SkTMin(bounds.width(), maxRTSize), | 160 bounds = SkIRect::MakeWH(SkTMin(bounds.width(), maxRTSize), |
142 SkTMin(bounds.height(), maxRTSize)); | 161 SkTMin(bounds.height(), maxRTSize)); |
| 162 #endif |
143 } | 163 } |
144 } else { | 164 } else { |
145 bounds = SkIRect::MakeWH(kDefaultWidth, kDefaultHeight); | 165 bounds = SkIRect::MakeWH(kDefaultWidth, kDefaultHeight); |
146 } | 166 } |
147 | 167 |
148 // We clip to kDefaultWidth / kDefaultHeight for performance reasons | 168 // We clip to kDefaultWidth / kDefaultHeight for performance reasons |
149 // TODO make this configurable | 169 // TODO make this configurable |
150 bounds = SkIRect::MakeWH(SkTMin(bounds.width(), kDefaultWidth), | 170 bounds = SkIRect::MakeWH(SkTMin(bounds.width(), kDefaultWidth), |
151 SkTMin(bounds.height(), kDefaultHeight)); | 171 SkTMin(bounds.height(), kDefaultHeight)); |
152 return bounds; | 172 return bounds; |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 canvas->flush(); | 277 canvas->flush(); |
258 SkAutoTDelete<SkBitmap> bitmap(this->getBitmapFromCanvas(canvas)); | 278 SkAutoTDelete<SkBitmap> bitmap(this->getBitmapFromCanvas(canvas)); |
259 SkASSERT(bitmap); | 279 SkASSERT(bitmap); |
260 bitmap->lockPixels(); | 280 bitmap->lockPixels(); |
261 uint8_t* start = ((uint8_t*) bitmap->getPixels()) + (y * bitmap->width() + x
) * 4; | 281 uint8_t* start = ((uint8_t*) bitmap->getPixels()) + (y * bitmap->width() + x
) * 4; |
262 SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]); | 282 SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]); |
263 bitmap->unlockPixels(); | 283 bitmap->unlockPixels(); |
264 return result; | 284 return result; |
265 } | 285 } |
266 | 286 |
OLD | NEW |