OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 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 "GrCaps.h" | 8 #include "GrCaps.h" |
9 #include "GrContext.h" | 9 #include "GrContext.h" |
10 #include "GrDrawContext.h" | 10 #include "GrDrawContext.h" |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 SkAutoTUnref<GrTexture> texture(GrUploadPixmapToTexture(ctx, pixmap)); | 318 SkAutoTUnref<GrTexture> texture(GrUploadPixmapToTexture(ctx, pixmap)); |
319 if (!texture) { | 319 if (!texture) { |
320 return nullptr; | 320 return nullptr; |
321 } | 321 } |
322 return new SkImage_Gpu(texture->width(), texture->height(), kNeedNewImageUni
queID, | 322 return new SkImage_Gpu(texture->width(), texture->height(), kNeedNewImageUni
queID, |
323 pixmap.alphaType(), texture, budgeted); | 323 pixmap.alphaType(), texture, budgeted); |
324 } | 324 } |
325 | 325 |
326 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 326 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
327 | 327 |
| 328 class DeferredTextureImage { |
| 329 public: |
| 330 SkImage* newImage(GrContext* context, SkBudgeted) const; |
| 331 |
| 332 private: |
| 333 uint32_t fContextUniqueID; |
| 334 struct Data { |
| 335 SkImageInfo fInfo; |
| 336 void* fPixelData; |
| 337 size_t fRowBytes; |
| 338 int fColorTableCnt; |
| 339 uint32_t* fColorTableData; |
| 340 }; |
| 341 Data fData; |
| 342 |
| 343 friend class SkImage; |
| 344 }; |
| 345 |
| 346 size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& prox
y, |
| 347 const DeferredTextureImageUsageParam
s[], |
| 348 int paramCnt, void* buffer) const { |
| 349 const bool fillMode = SkToBool(buffer); |
| 350 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) { |
| 351 return 0; |
| 352 } |
| 353 |
| 354 SkAutoPixmapStorage pixmap; |
| 355 SkImageInfo info; |
| 356 size_t pixelSize = 0; |
| 357 size_t ctSize = 0; |
| 358 int ctCount = 0; |
| 359 if (this->peekPixels(&pixmap)) { |
| 360 info = pixmap.info(); |
| 361 pixelSize = SkAlign8(pixmap.getSafeSize()); |
| 362 if (pixmap.ctable()) { |
| 363 ctCount = pixmap.ctable()->count(); |
| 364 ctSize = SkAlign8(pixmap.ctable()->count() * 4); |
| 365 } |
| 366 } else { |
| 367 // Here we're just using presence of data to know whether there is a cod
ec behind the image. |
| 368 // In the future we will access the cacherator and get the exact data th
at we want to (e.g. |
| 369 // yuv planes) upload. |
| 370 SkAutoTUnref<SkData> data(this->refEncoded()); |
| 371 if (!data) { |
| 372 return 0; |
| 373 } |
| 374 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlph
aType; |
| 375 info = SkImageInfo::MakeN32(this->width(), this->height(), at); |
| 376 pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr)); |
| 377 if (fillMode) { |
| 378 pixmap.alloc(info); |
| 379 if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint))
{ |
| 380 return 0; |
| 381 } |
| 382 SkASSERT(!pixmap.ctable()); |
| 383 } |
| 384 } |
| 385 size_t size = 0; |
| 386 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage)); |
| 387 size += dtiSize; |
| 388 size_t pixelOffset = size; |
| 389 size += pixelSize; |
| 390 size_t ctOffset = size; |
| 391 size += ctSize; |
| 392 if (!fillMode) { |
| 393 return size; |
| 394 } |
| 395 intptr_t bufferAsInt = reinterpret_cast<intptr_t>(buffer); |
| 396 void* pixels = reinterpret_cast<void*>(bufferAsInt + pixelOffset); |
| 397 SkPMColor* ct = nullptr; |
| 398 if (ctSize) { |
| 399 ct = reinterpret_cast<SkPMColor*>(bufferAsInt + ctOffset); |
| 400 } |
| 401 |
| 402 memcpy(pixels, pixmap.addr(), pixmap.getSafeSize()); |
| 403 if (ctSize) { |
| 404 memcpy(ct, pixmap.ctable()->readColors(), ctSize); |
| 405 } |
| 406 |
| 407 SkASSERT(info == pixmap.info()); |
| 408 size_t rowBytes = pixmap.rowBytes(); |
| 409 DeferredTextureImage* dti = new (buffer) DeferredTextureImage(); |
| 410 dti->fContextUniqueID = proxy.fContextUniqueID; |
| 411 dti->fData.fInfo = info; |
| 412 dti->fData.fPixelData = pixels; |
| 413 dti->fData.fRowBytes = rowBytes; |
| 414 dti->fData.fColorTableCnt = ctCount; |
| 415 dti->fData.fColorTableData = ct; |
| 416 return size; |
| 417 } |
| 418 |
| 419 SkImage* SkImage::NewFromDeferredTextureImageData(GrContext* context, const void
* data, |
| 420 SkBudgeted budgeted) { |
| 421 if (!data) { |
| 422 return nullptr; |
| 423 } |
| 424 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImag
e*>(data); |
| 425 |
| 426 if (!context || context->uniqueID() != dti->fContextUniqueID) { |
| 427 return nullptr; |
| 428 } |
| 429 SkAutoTUnref<SkColorTable> colorTable; |
| 430 if (dti->fData.fColorTableCnt) { |
| 431 SkASSERT(dti->fData.fColorTableData); |
| 432 colorTable.reset(new SkColorTable(dti->fData.fColorTableData, dti->fData
.fColorTableCnt)); |
| 433 } |
| 434 SkPixmap pixmap; |
| 435 pixmap.reset(dti->fData.fInfo, dti->fData.fPixelData, dti->fData.fRowBytes,
colorTable.get()); |
| 436 return SkImage::NewTextureFromPixmap(context, pixmap, budgeted); |
| 437 } |
| 438 |
| 439 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 440 |
328 GrTexture* GrDeepCopyTexture(GrTexture* src, SkBudgeted budgeted) { | 441 GrTexture* GrDeepCopyTexture(GrTexture* src, SkBudgeted budgeted) { |
329 GrContext* ctx = src->getContext(); | 442 GrContext* ctx = src->getContext(); |
330 | 443 |
331 GrSurfaceDesc desc = src->desc(); | 444 GrSurfaceDesc desc = src->desc(); |
332 GrTexture* dst = ctx->textureProvider()->createTexture(desc, budgeted, nullp
tr, 0); | 445 GrTexture* dst = ctx->textureProvider()->createTexture(desc, budgeted, nullp
tr, 0); |
333 if (!dst) { | 446 if (!dst) { |
334 return nullptr; | 447 return nullptr; |
335 } | 448 } |
336 | 449 |
337 const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight); | 450 const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight); |
338 const SkIPoint dstP = SkIPoint::Make(0, 0); | 451 const SkIPoint dstP = SkIPoint::Make(0, 0); |
339 ctx->copySurface(dst, src, srcR, dstP); | 452 ctx->copySurface(dst, src, srcR, dstP); |
340 ctx->flushSurfaceWrites(dst); | 453 ctx->flushSurfaceWrites(dst); |
341 return dst; | 454 return dst; |
342 } | 455 } |
343 | 456 |
OLD | NEW |