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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 return bmp; | 48 return bmp; |
49 } | 49 } |
50 | 50 |
51 SkData* Request::writeCanvasToPng(SkCanvas* canvas) { | 51 SkData* Request::writeCanvasToPng(SkCanvas* canvas) { |
52 // capture pixels | 52 // capture pixels |
53 SkAutoTDelete<SkBitmap> bmp(this->getBitmapFromCanvas(canvas)); | 53 SkAutoTDelete<SkBitmap> bmp(this->getBitmapFromCanvas(canvas)); |
54 SkASSERT(bmp); | 54 SkASSERT(bmp); |
55 | 55 |
56 // write to png | 56 // write to png |
57 SkDynamicMemoryWStream buffer; | 57 SkDynamicMemoryWStream buffer; |
58 SkDrawCommand::WritePNG((const png_bytep) bmp->getPixels(), bmp->width(), bm
p->height(), | 58 SkDrawCommand::WritePNG((const png_bytep) bmp->getPixels(), bmp->width(), bm
p->height(), |
59 buffer); | 59 buffer); |
60 return buffer.copyToData(); | 60 return buffer.copyToData(); |
61 } | 61 } |
62 | 62 |
63 SkCanvas* Request::getCanvas() { | 63 SkCanvas* Request::getCanvas() { |
64 #if SK_SUPPORT_GPU | 64 #if SK_SUPPORT_GPU |
65 GrContextFactory* factory = fContextFactory; | 65 GrContextFactory* factory = fContextFactory; |
66 SkGLContext* gl = factory->getContextInfo(GrContextFactory::kNative_GLContex
tType, | 66 SkGLContext* gl = factory->getContextInfo(GrContextFactory::kNative_GLContex
tType, |
67 GrContextFactory::kNone_GLContextO
ptions).fGLContext; | 67 GrContextFactory::kNone_GLContextO
ptions).fGLContext; |
68 gl->makeCurrent(); | 68 gl->makeCurrent(); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 SkIRect bounds = this->getBounds(); | 148 SkIRect bounds = this->getBounds(); |
149 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), | 149 SkImageInfo info = SkImageInfo::Make(bounds.width(), bounds.height(), |
150 kN32_SkColorType, kPremul_SkAlphaType); | 150 kN32_SkColorType, kPremul_SkAlphaType); |
151 uint32_t flags = 0; | 151 uint32_t flags = 0; |
152 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); | 152 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); |
153 SkSurface* surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, i
nfo, 0, | 153 SkSurface* surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, i
nfo, 0, |
154 &props).release(); | 154 &props).release(); |
155 return surface; | 155 return surface; |
156 } | 156 } |
157 | 157 |
158 bool Request::enableGPU(bool enable) { | 158 bool Request::enableGPU(bool enable) { |
159 if (enable) { | 159 if (enable) { |
160 SkSurface* surface = this->createGPUSurface(); | 160 SkSurface* surface = this->createGPUSurface(); |
161 if (surface) { | 161 if (surface) { |
162 fSurface.reset(surface); | 162 fSurface.reset(surface); |
163 fGPUEnabled = true; | 163 fGPUEnabled = true; |
164 | 164 |
165 // When we switch to GPU, there seems to be some mystery draws in th
e canvas. So we | 165 // When we switch to GPU, there seems to be some mystery draws in th
e canvas. So we |
166 // draw once to flush the pipe | 166 // draw once to flush the pipe |
167 // TODO understand what is actually happening here | 167 // TODO understand what is actually happening here |
168 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp()); | 168 fDebugCanvas->drawTo(this->getCanvas(), this->getLastOp()); |
169 this->getCanvas()->flush(); | 169 this->getCanvas()->flush(); |
170 | 170 |
171 return true; | 171 return true; |
172 } | 172 } |
173 return false; | 173 return false; |
174 } | 174 } |
175 fSurface.reset(this->createCPUSurface()); | 175 fSurface.reset(this->createCPUSurface()); |
176 fGPUEnabled = false; | 176 fGPUEnabled = false; |
177 return true; | 177 return true; |
178 } | 178 } |
179 | 179 |
180 bool Request::initPictureFromStream(SkStream* stream) { | 180 bool Request::initPictureFromStream(SkStream* stream) { |
181 // parse picture from stream | 181 // parse picture from stream |
182 fPicture = SkPicture::MakeFromStream(stream); | 182 fPicture = SkPicture::MakeFromStream(stream); |
183 if (!fPicture) { | 183 if (!fPicture) { |
184 fprintf(stderr, "Could not create picture from stream.\n"); | 184 fprintf(stderr, "Could not create picture from stream.\n"); |
185 return false; | 185 return false; |
186 } | 186 } |
187 | 187 |
188 // reinitialize canvas with the new picture dimensions | 188 // reinitialize canvas with the new picture dimensions |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 SkCanvas* canvas = this->getCanvas(); | 247 SkCanvas* canvas = this->getCanvas(); |
248 canvas->flush(); | 248 canvas->flush(); |
249 SkAutoTDelete<SkBitmap> bitmap(this->getBitmapFromCanvas(canvas)); | 249 SkAutoTDelete<SkBitmap> bitmap(this->getBitmapFromCanvas(canvas)); |
250 SkASSERT(bitmap); | 250 SkASSERT(bitmap); |
251 bitmap->lockPixels(); | 251 bitmap->lockPixels(); |
252 uint8_t* start = ((uint8_t*) bitmap->getPixels()) + (y * bitmap->width() + x
) * 4; | 252 uint8_t* start = ((uint8_t*) bitmap->getPixels()) + (y * bitmap->width() + x
) * 4; |
253 SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]); | 253 SkColor result = SkColorSetARGB(start[3], start[0], start[1], start[2]); |
254 bitmap->unlockPixels(); | 254 bitmap->unlockPixels(); |
255 return result; | 255 return result; |
256 } | 256 } |
257 | |
OLD | NEW |