Skip to content

Commit

Permalink
Update some of the dox code to GrDirectContext
Browse files Browse the repository at this point in the history
If we're to remove GrContext all uses must go!

Change-Id: I487a973004c4f080fc5802128770b417d54628e2
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/301981
Reviewed-by: Adlai Holler <adlai@google.com>
Commit-Queue: Robert Phillips <robertphillips@google.com>
  • Loading branch information
rphilli authored and Skia Commit-Bot committed Jul 13, 2020
1 parent 31890e2 commit d1ce4cb
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 57 deletions.
8 changes: 6 additions & 2 deletions docs/examples/Canvas_getGrContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
// HASH=c4ea949e5fa5a0630dcb6b0204bd498f
REG_FIDDLE(Canvas_getGrContext, 256, 256, false, 0) {
void draw(SkCanvas* canvas) {
if (canvas->getGrContext()) {
canvas->clear(SK_ColorRED);
if (auto context = canvas->recordingContext()) {
if (context->asDirectContext()) {
canvas->clear(SK_ColorRED);
} else {
canvas->clear(SK_ColorGREEN);
}
} else {
canvas->clear(SK_ColorBLUE);
}
Expand Down
31 changes: 22 additions & 9 deletions docs/examples/Image_getBackendTexture.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
#if 0 // Disabled until updated to use current API.
// Copyright 2019 Google LLC.
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
#include "tools/fiddle/examples.h"
// HASH=d093aad721261f421c4bef4a296aab48
REG_FIDDLE(Image_getBackendTexture, 256, 256, false, 3) {
void draw(SkCanvas* canvas) {
GrContext* grContext = canvas->getGrContext();
if (!grContext) {
canvas->drawString("GPU only!", 20, 40, SkPaint());
SkFont font;
SkPaint paint;

GrRecordingContext* context = canvas->recordingContext();
if (!context) {
canvas->drawString("GPU only!", 20, 40, font, paint);
return;
}
sk_sp<SkImage> imageFromBackend = SkImage::MakeFromAdoptedTexture(grContext, backEndTexture,
kBottomLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
GrDirectContext* direct = context->asDirectContext();
if (!direct) {
canvas->drawString("Direct context only!", 20, 40, font, paint);
return;
}

sk_sp<SkImage> imageFromBackend = SkImage::MakeFromAdoptedTexture(direct,
backEndTexture,
kBottomLeft_GrSurfaceOrigin,
kRGBA_8888_SkColorType,
kOpaque_SkAlphaType);
GrBackendTexture textureFromImage = imageFromBackend->getBackendTexture(false);
if (!textureFromImage.isValid()) {
return;
}
sk_sp<SkImage> imageFromTexture = SkImage::MakeFromAdoptedTexture(grContext, textureFromImage,
kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType, kOpaque_SkAlphaType);
sk_sp<SkImage> imageFromTexture = SkImage::MakeFromAdoptedTexture(direct,
textureFromImage,
kTopLeft_GrSurfaceOrigin,
kRGBA_8888_SkColorType,
kOpaque_SkAlphaType);
canvas->drawImage(imageFromTexture, 0, 0);
canvas->drawImage(imageFromBackend, 128, 128);
}
} // END FIDDLE
#endif // Disabled until updated to use current API.
20 changes: 12 additions & 8 deletions docs/examples/Image_isValid.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#if 0 // Disabled until updated to use current API.
// Copyright 2019 Google LLC.
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
#include "tools/fiddle/examples.h"
Expand All @@ -9,16 +8,22 @@ void draw(SkCanvas* canvas) {
if (nullptr == image) {
return;
}
SkFont font;
SkPaint paint;
paint.setAntiAlias(true);
canvas->drawImage(image, 0, 0);
canvas->drawString(label, image->width() / 2, image->height() / 4, paint);
if (canvas->getGrContext()) {
canvas->drawString(image->isValid(canvas->getGrContext()) ? "is valid on GPU" :
"not valid on GPU", 20, image->height() * 5 / 8, paint);
canvas->drawString(label, image->width() / 2, image->height() / 4, font, paint);
if (canvas->recordingContext()) {
const char* msg = image->isValid(canvas->recordingContext()) ? "is valid on GPU"
: "not valid on GPU";
canvas->drawString(msg, 20, image->height() * 5 / 8, font, paint);
}
canvas->drawString(image->isValid(nullptr) ? "is valid on CPU" :
"not valid on CPU", 20, image->height() * 7 / 8, paint);

// CONTEXT TODO: Once GrContext is gone, remove this cast
const char* msg = image->isValid((GrRecordingContext*) nullptr) ? "is valid on CPU"
: "not valid on CPU";

canvas->drawString(msg, 20, image->height() * 7 / 8, font, paint);
};
sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(source));
sk_sp<SkImage> textureImage(SkImage::MakeFromTexture(canvas->getGrContext(), backEndTexture,
Expand All @@ -31,4 +36,3 @@ void draw(SkCanvas* canvas) {
drawImage(textureImage, "backEndTexture");
}
} // END FIDDLE
#endif // Disabled until updated to use current API.
28 changes: 19 additions & 9 deletions docs/examples/Surface_MakeFromBackendTexture.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
#if 0 // Disabled until updated to use current API.
// Copyright 2019 Google LLC.
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
#include "tools/fiddle/examples.h"
// HASH=d3aec071998f871809f515e58abb1b0e
REG_FIDDLE(Surface_MakeFromBackendTexture, 256, 256, false, 3) {
void draw(SkCanvas* canvas) {
SkFont font(nullptr, 32);
SkPaint paint;
paint.setTextSize(32);
GrContext* context = canvas->getGrContext();

GrRecordingContext* context = canvas->recordingContext();
if (!context) {
canvas->drawString("GPU only!", 20, 40, paint);
canvas->drawString("GPU only!", 20, 40, font, paint);
return;
}
sk_sp<SkSurface> gpuSurface = SkSurface::MakeFromBackendTexture(context,
backEndTexture, kTopLeft_GrSurfaceOrigin, 0,
kRGBA_8888_SkColorType, nullptr, nullptr);
GrDirectContext* direct = context->asDirectContext();
if (!direct) {
canvas->drawString("Direct Context only!", 20, 40, font, paint);
return;
}

sk_sp<SkSurface> gpuSurface = SkSurface::MakeFromBackendTexture(direct,
backEndTexture,
kTopLeft_GrSurfaceOrigin,
0,
kRGBA_8888_SkColorType,
nullptr,
nullptr,
nullptr);
auto surfaceCanvas = gpuSurface->getCanvas();
surfaceCanvas->drawString("GPU rocks!", 20, 40, paint);
surfaceCanvas->drawString("GPU rocks!", 20, 40, font, paint);
sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
canvas->drawImage(image, 0, 0);
}
} // END FIDDLE
#endif // Disabled until updated to use current API.
11 changes: 5 additions & 6 deletions docs/examples/Surface_MakeRenderTarget.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#if 0 // Disabled until updated to use current API.
// Copyright 2019 Google LLC.
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
#include "tools/fiddle/examples.h"
// HASH=67b6609471a3f1ed0f4b1657004cdecb
REG_FIDDLE(Surface_MakeRenderTarget, 256, 64, false, 0) {
void draw(SkCanvas* canvas) {
SkFont font(nullptr, 32);
SkPaint paint;
paint.setTextSize(32);
GrContext* context = canvas->getGrContext();

auto context = canvas->getGrContext();
if (!context) {
canvas->drawString("GPU only!", 20, 40, paint);
canvas->drawString("GPU only!", 20, 40, font, paint);
return;
}
SkImageInfo info = SkImageInfo::MakeN32(256, 64, kOpaque_SkAlphaType);
Expand All @@ -18,11 +18,10 @@ void draw(SkCanvas* canvas) {
surfaceOrigin, nullptr));
auto surfaceCanvas = gpuSurface->getCanvas();
surfaceCanvas->clear(SK_ColorWHITE);
surfaceCanvas->drawString("GPU rocks!", 20, 40, paint);
surfaceCanvas->drawString("GPU rocks!", 20, 40, font, paint);
sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
canvas->drawImage(image, 0, 0);
canvas->translate(0, 128);
}
}
} // END FIDDLE
#endif // Disabled until updated to use current API.
18 changes: 10 additions & 8 deletions docs/examples/Surface_MakeRenderTarget_2.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
#if 0 // Disabled until updated to use current API.
// Copyright 2019 Google LLC.
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
#include "tools/fiddle/examples.h"
// HASH=640321e8ecfb3f9329f3bc6e1f02485f
REG_FIDDLE(Surface_MakeRenderTarget_2, 256, 256, false, 0) {
void draw(SkCanvas* canvas) {
auto test_draw = [](SkCanvas* surfaceCanvas) -> void {
SkFont font(nullptr, 32);

SkPaint paint;
paint.setAntiAlias(true);
paint.setLCDRenderText(true);
// TODO: where did this setting go?
//paint.setLCDRenderText(true);
paint.setColor(0xFFBBBBBB);

surfaceCanvas->drawRect(SkRect::MakeWH(128, 64), paint);
paint.setColor(SK_ColorWHITE);
paint.setTextSize(32);
surfaceCanvas->drawString("Pest", 0, 25, paint);
surfaceCanvas->drawString("Text", 0, 25, font, paint);
};
GrContext* context = canvas->getGrContext();
auto context = canvas->recordingContext();
SkImageInfo info = SkImageInfo::MakeN32(128, 64, kOpaque_SkAlphaType);
int y = 0;
for (auto geometry : { kRGB_H_SkPixelGeometry, kBGR_H_SkPixelGeometry,
kRGB_V_SkPixelGeometry, kBGR_V_SkPixelGeometry } ) {
SkSurfaceProps props(0, geometry);
sk_sp<SkSurface> surface = context ? SkSurface::MakeRenderTarget(
context, SkBudgeted::kNo, info, 0, &props) : SkSurface::MakeRaster(info, &props);
sk_sp<SkSurface> surface = context
? SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, &props)
: SkSurface::MakeRaster(info, &props);
test_draw(surface->getCanvas());
surface->draw(canvas, 0, y, nullptr);
sk_sp<SkImage> image(surface->makeImageSnapshot());
Expand All @@ -33,4 +36,3 @@ void draw(SkCanvas* canvas) {
}
}
} // END FIDDLE
#endif // Disabled until updated to use current API.
10 changes: 4 additions & 6 deletions docs/examples/Surface_MakeRenderTarget_3.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
#if 0 // Disabled until updated to use current API.
// Copyright 2019 Google LLC.
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
#include "tools/fiddle/examples.h"
// HASH=5c7629c15e9ac93f098335e72560fa2e
REG_FIDDLE(Surface_MakeRenderTarget_3, 256, 256, false, 0) {
void draw(SkCanvas* canvas) {
SkFont font(nullptr, 32);
SkPaint paint;
paint.setTextSize(32);
GrContext* context = canvas->getGrContext();
auto context = canvas->recordingContext();
if (!context) {
canvas->drawString("GPU only!", 20, 40, paint);
canvas->drawString("GPU only!", 20, 40, font, paint);
return;
}
SkImageInfo info = SkImageInfo::MakeN32(256, 64, kOpaque_SkAlphaType);
auto gpuSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
auto surfaceCanvas = gpuSurface->getCanvas();
surfaceCanvas->clear(SK_ColorWHITE);
surfaceCanvas->drawString("GPU rocks!", 20, 40, paint);
surfaceCanvas->drawString("GPU rocks!", 20, 40, font, paint);
sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
canvas->drawImage(image, 0, 0);
}
} // END FIDDLE
#endif // Disabled until updated to use current API.
10 changes: 4 additions & 6 deletions docs/examples/Surface_characterize.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
#if 0 // Disabled until updated to use current API.
// Copyright 2019 Google LLC.
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
#include "tools/fiddle/examples.h"
// HASH=6de6f3ef699a72ff26da1b26b23a3316
REG_FIDDLE(Surface_characterize, 256, 64, false, 0) {
void draw(SkCanvas* canvas) {
SkFont font(nullptr, 32);
SkPaint paint;
paint.setTextSize(32);
GrContext* context = canvas->getGrContext();
auto context = canvas->recordingContext();
if (!context) {
canvas->drawString("GPU only!", 20, 40, paint);
canvas->drawString("GPU only!", 20, 40, font, paint);
return;
}
sk_sp<SkSurface> gpuSurface = SkSurface::MakeRenderTarget(
context, SkBudgeted::kYes, SkImageInfo::MakeN32Premul(64, 64));
SkSurfaceCharacterization characterization;
if (!gpuSurface->characterize(&characterization)) {
canvas->drawString("characterization unsupported", 20, 40, paint);
canvas->drawString("characterization unsupported", 20, 40, font, paint);
return;
}
// start of threadable work
Expand All @@ -30,4 +29,3 @@ void draw(SkCanvas* canvas) {
canvas->drawImage(std::move(img), 0, 0);
}
} // END FIDDLE
#endif // Disabled until updated to use current API.
4 changes: 2 additions & 2 deletions docs/examples/blur4444.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ void draw(SkCanvas* canvas) {

sk_sp<SkSurface> surf;
auto ii = SkImageInfo::Make(650, 480, kARGB_4444_SkColorType, kPremul_SkAlphaType);
if (canvas->getGrContext() && !forceRaster) {
surf = SkSurface::MakeRenderTarget(canvas->getGrContext(), SkBudgeted::kNo, ii);
if (canvas->recordingContext() && !forceRaster) {
surf = SkSurface::MakeRenderTarget(canvas->recordingContext(), SkBudgeted::kNo, ii);
} else {
surf = SkSurface::MakeRaster(ii);
}
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/pong.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void draw(SkCanvas* canvas) {
float bX = ballX * 472 + 20;
float bY = ballY * 200 + 28;

if (canvas->getGrContext()) {
if (canvas->recordingContext()) {
canvas->drawRect(SkRect::MakeXYWH(236, bY - 15, 10, 30), p);
bX -= 256;
} else {
Expand Down

0 comments on commit d1ce4cb

Please sign in to comment.